add wordcount calculation

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Christine Dodrill 2021-04-23 07:36:10 -04:00
parent f9403010cd
commit d297449035
2 changed files with 32 additions and 0 deletions

View File

@ -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

30
src/wordcount.lua Normal file
View File

@ -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