diff --git a/src/build.sh b/src/build.sh index ad2dd22..675dcf8 100755 --- a/src/build.sh +++ b/src/build.sh @@ -9,3 +9,5 @@ echo $FILES pandoc -o ../out/Spellblade.epub --resource-path=. title.txt $FILES ebook-convert ../out/Spellblade.epub ../out/Spellblade.mobi + +pandoc --lua-filter wordcount.lua $FILES diff --git a/src/wordcount.lua b/src/wordcount.lua new file mode 100644 index 0000000..517e4ac --- /dev/null +++ b/src/wordcount.lua @@ -0,0 +1,30 @@ +-- counts words in a document +-- from https://pandoc.org/lua-filters.html#examples + +words = 0 + +wordcount = { + Str = function(el) + -- we don't count a word if it's entirely punctuation: + if el.text:match("%P") then + words = words + 1 + end + end, + + Code = function(el) + _,n = el.text:gsub("%S+","") + words = words + n + end, + + CodeBlock = function(el) + _,n = el.text:gsub("%S+","") + words = words + n + end +} + +function Pandoc(el) + -- skip metadata, just count body: + pandoc.walk_block(pandoc.Div(el.blocks), wordcount) + print(words .. " words in body") + os.exit(0) +end