From d683625444c15403264ecdc59afaf0ca73ae17cb Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Tue, 13 Dec 2016 14:20:40 +0200 Subject: [PATCH] Make julia Markdown the default when converting to html, Fix term chunks --- src/Weave.jl | 3 +-- src/format.jl | 24 ++++++++++++------ src/formatters.jl | 53 ++++++++++++++++++++++++++++++++++++---- templates/julia_html.txt | 6 +++-- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/Weave.jl b/src/Weave.jl index 8308cd4..1fe1dc3 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -70,7 +70,6 @@ function weave(source ; doctype = :auto, plotlib=:auto, doc = run(doc, doctype = doctype, plotlib=plotlib, out_path=out_path, fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache) - formatted = format(doc) outname = get_outname(out_path, doc) @@ -80,7 +79,7 @@ function weave(source ; doctype = :auto, plotlib=:auto, end #Convert using pandoc - if doc.doctype == "md2html" + if doc.doctype == "pandoc2html" outname = get_outname(out_path, doc, ext = "html") pandoc2html(formatted, doc, outname) elseif doc.doctype == "md2pdf" diff --git a/src/format.jl b/src/format.jl index ca2384e..bbd19ba 100644 --- a/src/format.jl +++ b/src/format.jl @@ -45,12 +45,11 @@ function render_doc(formatted, format::JMarkdown2HTML) theme_css = readstring(joinpath(dirname(@__FILE__), "../templates/skeleton_css.txt")) template = Mustache.template_from_file(joinpath(dirname(@__FILE__), "../templates/julia_html.txt")) - return Mustache.render(template, theme_css = theme_css, - highlight_css = css, body = formatted) + return Mustache.render(template, themecss = theme_css, + highlightcss = css, body = formatted) end function format_chunk(chunk::DocChunk, formatdict, docformat) - #info(typeof(docformat)) return chunk.content end @@ -88,7 +87,7 @@ function format_chunk(chunk::CodeChunk, formatdict, docformat) end if chunk.options[:term] - result = format_termchunk(chunk, formatdict) + result = format_termchunk(chunk, formatdict, docformat) else if chunk.options[:echo] @@ -153,11 +152,22 @@ function format_code(result::AbstractString, docformat::JMarkdown2HTML) return highlighted end - -function format_termchunk(chunk, formatdict) +function format_termchunk(chunk, formatdict, docformat) if chunk.options[:echo] && chunk.options[:results] != "hidden" result = "$(formatdict[:termstart])$(chunk.output)\n" * "$(formatdict[:termend])\n" - #chunk.options[:term_state] == :text && (result*= "$(formatdict[:termend])\n") + else + result = "" + end + return result +end + +function format_termchunk(chunk, formatdict, docformat::JMarkdown2HTML) + if chunk.options[:echo] && chunk.options[:results] != "hidden" + buf = PipeBuffer() + Highlights.highlight(buf, MIME("text/html"), strip(chunk.output), Highlights.Lexers.JuliaConsoleLexer) + flush(buf) + result = readstring(buf) + close(buf) else result = "" end diff --git a/src/formatters.jl b/src/formatters.jl index 109133c..f0efeca 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -57,7 +57,7 @@ const pandoc = Pandoc("Pandoc markdown", )) -const md2html = Pandoc("Markdown to HTML (requires Pandoc)", +const pdoc2html = Pandoc("Markdown to HTML (requires Pandoc)", Dict{Symbol,Any}( :codestart => "````julia", :codeend=> "````\n\n", @@ -105,20 +105,63 @@ type JMarkdown2HTML formatdict::Dict{Symbol,Any} end -const jmd2html = JMarkdown2HTML("Julia markdown", Dict{Symbol,Any}( +const md2html = JMarkdown2HTML("Julia markdown", Dict{Symbol,Any}( :codestart => "\n", :codeend=> "\n", :outputstart=> "
",
         :outputend=> "
\n", :fig_ext=> ".png", :extension=> "html", - :doctype=> "html")) + :doctype=> "md2html")) type MultiMarkdown description::AbstractString formatdict::Dict{Symbol,Any} end +function formatfigures(chunk, docformat::JMarkdown2HTML) + fignames = chunk.figures + caption = chunk.options[:fig_cap] + width = chunk.options[:out_width] + height = chunk.options[:out_height] + f_pos = chunk.options[:fig_pos] + f_env = chunk.options[:fig_env] + result = "" + figstring = "" + + #Set size + attribs = "" + width == nothing || (attribs = "width=\"$width\"") + (attribs != "" && height != nothing ) && (attribs *= ",") + height == nothing || (attribs *= " height=\"$height\" ") + + if f_env != nothing + result *= """
\n""" + end + + for fig = fignames + figstring *= """\n""" + end + + result *= figstring + + if caption != nothing + result *= """ +
$caption
+ """ + end + + if f_env != nothing + result *= "
\n" + end + + return result +end + + + + + const multimarkdown = MultiMarkdown("MultiMarkdown", Dict{Symbol,Any}( :codestart => "````julia", @@ -356,11 +399,11 @@ end const formats = Dict{AbstractString, Any}("tex" => tex, "texminted" => texminted, "pandoc" => pandoc, - "md2html" => md2html, + "pandoc2html" => pdoc2html, "md2pdf" => md2pdf, "github" => github, "multimarkdown" => multimarkdown, "rst" => rst, "asciidoc" => adoc, - "jmd2html" => jmd2html + "md2html" => md2html ) diff --git a/templates/julia_html.txt b/templates/julia_html.txt index e915877..ffa9453 100644 --- a/templates/julia_html.txt +++ b/templates/julia_html.txt @@ -5,15 +5,17 @@
+ {{{ :body }}} +