Skip to content

Commit

Permalink
[DOCKER] Pandoc: add wordcount custom filter (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-lancini authored Sep 21, 2023
1 parent 1d0c64d commit c24d128
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docker/pandoc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ COPY docker/pandoc/requirements.txt /requirements.txt
RUN pip3 install --no-cache-dir -r /requirements.txt \
&& rm -f /requirements.txt

# Install custom filters
COPY docker/pandoc/filters/* /root/.pandoc/filters/

# Set working directory
WORKDIR /data

# Command
Expand Down
29 changes: 29 additions & 0 deletions docker/pandoc/filters/wordcount.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- counts words in a document

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:
el.blocks:walk(wordcount)
print(words .. " words in body")
os.exit(0)
end

0 comments on commit c24d128

Please sign in to comment.