diff --git a/build.sh b/build.sh index 9e9a8c7..8a9b703 100755 --- a/build.sh +++ b/build.sh @@ -17,10 +17,10 @@ then INPUT_FILES="$INPUT_FILES $METADATA_FILE" 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..." pandoc $PANDOC_ARGS echo "> Running second pass..." -pandoc --lua-filter=second-pass.lua $PANDOC_ARGS +pandoc --lua-filter=word-count.lua $PANDOC_ARGS diff --git a/main.tex b/main.tex index 7c61eee..3345e3e 100644 --- a/main.tex +++ b/main.tex @@ -16,13 +16,13 @@ \author{Some Author} \date{2018-01-01} - $if(secondpass)$ + $if(wordcount)$ \include{header-footer} $endif$ \begin{document} - $if(secondpass)$ + $if(wordcount)$ \input{title} \tableofcontents $endif$ diff --git a/second-pass.lua b/variables.lua similarity index 58% rename from second-pass.lua rename to variables.lua index 402c641..02880cf 100644 --- a/second-pass.lua +++ b/variables.lua @@ -2,16 +2,6 @@ local vars = {} 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) for k, v in pairs(meta) do if v.t == 'MetaInlines' then @@ -24,18 +14,12 @@ local function getVars(meta) end end end + return meta end return { { - Meta = function(meta) - getVars(meta) - if fileExists("output.pdf") then - meta["wordcount"] = countWords() - meta["secondpass"] = true - end - return meta - end, + Meta = getVars }, { Str = function (el) diff --git a/word-count.lua b/word-count.lua new file mode 100644 index 0000000..3606331 --- /dev/null +++ b/word-count.lua @@ -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, + }, +}