mirror of https://github.com/mpastell/Weave.jl
commit
5b959a972c
12
src/Weave.jl
12
src/Weave.jl
|
@ -27,15 +27,11 @@ end
|
||||||
take2string!(io) = String(take!(io))
|
take2string!(io) = String(take!(io))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
list_out_formats()
|
list_out_formats(io = stdout)
|
||||||
|
|
||||||
List supported output formats
|
List supported output formats
|
||||||
"""
|
"""
|
||||||
function list_out_formats()
|
list_out_formats(io = stdout) = for (k, v) in FORMATS; println(io, string(k, ": ", v.formatdict[:description])); end
|
||||||
for format in keys(formats)
|
|
||||||
println(string(format, ": ", formats[format].description))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
tangle(source::AbstractString; kwargs...)
|
tangle(source::AbstractString; kwargs...)
|
||||||
|
@ -228,7 +224,7 @@ weave(doc::AbstractString, doctype::Union{Symbol,AbstractString}; kwargs...) =
|
||||||
weave(doc; doctype = doctype, kwargs...)
|
weave(doc; doctype = doctype, kwargs...)
|
||||||
|
|
||||||
function specific_options!(weave_options, doctype)
|
function specific_options!(weave_options, doctype)
|
||||||
fmts = keys(formats)
|
fmts = keys(FORMATS)
|
||||||
for (k,v) in weave_options
|
for (k,v) in weave_options
|
||||||
if k in fmts
|
if k in fmts
|
||||||
k == doctype && merge!(weave_options, v)
|
k == doctype && merge!(weave_options, v)
|
||||||
|
@ -335,7 +331,7 @@ include("display_methods.jl")
|
||||||
include("reader/reader.jl")
|
include("reader/reader.jl")
|
||||||
include("run.jl")
|
include("run.jl")
|
||||||
include("cache.jl")
|
include("cache.jl")
|
||||||
include("formatters.jl")
|
include("formats.jl")
|
||||||
include("format.jl")
|
include("format.jl")
|
||||||
include("pandoc.jl")
|
include("pandoc.jl")
|
||||||
include("converter.jl")
|
include("converter.jl")
|
||||||
|
|
|
@ -1,185 +1,71 @@
|
||||||
# so dirty, refactor
|
# TODO:
|
||||||
|
# - 1. do assertions for definition mandatory fields in `@define_format` macro
|
||||||
using Printf
|
# - 2. implement fallback format/rendering functions in format.jl
|
||||||
|
# - 3. export this as public API
|
||||||
|
|
||||||
|
|
||||||
struct Tex
|
abstract type WeaveFormat end
|
||||||
description::AbstractString
|
const FORMATS = Dict{String,WeaveFormat}()
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
|
macro define_format(type_name, supertype = :WeaveFormat)
|
||||||
|
return quote
|
||||||
|
@assert $(supertype) <: WeaveFormat "$($(supertype)) should be subtype of WeaveFormat"
|
||||||
|
struct $(type_name) <: $(supertype)
|
||||||
|
formatdict::Dict{Symbol,Any}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
# TODO: do some assertion for necessary fields of `formatdict`
|
||||||
|
register_format!(format_name::AbstractString, format::WeaveFormat) = push!(FORMATS, format_name => format)
|
||||||
|
|
||||||
const tex = Tex(
|
# HTML
|
||||||
"Latex with custom code environments",
|
# ----
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "\\begin{juliacode}",
|
|
||||||
:codeend => "\\end{juliacode}",
|
|
||||||
:outputstart => "\\begin{juliaout}",
|
|
||||||
:outputend => "\\end{juliaout}",
|
|
||||||
:termstart => "\\begin{juliaterm}",
|
|
||||||
:termend => "\\end{juliaterm}",
|
|
||||||
:fig_ext => ".pdf",
|
|
||||||
:extension => "tex",
|
|
||||||
:out_width => "\\linewidth",
|
|
||||||
:fig_env => "figure",
|
|
||||||
:fig_pos => "htpb",
|
|
||||||
:doctype => "tex",
|
|
||||||
:mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"],
|
|
||||||
:keep_unicode => false,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
const texminted = Tex(
|
@define_format JMarkdown2HTML
|
||||||
"Latex using minted for highlighting",
|
register_format!("md2html", JMarkdown2HTML(Dict(
|
||||||
Dict{Symbol,Any}(
|
:description => "Julia markdown to html",
|
||||||
:codestart =>
|
:codestart => "\n",
|
||||||
"\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}",
|
:codeend => "\n",
|
||||||
:codeend => "\\end{minted}",
|
:outputstart => "<pre class=\"output\">",
|
||||||
:outputstart =>
|
:outputend => "</pre>\n",
|
||||||
"\\begin{minted}[fontsize=\\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}",
|
:fig_ext => ".png",
|
||||||
:outputend => "\\end{minted}",
|
:mimetypes => [
|
||||||
:termstart =>
|
"image/png",
|
||||||
"\\begin{minted}[fontsize=\\footnotesize, xleftmargin=0.5em, mathescape]{jlcon}",
|
"image/jpg",
|
||||||
:termend => "\\end{minted}",
|
"image/svg+xml",
|
||||||
:fig_ext => ".pdf",
|
"text/html",
|
||||||
:extension => "tex",
|
"text/markdown",
|
||||||
:out_width => "\\linewidth",
|
"text/plain",
|
||||||
:fig_env => "figure",
|
],
|
||||||
:fig_pos => "htpb",
|
:extension => "html",
|
||||||
:doctype => "texminted",
|
)))
|
||||||
:mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"],
|
|
||||||
:keep_unicode => false,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
struct Pandoc
|
@define_format Pandoc2HTML
|
||||||
description::AbstractString
|
register_format!("pandoc2html", Pandoc2HTML(Dict(
|
||||||
formatdict::Dict{Symbol,Any}
|
:description => "Markdown to HTML (requires Pandoc 2)",
|
||||||
end
|
:codestart => "\n",
|
||||||
|
:codeend => "\n",
|
||||||
|
:outputstart => "\n",
|
||||||
|
:outputend => "\n",
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:extension => "md",
|
||||||
|
:mimetypes => [
|
||||||
|
"image/png",
|
||||||
|
"image/svg+xml",
|
||||||
|
"image/jpg",
|
||||||
|
"text/html",
|
||||||
|
"text/markdown",
|
||||||
|
"text/plain",
|
||||||
|
],
|
||||||
|
)))
|
||||||
|
|
||||||
const pandoc = Pandoc(
|
|
||||||
"Pandoc markdown",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "~~~~{.julia}",
|
|
||||||
:codeend => "~~~~~~~~~~~~~\n\n",
|
|
||||||
:outputstart => "~~~~",
|
|
||||||
:outputend => "~~~~\n\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:out_width => nothing,
|
|
||||||
:extension => "md",
|
|
||||||
# Prefer png figures for markdown conversion, svg doesn't work with latex
|
|
||||||
:mimetypes =>
|
|
||||||
["image/png", "image/jpg", "image/svg+xml", "text/markdown", "text/plain"],
|
|
||||||
:doctype => "pandoc",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
struct Pandoc2HTML
|
# PDF and Tex
|
||||||
description::AbstractString
|
# -----------
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
end
|
|
||||||
|
|
||||||
const pdoc2html = Pandoc2HTML(
|
@define_format JMarkdown2tex
|
||||||
"Markdown to HTML (requires Pandoc 2)",
|
let t = JMarkdown2tex(Dict(
|
||||||
Dict{Symbol,Any}(
|
:description => "Julia markdown to latex",
|
||||||
:codestart => "\n",
|
|
||||||
:codeend => "\n",
|
|
||||||
:outputstart => "\n",
|
|
||||||
:outputend => "\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:extension => "md",
|
|
||||||
:mimetypes => [
|
|
||||||
"image/png",
|
|
||||||
"image/svg+xml",
|
|
||||||
"image/jpg",
|
|
||||||
"text/html",
|
|
||||||
"text/markdown",
|
|
||||||
"text/plain",
|
|
||||||
],
|
|
||||||
:doctype => "pandoc2html",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
struct GitHubMarkdown
|
|
||||||
description::AbstractString
|
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
end
|
|
||||||
|
|
||||||
const github = GitHubMarkdown(
|
|
||||||
"GitHub markdown",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "````julia",
|
|
||||||
:codeend => "````\n\n",
|
|
||||||
:outputstart => "````",
|
|
||||||
:outputend => "````\n\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:extension => "md",
|
|
||||||
:mimetypes =>
|
|
||||||
["image/png", "image/svg+xml", "image/jpg", "text/markdown", "text/plain"],
|
|
||||||
:doctype => "github",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
"""
|
|
||||||
Formatter for Hugo: https://gohugo.io/
|
|
||||||
|
|
||||||
When `uglyURLs` is `false`, prepend figure path by `..`.
|
|
||||||
"""
|
|
||||||
struct Hugo
|
|
||||||
description::AbstractString
|
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
uglyURLs::Bool
|
|
||||||
end
|
|
||||||
|
|
||||||
const hugo = Hugo(
|
|
||||||
"Hugo markdown (using shortcodes)",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "````julia",
|
|
||||||
:codeend => "````\n\n",
|
|
||||||
:outputstart => "````",
|
|
||||||
:outputend => "````\n\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:extension => "md",
|
|
||||||
:doctype => "hugo",
|
|
||||||
),
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Julia markdown
|
|
||||||
struct JMarkdown2HTML
|
|
||||||
description::AbstractString
|
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
end
|
|
||||||
|
|
||||||
const md2html = JMarkdown2HTML(
|
|
||||||
"Julia markdown to html",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "\n",
|
|
||||||
:codeend => "\n",
|
|
||||||
:outputstart => "<pre class=\"output\">",
|
|
||||||
:outputend => "</pre>\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:mimetypes => [
|
|
||||||
"image/png",
|
|
||||||
"image/jpg",
|
|
||||||
"image/svg+xml",
|
|
||||||
"text/html",
|
|
||||||
"text/markdown",
|
|
||||||
"text/plain",
|
|
||||||
],
|
|
||||||
:extension => "html",
|
|
||||||
:doctype => "md2html",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Julia markdown
|
|
||||||
struct JMarkdown2tex
|
|
||||||
description::AbstractString
|
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
end
|
|
||||||
|
|
||||||
const md2tex = JMarkdown2tex(
|
|
||||||
"Julia markdown to latex",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "",
|
:codestart => "",
|
||||||
:codeend => "",
|
:codeend => "",
|
||||||
:outputstart => "\\begin{lstlisting}",
|
:outputstart => "\\begin{lstlisting}",
|
||||||
|
@ -195,14 +81,160 @@ const md2tex = JMarkdown2tex(
|
||||||
"text/markdown",
|
"text/markdown",
|
||||||
"text/plain",
|
"text/plain",
|
||||||
],
|
],
|
||||||
:doctype => "md2tex",
|
|
||||||
:keep_unicode => false,
|
:keep_unicode => false,
|
||||||
),
|
))
|
||||||
)
|
register_format!("md2pdf", t)
|
||||||
|
register_format!("md2tex", t)
|
||||||
|
end
|
||||||
|
|
||||||
struct MultiMarkdown
|
@define_format Tex
|
||||||
description::AbstractString
|
register_format!("tex", Tex(Dict(
|
||||||
formatdict::Dict{Symbol,Any}
|
:description => "Latex with custom code environments",
|
||||||
|
:codestart => "\\begin{juliacode}",
|
||||||
|
:codeend => "\\end{juliacode}",
|
||||||
|
:outputstart => "\\begin{juliaout}",
|
||||||
|
:outputend => "\\end{juliaout}",
|
||||||
|
:termstart => "\\begin{juliaterm}",
|
||||||
|
:termend => "\\end{juliaterm}",
|
||||||
|
:fig_ext => ".pdf",
|
||||||
|
:extension => "tex",
|
||||||
|
:out_width => "\\linewidth",
|
||||||
|
:fig_env => "figure",
|
||||||
|
:fig_pos => "htpb",
|
||||||
|
:mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"],
|
||||||
|
:keep_unicode => false,
|
||||||
|
)))
|
||||||
|
register_format!("texminted", Tex(Dict(
|
||||||
|
:description => "Latex using minted for highlighting",
|
||||||
|
:codestart =>
|
||||||
|
"\\begin{minted}[mathescape, fontsize=\\small, xleftmargin=0.5em]{julia}",
|
||||||
|
:codeend => "\\end{minted}",
|
||||||
|
:outputstart =>
|
||||||
|
"\\begin{minted}[fontsize=\\small, xleftmargin=0.5em, mathescape, frame = leftline]{text}",
|
||||||
|
:outputend => "\\end{minted}",
|
||||||
|
:termstart =>
|
||||||
|
"\\begin{minted}[fontsize=\\footnotesize, xleftmargin=0.5em, mathescape]{jlcon}",
|
||||||
|
:termend => "\\end{minted}",
|
||||||
|
:fig_ext => ".pdf",
|
||||||
|
:extension => "tex",
|
||||||
|
:out_width => "\\linewidth",
|
||||||
|
:fig_env => "figure",
|
||||||
|
:fig_pos => "htpb",
|
||||||
|
:mimetypes => ["application/pdf", "image/png", "text/latex", "text/plain"],
|
||||||
|
:keep_unicode => false,
|
||||||
|
)))
|
||||||
|
|
||||||
|
|
||||||
|
# pandoc
|
||||||
|
# ------
|
||||||
|
|
||||||
|
@define_format Pandoc
|
||||||
|
let p = Pandoc(Dict(
|
||||||
|
:description => "Pandoc markdown",
|
||||||
|
:codestart => "~~~~{.julia}",
|
||||||
|
:codeend => "~~~~~~~~~~~~~\n\n",
|
||||||
|
:outputstart => "~~~~",
|
||||||
|
:outputend => "~~~~\n\n",
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:out_width => nothing,
|
||||||
|
:extension => "md",
|
||||||
|
# Prefer png figures for markdown conversion, svg doesn't work with latex
|
||||||
|
:mimetypes =>
|
||||||
|
["image/png", "image/jpg", "image/svg+xml", "text/markdown", "text/plain"],
|
||||||
|
))
|
||||||
|
register_format!("pandoc", p)
|
||||||
|
register_format!("pandoc2pdf", p)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# markdown
|
||||||
|
# --------
|
||||||
|
|
||||||
|
@define_format GitHubMarkdown
|
||||||
|
register_format!("github", GitHubMarkdown(Dict(
|
||||||
|
:description => "GitHub markdown",
|
||||||
|
:codestart => "````julia",
|
||||||
|
:codeend => "````\n\n",
|
||||||
|
:outputstart => "````",
|
||||||
|
:outputend => "````\n\n",
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:extension => "md",
|
||||||
|
:mimetypes =>
|
||||||
|
["image/png", "image/svg+xml", "image/jpg", "text/markdown", "text/plain"],
|
||||||
|
)))
|
||||||
|
|
||||||
|
@define_format Hugo
|
||||||
|
register_format!("hugo", Hugo(Dict(
|
||||||
|
:description => "Hugo markdown (using shortcodes)",
|
||||||
|
:codestart => "````julia",
|
||||||
|
:codeend => "````\n\n",
|
||||||
|
:outputstart => "````",
|
||||||
|
:outputend => "````\n\n",
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:extension => "md",
|
||||||
|
:uglyURLs => false, # if `false`, prepend figure path by `..`
|
||||||
|
)))
|
||||||
|
|
||||||
|
@define_format MultiMarkdown
|
||||||
|
register_format!("multimarkdown", MultiMarkdown(Dict(
|
||||||
|
:description => "MultiMarkdown",
|
||||||
|
:codestart => "````julia",
|
||||||
|
:codeend => "````\n\n",
|
||||||
|
:outputstart => "````",
|
||||||
|
:outputend => "````\n\n",
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:extension => "md",
|
||||||
|
)))
|
||||||
|
|
||||||
|
|
||||||
|
# Rest
|
||||||
|
# ----
|
||||||
|
|
||||||
|
@define_format Rest
|
||||||
|
register_format!("rst", Rest(Dict(
|
||||||
|
:description => "reStructuredText and Sphinx",
|
||||||
|
:codestart => ".. code-block:: julia\n",
|
||||||
|
:codeend => "\n\n",
|
||||||
|
:outputstart => "::\n",
|
||||||
|
:outputend => "\n\n",
|
||||||
|
:indent => 4,
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:extension => "rst",
|
||||||
|
:out_width => "15 cm",
|
||||||
|
)))
|
||||||
|
|
||||||
|
|
||||||
|
# Ansii
|
||||||
|
# -----
|
||||||
|
|
||||||
|
# asciidoc -b html5 -a source-highlighter=pygments ...
|
||||||
|
@define_format AsciiDoc
|
||||||
|
register_format!("asciidoc", AsciiDoc(Dict(
|
||||||
|
:description => "AsciiDoc",
|
||||||
|
:codestart => "[source,julia]\n--------------------------------------",
|
||||||
|
:codeend => "--------------------------------------\n\n",
|
||||||
|
:outputstart => "--------------------------------------",
|
||||||
|
:outputend => "--------------------------------------\n\n",
|
||||||
|
:fig_ext => ".png",
|
||||||
|
:extension => "txt",
|
||||||
|
:out_width => "600",
|
||||||
|
)))
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: move this functions where used
|
||||||
|
# ------------------------------------
|
||||||
|
|
||||||
|
using Printf
|
||||||
|
|
||||||
|
|
||||||
|
function md_length_to_latex(def, reference)
|
||||||
|
if occursin("%", def)
|
||||||
|
_def = tryparse(Float64, replace(def, "%" => ""))
|
||||||
|
_def == nothing && return def
|
||||||
|
perc = round(_def / 100, digits = 2)
|
||||||
|
return "$perc$reference"
|
||||||
|
end
|
||||||
|
return def
|
||||||
end
|
end
|
||||||
|
|
||||||
function formatfigures(chunk, docformat::JMarkdown2HTML)
|
function formatfigures(chunk, docformat::JMarkdown2HTML)
|
||||||
|
@ -244,69 +276,6 @@ function formatfigures(chunk, docformat::JMarkdown2HTML)
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
const multimarkdown = MultiMarkdown(
|
|
||||||
"MultiMarkdown",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "````julia",
|
|
||||||
:codeend => "````\n\n",
|
|
||||||
:outputstart => "````",
|
|
||||||
:outputend => "````\n\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:extension => "md",
|
|
||||||
:doctype => "github",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
struct Rest
|
|
||||||
description::AbstractString
|
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
end
|
|
||||||
|
|
||||||
const rst = Rest(
|
|
||||||
"reStructuredText and Sphinx",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => ".. code-block:: julia\n",
|
|
||||||
:codeend => "\n\n",
|
|
||||||
:outputstart => "::\n",
|
|
||||||
:outputend => "\n\n",
|
|
||||||
:indent => 4,
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:extension => "rst",
|
|
||||||
:out_width => "15 cm",
|
|
||||||
:doctype => "rst",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
struct AsciiDoc
|
|
||||||
description::AbstractString
|
|
||||||
formatdict::Dict{Symbol,Any}
|
|
||||||
end
|
|
||||||
|
|
||||||
# asciidoc -b html5 -a source-highlighter=pygments ...
|
|
||||||
const adoc = AsciiDoc(
|
|
||||||
"AsciiDoc",
|
|
||||||
Dict{Symbol,Any}(
|
|
||||||
:codestart => "[source,julia]\n--------------------------------------",
|
|
||||||
:codeend => "--------------------------------------\n\n",
|
|
||||||
:outputstart => "--------------------------------------",
|
|
||||||
:outputend => "--------------------------------------\n\n",
|
|
||||||
:fig_ext => ".png",
|
|
||||||
:extension => "txt",
|
|
||||||
:out_width => "600",
|
|
||||||
:doctype => "asciidoc",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
function md_length_to_latex(def, reference)
|
|
||||||
if occursin("%", def)
|
|
||||||
_def = tryparse(Float64, replace(def, "%" => ""))
|
|
||||||
_def == nothing && return def
|
|
||||||
perc = round(_def / 100, digits = 2)
|
|
||||||
return "$perc$reference"
|
|
||||||
end
|
|
||||||
return def
|
|
||||||
end
|
|
||||||
|
|
||||||
function formatfigures(chunk, docformat::Union{Tex,JMarkdown2tex})
|
function formatfigures(chunk, docformat::Union{Tex,JMarkdown2tex})
|
||||||
fignames = chunk.figures
|
fignames = chunk.figures
|
||||||
caption = chunk.options[:fig_cap]
|
caption = chunk.options[:fig_cap]
|
||||||
|
@ -423,7 +392,7 @@ function formatfigures(chunk, docformat::GitHubMarkdown)
|
||||||
end
|
end
|
||||||
|
|
||||||
function formatfigures(chunk, docformat::Hugo)
|
function formatfigures(chunk, docformat::Hugo)
|
||||||
relpath = docformat.uglyURLs ? "" : ".."
|
relpath = docformat.formatdict[:uglyURLs] ? "" : ".."
|
||||||
function format_shortcode(index_and_fig)
|
function format_shortcode(index_and_fig)
|
||||||
index, fig = index_and_fig
|
index, fig = index_and_fig
|
||||||
if index > 1
|
if index > 1
|
||||||
|
@ -510,20 +479,3 @@ function formatfigures(chunk, docformat::AsciiDoc)
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add new supported formats here
|
|
||||||
const formats = Dict(
|
|
||||||
"tex" => tex,
|
|
||||||
"texminted" => texminted,
|
|
||||||
"pandoc" => pandoc,
|
|
||||||
"pandoc2html" => pdoc2html,
|
|
||||||
"pandoc2pdf" => pandoc,
|
|
||||||
"md2pdf" => md2tex,
|
|
||||||
"github" => github,
|
|
||||||
"hugo" => hugo,
|
|
||||||
"multimarkdown" => multimarkdown,
|
|
||||||
"rst" => rst,
|
|
||||||
"asciidoc" => adoc,
|
|
||||||
"md2html" => md2html,
|
|
||||||
"md2tex" => md2tex,
|
|
||||||
)
|
|
|
@ -45,7 +45,7 @@ function run_doc(
|
||||||
# cache :all, :user, :off, :refresh
|
# cache :all, :user, :off, :refresh
|
||||||
|
|
||||||
doc.doctype = isnothing(doctype) ? (doctype = detect_doctype(doc.source)) : doctype
|
doc.doctype = isnothing(doctype) ? (doctype = detect_doctype(doc.source)) : doctype
|
||||||
doc.format = deepcopy(formats[doctype])
|
doc.format = deepcopy(FORMATS[doctype])
|
||||||
|
|
||||||
doc.cwd = get_cwd(doc, out_path)
|
doc.cwd = get_cwd(doc, out_path)
|
||||||
isdir(doc.cwd) || mkpath(doc.cwd)
|
isdir(doc.cwd) || mkpath(doc.cwd)
|
||||||
|
@ -136,7 +136,7 @@ end
|
||||||
|
|
||||||
function run_chunk(chunk::CodeChunk, doc, report, mod)
|
function run_chunk(chunk::CodeChunk, doc, report, mod)
|
||||||
result = eval_chunk(chunk, report, mod)
|
result = eval_chunk(chunk, report, mod)
|
||||||
occursin("2html", report.formatdict[:doctype]) && (embed_figures!(result, report.cwd))
|
occursin("2html", doc.doctype) && (embed_figures!(result, report.cwd))
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -188,7 +188,7 @@ function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, SandBox::
|
||||||
merge!(chunk.options, options)
|
merge!(chunk.options, options)
|
||||||
|
|
||||||
chunks = eval_chunk(chunk, report, SandBox)
|
chunks = eval_chunk(chunk, report, SandBox)
|
||||||
occursin("2html", report.formatdict[:doctype]) && (embed_figures!(chunks, report.cwd))
|
occursin("2html", doc.doctype) && (embed_figures!(chunks, report.cwd))
|
||||||
|
|
||||||
output = chunks[1].output
|
output = chunks[1].output
|
||||||
endswith(output, "\n") && (output = output[1:end-1])
|
endswith(output, "\n") && (output = output[1:end-1])
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
using Weave
|
test_formatfigures(chunk, format) = Weave.formatfigures(chunk, Weave.FORMATS[format])
|
||||||
using Test
|
|
||||||
|
|
||||||
# Make a dummy codehunk with figure
|
# Make a dummy codehunk with figure
|
||||||
chunk = Weave.CodeChunk("plot(x)", 1, 1, "", Dict())
|
chunk = Weave.CodeChunk("plot(x)", 1, 1, "", Dict())
|
||||||
|
@ -7,43 +7,49 @@ options = merge(Weave.get_chunk_defaults(), chunk.options)
|
||||||
merge!(chunk.options, options)
|
merge!(chunk.options, options)
|
||||||
chunk.figures = ["figs/figures_plot1.png"]
|
chunk.figures = ["figs/figures_plot1.png"]
|
||||||
|
|
||||||
@test Weave.formatfigures(chunk, Weave.md2tex) == "\\includegraphics{figs/figures_plot1.png}\n"
|
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == "\\includegraphics{figs/figures_plot1.png}\n"
|
@test test_formatfigures(chunk, "md2tex") == "\\includegraphics{figs/figures_plot1.png}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.texminted) == "\\includegraphics{figs/figures_plot1.png}\n"
|
@test test_formatfigures(chunk, "tex") == "\\includegraphics{figs/figures_plot1.png}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.pandoc) == "![](figs/figures_plot1.png)\\ \n\n"
|
@test test_formatfigures(chunk, "texminted") == "\\includegraphics{figs/figures_plot1.png}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.github) == "![](figs/figures_plot1.png)\n"
|
@test test_formatfigures(chunk, "pandoc") == "![](figs/figures_plot1.png)\\ \n\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.hugo) == "{{< figure src=\"../figs/figures_plot1.png\" >}}"
|
@test test_formatfigures(chunk, "github") == "![](figs/figures_plot1.png)\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.multimarkdown) == "![][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png \n"
|
@test test_formatfigures(chunk, "hugo") == "{{< figure src=\"../figs/figures_plot1.png\" >}}"
|
||||||
@test Weave.formatfigures(chunk, Weave.md2html) == "<img src=\"figs/figures_plot1.png\" />\n"
|
@test test_formatfigures(chunk, "multimarkdown") == "![][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png \n"
|
||||||
|
@test test_formatfigures(chunk, "md2html") == "<img src=\"figs/figures_plot1.png\" />\n"
|
||||||
|
|
||||||
|
|
||||||
chunk.options[:out_width] = "100%"
|
chunk.options[:out_width] = "100%"
|
||||||
@test Weave.formatfigures(chunk, Weave.adoc) == "image::figs/figures_plot1.png[width=100%]\n"
|
@test test_formatfigures(chunk, "asciidoc") == "image::figs/figures_plot1.png[width=100%]\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.rst) == ".. image:: figs/figures_plot1.png\n :width: 100%\n\n"
|
@test test_formatfigures(chunk, "rst") == ".. image:: figs/figures_plot1.png\n :width: 100%\n\n"
|
||||||
|
|
||||||
|
|
||||||
chunk.options[:fig_cap] = "Nice plot"
|
chunk.options[:fig_cap] = "Nice plot"
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n"
|
@test test_formatfigures(chunk, "tex") == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.pandoc) == "![Nice plot](figs/figures_plot1.png){width=100%}\n"
|
@test test_formatfigures(chunk, "pandoc") == "![Nice plot](figs/figures_plot1.png){width=100%}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.md2tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n"
|
@test test_formatfigures(chunk, "md2tex") == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\end{figure}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.md2html) == "<figure>\n<img src=\"figs/figures_plot1.png\" width=\"100%\" />\n<figcaption>Nice plot</figcaption>\n</figure>\n"
|
@test test_formatfigures(chunk, "md2html") == "<figure>\n<img src=\"figs/figures_plot1.png\" width=\"100%\" />\n<figcaption>Nice plot</figcaption>\n</figure>\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.rst) == ".. figure:: figs/figures_plot1.png\n :width: 100%\n\n Nice plot\n\n"
|
@test test_formatfigures(chunk, "rst") == ".. figure:: figs/figures_plot1.png\n :width: 100%\n\n Nice plot\n\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.multimarkdown) == "![Nice plot][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png width=100%\n"
|
@test test_formatfigures(chunk, "multimarkdown") == "![Nice plot][figs/figures_plot1.png]\n\n[figs/figures_plot1.png]: figs/figures_plot1.png width=100%\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.adoc) == "image::figs/figures_plot1.png[width=100%,title=\"Nice plot\"]"
|
@test test_formatfigures(chunk, "asciidoc") == "image::figs/figures_plot1.png[width=100%,title=\"Nice plot\"]"
|
||||||
|
|
||||||
|
|
||||||
chunk.options[:label] = "somefig"
|
chunk.options[:label] = "somefig"
|
||||||
@test Weave.formatfigures(chunk, Weave.pandoc) == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n"
|
@test test_formatfigures(chunk, "pandoc") == "![Nice plot](figs/figures_plot1.png){width=100% #fig:somefig}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\label{fig:somefig}\n\\end{figure}\n"
|
@test test_formatfigures(chunk, "tex") == "\\begin{figure}[!h]\n\\center\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\caption{Nice plot}\n\\label{fig:somefig}\n\\end{figure}\n"
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == Weave.formatfigures(chunk, Weave.md2tex)
|
|
||||||
|
|
||||||
chunk.options[:label] = nothing
|
chunk.options[:label] = nothing
|
||||||
chunk.options[:fig_cap] = nothing
|
chunk.options[:fig_cap] = nothing
|
||||||
chunk.options[:fig_env] = "center"
|
chunk.options[:fig_env] = "center"
|
||||||
chunk.options[:fig_pos] = ""
|
chunk.options[:fig_pos] = ""
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\end{center}\n"
|
@test test_formatfigures(chunk, "tex") == "\\begin{center}\n\\includegraphics[width=1.0\\linewidth]{figs/figures_plot1.png}\n\\end{center}\n"
|
||||||
|
|
||||||
|
|
||||||
chunk.options[:out_width] = "50%"
|
chunk.options[:out_width] = "50%"
|
||||||
chunk.options[:out_height] = "75 %"
|
chunk.options[:out_height] = "75 %"
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=0.5\\linewidth,height=0.75\\paperheight]{figs/figures_plot1.png}\n\\end{center}\n"
|
@test test_formatfigures(chunk, "tex") == "\\begin{center}\n\\includegraphics[width=0.5\\linewidth,height=0.75\\paperheight]{figs/figures_plot1.png}\n\\end{center}\n"
|
||||||
|
|
||||||
|
|
||||||
chunk.options[:out_width] = "A%"
|
chunk.options[:out_width] = "A%"
|
||||||
chunk.options[:out_height] = "0.5\\textwidth"
|
chunk.options[:out_height] = "0.5\\textwidth"
|
||||||
@test Weave.formatfigures(chunk, Weave.tex) == "\\begin{center}\n\\includegraphics[width=A%,height=0.5\\textwidth]{figs/figures_plot1.png}\n\\end{center}\n"
|
@test test_formatfigures(chunk, "tex") == "\\begin{center}\n\\includegraphics[width=A%,height=0.5\\textwidth]{figs/figures_plot1.png}\n\\end{center}\n"
|
||||||
|
|
|
@ -11,11 +11,11 @@ Test rendering \$\alpha\$
|
||||||
|
|
||||||
dchunk = Weave.DocChunk(content, 1, 1)
|
dchunk = Weave.DocChunk(content, 1, 1)
|
||||||
|
|
||||||
pformat = Weave.formats["github"]
|
pformat = Weave.FORMATS["github"]
|
||||||
f = Weave.format_chunk(dchunk, pformat)
|
f = Weave.format_chunk(dchunk, pformat)
|
||||||
@test f == content
|
@test f == content
|
||||||
|
|
||||||
docformat = Weave.formats["md2html"]
|
docformat = Weave.FORMATS["md2html"]
|
||||||
f_check = "<h1>Test chunk</h1>\n<p>Test rendering <span class=\"math\">\$\alpha\$</span></p>\n"
|
f_check = "<h1>Test chunk</h1>\n<p>Test rendering <span class=\"math\">\$\alpha\$</span></p>\n"
|
||||||
f = Weave.format_chunk(dchunk, docformat)
|
f = Weave.format_chunk(dchunk, docformat)
|
||||||
@test f_check == f
|
@test f_check == f
|
||||||
|
@ -88,7 +88,7 @@ content = """
|
||||||
α
|
α
|
||||||
"""
|
"""
|
||||||
chunk = Weave.DocChunk(content, 1, 1)
|
chunk = Weave.DocChunk(content, 1, 1)
|
||||||
fmt = deepcopy(Weave.formats["md2tex"])
|
fmt = deepcopy(Weave.FORMATS["md2tex"])
|
||||||
|
|
||||||
f = Weave.format_chunk(chunk, fmt)
|
f = Weave.format_chunk(chunk, fmt)
|
||||||
@test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n"
|
@test f == "\\section{Test chunk}\n\\ensuremath{\\alpha}\n\n"
|
||||||
|
|
Loading…
Reference in New Issue