summaryrefslogtreecommitdiff
path: root/paper/lua-filters/wordcount/wordcount.lua
blob: 19aec11fe46443450aea8ed216642dbc12fd9b58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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: 
    pandoc.walk_block(pandoc.Div(el.blocks), wordcount) 
    print(words .. " words in body") 
    os.exit(0) 
end