Add citation parser for markdown

pull/185/merge^2
Matti Pastell 2019-02-28 23:31:10 +02:00
parent edd558de0e
commit 9c75cf4103
3 changed files with 120 additions and 1 deletions

View File

@ -2,11 +2,24 @@
module WeaveMarkdown
using Markdown
import Markdown: @trigger, @breaking, Code, MD, withstream, startswith, LaTeX
using BibTeX
mutable struct Comment
text::String
end
mutable struct Citation
key::String
no::Int64
bib::Dict
end
Citation(key, no) = Citation(key, no, Dict())
mutable struct Citations
content::Array{Citation}
end
@breaking true ->
function dollarmath(stream::IO, block::MD)
withstream(stream) do
@ -61,8 +74,45 @@ 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
Markdown.startswith(stream, "[@") || return
text = Markdown.readuntil(stream, ']', match = '[')
text nothing && return
citations = strip.(split(text, ";"))
cites = Citation[]
for c in citations
c = replace(c, r"^@" => "")
#Check for matcthing bixtex key
if haskey(CITATIONS[:bibtex], c)
bib = CITATIONS[:bibtex][c]
# Check for repeated citations
if haskey(CITATIONS[:refnumbers], c)
no = CITATIONS[:refnumbers][c]
else
no = CITATIONS[:no]
CITATIONS[:refnumbers][c] = no
CITATIONS[:no] += 1
end
push!(cites, Citation(c, no, bib))
CITATIONS[:references][c] = bib
else
push!(cites, Citation(c, 0))
end
end
return Citations(cites)
end
end
# Create own flavor and copy all the features from julia flavor
Markdown.@flavor weavemd [dollarmath, comment, topcomment]
Markdown.@flavor weavemd [dollarmath, comment, topcomment, citation]
weavemd.breaking = [weavemd.breaking; Markdown.julia.breaking]
weavemd.regular = [weavemd.regular; Markdown.julia.regular]
for key in keys(Markdown.julia.inner)
@ -73,6 +123,17 @@ for key in keys(Markdown.julia.inner)
end
end
function parse_markdown(text, bibfile)
CITATIONS[:no] = 1
header, refs = parse_bibtex(read(bibfile, String))
CITATIONS[:bibtex] = refs
CITATIONS[:references] = Dict()
CITATIONS[:refnumbers] = Dict()
m = Markdown.parse(text, flavor = weavemd);
m.content
end
include("html.jl")
include("latex.jl")
end

View File

@ -0,0 +1,41 @@
@Article{LubiDunn15,
Title = {Computing in Operations Research Using Julia},
Author = {Miles Lubin and Iain Dunning},
Journal = {INFORMS Journal on Computing},
Year = {2015},
Number = {2},
Pages = {238--248},
Volume = {27},
Optdoi = {10.1287/ijoc.2014.0623},
Opturl = {http://dx.doi.org/10.1287/ijoc.2014.0623}
}
@article{pastell_filtering_2018,
title = {Filtering methods to improve the accuracy of indoor positioning data for dairy cows},
volume = {169},
issn = {1537-5110},
url = {http://www.sciencedirect.com/science/article/pii/S1537511017308711},
doi = {10.1016/j.biosystemseng.2018.01.008},
urldate = {2018-03-28},
journal = {Biosystems Engineering},
author = {Pastell, Matti and Frondelius, Lilli and Järvinen, Mikko and Backman, Juha},
month = may,
year = {2018},
keywords = {Indoor positioning, Dairy, Extended Kalman filter, Ultra-wide band},
pages = {22--31}
}
@article{Bezanson2017,
doi = {10.1137/141000671},
url = {https://doi.org/10.1137/141000671},
year = {2017},
month = {jan},
publisher = {Society for Industrial {\&} Applied Mathematics ({SIAM})},
volume = {59},
number = {1},
pages = {65--98},
author = {Jeff Bezanson and Alan Edelman and Stefan Karpinski and Viral B. Shah},
title = {Julia: A Fresh Approach to Numerical Computing},
journal = {{SIAM} Review}
}

View File

@ -100,3 +100,20 @@ Multiple lines
@test WeaveMarkdown.html(md.content[2]) == "<p class=\"math\">\\[\nx = 2\n\\]</p>"
@test WeaveMarkdown.html(md.content[4]) == "\n<!-- \nMultiple lines\n -->\n"
##
using Revise
import Weave: WeaveMarkdown
md = """
[@Bezanson2017]
citing [@pastell_filtering_2018; @someref]
cite [@Bezanson2017] again
"""
m = WeaveMarkdown.parse_markdown(md, "test/documents/bibtex/testdocs.bib");
m