Skip to content

Commit

Permalink
Merge pull request #377 from JunoLab/avi/simpletest
Browse files Browse the repository at this point in the history
add simple end2end test
  • Loading branch information
aviatesk authored Jun 14, 2020
2 parents ba5bc9c + 82c7c2a commit 957d12e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 39 deletions.
18 changes: 9 additions & 9 deletions src/rendering/htmlformats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ render_termchunk(docformat::HTMLFormat, chunk) =
# Julia markdown
# --------------

Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
description = "Julia markdown to html"
Base.@kwdef mutable struct WeaveHTML <: HTMLFormat
description = "Weave-style HTML"
extension = "html"
codestart = "\n"
codeend = "\n"
Expand All @@ -33,9 +33,9 @@ Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
stylesheet = nothing
highlight_theme = nothing
end
register_format!("md2html", JMarkdown2HTML())
register_format!("md2html", WeaveHTML())

function set_format_options!(docformat::JMarkdown2HTML; template = nothing, css = nothing, highlight_theme = nothing, _kwargs...)
function set_format_options!(docformat::WeaveHTML; template = nothing, css = nothing, highlight_theme = nothing, _kwargs...)
template_path = isnothing(template) ? normpath(TEMPLATE_DIR, "md2html.tpl") : template
docformat.template = get_mustache_template(template_path)
stylesheet_path = isnothing(css) ? normpath(STYLESHEET_DIR, "skeleton.css") : css
Expand All @@ -44,7 +44,7 @@ function set_format_options!(docformat::JMarkdown2HTML; template = nothing, css
end

# very similar to tex version of function
function render_chunk(docformat::JMarkdown2HTML, chunk::DocChunk)
function render_chunk(docformat::WeaveHTML, chunk::DocChunk)
out = IOBuffer()
io = IOBuffer()
for inline in chunk.content
Expand All @@ -63,9 +63,9 @@ function render_chunk(docformat::JMarkdown2HTML, chunk::DocChunk)
return take2string!(out)
end

render_output(docformat::JMarkdown2HTML, output) = Markdown.htmlesc(output)
render_output(docformat::WeaveHTML, output) = Markdown.htmlesc(output)

function render_figures(docformat::JMarkdown2HTML, chunk)
function render_figures(docformat::WeaveHTML, chunk)
fignames = chunk.figures
caption = chunk.options[:fig_cap]
width = chunk.options[:out_width]
Expand Down Expand Up @@ -104,7 +104,7 @@ function render_figures(docformat::JMarkdown2HTML, chunk)
return result
end

function render_doc(docformat::JMarkdown2HTML, body, doc; css = nothing)
function render_doc(docformat::WeaveHTML, body, doc; css = nothing)
_, weave_source = splitdir(abspath(doc.source))
weave_version, weave_date = weave_info()

Expand All @@ -125,7 +125,7 @@ end
# ------

Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
description = "Markdown to HTML (requires Pandoc 2)"
description = "HTML via intermediate Pandoc Markdown (requires Pandoc 2)"
extension = "md"
codestart = "\n"
codeend = "\n"
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/miscformats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# ---------------

Base.@kwdef mutable struct GitHubMarkdown <: WeaveFormat
description = "GitHub markdown"
description = "GitHub Markdown"
extension = "md"
codestart = "````julia"
codeend = "````\n\n"
Expand Down Expand Up @@ -48,7 +48,7 @@ end
# -------------

Base.@kwdef mutable struct Hugo <: WeaveFormat
description = "Hugo markdown (using shortcodes)"
description = "Hugo Markdown (using shortcodes)"
extension = "md"
codestart = "````julia"
codeend = "````\n\n"
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/pandocformats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function render_figures(docformat::PandocFormat, chunk)
end

Base.@kwdef mutable struct Pandoc <: PandocFormat
description = "Pandoc markdown"
description = "Pandoc Markdown"
extension = "md"
codestart = "~~~~{.julia}"
codeend = "~~~~~~~~~~~~~\n\n"
Expand All @@ -57,7 +57,7 @@ register_format!("pandoc", Pandoc())
const DEFAULT_PANDOC_OPTIONS = String[]

Base.@kwdef mutable struct Pandoc2PDF <: PandocFormat
description = "Pandoc markdown to PDF"
description = "PDF via intermediate Pandoc Markdown"
extension = "md"
codestart = "~~~~{.julia}"
codeend = "~~~~~~~~~~~~~\n\n"
Expand Down
50 changes: 25 additions & 25 deletions src/rendering/texformats.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Tex
# ---

abstract type TexFormat <: WeaveFormat end
abstract type LaTeXFormat <: WeaveFormat end

function set_format_options!(docformat::TexFormat; keep_unicode = false, template = nothing, _kwargs...)
function set_format_options!(docformat::LaTeXFormat; keep_unicode = false, template = nothing, _kwargs...)
docformat.keep_unicode |= keep_unicode
docformat.template =
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
end

# very similar to export to html
function render_chunk(docformat::TexFormat, chunk::DocChunk)
function render_chunk(docformat::LaTeXFormat, chunk::DocChunk)
out = IOBuffer()
io = IOBuffer()
for inline in chunk.content
Expand All @@ -30,11 +30,11 @@ function render_chunk(docformat::TexFormat, chunk::DocChunk)
return unicode2latex(docformat, out)
end

render_output(docformat::TexFormat, output) = unicode2latex(docformat, output, true)
render_output(docformat::LaTeXFormat, output) = unicode2latex(docformat, output, true)

render_code(docformat::TexFormat, code) = unicode2latex(docformat, code, true)
render_code(docformat::LaTeXFormat, code) = unicode2latex(docformat, code, true)

render_termchunk(docformat::TexFormat, chunk) = string(docformat.termstart, chunk.output, docformat.termend, "\n")
render_termchunk(docformat::LaTeXFormat, chunk) = string(docformat.termstart, chunk.output, docformat.termend, "\n")

# from julia symbols (e.g. "\bfhoge") to valid latex
const UNICODE2LATEX = let
Expand All @@ -54,7 +54,7 @@ const UNICODE2LATEX = let
Dict(unicode => texify(sym) for (sym, unicode) in REPL.REPLCompletions.latex_symbols)
end

function unicode2latex(docformat::TexFormat, s, escape = false)
function unicode2latex(docformat::LaTeXFormat, s, escape = false)
# Check whether to convert at all and return input if not
docformat.keep_unicode && return s
for (unicode, latex) in UNICODE2LATEX
Expand All @@ -65,7 +65,7 @@ function unicode2latex(docformat::TexFormat, s, escape = false)
return s
end

function render_figures(docformat::TexFormat, chunk)
function render_figures(docformat::LaTeXFormat, chunk)
fignames = chunk.figures
caption = chunk.options[:fig_cap]
width = chunk.options[:out_width]
Expand Down Expand Up @@ -133,7 +133,7 @@ function md_length_to_latex(def, reference)
return def
end

function render_doc(docformat::TexFormat, body, doc)
function render_doc(docformat::LaTeXFormat, body, doc)
return Mustache.render(
docformat.template;
body = body,
Expand All @@ -146,8 +146,8 @@ end
# minted Tex
# ----------

Base.@kwdef mutable struct TexMinted <: TexFormat
description = "Latex using minted for highlighting"
Base.@kwdef mutable struct LaTeXMinted <: LaTeXFormat
description = "LaTeX using minted package for code highlighting"
extension = "tex"
codestart = "\\begin{minted}[escapeinside=||, mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}"
codeend = "\\end{minted}"
Expand All @@ -169,21 +169,21 @@ Base.@kwdef mutable struct TexMinted <: TexFormat
escape_starter = "|\$"
escape_closer = reverse(escape_starter)
end
register_format!("texminted", TexMinted())
register_format!("texminted", LaTeXMinted())

# Tex (directly to PDF)
# ---------------------

abstract type JMarkdownTexFormat <: TexFormat end
abstract type WeaveLaTeXFormat <: LaTeXFormat end

function set_format_options!(docformat::JMarkdownTexFormat; template = nothing, highlight_theme = nothing, keep_unicode = false, _kwargs...)
function set_format_options!(docformat::WeaveLaTeXFormat; template = nothing, highlight_theme = nothing, keep_unicode = false, _kwargs...)
docformat.template =
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
docformat.highlight_theme = get_highlight_theme(highlight_theme)
docformat.keep_unicode |= keep_unicode
end

function render_output(docformat::JMarkdownTexFormat, output)
function render_output(docformat::WeaveLaTeXFormat, output)
# Highligts has some extra escaping defined, eg of $, ", ...
output_escaped = sprint(
(io, x) ->
Expand All @@ -193,15 +193,15 @@ function render_output(docformat::JMarkdownTexFormat, output)
return unicode2latex(docformat, output_escaped, true)
end

function render_code(docformat::JMarkdownTexFormat, code)
function render_code(docformat::WeaveLaTeXFormat, code)
ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme)
unicode2latex(docformat, ret, false)
end

render_termchunk(docformat::JMarkdownTexFormat, chunk) =
render_termchunk(docformat::WeaveLaTeXFormat, chunk) =
should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, docformat.highlight_theme) : ""

function render_doc(docformat::JMarkdownTexFormat, body, doc)
function render_doc(docformat::WeaveLaTeXFormat, body, doc)
return Mustache.render(
docformat.template;
body = body,
Expand All @@ -211,8 +211,8 @@ function render_doc(docformat::JMarkdownTexFormat, body, doc)
)
end

Base.@kwdef mutable struct JMarkdown2Tex <: JMarkdownTexFormat
description = "Julia markdown to LaTeX"
Base.@kwdef mutable struct WeaveLaTeX <: WeaveLaTeXFormat
description = "Weave-styled LaTeX"
extension = "tex"
codestart = ""
codeend = ""
Expand All @@ -235,13 +235,13 @@ Base.@kwdef mutable struct JMarkdown2Tex <: JMarkdownTexFormat
escape_starter = "(*@"
escape_closer = reverse(escape_starter)
end
register_format!("md2tex", JMarkdown2Tex())
register_format!("md2tex", WeaveLaTeX())

# will be used by `write_doc`
const DEFAULT_LATEX_CMD = ["xelatex", "-shell-escape", "-halt-on-error"]

Base.@kwdef mutable struct JMarkdown2PDF <: JMarkdownTexFormat
description = "Julia markdown to LaTeX"
Base.@kwdef mutable struct WeaveLaTeX2PDF <: WeaveLaTeXFormat
description = "PDF via Weave-styled LaTeX"
extension = "tex"
codestart = ""
codeend = ""
Expand All @@ -265,9 +265,9 @@ Base.@kwdef mutable struct JMarkdown2PDF <: JMarkdownTexFormat
escape_starter = "(*@"
escape_closer = reverse(escape_starter)
end
register_format!("md2pdf", JMarkdown2PDF())
register_format!("md2pdf", WeaveLaTeX2PDF())

function set_format_options!(docformat::JMarkdown2PDF; template = nothing, highlight_theme = nothing, keep_unicode = false, latex_cmd = DEFAULT_LATEX_CMD, _kwargs...)
function set_format_options!(docformat::WeaveLaTeX2PDF; template = nothing, highlight_theme = nothing, keep_unicode = false, latex_cmd = DEFAULT_LATEX_CMD, _kwargs...)
docformat.template =
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
docformat.highlight_theme = get_highlight_theme(highlight_theme)
Expand Down
2 changes: 1 addition & 1 deletion src/writer/latex.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function write_doc(docformat::JMarkdown2PDF, doc, rendered, out_path)
function write_doc(docformat::WeaveLaTeX2PDF, doc, rendered, out_path)
cd_back = let d = pwd(); () -> cd(d); end
cd(doc.cwd)
try
Expand Down
11 changes: 11 additions & 0 deletions test/end2end/test_end2end.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# NOTE:
# This test file does end to end tests, while we don't want to check the details in the generated documents.
# Rather, we just assert we can `weave` all the supported formats without errors here.

# TODO:
# - more complex example
# - integration with other libraries, like Plots

@testset "end2end simple" begin
include("test_simple.jl")
end # @testset "end2end"
48 changes: 48 additions & 0 deletions test/end2end/test_simple.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Weave.Dates


test_doctypes = filter(first.(Weave.list_out_formats())) do doctype
# don't test doctypes which need external programs
doctype ("pandoc2html", "pandoc2pdf", "md2pdf")
end

function test_func(body)
@test !isempty(body)
date_str = string(Date(now()))
@test occursin(date_str, body)
end

# julia markdown
julia_markdown_body = """
# doc chunk
this is text with `j :inline` code
code chunk:
```julia
using Dates
Date(now())
```
"""

for doctype in test_doctypes
test_mock_weave(test_func, julia_markdown_body; informat = "markdown", doctype = doctype)
end

# TODO: test noweb format

# julia script
julia_script_body = """
#' # doc chunk
#'
#' this is text with `j :inline` code
#'
#' code chunk:
#+
using Dates
Date(now())
"""
for doctype in test_doctypes
test_mock_weave(test_func, julia_script_body; informat = "script", doctype = doctype)
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ end
include("test_display.jl")
end

@testset "end2end" begin
include("end2end/test_end2end.jl")
end

@testset "legacy" begin
include("markdown_test.jl")
include("render_figures_test.jl")
Expand Down

0 comments on commit 957d12e

Please sign in to comment.