Created
January 4, 2018 20:09
-
-
Save tarleb/febcc78e5c7f6384299297232ab7dfe2 to your computer and use it in GitHub Desktop.
Ugly, proof of concept filter to create section refs.
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
dkjson = require 'dkjson' | |
List = require 'pandoc.List' | |
utils = require 'pandoc.utils' | |
CitationOrig = pandoc.Citation | |
-- monkey-patch citation creation | |
pandoc.Citation = function(id, mode, prefix, suffix, note_num, hash) | |
local res = CitationOrig(id, mode, prefix, suffix, note_num, hash) | |
setmetatable( | |
res, | |
{ | |
__tojson = function (x) | |
local tmp = {} | |
for k, v in pairs(x) do tmp[k] = v end | |
tmp.citationMode = {t = x.citationMode} | |
return dkjson.encode(tmp) | |
end | |
} | |
) | |
return res | |
end | |
-- hacky, ad-hoc JSON conversion. Will fail for MetaString and MetaBoolean | |
function tojson(el) return dkjson.encode{t = el.t, c = el.c} end | |
for _, const in pairs(pandoc.Inline.constructor) do const.__tojson = tojson end | |
for _, const in pairs(pandoc.Block.constructor) do const.__tojson = tojson end | |
for _, const in pairs(pandoc.MetaValue.constructor) do | |
const.__tojson = function (el) | |
local tmp = el | |
local mt = getmetatable(el) | |
return dkjson.encode{t = el.t, c = setmetatable(tmp, {})}, setmetatable(el, mt) | |
end | |
end | |
function flatten (lst) | |
local res = List:new{} | |
for i, el in ipairs(lst) do | |
if el.t == 'Sec' then | |
res[#res + 1] = pandoc.Header(el.level, el.label, el.attr) | |
res:extend(flatten(el.contents)) | |
else | |
res[#res + 1] = el | |
end | |
end | |
return res | |
end | |
function Pandoc (doc) | |
local res = List:new{} | |
local sections = utils.hierarchicalize(doc.blocks) | |
for _, sec in ipairs(sections) do | |
local section_doc = pandoc.Pandoc(flatten({sec}), doc.meta) | |
local section_json = dkjson.encode(section_doc) | |
local filtered = pandoc.pipe('pandoc-citeproc', {}, section_json) | |
res:extend(pandoc.read(filtered, 'json').blocks) | |
end | |
return pandoc.Pandoc(res, doc.meta) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment