From 6fcb1954ac184bb4837812b786f267f8755bb50b Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 22 May 2019 21:43:22 +0530 Subject: [PATCH 1/2] add function parse_bibtex_ordered Adds a function `parse_bibtex_ordered`, which, instead of returning a Dict of Dicts, returns an Array of Dicts. This may be useful for determination of primacy in the CITATION.bib file format. --- src/BibTeX.jl | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/BibTeX.jl b/src/BibTeX.jl index 2a628ca..d9aee11 100644 --- a/src/BibTeX.jl +++ b/src/BibTeX.jl @@ -143,7 +143,7 @@ ERROR: Expected { on line 1 [...] ``` """ -parse_bibtex(text) = begin +function parse_bibtex(text) parser = parse_text(text) token = next_token_default!(parser) preamble = "" @@ -170,4 +170,34 @@ parse_bibtex(text) = begin preamble, parser.records end +function parse_bibtex_ordered(text) + ordered = [] + parser = parse_text(text) + token = next_token_default!(parser) + preamble = "" + while token != "" + if token == "@" + record_type = lowercase(next_token!(parser)) + if record_type == "preamble" + trash, preamble = value!(parser) + elseif record_type != "comment" + expect!(parser, "{") + if record_type == "string" + field!(parser, parser.substitutions) + else + id = next_token!(parser) + dict = Dict("type" => record_type) + expect!(parser, ",") + field!(parser, dict) + parser.records[id] = dict + push!(ordered, Dict(id => dict)) + + end + end + end + token = next_token_default!(parser) + end + preamble, ordered +end + end From a83362093de71539f3d52d2a7faa037f82122c7c Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 22 May 2019 21:54:23 +0530 Subject: [PATCH 2/2] stringify result --- src/BibTeX.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/BibTeX.jl b/src/BibTeX.jl index d9aee11..1a1c947 100644 --- a/src/BibTeX.jl +++ b/src/BibTeX.jl @@ -97,7 +97,6 @@ field!(parser, dict) = begin expect(parser, token, "}") end -export parse_bibtex """ parse_bibtex(text) @@ -171,7 +170,7 @@ function parse_bibtex(text) end function parse_bibtex_ordered(text) - ordered = [] + ordered = Array{Dict{String, Dict{String,String}}, 1}() parser = parse_text(text) token = next_token_default!(parser) preamble = "" @@ -190,7 +189,7 @@ function parse_bibtex_ordered(text) expect!(parser, ",") field!(parser, dict) parser.records[id] = dict - push!(ordered, Dict(id => dict)) + push!(ordered, Dict(string(id) => dict)) end end @@ -200,4 +199,8 @@ function parse_bibtex_ordered(text) preamble, ordered end +export parse_bibtex_ordered +export parse_bibtex + + end