diff --git a/REQUIRE b/REQUIRE index dec20ce..289234b 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,3 +1,2 @@ julia 0.6 JSON -Documenter diff --git a/src/Documenter.jl b/src/Documenter.jl new file mode 100644 index 0000000..11bfc5f --- /dev/null +++ b/src/Documenter.jl @@ -0,0 +1,55 @@ +# copied from Documenter.jl +module Documenter + +@static if VERSION < v"0.7.0-DEV.3951" + link_pipe!(pipe; reader_supports_async = true, writer_supports_async = true) = + Base.link_pipe(pipe, julia_only_read = reader_supports_async, julia_only_write = writer_supports_async) +else + import Base: link_pipe! +end +@static if isdefined(Base, :with_logger) + using Logging +else # make things a no-op since warnings/info already print to stdout + struct ConsoleLogger end + ConsoleLogger(io) = ConsoleLogger() + with_logger(f, logger) = f() +end + +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() + 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 = ConsoleLogger(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 = 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 diff --git a/src/Examples.jl b/src/Examples.jl index 17334a0..9c147b4 100644 --- a/src/Examples.jl +++ b/src/Examples.jl @@ -6,6 +6,8 @@ import JSON include("IJulia.jl") import .IJulia +include("Documenter.jl") +import .Documenter # # Some simple rules: # @@ -326,8 +328,6 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide return outputfile end -import Documenter # just copy paste Documenter.Utilities.withoutput instead - function execute_notebook(nb) # sandbox module for the notebook (TODO: Do this in Main?) m = Module(gensym()) @@ -343,7 +343,7 @@ function execute_notebook(nb) # status = (true|false) # _: backtrace # str combined stdout, stderr output - r, status, _, str = Documenter.Utilities.withoutput() do + r, status, _, str = Documenter.withoutput() do include_string(m, block) end status || error("something went wrong when evaluating code") diff --git a/src/IJulia.jl b/src/IJulia.jl index be4af52..facb772 100644 --- a/src/IJulia.jl +++ b/src/IJulia.jl @@ -1,4 +1,3 @@ - # minimal (modified) subset of IJulia.jl to evaluate notebooks module IJulia