mirror of https://github.com/mpastell/Weave.jl
rename formats
parent
5f0d9d4627
commit
82c7c2a8c6
|
@ -12,8 +12,8 @@ render_termchunk(docformat::HTMLFormat, chunk) =
|
||||||
# Julia markdown
|
# Julia markdown
|
||||||
# --------------
|
# --------------
|
||||||
|
|
||||||
Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
|
Base.@kwdef mutable struct WeaveHTML <: HTMLFormat
|
||||||
description = "Julia markdown to html"
|
description = "Weave-style HTML"
|
||||||
extension = "html"
|
extension = "html"
|
||||||
codestart = "\n"
|
codestart = "\n"
|
||||||
codeend = "\n"
|
codeend = "\n"
|
||||||
|
@ -33,9 +33,9 @@ Base.@kwdef mutable struct JMarkdown2HTML <: HTMLFormat
|
||||||
stylesheet = nothing
|
stylesheet = nothing
|
||||||
highlight_theme = nothing
|
highlight_theme = nothing
|
||||||
end
|
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
|
template_path = isnothing(template) ? normpath(TEMPLATE_DIR, "md2html.tpl") : template
|
||||||
docformat.template = get_mustache_template(template_path)
|
docformat.template = get_mustache_template(template_path)
|
||||||
stylesheet_path = isnothing(css) ? normpath(STYLESHEET_DIR, "skeleton.css") : css
|
stylesheet_path = isnothing(css) ? normpath(STYLESHEET_DIR, "skeleton.css") : css
|
||||||
|
@ -44,7 +44,7 @@ function set_format_options!(docformat::JMarkdown2HTML; template = nothing, css
|
||||||
end
|
end
|
||||||
|
|
||||||
# very similar to tex version of function
|
# very similar to tex version of function
|
||||||
function render_chunk(docformat::JMarkdown2HTML, chunk::DocChunk)
|
function render_chunk(docformat::WeaveHTML, chunk::DocChunk)
|
||||||
out = IOBuffer()
|
out = IOBuffer()
|
||||||
io = IOBuffer()
|
io = IOBuffer()
|
||||||
for inline in chunk.content
|
for inline in chunk.content
|
||||||
|
@ -63,9 +63,9 @@ function render_chunk(docformat::JMarkdown2HTML, chunk::DocChunk)
|
||||||
return take2string!(out)
|
return take2string!(out)
|
||||||
end
|
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
|
fignames = chunk.figures
|
||||||
caption = chunk.options[:fig_cap]
|
caption = chunk.options[:fig_cap]
|
||||||
width = chunk.options[:out_width]
|
width = chunk.options[:out_width]
|
||||||
|
@ -104,7 +104,7 @@ function render_figures(docformat::JMarkdown2HTML, chunk)
|
||||||
return result
|
return result
|
||||||
end
|
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_source = splitdir(abspath(doc.source))
|
||||||
weave_version, weave_date = weave_info()
|
weave_version, weave_date = weave_info()
|
||||||
|
|
||||||
|
@ -125,7 +125,7 @@ end
|
||||||
# ------
|
# ------
|
||||||
|
|
||||||
Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
|
Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
|
||||||
description = "Markdown to HTML (requires Pandoc 2)"
|
description = "HTML via intermediate Pandoc Markdown (requires Pandoc 2)"
|
||||||
extension = "md"
|
extension = "md"
|
||||||
codestart = "\n"
|
codestart = "\n"
|
||||||
codeend = "\n"
|
codeend = "\n"
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# ---------------
|
# ---------------
|
||||||
|
|
||||||
Base.@kwdef mutable struct GitHubMarkdown <: WeaveFormat
|
Base.@kwdef mutable struct GitHubMarkdown <: WeaveFormat
|
||||||
description = "GitHub markdown"
|
description = "GitHub Markdown"
|
||||||
extension = "md"
|
extension = "md"
|
||||||
codestart = "````julia"
|
codestart = "````julia"
|
||||||
codeend = "````\n\n"
|
codeend = "````\n\n"
|
||||||
|
@ -48,7 +48,7 @@ end
|
||||||
# -------------
|
# -------------
|
||||||
|
|
||||||
Base.@kwdef mutable struct Hugo <: WeaveFormat
|
Base.@kwdef mutable struct Hugo <: WeaveFormat
|
||||||
description = "Hugo markdown (using shortcodes)"
|
description = "Hugo Markdown (using shortcodes)"
|
||||||
extension = "md"
|
extension = "md"
|
||||||
codestart = "````julia"
|
codestart = "````julia"
|
||||||
codeend = "````\n\n"
|
codeend = "````\n\n"
|
||||||
|
|
|
@ -34,7 +34,7 @@ function render_figures(docformat::PandocFormat, chunk)
|
||||||
end
|
end
|
||||||
|
|
||||||
Base.@kwdef mutable struct Pandoc <: PandocFormat
|
Base.@kwdef mutable struct Pandoc <: PandocFormat
|
||||||
description = "Pandoc markdown"
|
description = "Pandoc Markdown"
|
||||||
extension = "md"
|
extension = "md"
|
||||||
codestart = "~~~~{.julia}"
|
codestart = "~~~~{.julia}"
|
||||||
codeend = "~~~~~~~~~~~~~\n\n"
|
codeend = "~~~~~~~~~~~~~\n\n"
|
||||||
|
@ -55,7 +55,7 @@ register_format!("pandoc", Pandoc())
|
||||||
const DEFAULT_PANDOC_OPTIONS = String[]
|
const DEFAULT_PANDOC_OPTIONS = String[]
|
||||||
|
|
||||||
Base.@kwdef mutable struct Pandoc2PDF <: PandocFormat
|
Base.@kwdef mutable struct Pandoc2PDF <: PandocFormat
|
||||||
description = "Pandoc markdown to PDF"
|
description = "PDF via intermediate Pandoc Markdown"
|
||||||
extension = "md"
|
extension = "md"
|
||||||
codestart = "~~~~{.julia}"
|
codestart = "~~~~{.julia}"
|
||||||
codeend = "~~~~~~~~~~~~~\n\n"
|
codeend = "~~~~~~~~~~~~~\n\n"
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
# Tex
|
# 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.keep_unicode |= keep_unicode
|
||||||
docformat.template =
|
docformat.template =
|
||||||
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
|
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
|
||||||
end
|
end
|
||||||
|
|
||||||
# very similar to export to html
|
# very similar to export to html
|
||||||
function render_chunk(docformat::TexFormat, chunk::DocChunk)
|
function render_chunk(docformat::LaTeXFormat, chunk::DocChunk)
|
||||||
out = IOBuffer()
|
out = IOBuffer()
|
||||||
io = IOBuffer()
|
io = IOBuffer()
|
||||||
for inline in chunk.content
|
for inline in chunk.content
|
||||||
|
@ -30,11 +30,11 @@ function render_chunk(docformat::TexFormat, chunk::DocChunk)
|
||||||
return unicode2latex(docformat, out)
|
return unicode2latex(docformat, out)
|
||||||
end
|
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
|
# from julia symbols (e.g. "\bfhoge") to valid latex
|
||||||
const UNICODE2LATEX = let
|
const UNICODE2LATEX = let
|
||||||
|
@ -54,7 +54,7 @@ const UNICODE2LATEX = let
|
||||||
Dict(unicode => texify(sym) for (sym, unicode) in REPL.REPLCompletions.latex_symbols)
|
Dict(unicode => texify(sym) for (sym, unicode) in REPL.REPLCompletions.latex_symbols)
|
||||||
end
|
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
|
# Check whether to convert at all and return input if not
|
||||||
docformat.keep_unicode && return s
|
docformat.keep_unicode && return s
|
||||||
for (unicode, latex) in UNICODE2LATEX
|
for (unicode, latex) in UNICODE2LATEX
|
||||||
|
@ -65,7 +65,7 @@ function unicode2latex(docformat::TexFormat, s, escape = false)
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
function render_figures(docformat::TexFormat, chunk)
|
function render_figures(docformat::LaTeXFormat, chunk)
|
||||||
fignames = chunk.figures
|
fignames = chunk.figures
|
||||||
caption = chunk.options[:fig_cap]
|
caption = chunk.options[:fig_cap]
|
||||||
width = chunk.options[:out_width]
|
width = chunk.options[:out_width]
|
||||||
|
@ -133,7 +133,7 @@ function md_length_to_latex(def, reference)
|
||||||
return def
|
return def
|
||||||
end
|
end
|
||||||
|
|
||||||
function render_doc(docformat::TexFormat, body, doc)
|
function render_doc(docformat::LaTeXFormat, body, doc)
|
||||||
return Mustache.render(
|
return Mustache.render(
|
||||||
docformat.template;
|
docformat.template;
|
||||||
body = body,
|
body = body,
|
||||||
|
@ -146,8 +146,8 @@ end
|
||||||
# minted Tex
|
# minted Tex
|
||||||
# ----------
|
# ----------
|
||||||
|
|
||||||
Base.@kwdef mutable struct TexMinted <: TexFormat
|
Base.@kwdef mutable struct LaTeXMinted <: LaTeXFormat
|
||||||
description = "Latex using minted for highlighting"
|
description = "LaTeX using minted package for code highlighting"
|
||||||
extension = "tex"
|
extension = "tex"
|
||||||
codestart = "\\begin{minted}[escapeinside=||, mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}"
|
codestart = "\\begin{minted}[escapeinside=||, mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}"
|
||||||
codeend = "\\end{minted}"
|
codeend = "\\end{minted}"
|
||||||
|
@ -169,21 +169,21 @@ Base.@kwdef mutable struct TexMinted <: TexFormat
|
||||||
escape_starter = "|\$"
|
escape_starter = "|\$"
|
||||||
escape_closer = reverse(escape_starter)
|
escape_closer = reverse(escape_starter)
|
||||||
end
|
end
|
||||||
register_format!("texminted", TexMinted())
|
register_format!("texminted", LaTeXMinted())
|
||||||
|
|
||||||
# Tex (directly to PDF)
|
# 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 =
|
docformat.template =
|
||||||
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
|
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
|
||||||
docformat.highlight_theme = get_highlight_theme(highlight_theme)
|
docformat.highlight_theme = get_highlight_theme(highlight_theme)
|
||||||
docformat.keep_unicode |= keep_unicode
|
docformat.keep_unicode |= keep_unicode
|
||||||
end
|
end
|
||||||
|
|
||||||
function render_output(docformat::JMarkdownTexFormat, output)
|
function render_output(docformat::WeaveLaTeXFormat, output)
|
||||||
# Highligts has some extra escaping defined, eg of $, ", ...
|
# Highligts has some extra escaping defined, eg of $, ", ...
|
||||||
output_escaped = sprint(
|
output_escaped = sprint(
|
||||||
(io, x) ->
|
(io, x) ->
|
||||||
|
@ -193,15 +193,15 @@ function render_output(docformat::JMarkdownTexFormat, output)
|
||||||
return unicode2latex(docformat, output_escaped, true)
|
return unicode2latex(docformat, output_escaped, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function render_code(docformat::JMarkdownTexFormat, code)
|
function render_code(docformat::WeaveLaTeXFormat, code)
|
||||||
ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme)
|
ret = highlight_code(MIME("text/latex"), code, docformat.highlight_theme)
|
||||||
unicode2latex(docformat, ret, false)
|
unicode2latex(docformat, ret, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
render_termchunk(docformat::JMarkdownTexFormat, chunk) =
|
render_termchunk(docformat::WeaveLaTeXFormat, chunk) =
|
||||||
should_render(chunk) ? highlight_term(MIME("text/latex"), chunk.output, docformat.highlight_theme) : ""
|
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(
|
return Mustache.render(
|
||||||
docformat.template;
|
docformat.template;
|
||||||
body = body,
|
body = body,
|
||||||
|
@ -211,8 +211,8 @@ function render_doc(docformat::JMarkdownTexFormat, body, doc)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
Base.@kwdef mutable struct JMarkdown2Tex <: JMarkdownTexFormat
|
Base.@kwdef mutable struct WeaveLaTeX <: WeaveLaTeXFormat
|
||||||
description = "Julia markdown to LaTeX"
|
description = "Weave-styled LaTeX"
|
||||||
extension = "tex"
|
extension = "tex"
|
||||||
codestart = ""
|
codestart = ""
|
||||||
codeend = ""
|
codeend = ""
|
||||||
|
@ -235,13 +235,13 @@ Base.@kwdef mutable struct JMarkdown2Tex <: JMarkdownTexFormat
|
||||||
escape_starter = "(*@"
|
escape_starter = "(*@"
|
||||||
escape_closer = reverse(escape_starter)
|
escape_closer = reverse(escape_starter)
|
||||||
end
|
end
|
||||||
register_format!("md2tex", JMarkdown2Tex())
|
register_format!("md2tex", WeaveLaTeX())
|
||||||
|
|
||||||
# will be used by `write_doc`
|
# will be used by `write_doc`
|
||||||
const DEFAULT_LATEX_CMD = ["xelatex", "-shell-escape", "-halt-on-error"]
|
const DEFAULT_LATEX_CMD = ["xelatex", "-shell-escape", "-halt-on-error"]
|
||||||
|
|
||||||
Base.@kwdef mutable struct JMarkdown2PDF <: JMarkdownTexFormat
|
Base.@kwdef mutable struct WeaveLaTeX2PDF <: WeaveLaTeXFormat
|
||||||
description = "Julia markdown to LaTeX"
|
description = "PDF via Weave-styled LaTeX"
|
||||||
extension = "tex"
|
extension = "tex"
|
||||||
codestart = ""
|
codestart = ""
|
||||||
codeend = ""
|
codeend = ""
|
||||||
|
@ -265,9 +265,9 @@ Base.@kwdef mutable struct JMarkdown2PDF <: JMarkdownTexFormat
|
||||||
escape_starter = "(*@"
|
escape_starter = "(*@"
|
||||||
escape_closer = reverse(escape_starter)
|
escape_closer = reverse(escape_starter)
|
||||||
end
|
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 =
|
docformat.template =
|
||||||
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
|
get_mustache_template(isnothing(template) ? normpath(TEMPLATE_DIR, "md2pdf.tpl") : template)
|
||||||
docformat.highlight_theme = get_highlight_theme(highlight_theme)
|
docformat.highlight_theme = get_highlight_theme(highlight_theme)
|
||||||
|
|
|
@ -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_back = let d = pwd(); () -> cd(d); end
|
||||||
cd(doc.cwd)
|
cd(doc.cwd)
|
||||||
try
|
try
|
||||||
|
|
Loading…
Reference in New Issue