Created
March 16, 2022 13:28
-
-
Save adigitoleo/52d793e5fa4a8d4787ff556fae7f9454 to your computer and use it in GitHub Desktop.
Franklin.jl utils for reference lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using TOML | |
using HTTP | |
""" | |
get_ref(key) | |
Get HTML-formatted reference text for the given referernce key. | |
The key must match either a key in the `_assets/refcache.toml` | |
or the `_assets/doilist.toml` file. The text will be formatted with | |
`<strong>` and `<emph>` tags, and the doi link will be hyperlinked. | |
The result will be nested in a `<li>` element with the `id` attribute | |
set to the given key string. | |
""" | |
function get_ref(key) | |
cachefile = "_assets/refcache.toml" | |
if isfile(cachefile) | |
refcache::Dict{String,String} = TOML.parsefile(cachefile) | |
else | |
refcache = Dict{String,String}() | |
end | |
if !haskey(refcache, key) | |
doimap::Dict{String,String} = TOML.parsefile("_assets/doilist.toml") | |
doi = doimap[key] | |
url = startswith(doi, "https://doi.org/") ? doi : "https://doi.org/" * doi | |
response = HTTP.request("GET", url, Dict("Accept" => "text/x-bibliography")) | |
raw = String(response.body) | |
ref = "<li id=\"$key\">" * strip( | |
replace( | |
raw, | |
r"(^.*\(\d+\)\.)\s([A-Z][^\.]*\.)\s(.*)\s(https\://doi\.org/.*)" => | |
s"<strong>\1</strong> <em>\2</em> \3 <a href=\"\4\">\4</a>", | |
), | |
) * "</li>" | |
refcache[key] = ref | |
open(cachefile, "a") do io | |
TOML.print(io, Dict(key => ref)) | |
end | |
end | |
return refcache[key] | |
end | |
function hfun_printrefs(refs) # Usage: {{printrefs key1 key2 ...}} | |
out = IOBuffer() | |
write(out, "<ul>") | |
for ref in refs | |
write(out, get_ref(ref)) | |
end | |
write(out, "</ul>") | |
return String(take!(out)) | |
end | |
function hfun_citeref(key_and_text) # Usage: {{citeref key Actual displayed text}} | |
key = popfirst!(key_and_text) | |
text = join(key_and_text, ' ') | |
return "<a href=\"#$key\">$text</a>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An improved version is now available in my website source repository. This version was a prototype and will not be updated.