Skip to content

Instantly share code, notes, and snippets.

@reyman
Created October 23, 2021 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyman/12e31a533499790547179a2d40d7e021 to your computer and use it in GitHub Desktop.
Save reyman/12e31a533499790547179a2d40d7e021 to your computer and use it in GitHub Desktop.
filter to get url from citeproc pandoc
local List = require 'pandoc.List'
local utils = require 'pandoc.utils'
local stringify = utils.stringify
local run_json_filter = utils.run_json_filter
local pretty_dump = require"pl.pretty".dump
function isItALink (spc)
return spc and spc.t == 'Link'
end
function read_inline (inlines)
for i = #inlines-1,1,-1 do
if isItALink(inlines[i]) then
print(stringify(inlines[i]))
end
end
return inlines
end
function modify_doc_citation(doc)
for i,el in pairs(doc.blocks) do
if (el.t == "Div") then
if el.identifier:match('^refs([_%w]*)$') then
--pretty_dump(el.identifier)
pandoc.walk_block(el, { Inlines = function (e) read_inline(e) end})
end
end
end
return doc
end
function Inlines (inlines)
for i = #inlines-1,1,-1 do
print(pandoc.utils.stringify(inlines[i]))
end
return inlines
end
local function run_citeproc(doc, quiet)
if PANDOC_VERSION >= "2.11" then
return run_json_filter(
doc,
'pandoc',
{'--from=json', '--to=json', '--citeproc', quiet and '--quiet' or nil}
)
else
-- doc = run_json_filter(doc, 'pandoc-citeproc')
return run_json_filter(
doc,
'pandoc-citeproc',
{FORMAT, (quiet and supports_quiet_flag) and '-q' or nil}
)
end
end
local function resolve_doc_citations (doc)
doc = run_citeproc(doc)
return doc
end
return {
{ Pandoc = resolve_doc_citations },
{Pandoc = modify_doc_citation}
}
pandoc --lua-filter=filter.lua -f markdown -t json --citeproc -o test.json test.md
> https://duckduckgo.com/cite2020
> https://duckduckgo.com/cite2021
@book{cite2020,
title = {Title},
author = {Author},
year = {2020},
url = {https://duckduckgo.com/cite2020}
}
@book{cite2021,
title = {Title},
author = {Author2},
year = {2021},
url = {https://duckduckgo.com/cite2021}
}
bibliography biblio-style link-citations
test.bib
apalike
true

Section One

Hello world, citing [@cite2020] and [@cite2021] !

This i the last line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment