diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..941d115 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,67 @@ +# TODO: build docs via github-actions + +name: CI +on: + push: + branches: + - master + pull_request: +jobs: + test: + name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} + runs-on: ${{ matrix.os }} + continue-on-error: ${{ matrix.allow-failure }} + strategy: + fail-fast: false # don't stop CI even when one of them fails + matrix: + version: + - '1.4' + os: + - ubuntu-latest + - macOS-latest + # - windows-latest # TODO: test on Windows + arch: + - x64 + - x86 + allow-failure: [false] + include: + # this is so verbose... any other way to simplify this ? + - version: 'nightly' + os: ubuntu-latest + arch: x64 + allow-failure: true + - version: 'nightly' + os: ubuntu-latest + arch: x86 + allow-failure: true + - version: 'nightly' + os: macOS-latest + arch: x64 + allow-failure: true + # - version: 'nightly' + # os: windows-latest + # arch: x64 + # allow-failure: true + # - version: 'nightly' + # os: windows-latest + # arch: x86 + # allow-failure: true + exclude: + - os: macOS-latest + arch: x86 + steps: + - uses: actions/checkout@v2 + - uses: julia-actions/setup-julia@v1 + with: + version: ${{ matrix.version }} + arch: ${{ matrix.arch }} + - uses: julia-actions/julia-buildpkg@latest + - uses: julia-actions/julia-runtest@latest + - uses: julia-actions/julia-processcoverage@v1 + - uses: codecov/codecov-action@v1 + with: + file: ./lcov.info + flags: unittests + name: codecov-umbrella + fail_ci_if_error: false + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore index 5b29fbd..c96e00f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ +Manifest.toml -src/*.cov -test.jl examples/figures/ examples/*.md examples/*.pdf @@ -22,15 +21,9 @@ test/**/*.ipynb doc/build doc/site -stable/ -doc/Manifest.toml -Manifest.toml -tmp/ .idea *.*~ *.aux *.log *.out -\#*\# -.juliahistory diff --git a/.travis.yml b/.travis.yml index fa4c131..2baf28a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,6 @@ language: julia -julia: - - 1 # current stable - - nightly - -script: - - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi - - julia --check-bounds=yes -e 'using Pkg; Pkg.build()' - - xvfb-run julia -e 'using Pkg; Pkg.test("Weave", coverage=true)' -after_success: - - julia -e 'using Pkg; cd(Pkg.dir("Weave")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())' - jobs: - allow_failures: - - julia: nightly include: - stage: "Documentation" julia: 1 diff --git a/Project.toml b/Project.toml index 1183ac6..46a710b 100644 --- a/Project.toml +++ b/Project.toml @@ -26,13 +26,10 @@ YAML = "0.3, 0.4" julia = "1.2" [extras] -Cairo = "159f3aea-2a34-519c-b102-8c37f9878175" -Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004" -IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Cairo", "Conda", "DataFrames", "Gadfly", "IJulia", "Plots", "Test"] +test = ["DataFrames", "Test"] diff --git a/README.md b/README.md index ea20f68..847565f 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Weave -[![Build Status](https://travis-ci.org/JunoLab/Weave.jl.svg?branch=master)](https://travis-ci.org/JunoLab/Weave.jl) +![CI](https://github.com/JunoLab/Weave.jl/workflows/CI/badge.svg) +[![codecov](https://codecov.io/gh/JunoLab/Weave.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JunoLab/Weave.jl) [![](https://img.shields.io/badge/docs-stable-blue.svg)](http://weavejl.mpastell.com/stable/) [![](https://img.shields.io/badge/docs-dev-blue.svg)](http://weavejl.mpastell.com/dev/) [![](http://joss.theoj.org/papers/10.21105/joss.00204/status.svg)](http://dx.doi.org/10.21105/joss.00204) -Weave is a scientific report generator/literate programming tool -for Julia. +Weave is a scientific report generator/literate programming tool for the [Julia programming language](https://julialang.org/). It resembles [Pweave](http://mpastell.com/pweave), [knitr](https://yihui.org/knitr/), diff --git a/doc/src/chunk_options.md b/doc/src/chunk_options.md index 69dfd0d..ca124a0 100644 --- a/doc/src/chunk_options.md +++ b/doc/src/chunk_options.md @@ -33,6 +33,7 @@ we've mostly followed [RMarkdown's namings](http://yihui.name/knitr/options), bu ### Evaluation - `eval = true`: Evaluate the code chunk. If `false` the chunk won’t be executed. +- `error = true`: If `true` [`weave`](@ref) won't stop on errors and rather they will be included in output document. If `false`, [`weave`](@ref) will halt on any of un-caught errors. - `cache = false`: Cache results, depending on `cache` parameter on [`weave`](@ref) function. - `tangle = true`: Set tangle to `false` to exclude chunk from tangled code. diff --git a/doc/src/publish.md b/doc/src/publish.md index d82f120..428ded3 100644 --- a/doc/src/publish.md +++ b/doc/src/publish.md @@ -41,8 +41,8 @@ e.g.: `weave("FIR_design_plots.jl", template = "custom.tpl"`. As starting point, you can use the existing templates: -- HTML (`md2html`): [`md2html.tpl`](https://github.com/mpastell/Weave.jl/blob/master/templates/md2html.tpl) -- LaTex (`md2pdf`): [`md2pdf.tpl`](https://github.com/mpastell/Weave.jl/blob/master/templates/md2pdf.tpl) +- HTML (`md2html`): [`md2html.tpl`](https://github.com/JunoLab/Weave.jl/blob/master/templates/md2html.tpl) +- LaTex (`md2pdf`): [`md2pdf.tpl`](https://github.com/JunoLab/Weave.jl/blob/master/templates/md2pdf.tpl) Templates are rendered using [Mustache.jl](https://github.com/jverzani/Mustache.jl). diff --git a/doc/src/usage.md b/doc/src/usage.md index ed869a5..a6cb589 100644 --- a/doc/src/usage.md +++ b/doc/src/usage.md @@ -152,8 +152,8 @@ Weave will remove the first empty space from each line of documentation. !!! tip - Here are sample documents: - + [markdown format](https://github.com/mpastell/Weave.jl/blob/master/examples/FIR_design.jmd) - + [script format](https://github.com/mpastell/Weave.jl/blob/master/examples/FIR_design.jl) + + [markdown format](https://github.com/JunoLab/Weave.jl/blob/master/examples/FIR_design.jmd) + + [script format](https://github.com/JunoLab/Weave.jl/blob/master/examples/FIR_design.jl) - [Details about chunk options](@ref chunk-options) diff --git a/examples/FIR_design.jl b/examples/FIR_design.jl index b51762f..0b1185b 100644 --- a/examples/FIR_design.jl +++ b/examples/FIR_design.jl @@ -7,7 +7,7 @@ #' # Introduction #' This an example of a julia script that can be published using -#' [Weave](http://mpastell.github.io/Weave.jl/latest/usage/). +#' [Weave](http://weavejl.mpastell.com/dev/usage/). #' The script can be executed normally using Julia #' or published to HTML or pdf with Weave. #' Text is written in markdown in lines starting with "`#'` " and code diff --git a/examples/FIR_design.jmd b/examples/FIR_design.jmd index ddb1858..184d647 100644 --- a/examples/FIR_design.jmd +++ b/examples/FIR_design.jmd @@ -9,7 +9,7 @@ date: 21th April 2016 This an example of a julia script that can be published using -[Weave](http://mpastell.github.io/Weave.jl/latest/usage/). +[Weave](http://weavejl.mpastell.com/dev/usage/). The script can be executed normally using Julia or published to HTML or pdf with Weave. Text is written in markdown in lines starting with "`#'` " and code diff --git a/examples/FIR_design_plots.jl b/examples/FIR_design_plots.jl index 75844b1..b3d8eca 100644 --- a/examples/FIR_design_plots.jl +++ b/examples/FIR_design_plots.jl @@ -7,7 +7,7 @@ #' # Introduction #' This an example of a julia script that can be published using -#' [Weave](http://mpastell.github.io/Weave.jl/latest/usage/). +#' [Weave](http://weavejl.mpastell.com/dev/usage/). #' The script can be executed normally using Julia #' or published to HTML or pdf with Weave. #' Text is written in markdown in lines starting with "`#'` " and code diff --git a/examples/gadfly_md_sample.jmd b/examples/gadfly_md_sample.jmd index 16128a2..041843b 100644 --- a/examples/gadfly_md_sample.jmd +++ b/examples/gadfly_md_sample.jmd @@ -7,7 +7,7 @@ date : 13th December 2016 # Intro This a sample [Julia](http://julialang.org/) markdown document that can -be executed using [Weave.jl](https://github.com/mpastell/Weave.jl). +be executed using [Weave.jl](https://github.com/JunoLab/Weave.jl). The code is delimited from docs using markdown fenced code blocks markup which can be seen looking at the source document @@ -71,6 +71,6 @@ plot(y = cumsum(randn(1000, 1)), Geom.line) Read the documentation: - stable: [http://mpastell.github.io/Weave.jl/stable/](http://mpastell.github.io/Weave.jl/stable/) - - latest: [http://mpastell.github.io/Weave.jl/latest/](http://mpastell.github.io/Weave.jl/latest/) + - latest: [http://weavejl.mpastell.com/dev/](http://weavejl.mpastell.com/dev/) -See other examples in the [GitHub repo](https://github.com/mpastell/Weave.jl/tree/master/examples) +See other examples in the [GitHub repo](https://github.com/JunoLab/Weave.jl/tree/master/examples) diff --git a/examples/gadfly_pgf.texw b/examples/gadfly_pgf.texw index 7c1ffee..7f5276c 100644 --- a/examples/gadfly_pgf.texw +++ b/examples/gadfly_pgf.texw @@ -29,10 +29,10 @@ \section{Intro} This is a minimal example on using PGF format with Gadfly plots in -\href{https://github.com/mpastell/Weave.jl}{Weave.jl} document. +\href{https://github.com/JunoLab/Weave.jl}{Weave.jl} document. The source is in github: -\url{https://github.com/mpastell/Weave.jl/blob/master/examples/gadfly_pgf.texw}. +\url{https://github.com/JunoLab/Weave.jl/blob/master/examples/gadfly_pgf.texw}. You can run this example with first weaving it from Julia using: diff --git a/examples/gadfly_sample.mdw b/examples/gadfly_sample.mdw index 99d468e..0055cee 100644 --- a/examples/gadfly_sample.mdw +++ b/examples/gadfly_sample.mdw @@ -5,7 +5,7 @@ # Introduction This a sample [Julia](http://julialang.org/) noweb document that can -be executed using [Weave.jl](https://github.com/mpastell/Weave.jl). +be executed using [Weave.jl](https://github.com/JunoLab/Weave.jl). The code is delimited from docs using `<<>>=` and `@` markup which can be seen looking at the source document `gadfly_sample.mdw` in the examples directory @@ -65,6 +65,6 @@ plot(y = cumsum(randn(1000, 1)), Geom.line) Read the documentation: - stable: - - latest: + - latest: -See other examples in: +See other examples in: diff --git a/src/Weave.jl b/src/Weave.jl index 55185ae..09646ff 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -30,6 +30,7 @@ end # utilitity functions take2string!(io) = String(take!(io)) +joinlines(lines) = join(lines, '\n') """ list_out_formats() @@ -96,7 +97,6 @@ Weave an input document to output file. * `:all` caches everything * `:user` caches based on chunk options * `:refresh` runs all code chunks and save new cache -- `throw_errors::Bool = false`: If `false` errors are included in output document and the whole document is executed. If `true` errors are thrown when they occur - `template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing`: Template (file path) or `Mustache.MustacheTokens`s for `md2html` or `md2tex` formats - `css::Union{Nothing,AbstractString} = nothing`: Path of a CSS file used for md2html format - `highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing`: Theme used for syntax highlighting (defaults to `Highlights.Themes.DefaultTheme`) @@ -118,7 +118,6 @@ function weave( fig_ext::Union{Nothing,AbstractString} = nothing, cache_path::AbstractString = "cache", cache::Symbol = :off, - throw_errors::Bool = false, template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing, css::Union{Nothing,AbstractString} = nothing, # TODO: rename to `stylesheet` highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing, @@ -158,7 +157,6 @@ function weave( fig_ext = get(weave_options, "fig_ext", fig_ext) cache_path = get(weave_options, "cache_path", cache_path) cache = Symbol(get(weave_options, "cache", cache)) - throw_errors = get(weave_options, "throw_errors", throw_errors) end doc = run_doc( @@ -171,7 +169,6 @@ function weave( fig_ext = fig_ext, cache_path = cache_path, cache = cache, - throw_errors = throw_errors, ) # render document @@ -221,7 +218,7 @@ function weave( rm(intermediate) elseif doctype == "md2pdf" run_latex(doc, out_path, latex_cmd) - out_path = get_out_path(doc, out_path, ext = "pdf") + out_path = get_out_path(doc, out_path, "pdf") end @info "Weaved to $(out_path)" @@ -263,7 +260,7 @@ using [`nbconvert`](https://nbconvert.readthedocs.io/en/latest/). !!! warning The code is _**not**_ executed by Weave, but by [`nbconvert`](https://nbconvert.readthedocs.io/en/latest/). - This means that the output doesn't necessarily always work properly; see [#116](https://github.com/mpastell/Weave.jl/issues/116). + This means that the output doesn't necessarily always work properly; see [#116](https://github.com/JunoLab/Weave.jl/issues/116). !!! note In order to _just_ convert Weave document to Jupyter Notebook, @@ -321,18 +318,6 @@ end include_weave(source, informat = nothing) = include_weave(Main, source, informat) -# Hooks to run before and after chunks, this is form IJulia, -# but note that Weave hooks take the chunk as input -const preexecute_hooks = Function[] -push_preexecute_hook(f::Function) = push!(preexecute_hooks, f) -pop_preexecute_hook(f::Function) = - splice!(preexecute_hooks, findfirst(x -> x == f, preexecute_hooks)) - -const postexecute_hooks = Function[] -push_postexecute_hook(f::Function) = push!(postexecute_hooks, f) -pop_postexecute_hook(f::Function) = - splice!(postexecute_hooks, findfirst(x -> x == f, postexecute_hooks)) - include("types.jl") include("config.jl") include("WeaveMarkdown/markdown.jl") diff --git a/src/config.jl b/src/config.jl index 2a950a0..956c259 100644 --- a/src/config.jl +++ b/src/config.jl @@ -1,10 +1,11 @@ +# TODO: follow RMarkdown convention more const _DEFAULT_PARAMS = Dict{Symbol,Any}( :echo => true, :results => "markup", :hold => false, :fig => true, - :include => true, :eval => true, + :error => true, :tangle => true, :cache => false, :fig_cap => nothing, @@ -14,11 +15,10 @@ const _DEFAULT_PARAMS = Dict{Symbol,Any}( :fig_path => DEFAULT_FIG_PATH, :dpi => 96, :term => false, - :prompt => "\njulia> ", + :prompt => "julia>", :label => nothing, :wrap => true, :line_width => 75, - :engine => "julia", :fig_ext => nothing, :fig_pos => nothing, :fig_env => nothing, diff --git a/src/display_methods.jl b/src/display_methods.jl index 546bcb7..779cb24 100644 --- a/src/display_methods.jl +++ b/src/display_methods.jl @@ -2,34 +2,20 @@ using Markdown, .WeaveMarkdown # Contains report global properties mutable struct Report <: AbstractDisplay - cwd::AbstractString - basename::AbstractString + cwd::String + basename::String format::WeaveFormat - rich_output::AbstractString + rich_output::String fignum::Int figures::Vector{String} - cur_chunk::Any - mimetypes::Array{AbstractString} + cur_chunk::Union{Nothing,CodeChunk} + mimetypes::Vector{String} first_plot::Bool header_script::String - throw_errors::Bool end -function Report(cwd, basename, format, mimetypes, throw_errors) - Report( - cwd, - basename, - format, - "", - 1, - String[], - nothing, - mimetypes, - true, - "", - throw_errors, - ) -end +Report(cwd, basename, format, mimetypes) = + Report(cwd, basename, format, "", 1, String[], nothing, mimetypes, true, "") # Default mimetypes in order, can be overridden for some inside `run method` formats const default_mime_types = ["image/svg+xml", "image/png", "text/html", "text/plain"] diff --git a/src/plots.jl b/src/plots.jl index 57fbd59..9211c35 100644 --- a/src/plots.jl +++ b/src/plots.jl @@ -4,14 +4,13 @@ using Base64, ..Plots, ..Weave # Pre-execute hooks to set the plot size for the chunk -function plots_set_size(chunk) +function plots_set_size!(chunk) w = chunk.options[:fig_width] * chunk.options[:dpi] h = chunk.options[:fig_height] * chunk.options[:dpi] Plots.default(size = (w, h)) - return chunk end -Weave.push_preexecute_hook(plots_set_size) +Weave.push_preexecution_hook!(plots_set_size!) # PNG or SVG is not working, output html function Base.display( diff --git a/src/rendering/common.jl b/src/rendering/common.jl index 5db5fe9..49bb813 100644 --- a/src/rendering/common.jl +++ b/src/rendering/common.jl @@ -91,9 +91,7 @@ function format_chunk(chunk::CodeChunk, docformat) # Handle figures if chunk.options[:fig] && length(chunk.figures) > 0 - if chunk.options[:include] - result *= formatfigures(chunk, docformat) - end + result *= formatfigures(chunk, docformat) end return result diff --git a/src/run.jl b/src/run.jl index fe93a8e..3f3013c 100644 --- a/src/run.jl +++ b/src/run.jl @@ -13,7 +13,6 @@ function run_doc( fig_ext::Union{Nothing,AbstractString} = nothing, cache_path::AbstractString = "cache", cache::Symbol = :off, - throw_errors::Bool = false, ) # cache :all, :user, :off, :refresh @@ -43,7 +42,7 @@ function run_doc( mimetypes = doc.format.mimetypes - report = Report(cwd, doc.basename, doc.format, mimetypes, throw_errors) + report = Report(cwd, doc.basename, doc.format, mimetypes) cd_back = let d = pwd(); () -> cd(d); end cd(cwd) pushdisplay(report) @@ -193,58 +192,43 @@ function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, mod::Modu return inline end -reset_report(report::Report) = report.figures = String[] - function run_code(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module) - ss = parse_input(chunk.content) - n = length(ss) - results = ChunkOutput[] - for (i, s) in enumerate(ss) - reset_report(report) - obj, out = capture_output( - mod, - s, - doc.path, - chunk.options[:term], - i == n, - report.throw_errors, - ) - figures = report.figures # Captured figures - result = ChunkOutput(s, out, report.rich_output, figures) - report.rich_output = "" - push!(results, result) - end - return results + code = chunk.content + path = doc.path + error = chunk.options[:error] + codes = chunk.options[:term] ? split_code(code) : [code] + capture = code -> capture_output(code, mod, path, error, report) + return capture.(codes) end -# Parse chunk input to array of expressions -function parse_input(s) +function split_code(code) res = String[] - s = lstrip(s) - n = sizeof(s) - pos = 1 - while (oldpos = pos) ≤ n - _, pos = Meta.parse(s, pos) - push!(res, s[oldpos:pos-1]) + e = 1 + ex = :init + while true + s = e + ex, e = Meta.parse(code, s) + isnothing(ex) && break + push!(res, strip(code[s:e-1])) end return res end -function capture_output(mod, s, path, term, lastline, throw_errors = false) - local out = nothing - local obj = nothing +function capture_output(code, mod, path, error, report) + reset_report!(report) old = stdout rw, wr = redirect_stdout() reader = @async read(rw, String) + local out = nothing task_local_storage(:SOURCE_PATH, path) do try - obj = include_string(mod, s, path) # TODO: fix line number - !isnothing(obj) && (term || lastline) && display(obj) + obj = include_string(mod, code, path) # TODO: fix line number + !isnothing(obj) && display(obj) catch _err err = unwrap_load_err(_err) - throw_errors && throw(err) + error || throw(err) display(err) @warn "ERROR: $(typeof(err)) occurred, including output in Weaved document" finally @@ -255,13 +239,20 @@ function capture_output(mod, s, path, term, lastline, throw_errors = false) end end - out = replace(out, r"\u001b\[.*?m" => "") # remove ANSI color codes - return (obj, out) + return ChunkOutput(code, remove_ansi_control_chars(out), report.rich_output, report.figures) +end + +function reset_report!(report) + report.rich_output = "" + report.figures = String[] end unwrap_load_err(err) = return err unwrap_load_err(err::LoadError) = return err.error +# https://stackoverflow.com/a/33925425/12113178 +remove_ansi_control_chars(s) = replace(s, r"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]" => "") + function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module) if !chunk.options[:eval] chunk.output = "" @@ -269,10 +260,7 @@ function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module return chunk end - # Run preexecute_hooks - for hook in preexecute_hooks - chunk = Base.invokelatest(hook, chunk) - end + execute_prehooks!(chunk) report.fignum = 1 report.cur_chunk = chunk @@ -283,10 +271,7 @@ function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module chunk.result = run_code(doc, chunk, report, mod) - # Run post_execute chunks - for hook in postexecute_hooks - chunk = Base.invokelatest(hook, chunk) - end + execute_posthooks!(chunk) chunks = if chunk.options[:term] collect_term_results(chunk) @@ -296,13 +281,28 @@ function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module collect_results(chunk) end - # else - # chunk.options[:fig] && (chunk.figures = copy(report.figures)) - # end - return chunks end +# Hooks to run before and after chunks, this is form IJulia, +const preexecution_hooks = Function[] +push_preexecution_hook!(f::Function) = push!(preexecution_hooks, f) +function pop_preexecution_hook!(f::Function) + i = findfirst(x -> x == f, preexecution_hooks) + isnothing(i) && error("this function has not been registered in the pre-execution hook yet") + return splice!(preexecution_hooks, i) +end +execute_prehooks!(chunk::CodeChunk) = for prehook in preexecution_hooks; Base.invokelatest(prehook, chunk); end + +const postexecution_hooks = Function[] +push_postexecution_hook!(f::Function) = push!(postexecution_hooks, f) +function pop_postexecution_hook!(f::Function) + i = findfirst(x -> x == f, postexecution_hooks) + isnothing(i) && error("this function has not been registered in the post-execution hook yet") + return splice!(postexecution_hooks, i) +end +execute_posthooks!(chunk::CodeChunk) = for posthook in postexecution_hooks; Base.invokelatest(posthook, chunk); end + """ clear_module!(mod::Module) @@ -391,7 +391,7 @@ function collect_term_results(chunk::CodeChunk) prompt = chunk.options[:prompt] result_chunks = CodeChunk[] for r in chunk.result - output *= string(prompt, r.code, r.stdout) + output *= string('\n', indent_term_code(prompt, r.code), '\n', r.stdout) if !isempty(r.figures) rchunk = CodeChunk( "", @@ -421,6 +421,15 @@ function collect_term_results(chunk::CodeChunk) return result_chunks end +function indent_term_code(prompt, code) + prompt_with_space = string(prompt, ' ') + n = length(prompt_with_space) + pads = ' ' ^ n + return map(enumerate(split(code, '\n'))) do (i,line) + isone(i) ? string(prompt_with_space, line) : string(pads, line) + end |> joinlines +end + function collect_hold_results(chunk::CodeChunk) for r in chunk.result chunk.output *= r.stdout diff --git a/test/cache_test.jl b/test/cache_test.jl index ee87b78..d2d8943 100644 --- a/test/cache_test.jl +++ b/test/cache_test.jl @@ -1,7 +1,4 @@ -using Weave -using Test - -#Test if running document with and without cache works +# Test if running document with and without cache works isdir("documents/cache") && rm("documents/cache", recursive = true) weave("documents/chunk_options.noweb", cache=:all) result = read("documents/chunk_options.md", String) diff --git a/test/documents/gadfly/gadfly_formats_test.md.js.svg.pandoc b/test/documents/gadfly/gadfly_formats_test.md.js.svg.pandoc deleted file mode 100644 index 14351fb..0000000 --- a/test/documents/gadfly/gadfly_formats_test.md.js.svg.pandoc +++ /dev/null @@ -1,18 +0,0 @@ -~~~~{.julia} -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -~~~~~~~~~~~~~ - - -![sin(x) function.](figures/gadfly_formats_test_sin_fun_1.js.svg){#fig:sin_fun} - -![cos(x) function.](figures/gadfly_formats_test_2_1.js.svg) - -~~~~{.julia} -plot(x=x, y = cos.(2x), Geom.line) -~~~~~~~~~~~~~ - - -![](figures/gadfly_formats_test_cos2_fun_1.js.svg){width=15cm #fig:cos2_fun}\ - diff --git a/test/documents/gadfly/gadfly_formats_test.md.pdf.github b/test/documents/gadfly/gadfly_formats_test.md.pdf.github deleted file mode 100644 index f46ade6..0000000 --- a/test/documents/gadfly/gadfly_formats_test.md.pdf.github +++ /dev/null @@ -1,17 +0,0 @@ -````julia -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -```` - - -![sin(x) function.](figures/gadfly_formats_test_sin_fun_1.pdf) - -![cos(x) function.](figures/gadfly_formats_test_2_1.pdf) - -````julia -plot(x=x, y = cos.(2x), Geom.line) -```` - - -![](figures/gadfly_formats_test_cos2_fun_1.pdf) diff --git a/test/documents/gadfly/gadfly_formats_test.md.png.github b/test/documents/gadfly/gadfly_formats_test.md.png.github deleted file mode 100644 index 2514e62..0000000 --- a/test/documents/gadfly/gadfly_formats_test.md.png.github +++ /dev/null @@ -1,17 +0,0 @@ -````julia -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -```` - - -![sin(x) function.](figures/gadfly_formats_test_sin_fun_1.png) - -![cos(x) function.](figures/gadfly_formats_test_2_1.png) - -````julia -plot(x=x, y = cos.(2x), Geom.line) -```` - - -![](figures/gadfly_formats_test_cos2_fun_1.png) diff --git a/test/documents/gadfly/gadfly_formats_test.md.png.pandoc b/test/documents/gadfly/gadfly_formats_test.md.png.pandoc deleted file mode 100644 index cb33b5b..0000000 --- a/test/documents/gadfly/gadfly_formats_test.md.png.pandoc +++ /dev/null @@ -1,18 +0,0 @@ -~~~~{.julia} -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -~~~~~~~~~~~~~ - - -![sin(x) function.](figures/gadfly_formats_test_sin_fun_1.png){#fig:sin_fun} - -![cos(x) function.](figures/gadfly_formats_test_2_1.png) - -~~~~{.julia} -plot(x=x, y = cos.(2x), Geom.line) -~~~~~~~~~~~~~ - - -![](figures/gadfly_formats_test_cos2_fun_1.png){width=15cm #fig:cos2_fun}\ - diff --git a/test/documents/gadfly/gadfly_formats_test.md.svg.github b/test/documents/gadfly/gadfly_formats_test.md.svg.github deleted file mode 100644 index 0f11e89..0000000 --- a/test/documents/gadfly/gadfly_formats_test.md.svg.github +++ /dev/null @@ -1,17 +0,0 @@ -````julia -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -```` - - -![sin(x) function.](figures/gadfly_formats_test_sin_fun_1.svg) - -![cos(x) function.](figures/gadfly_formats_test_2_1.svg) - -````julia -plot(x=x, y = cos.(2x), Geom.line) -```` - - -![](figures/gadfly_formats_test_cos2_fun_1.svg) diff --git a/test/documents/gadfly/gadfly_formats_test.tex.pdf.tex b/test/documents/gadfly/gadfly_formats_test.tex.pdf.tex deleted file mode 100644 index d4729e0..0000000 --- a/test/documents/gadfly/gadfly_formats_test.tex.pdf.tex +++ /dev/null @@ -1,22 +0,0 @@ -\begin{juliacode} -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -\end{juliacode} -\begin{figure}[ht] -\center -\includegraphics[width=\linewidth]{figures/gadfly_formats_test_sin_fun_1.pdf} -\caption{sin(x) function.} -\label{fig:sin_fun} -\end{figure} - -\begin{figure}[htpb] -\center -\includegraphics[width=\linewidth]{figures/gadfly_formats_test_2_1.pdf} -\caption{cos(x) function.} -\end{figure} - -\begin{juliacode} -plot(x=x, y = cos.(2x), Geom.line) -\end{juliacode} -\includegraphics[width=15cm]{figures/gadfly_formats_test_cos2_fun_1.pdf} diff --git a/test/documents/gadfly/gadfly_formats_test.tex.png.tex b/test/documents/gadfly/gadfly_formats_test.tex.png.tex deleted file mode 100644 index ed629ad..0000000 --- a/test/documents/gadfly/gadfly_formats_test.tex.png.tex +++ /dev/null @@ -1,22 +0,0 @@ -\begin{juliacode} -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -\end{juliacode} -\begin{figure}[ht] -\center -\includegraphics[width=\linewidth]{figures/gadfly_formats_test_sin_fun_1.png} -\caption{sin(x) function.} -\label{fig:sin_fun} -\end{figure} - -\begin{figure}[htpb] -\center -\includegraphics[width=\linewidth]{figures/gadfly_formats_test_2_1.png} -\caption{cos(x) function.} -\end{figure} - -\begin{juliacode} -plot(x=x, y = cos.(2x), Geom.line) -\end{juliacode} -\includegraphics[width=15cm]{figures/gadfly_formats_test_cos2_fun_1.png} diff --git a/test/documents/gadfly/gadfly_formats_test.tex.ps.tex b/test/documents/gadfly/gadfly_formats_test.tex.ps.tex deleted file mode 100644 index 8a5b5b1..0000000 --- a/test/documents/gadfly/gadfly_formats_test.tex.ps.tex +++ /dev/null @@ -1,22 +0,0 @@ -\begin{juliacode} -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -\end{juliacode} -\begin{figure}[ht] -\center -\includegraphics[width=\linewidth]{figures/gadfly_formats_test_sin_fun_1.ps} -\caption{sin(x) function.} -\label{fig:sin_fun} -\end{figure} - -\begin{figure}[htpb] -\center -\includegraphics[width=\linewidth]{figures/gadfly_formats_test_2_1.ps} -\caption{cos(x) function.} -\end{figure} - -\begin{juliacode} -plot(x=x, y = cos.(2x), Geom.line) -\end{juliacode} -\includegraphics[width=15cm]{figures/gadfly_formats_test_cos2_fun_1.ps} diff --git a/test/documents/gadfly/gadfly_formats_test.tex.tex.tex b/test/documents/gadfly/gadfly_formats_test.tex.tex.tex deleted file mode 100644 index 4989cb7..0000000 --- a/test/documents/gadfly/gadfly_formats_test.tex.tex.tex +++ /dev/null @@ -1,22 +0,0 @@ -\begin{juliacode} -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -\end{juliacode} -\begin{figure}[ht] -\center -\resizebox{\linewidth}{!}{\input{figures/gadfly_formats_test_sin_fun_1.tex}} -\caption{sin(x) function.} -\label{fig:sin_fun} -\end{figure} - -\begin{figure}[htpb] -\center -\resizebox{\linewidth}{!}{\input{figures/gadfly_formats_test_2_1.tex}} -\caption{cos(x) function.} -\end{figure} - -\begin{juliacode} -plot(x=x, y = cos.(2x), Geom.line) -\end{juliacode} -\resizebox{15cm}{!}{\input{figures/gadfly_formats_test_cos2_fun_1.tex}} diff --git a/test/documents/gadfly_formats_test.jnw b/test/documents/gadfly_formats_test.jnw deleted file mode 100644 index 8691c2a..0000000 --- a/test/documents/gadfly_formats_test.jnw +++ /dev/null @@ -1,19 +0,0 @@ ---- -weave_options: - out_path: gadfly ---- - -<>= -using Gadfly -x = range(0, stop =2π, step=0.05) -plot(x=x, y = sin.(x), Geom.line) -@ - - -<>= -plot(x=x, y = cos.(x), Geom.line) -@ - -<>= -plot(x=x, y = cos.(2x), Geom.line) -@ diff --git a/test/documents/gadfly_markdown_test.jmd b/test/documents/gadfly_markdown_test.jmd deleted file mode 100644 index 1ab3741..0000000 --- a/test/documents/gadfly_markdown_test.jmd +++ /dev/null @@ -1,12 +0,0 @@ - -# Gadfly - -```{julia;fig_ext=".svg";dpi=300} -using Gadfly -x = collect(range(0, stop=2π, length=200)) -plot( - layer(x=x, y = sin.(x), Geom.line), - layer(x=x, y = cos.(x), Geom.line, Theme(default_color=colorant"red")), - Guide.manual_color_key("Legend", ["sin", "cos"], ["deepskyblue", "red"]) -) -``` \ No newline at end of file diff --git a/test/documents/inline/markdown_beamer.html.ref b/test/documents/inline/markdown_beamer.html.ref deleted file mode 100644 index 6cfa31c..0000000 --- a/test/documents/inline/markdown_beamer.html.ref +++ /dev/null @@ -1,22 +0,0 @@ - - - -
- -

Some inline output

- - -
-println("Testing output")
-
- - -
-Testing output
-
- - - - -
- diff --git a/test/documents/inline/markdown_beamer.tex.ref b/test/documents/inline/markdown_beamer.tex.ref deleted file mode 100644 index f3bc53d..0000000 --- a/test/documents/inline/markdown_beamer.tex.ref +++ /dev/null @@ -1,22 +0,0 @@ - - - -\begin{frame}[fragile] -\frametitle{Random plot} - -Some inline output - - -\begin{lstlisting} -(*@\HLJLnf{println}@*)(*@\HLJLp{(}@*)(*@\HLJLs{"{}Testing}@*) (*@\HLJLs{output"{}}@*)(*@\HLJLp{)}@*) -\end{lstlisting} - -\begin{lstlisting} -Testing output -\end{lstlisting} - - - - -\end{frame} - diff --git a/test/documents/jupyter_test.jmd b/test/documents/jupyter_test.jmd deleted file mode 100644 index ae0570a..0000000 --- a/test/documents/jupyter_test.jmd +++ /dev/null @@ -1,17 +0,0 @@ -Here's some text - -And here's some code - -```julia - x = 1 - y = 2 - @show x + y -``` - -Here's some more complicated code - -```julia - @code_native +(1.0, π) - using Test - @test 1 == 1 -``` diff --git a/test/documents/markdown_beamer.jmd b/test/documents/markdown_beamer.jmd deleted file mode 100644 index a07602c..0000000 --- a/test/documents/markdown_beamer.jmd +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: A minimal beamer example using Weave markdown -author: Matti Pastell -weave_options: - out_path: inline ---- - -```julia; echo=false -struct Begin - text - title -end - -struct End - text -end -Base.show(io::IO, m::MIME"text/latex", b::Begin) = write(io, "\\begin{$(b.text)}[fragile]\n\\frametitle{$(b.title)}\n") -Base.show(io::IO, m::MIME"text/latex", e::End) = write(io, "\\end{$(e.text)}") - -Base.show(io::IO, m::MIME"text/html", b::Begin) = write(io, "
\n") -Base.show(io::IO, m::MIME"text/html", e::End) = write(io, "
") -``` - -! Begin("frame", "Random plot") - -Some inline `j print("output")` - -```julia -println("Testing output") -``` -! End("frame") diff --git a/test/documents/markdown_output.jmd b/test/documents/markdown_output.jmd deleted file mode 100644 index 68769cc..0000000 --- a/test/documents/markdown_output.jmd +++ /dev/null @@ -1,31 +0,0 @@ - - -```julia -display("text/markdown", -""" - -### Small markdown sample - -**Hello** from `code` block. - -""") -``` - - -```julia -struct Dummy - s::String -end - -function Base.show(io::IO, m::MIME"text/markdown", d::Dummy) - print(io, d.s) -end - -Dummy(""" - -* one -* two -* three - -""") -``` diff --git a/test/documents/plotsjl/plotsjl_test.jl b/test/documents/plotsjl/plotsjl_test.jl deleted file mode 100644 index d5db5e2..0000000 --- a/test/documents/plotsjl/plotsjl_test.jl +++ /dev/null @@ -1,16 +0,0 @@ - -using Plots -pyplot() -x = range(0, stop=2*pi, length=50) -println(x) -p = plot(x = x, y = sin(x), size =(900,300)) - -plot(x = x, y = sin(x)) - -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) - -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) - -plot(y = cumsum(randn(1000, 1))) diff --git a/test/documents/plotsjl/plotsjl_test.md.ref b/test/documents/plotsjl/plotsjl_test.md.ref deleted file mode 100644 index d9a16a5..0000000 --- a/test/documents/plotsjl/plotsjl_test.md.ref +++ /dev/null @@ -1,60 +0,0 @@ -~~~~{.julia} -using Plots -pyplot() -x = linspace(0, 2*pi) -println(x) -~~~~~~~~~~~~~ - - -~~~~ -linspace(0.0,6.283185307179586,50) -~~~~ - - - -~~~~{.julia} -p = plot(x, sin(x), size =(900,300)) -~~~~~~~~~~~~~ - - - -~~~~{.julia} -julia> plot(x, sin(x)) - - -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_2_1.png)\ - - -~~~~{.julia} -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_3_1.png)\ - - -~~~~{.julia} -julia> plot(rand(100) / 3,reg=true,fill=(0,:green)) - - -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_4_1.png)\ - - -~~~~{.julia} -julia> scatter!(rand(100),markersize=6,c=:orange) - - -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_4_2.png)\ - - -![A random walk.](figures/plotsjl_test_random_1.png) diff --git a/test/documents/plotsjl/plotsjl_test.tex.ref b/test/documents/plotsjl/plotsjl_test.tex.ref deleted file mode 100644 index f838604..0000000 --- a/test/documents/plotsjl/plotsjl_test.tex.ref +++ /dev/null @@ -1,47 +0,0 @@ -\begin{juliacode} -using Plots -pyplot() -x = linspace(0, 2*pi) -println(x) -\end{juliacode} -\begin{juliaout} -linspace(0.0,6.283185307179586,50) -\end{juliaout} - -\begin{juliacode} -p = plot(x, sin(x), size =(900,300)) -\end{juliacode} - -\begin{juliaterm} -julia> plot(x, sin(x)) - - -\end{juliaterm} -\includegraphics[width=\linewidth]{figures/plotsjl_test_2_1.pdf} - -\begin{juliacode} -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -\end{juliacode} -\includegraphics[width=\linewidth]{figures/plotsjl_test_3_1.pdf} - -\begin{juliaterm} -julia> plot(rand(100) / 3,reg=true,fill=(0,:green)) - - -\end{juliaterm} -\includegraphics[width=\linewidth]{figures/plotsjl_test_4_1.pdf} - -\begin{juliaterm} -julia> scatter!(rand(100),markersize=6,c=:orange) - - -\end{juliaterm} -\includegraphics[width=\linewidth]{figures/plotsjl_test_4_2.pdf} - -\begin{figure}[htpb] -\center -\includegraphics[width=\linewidth]{figures/plotsjl_test_random_1.pdf} -\caption{A random walk.} -\label{fig:random} -\end{figure} diff --git a/test/documents/plotsjl/plotsjl_test_gr.md.ref b/test/documents/plotsjl/plotsjl_test_gr.md.ref deleted file mode 100644 index 1ae8320..0000000 --- a/test/documents/plotsjl/plotsjl_test_gr.md.ref +++ /dev/null @@ -1,41 +0,0 @@ -~~~~{.julia} -using Plots -gr() -x = range(0, stop=2π, length=50) -println(x) -~~~~~~~~~~~~~ - - -~~~~ -0.0:0.1282282715750936:6.283185307179586 -~~~~ - - - -~~~~{.julia} -p = plot(x, sin.(x), size =(900,300)) -p -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_gr_1_1.png)\ - - -~~~~{.julia} -plot(x, sin.(x)) -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_gr_2_1.png)\ - - -~~~~{.julia} -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -~~~~~~~~~~~~~ - - -![](figures/plotsjl_test_gr_3_1.png)\ - - -![A random walk.](figures/plotsjl_test_gr_random_1.png){#fig:random} diff --git a/test/documents/plotsjl/plotsjl_test_gr.tex.ref b/test/documents/plotsjl/plotsjl_test_gr.tex.ref deleted file mode 100644 index 9705a33..0000000 --- a/test/documents/plotsjl/plotsjl_test_gr.tex.ref +++ /dev/null @@ -1,33 +0,0 @@ -\begin{juliacode} -using Plots -gr() -x = range(0, stop=2π, length=50) -println(x) -\end{juliacode} -\begin{juliaout} -0.0:0.1282282715750936:6.283185307179586 -\end{juliaout} - -\begin{juliacode} -p = plot(x, sin.(x), size =(900,300)) -p -\end{juliacode} -\includegraphics[width=\linewidth]{figures/plotsjl_test_gr_1_1.pdf} - -\begin{juliacode} -plot(x, sin.(x)) -\end{juliacode} -\includegraphics[width=\linewidth]{figures/plotsjl_test_gr_2_1.pdf} - -\begin{juliacode} -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -\end{juliacode} -\includegraphics[width=\linewidth]{figures/plotsjl_test_gr_3_1.pdf} - -\begin{figure}[htpb] -\center -\includegraphics[width=\linewidth]{figures/plotsjl_test_gr_random_1.pdf} -\caption{A random walk.} -\label{fig:random} -\end{figure} diff --git a/test/documents/plotsjl_test.jmd b/test/documents/plotsjl_test.jmd deleted file mode 100644 index 04bfd80..0000000 --- a/test/documents/plotsjl_test.jmd +++ /dev/null @@ -1,29 +0,0 @@ - - -```julia -using Plots -pyplot() -x = linspace(0, 2*pi) -println(x) -p = plot(x, sin(x), size =(900,300)) -``` - - -```julia; term=true -plot(x, sin(x)) -``` - -```julia -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -``` - -```julia; term=true -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -``` - - -```{julia;echo=false; fig_cap="A random walk."; label="random"} -plot(cumsum(randn(1000, 1))) -``` diff --git a/test/documents/plotsjl_test_gr.jmd b/test/documents/plotsjl_test_gr.jmd deleted file mode 100644 index 77a3396..0000000 --- a/test/documents/plotsjl_test_gr.jmd +++ /dev/null @@ -1,25 +0,0 @@ - - -```julia -using Plots -gr() -x = range(0, stop=2π, length=50) -println(x) -p = plot(x, sin.(x), size =(900,300)) -p -``` - - -```julia -plot(x, sin.(x)) -``` - -```julia -plot(rand(100) / 3,reg=true,fill=(0,:green)) -scatter!(rand(100),markersize=6,c=:orange) -``` - - -```{julia;echo=false; fig_cap="A random walk."; label="random"} -plot(cumsum(randn(1000, 1), dims=1)) -``` diff --git a/test/documents/tangle/chunk_options.jl.ref b/test/documents/tangle/chunk_options.jl.ref deleted file mode 100644 index e6c6d0d..0000000 --- a/test/documents/tangle/chunk_options.jl.ref +++ /dev/null @@ -1,37 +0,0 @@ - -y= [2, 5, 12] - - -x = 1:10 -d = Dict("Weave" => "testing") -y = [2, 4 ,8] - - -x = [12, 10] -println(y) -println(x) - - -println("Results without code") -println(x) - - -y = 1:5 -println(y) - - -a = "Don't print me" -println(a) - - -println("No markup for results.") - - -println(collect(0:10:1000)) - - -println(collect(0:10:1000)) - - -println(collect(0:10:1000)) - diff --git a/test/end2end.jl b/test/end2end.jl deleted file mode 100644 index ac9af4e..0000000 --- a/test/end2end.jl +++ /dev/null @@ -1,32 +0,0 @@ -# NOTE -# this file keeps old end2end tests, which are very fragile -# - they are being gradually replaced with unit tests, that are much more maintainable and -# much more helpful for detecting bugs -# - the purpose of this file is to temporarily keep the old end2end tests in a way that -# they're allowed to fail - -tpl = mt""" -{{{ :body }}} -""" - -out = weave(joinpath(@__DIR__, "documents", "markdown_beamer.jmd"), doctype="md2html", template=tpl) -@test read(out, String) == read(out*".ref", String) -rm(out) - -out = weave(joinpath(@__DIR__, "documents", "markdown_beamer.jmd"), doctype="md2tex", template=tpl) -@test read(out, String) == read(out*".ref", String) -rm(out) - - -@testset "chunk options" begin - -result = read("documents/chunk_options.md", String) -ref = read("documents/chunk_options_ref.md", String) -@test result == ref - -tangle("documents/chunk_options.noweb", out_path = "documents/tangle") -result = read("documents/tangle/chunk_options.jl", String) -ref = read("documents/tangle/chunk_options.jl.ref", String) -@test ref == result - -end diff --git a/test/formatter_test.jl b/test/formatter_test.jl index 11a03e8..52fb9d5 100644 --- a/test/formatter_test.jl +++ b/test/formatter_test.jl @@ -1,85 +1,3 @@ -# TODO: this test is horrible, refactor - -using Weave: Highlights.Themes.DefaultTheme - -# Test rendering of doc chunks -content = """ -# Test chunk - -Test rendering \$\alpha\$ -""" - -dchunk = Weave.DocChunk(content, 1, 1) - -pformat = Weave.FORMATS["github"] -f = Weave.format_chunk(dchunk, pformat) -@test f == content - -docformat = Weave.FORMATS["md2html"] -f_check = "

