1
Fork 0

Split out wordcount and variable filters

This commit is contained in:
Jake Howard 2018-04-03 12:23:27 +01:00
parent 3482586880
commit aa320a927d
4 changed files with 15 additions and 22 deletions

View file

@ -17,10 +17,10 @@ then
INPUT_FILES="$INPUT_FILES $METADATA_FILE" INPUT_FILES="$INPUT_FILES $METADATA_FILE"
fi fi
PANDOC_ARGS="$INPUT_FILES --template $SCRIPTPATH/main.tex -o $OUTPUT_FILE $ADDITIONAL_ARGS" PANDOC_ARGS="$INPUT_FILES --lua-filter=variables.lua --template $SCRIPTPATH/main.tex -o $OUTPUT_FILE $ADDITIONAL_ARGS"
echo "> Building document..." echo "> Building document..."
pandoc $PANDOC_ARGS pandoc $PANDOC_ARGS
echo "> Running second pass..." echo "> Running second pass..."
pandoc --lua-filter=second-pass.lua $PANDOC_ARGS pandoc --lua-filter=word-count.lua $PANDOC_ARGS

View file

@ -16,13 +16,13 @@
\author{Some Author} \author{Some Author}
\date{2018-01-01} \date{2018-01-01}
$if(secondpass)$ $if(wordcount)$
\include{header-footer} \include{header-footer}
$endif$ $endif$
\begin{document} \begin{document}
$if(secondpass)$ $if(wordcount)$
\input{title} \input{title}
\tableofcontents \tableofcontents
$endif$ $endif$

View file

@ -2,16 +2,6 @@ local vars = {}
local additionalChars = {".", ",", "!", ":", ";"} local additionalChars = {".", ",", "!", ":", ";"}
function fileExists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local function countWords()
local rawText = pandoc.pipe("pdftotext", {"output.pdf", "-"}, "")
return pandoc.pipe("wc", {"-w"}, rawText)
end
local function getVars(meta) local function getVars(meta)
for k, v in pairs(meta) do for k, v in pairs(meta) do
if v.t == 'MetaInlines' then if v.t == 'MetaInlines' then
@ -24,18 +14,12 @@ local function getVars(meta)
end end
end end
end end
return meta
end end
return { return {
{ {
Meta = function(meta) Meta = getVars
getVars(meta)
if fileExists("output.pdf") then
meta["wordcount"] = countWords()
meta["secondpass"] = true
end
return meta
end,
}, },
{ {
Str = function (el) Str = function (el)

9
word-count.lua Normal file
View file

@ -0,0 +1,9 @@
return {
{
Meta = function(meta)
local rawText = pandoc.pipe("pdftotext", {"output.pdf", "-"}, "")
meta["wordcount"] = pandoc.pipe("wc", {"-w"}, rawText)
return meta
end,
},
}