From a6ed61b4eb8ffbdd692712cca3f5a054169a145c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 2 Apr 2018 23:07:02 +0100 Subject: [PATCH] Make metadata accessible from templates --- second-pass.lua | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/second-pass.lua b/second-pass.lua index ccd6120..b1b305f 100644 --- a/second-pass.lua +++ b/second-pass.lua @@ -1,23 +1,41 @@ +local vars = {} + function fileExists(name) local f=io.open(name,"r") if f~=nil then io.close(f) return true else return false end - end - +end local function countWords() local rawText = pandoc.pipe("pdftotext", {"output.pdf", "-"}, "") return pandoc.pipe("wc", {"-w"}, rawText) end +local function getVars(meta) + for k, v in pairs(meta) do + if v.t == 'MetaInlines' then + vars["$" .. k .. "$"] = v + end + end +end return { { - Meta = function(meta) - if fileExists("output.pdf") then - meta["wordcount"] = countWords() - meta["secondpass"] = true + Meta = function(meta) + getVars(meta) + if fileExists("output.pdf") then + meta["wordcount"] = countWords() + meta["secondpass"] = true + end + return meta + end, + }, + { + Str = function (el) + if vars[el.text] then + return pandoc.Span(vars[el.text]) + else + return el + end end - return meta - end, } }