Browse Source

Use IOCapture instead of withoutput

pull/124/head
Morten Piibeleht 5 years ago
parent
commit
c36cde6c4d
  1. 2
      Project.toml
  2. 42
      src/Documenter.jl
  3. 16
      src/Literate.jl

2
Project.toml

@ -4,10 +4,12 @@ version = "2.7.0"
[deps] [deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
IOCapture = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
[compat] [compat]
IOCapture = "0.1"
JSON = "0.18, 0.19, 0.20, 0.21, 1" JSON = "0.18, 0.19, 0.20, 0.21, 1"
julia = "1" julia = "1"

42
src/Documenter.jl

@ -1,42 +0,0 @@
# this file contains some utilities copied from the Documenter.jl package
# (https://github.com/JuliaDocs/Documenter.jl), see LICENSE.md for license
module Documenter
function withoutput(f)
# Save the default output streams.
default_stdout = stdout
default_stderr = stderr
# Redirect both the `stdout` and `stderr` streams to a single `Pipe` object.
pipe = Pipe()
Base.link_pipe!(pipe; reader_supports_async = true, writer_supports_async = true)
redirect_stdout(pipe.in)
redirect_stderr(pipe.in)
# Also redirect logging stream to the same pipe
logger = Base.CoreLogging.SimpleLogger(pipe.in)
# Bytes written to the `pipe` are captured in `output` and converted to a `String`.
output = UInt8[]
# Run the function `f`, capturing all output that it might have generated.
# Success signals whether the function `f` did or did not throw an exception.
result, success, backtrace = Base.CoreLogging.with_logger(logger) do
try
f(), true, Vector{Ptr{Cvoid}}()
catch err
err, false, catch_backtrace()
finally
# Force at least a single write to `pipe`, otherwise `readavailable` blocks.
println()
# Restore the original output streams.
redirect_stdout(default_stdout)
redirect_stderr(default_stderr)
# NOTE: `close` must always be called *after* `readavailable`.
append!(output, readavailable(pipe))
close(pipe)
end
end
return result, success, backtrace, chomp(String(output))
end
end

16
src/Literate.jl

@ -6,12 +6,10 @@ https://fredrikekre.github.io/Literate.jl/ for documentation.
""" """
module Literate module Literate
import JSON, REPL import JSON, REPL, IOCapture
include("IJulia.jl") include("IJulia.jl")
import .IJulia import .IJulia
include("Documenter.jl")
import .Documenter
# # Some simple rules: # # Some simple rules:
# #
@ -670,16 +668,12 @@ end
# Execute a code-block in a module and capture stdout/stderr and the result # Execute a code-block in a module and capture stdout/stderr and the result
function execute_block(sb::Module, block::String) function execute_block(sb::Module, block::String)
# r is the result c = IOCapture.iocapture(throwerrors=false) do
# status = (true|false)
# _: backtrace
# str combined stdout, stderr output
r, status, _, str = Documenter.withoutput() do
include_string(sb, block) include_string(sb, block)
end end
if !status if c.error
error(""" error("""
$(sprint(showerror, r)) $(sprint(showerror, c.value))
when executing the following code block when executing the following code block
```julia ```julia
@ -687,7 +681,7 @@ function execute_block(sb::Module, block::String)
``` ```
""") """)
end end
return r, str return c.value, c.output
end end
end # module end # module

Loading…
Cancel
Save