Add simple list of references

pull/185/merge^2
Matti Pastell 2019-03-01 16:59:49 +02:00
parent b6eb19011c
commit a6c7f1abb3
6 changed files with 54 additions and 6 deletions

View File

@ -121,6 +121,8 @@ function weave(source ; doctype = :auto,
write(io, formatted)
end
WeaveMarkdown.reset_parser()
#Special for that need external programs
if doc.doctype == "pandoc2html"
mdname = outname

View File

@ -0,0 +1,26 @@
import Mustache
function list_references(m::MIME"text/html")
tpl = Mustache.template_from_file(joinpath(@__DIR__, "../../templates/html_citations.tpl"))
refs = Dict()
for key in keys(CITATIONS[:refnumbers])
refs[CITATIONS[:refnumbers][key]] = key
end
io = IOBuffer()
write(io, "<h3>References</h3>")
write(io, "<ol>")
for i in 1:length(refs)
ref = CITATIONS[:references][refs[i]]
ref[ref["type"]] = "true"
ref["author"] = replace(ref["author"], r"\sand\s"i => ", ")
for key in keys(ref)
ref[key] = replace(ref[key], r"\{|\}" => "")
ref[key] = replace(ref[key], "--" => "&ndash;")
end
write(io, "<li>")
write(io, Mustache.render(tpl, ref))
write(io, "</li>")
end
write(io, "</ol>")
return String(take!(io))
end

View File

@ -74,12 +74,6 @@ function comment(stream::IO, md::MD)
end
end
global const CITATIONS = Dict{Symbol, Any}(
:no => 1,
:bibtex => Dict(),
:references => []
)
@trigger '[' ->
function citation(stream::IO, md::MD)
withstream(stream) do
@ -123,6 +117,13 @@ for key in keys(Markdown.julia.inner)
end
end
const CITATIONS = Dict{Symbol, Any}(
:no => 1,
:bibtex => Dict(),
:references => Dict(),
:refnumbers => Dict()
)
#Init dictionary for parsing citations
function init_parser(bibfile)
CITATIONS[:no] = 1
@ -132,10 +133,18 @@ function init_parser(bibfile)
CITATIONS[:refnumbers] = Dict()
end
function reset_parser()
CITATIONS[:no] = 1
CITATIONS[:references] = Dict()
CITATIONS[:bibtex] = Dict()
CITATIONS[:refnumbers] = Dict()
end
function parse(text)
Markdown.parse(text, flavor = weavemd);
end
include("html.jl")
include("latex.jl")
include("bibliography.jl")
end

View File

@ -77,9 +77,16 @@ function render_doc(formatted, doc::WeaveDoc, format::JMarkdown2HTML)
template = Mustache.template_from_file(doc.template)
end
if isempty(WeaveMarkdown.CITATIONS[:references])
references = ""
else
references = WeaveMarkdown.list_references(MIME"text/html"())
end
return Mustache.render(template; themecss = theme_css,
highlightcss = css, body = formatted, header_script = doc.header_script,
source = wsource, wtime = wtime, wversion = wversion,
references = references,
[Pair(Symbol(k), v) for (k,v) in doc.header]...)
end

View File

@ -38,6 +38,7 @@
{{{ :body }}}
{{{ :references }}}
<HR/>
<div class="footer"><p>

View File

@ -132,3 +132,6 @@ end
Mustache.render(tpl, ref)
r2 = Dict("author" => "Matti Pastell", "title" => "Some paper", "article" => "true")
Mustache.render(tpl, r2)
WeaveMarkdown.list_references(MIME"text/html"())
isempty(WeaveMarkdown.CITATIONS[:references])