|
1 |
| --- counts words in a document |
| 1 | +-- counts words in a document |
2 | 2 |
|
3 |
| -words = 0 |
| 3 | +words = 0 |
4 | 4 | characters = 0
|
5 | 5 | characters_and_spaces = 0
|
| 6 | +process_anyway = false |
6 | 7 |
|
7 |
| -wordcount = { |
8 |
| - Str = function(el) |
9 |
| - -- we don't count a word if it's entirely punctuation: |
10 |
| - if el.text:match("%P") then |
11 |
| - words = words + 1 |
12 |
| - end |
| 8 | +wordcount = { |
| 9 | + Str = function(el) |
| 10 | + -- we don't count a word if it's entirely punctuation: |
| 11 | + if el.text:match("%P") then |
| 12 | + words = words + 1 |
| 13 | + end |
13 | 14 | characters = characters + utf8.len(el.text)
|
14 | 15 | characters_and_spaces = characters_and_spaces + utf8.len(el.text)
|
15 |
| - end, |
| 16 | + end, |
16 | 17 |
|
17 | 18 | Space = function(el)
|
18 | 19 | characters_and_spaces = characters_and_spaces + 1
|
19 | 20 | end,
|
20 | 21 |
|
21 |
| - Code = function(el) |
22 |
| - _,n = el.text:gsub("%S+","") |
23 |
| - words = words + n |
| 22 | + Code = function(el) |
| 23 | + _,n = el.text:gsub("%S+","") |
| 24 | + words = words + n |
24 | 25 | text_nospace = el.text:gsub("%s", "")
|
25 | 26 | characters = characters + utf8.len(text_nospace)
|
26 | 27 | characters_and_spaces = characters_and_spaces + utf8.len(el.text)
|
27 |
| - end, |
| 28 | + end, |
28 | 29 |
|
29 |
| - CodeBlock = function(el) |
30 |
| - _,n = el.text:gsub("%S+","") |
31 |
| - words = words + n |
| 30 | + CodeBlock = function(el) |
| 31 | + _,n = el.text:gsub("%S+","") |
| 32 | + words = words + n |
32 | 33 | text_nospace = el.text:gsub("%s", "")
|
33 | 34 | characters = characters + utf8.len(text_nospace)
|
34 | 35 | characters_and_spaces = characters_and_spaces + utf8.len(el.text)
|
35 |
| - end |
36 |
| -} |
| 36 | + end |
| 37 | +} |
| 38 | + |
| 39 | +-- check if the `wordcount` variable is set to `process-anyway` |
| 40 | +function Meta(meta) |
| 41 | + if meta.wordcount and (meta.wordcount=="process-anyway" |
| 42 | + or meta.wordcount=="process" or meta.wordcount=="convert") then |
| 43 | + process_anyway = true |
| 44 | + end |
| 45 | +end |
37 | 46 |
|
38 |
| -function Pandoc(el) |
39 |
| - -- skip metadata, just count body: |
40 |
| - pandoc.walk_block(pandoc.Div(el.blocks), wordcount) |
41 |
| - print(words .. " words in body") |
| 47 | +function Pandoc(el) |
| 48 | + -- skip metadata, just count body: |
| 49 | + pandoc.walk_block(pandoc.Div(el.blocks), wordcount) |
| 50 | + print(words .. " words in body") |
42 | 51 | print(characters .. " characters in body")
|
43 | 52 | print(characters_and_spaces .. " characters in body (including spaces)")
|
44 |
| - os.exit(0) |
| 53 | + if not process_anyway then |
| 54 | + os.exit(0) |
| 55 | + end |
45 | 56 | end
|
0 commit comments