Test chunk

\n

Test rendering \$\alpha\$

\n" -f = Weave.format_chunk(dchunk, docformat) -@test f_check == f - -# Test with actual doc - -parsed = Weave.WeaveDoc("documents/chunk_options.noweb") -doc = run_doc(parsed, doctype = "md2html") - -c_check = "
\nx = [12, 10]\nprintln(y)\n
\n" -doc.format.highlight_theme = DefaultTheme -c = Weave.format_code(doc.chunks[3].content, doc.format) -@test c_check == c - -o_check = "\nprintln(x)\n" -o = Weave.format_output(doc.chunks[4].content, doc.format) -@test o_check == o - -# Tex format -parsed = Weave.WeaveDoc("documents/chunk_options.noweb") -doc = run_doc(parsed, doctype = "md2tex") - -c_check = "\\begin{lstlisting}\n(*@\\HLJLnf{println}@*)(*@\\HLJLp{(}@*)(*@\\HLJLn{x}@*)(*@\\HLJLp{)}@*)\n\\end{lstlisting}\n" -doc.format.highlight_theme = DefaultTheme -c = Weave.format_code(doc.chunks[4].content, doc.format) -@test c_check == c - -o_check = "\nx = [12, 10]\nprintln(y)\n" -o = Weave.format_output(doc.chunks[3].content, doc.format) -@test o_check == o - -# Test wrapping - -cows = repeat("🐄", 100) -testcows = """ -🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄 -🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄🐄""" - -wcows = Weave.wrapline(cows) - -@test wcows == testcows -@test length(split(wcows, "\n")[1]) == 75 -@test length(split(wcows, "\n")[2]) == 25 - - -tfied = "\\ensuremath{\\bm{\\mathrm{L}}} \\ensuremath{\\bm{\\mathfrak{F}}} \\ensuremath{\\bm{\\iota}} \\ensuremath{\\mathfrak{A}} \\ensuremath{\\bm{\\varTheta}}" - -@test Weave.uc2tex("𝐋 𝕱 𝛊 𝔄 𝚹") == tfied - -# Test markdown output from chunks -parsed = Weave.WeaveDoc("documents/markdown_output.jmd") -doc = run_doc(parsed, doctype = "md2html") -@test doc.chunks[1].rich_output == "\n

Small markdown sample

\n

Hello from code block.

\n
" -@test doc.chunks[2].rich_output == "\n
    \n
  • one

    \n
  • \n
  • two

    \n
  • \n
  • three

    \n
  • \n
\n
" - -ldoc = run_doc(parsed, doctype = "md2tex") -@test ldoc.chunks[1].rich_output == "\n\\subsubsection{Small markdown sample}\n\\textbf{Hello} from \\texttt{code} block.\n\n" -@test ldoc.chunks[2].rich_output == "\n\\begin{itemize}\n\\item one\n\n\n\\item two\n\n\n\\item three\n\n\\end{itemize}\n" - -mdoc = run_doc(parsed, doctype = "github") -@test mdoc.chunks[1].rich_output == "\n\n### Small markdown sample\n\n**Hello** from `code` block.\n\n" -@test mdoc.chunks[2].rich_output == "\n\n* one\n* two\n* three\n\n" - - # Test disable escaping of unicode @testset "escape/unescape unicode characters" begin diff --git a/test/gadfly_formats.jl b/test/gadfly_formats.jl deleted file mode 100644 index f9c5afd..0000000 --- a/test/gadfly_formats.jl +++ /dev/null @@ -1,31 +0,0 @@ -# Test for Gadfly with different chunk options and figure formatsusing Weave - -using Gadfly, Cairo - - -function test_gadfly(doctype, fig_ext) - out = weave( - joinpath(@__DIR__ , "documents/gadfly_formats_test.jnw"), - doctype = doctype, - fig_ext = fig_ext - ) - result = read(out, String) - # cp(out, out*fig_ext*"."*doctype, force=true) # Used when adding new tests - ref = read(out*fig_ext*"."*doctype, String) - @test result == ref - rm(out) -end - -test_gadfly("github", ".png") -test_gadfly("github", ".pdf") -test_gadfly("github", ".svg") -test_gadfly("pandoc", ".png") -test_gadfly("pandoc", ".js.svg") -test_gadfly("tex", ".pdf") -test_gadfly("tex", ".png") -test_gadfly("tex", ".ps") -test_gadfly("tex", ".tex") - -p = Gadfly.plot(x=1:10, y=1:10) -@test showable(MIME"application/pdf"(), p) == true -@test showable(MIME"application/png"(), p) == true diff --git a/test/markdown_test.jl b/test/markdown_test.jl index ce54618..5d23ccd 100644 --- a/test/markdown_test.jl +++ b/test/markdown_test.jl @@ -1,6 +1,9 @@ -using Test -import Weave: WeaveMarkdown -import Markdown +# TODO: make this more sensible: +# - separate tests for +# 1. features that are "copy-and-pasted" from `Markdown` module +# 2. features that are extended by Weave + +using Weave: WeaveMarkdown, Markdown # Test markdown2html writer diff --git a/test/notebooks.jl b/test/notebooks.jl deleted file mode 100644 index ece21ac..0000000 --- a/test/notebooks.jl +++ /dev/null @@ -1,7 +0,0 @@ -file = joinpath(@__DIR__, "documents", "jupyter_test.jmd") -using IJulia, Conda - -Conda.add("nbconvert") # should be the same as IJulia.JUPYTER, i.e. the miniconda Python - -Weave.notebook(file, jupyter_path = IJulia.JUPYTER) -@test "jupyter_test.ipynb" ∈ readdir(@__DIR__) # test if the result was weaved diff --git a/test/plotsjl_test.jl b/test/plotsjl_test.jl deleted file mode 100644 index eff1ec1..0000000 --- a/test/plotsjl_test.jl +++ /dev/null @@ -1,22 +0,0 @@ -using Plots - - -function pljtest(source, resfile, doctype) - weave("documents/$source", out_path = "documents/plotsjl/$resfile", doctype=doctype) - result = read("documents/plotsjl/$resfile", String) - ref = read("documents/plotsjl/$resfile.ref", String) - @test result == ref - rm("documents/plotsjl/$resfile") -end - -pljtest("plotsjl_test_gr.jmd", "plotsjl_test_gr.md", "pandoc") -pljtest("plotsjl_test_gr.jmd", "plotsjl_test_gr.tex", "tex") - -# test cache with plots -isdir("documents/cache") && rm("documents/cache", recursive = true) -weave("documents/plotsjl_test_gr.jmd", cache=:all) -result = read("documents/plotsjl_test_gr.html", String) -rm("documents/plotsjl_test_gr.html") -weave("documents/plotsjl_test_gr.jmd", cache=:all) -cached_result = read("documents/plotsjl_test_gr.html", String) -@test result == cached_result diff --git a/test/test_chunk_options.jl b/test/reader/test_chunk_options.jl similarity index 100% rename from test/test_chunk_options.jl rename to test/reader/test_chunk_options.jl diff --git a/test/test_inline.jl b/test/reader/test_inline.jl similarity index 100% rename from test/test_inline.jl rename to test/reader/test_inline.jl diff --git a/test/test_error_rendering.jl b/test/run/test_error.jl similarity index 69% rename from test/test_error_rendering.jl rename to test/run/test_error.jl index 4c4c27d..f3820a8 100644 --- a/test/test_error_rendering.jl +++ b/test/run/test_error.jl @@ -1,3 +1,5 @@ +@testset "evaluation error handling" begin + using Weave: unwrap_load_err @@ -27,7 +29,7 @@ $err_stmt1 $err_stmt2 ``` -```julia; term=true +```julia; term = true $err_stmt3 ``` """ @@ -47,6 +49,17 @@ let doc = mock_run(str; doctype = "github") @test occursin(err_str3_2, get_output(3)) end -@test_throws ArgumentError mock_run(str; doctype = "github", throw_errors = true) +# TODO: move this into chunk option tests +str = """ +```julia; error = true +using # won't be thrown +``` -# TODO: test error rendering in `rich_output` +```julia; error = false +using NonExisting # will be thrown +``` +""" + +@test_throws ArgumentError mock_run(str; doctype = "github") + +end # @testset "evaluation error handling" diff --git a/test/test_meta.jl b/test/run/test_meta.jl similarity index 74% rename from test/test_meta.jl rename to test/run/test_meta.jl index cacb0b8..144109a 100644 --- a/test/test_meta.jl +++ b/test/run/test_meta.jl @@ -1,3 +1,5 @@ +@testset "meta information for evaluation" begin + doc_body = """ ```julia include("test_include.jl") @@ -23,13 +25,13 @@ include("test_include.jl") read("./test_include.jl", String) ``` """ -doc_dir = joinpath(@__DIR__, "mocks") -doc_path = joinpath(doc_dir, "test_meta.jmd") +doc_dir = normpath(@__DIR__, "..", "mocks") +doc_path = normpath(doc_dir, "test_meta.jmd") write(doc_path, doc_body) script_line = ":include_me" script_body = "$script_line" -script_path = joinpath(@__DIR__, "mocks", "test_include.jl") +script_path = normpath(@__DIR__, "..", "mocks", "test_include.jl") write(script_path, script_body) @@ -43,3 +45,5 @@ check_output(i, s) = occursin(s, mock.chunks[i].output) @test check_output(4, doc_path) @test_broken check_output(5, 18) @test check_output(6, string('"', script_line, '"')) # current working directory + +end # @testset "meta information for evaluation" diff --git a/test/run/test_module.jl b/test/run/test_module.jl new file mode 100644 index 0000000..8757953 --- /dev/null +++ b/test/run/test_module.jl @@ -0,0 +1,49 @@ +@testset "module evaluation" begin + +function mock_output(str, mod = nothing) + result_doc = mock_run(str; mod = mod) + return result_doc.chunks[1].output +end + +str = """ +```julia +@__MODULE__ +``` +""" + +# in sandbox +@test occursin(r"\#+WeaveSandBox[\#\d]+", mock_output(str)) + +# in Main +@test strip(mock_output(str, Main)) == "Main" + +end # @testset "module evaluation" + +@testset "clear_module!" begin + +ary = rand(1000000) +size = Base.summarysize(ary) + +# simple case +m = Core.eval(@__MODULE__, :(module $(gensym(:WeaveTestModule)) end)) +Core.eval(m, :(a = $ary)) +Weave.clear_module!(m) +@test Base.summarysize(m) < size + +# recursive case +m = Core.eval(@__MODULE__, :(module $(gensym(:WeaveTestModule)) end)) +Core.eval(m, :( + module $(gensym(:WeaveTestSubModule)) + a = $ary + end +)) +Weave.clear_module!(m) +@test Base.summarysize(m) < size + +# doesn't work with constants +m = Core.eval(@__MODULE__, :(module $(gensym(:WeaveTestModule)) end)) +Core.eval(m, :(const a = $ary)) +Weave.clear_module!(m) +@test_broken Base.summarysize(m) < size + +end # @testset "clear_module!" diff --git a/test/runtests.jl b/test/runtests.jl index 84ba8ed..8831c72 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,10 +1,15 @@ +# TODO: +# - reorganize this +# - test for `include_weave` +# - fire horrible tests +# - test for ipynb integration +# - test for integrations with other libraries, especially for Plots.jl and Gadfly.jl + +# %% using Weave, Test using Weave: WeaveDoc, run_doc -# TODO: add test for header processsing -# TODO: add test for `include_weave` - function mock_doc(str, informat = "markdown") f = tempname() write(f, str) @@ -27,29 +32,21 @@ function test_mock_weave(test_function, str; kwargs...) end +# %% @testset "Weave" begin - @testset "module evaluation" begin - include("test_module_evaluation.jl") + @testset "reader" begin + include("reader/test_chunk_options.jl") + include("reader/test_inline.jl") end - @testset "header" begin + @testset "header processing" begin include("test_header.jl") end - @testset "inline" begin - include("test_inline.jl") - end - - @testset "chunk options" begin - include("test_chunk_options.jl") - end - - @testset "evaluation's meta info" begin - include("test_meta.jl") - end - - @testset "error rendering" begin - include("test_error_rendering.jl") + @testset "run" begin + include("run/test_module.jl") + include("run/test_meta.jl") + include("run/test_error.jl") end @testset "conversions" begin @@ -60,39 +57,10 @@ end include("test_display.jl") end - @testset "Formatters" begin + @testset "legacy" begin include("formatter_test.jl") include("markdown_test.jl") include("figureformatter_test.jl") - end - - @testset "Cache" begin include("cache_test.jl") end - - # @testset "Notebooks" begin - # @info("Testing Jupyter options") - # include("notebooks.jl") - # end - - # trigger only on CI - if get(ENV, "CI", nothing) == "true" - @testset "Plots" begin - include("plotsjl_test.jl") - end - - @testset "Gadfly" begin - include("gadfly_formats.jl") - end - else - @info "skipped Plots.jl and Gadfly.jl integration test" - end - - try - @testset "end2end (maybe fail)" begin - include("end2end.jl") - end - catch err - @error err - end end diff --git a/test/templates/mini.tpl b/test/templates/mini.tpl deleted file mode 100644 index 6279a0f..0000000 --- a/test/templates/mini.tpl +++ /dev/null @@ -1,2 +0,0 @@ - -{{{ :body }}} diff --git a/test/test_module_evaluation.jl b/test/test_module_evaluation.jl deleted file mode 100644 index bc2d3cb..0000000 --- a/test/test_module_evaluation.jl +++ /dev/null @@ -1,45 +0,0 @@ -@testset "evaluation module" begin - function mock_output(str, mod = nothing) - result_doc = mock_run(str; mod = mod) - return result_doc.chunks[1].output - end - - str = """ - ```julia - @__MODULE__ - ``` - """ - - # in sandbox - @test occursin(r"\#+WeaveSandBox[\#\d]+", mock_output(str)) - - # in Main - @test strip(mock_output(str, Main)) == "Main" -end - -@testset "clear_module!" begin - ary = rand(1000000) - size = Base.summarysize(ary) - - # simple case - m = Core.eval(@__MODULE__, :(module $(gensym(:WeaveTestModule)) end)) - Core.eval(m, :(a = $ary)) - Weave.clear_module!(m) - @test Base.summarysize(m) < size - - # recursive case - m = Core.eval(@__MODULE__, :(module $(gensym(:WeaveTestModule)) end)) - Core.eval(m, :( - module $(gensym(:WeaveTestSubModule)) - a = $ary - end - )) - Weave.clear_module!(m) - @test Base.summarysize(m) < size - - # doesn't work with constants - m = Core.eval(@__MODULE__, :(module $(gensym(:WeaveTestModule)) end)) - Core.eval(m, :(const a = $ary)) - Weave.clear_module!(m) - @test_broken Base.summarysize(m) < size -end