From d29744903551239334816a8c278b74635229e723 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 23 Apr 2021 07:36:10 -0400 Subject: [PATCH] add wordcount calculation Signed-off-by: Christine Dodrill --- src/build.sh | 2 ++ src/wordcount.lua | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/wordcount.lua 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