-
Notifications
You must be signed in to change notification settings - Fork 6
/
wordcount.lua
347 lines (289 loc) · 7.55 KB
/
wordcount.lua
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
--[[
Counts words in a document
Images and tables are ignored; words in text body do not include referece section
This filter is an adapted mixture of
https://github.com/pandoc/lua-filters/blob/master/wordcount/wordcount.lua
and
https://github.com/pandoc/lua-filters/blob/master/section-refs/section-refs.lua
]]
local body_words = 0
local ref_words = 0
local note_words = 0
local appendix_words = 0
local total_words = 0
function set_meta(m)
m.wordcount_body_words = body_words
m.wordcount_ref_words = ref_words
m.wordcount_appendix_words = appendix_words
m.wordcount_note_words = note_words
m.wordcount_total_words = total_words
return m
end
function count_words(blks)
local count = 0
pandoc.walk_block(pandoc.Div(blks), {
Str = function(el)
if is_word(el.text) then
count = count + 1
end
end
})
return count
end
-- Ignore content in `no-count` divs
function is_no_count_div (blk)
if (blk and blk.t=="Div" and blk.classes and #blk.classes > 0) then
for _, class in pairs(blk.classes) do
if class=="no-count" then
return true
end
end
end
return false
end
function is_table (blk)
return (blk.t == "Table")
end
function is_image (blk)
return (blk.t == "Image" or blk.t == "Figure")
end
function is_word (text)
return text:match("%P") and not (text:match("“") or text:match("”"))
end
function remove_all_tables_images (blks)
return pandoc.walk_block(pandoc.Div(blks),
{
Table = function(el)
return {}
end,
Image = function(el)
return {}
end,
Figure = function(el)
return {}
end,
Div = function(el)
if is_no_count_div(el) then
return {}
end
return el
end
}).content
end
function is_ref_div (blk)
return (blk.t == "Div" and blk.identifier == "refs")
end
function get_all_notes (blks)
local all_notes = {}
pandoc.walk_block(pandoc.Div(blks),
{
Note = function(el)
table.insert(all_notes, el)
end
})
return all_notes
end
function remove_all_notes (blks)
return pandoc.walk_block(
pandoc.Div(blks),
{
Note = function(el)
return {}
end
}
).content
end
function get_all_refs (blks)
local out = {}
for _, b in pairs(blks) do
if is_ref_div(b) then
table.insert(out, b)
end
end
return out
end
function remove_all_refs (blks)
local out = {}
for _, b in pairs(blks) do
if not (is_ref_div(b)) then
table.insert(out, b)
end
end
return out
end
-- Check if the block is an appendix div
function is_appendix_div (blk)
return (blk.t == "Div" and blk.identifier == "appendix-count")
end
-- Get all appendix divs
function get_all_appendix (blks)
local out = {}
for _, b in pairs(blks) do
if is_appendix_div(b) then
table.insert(out, b)
end
end
return out
end
-- Remove all appendix divs
function remove_all_appendix (blks)
local out = {}
for _, b in pairs(blks) do
if not is_appendix_div(b) then
table.insert(out, b)
end
end
return out
end
-- Function for printing word counts to the terminal
function print_word_counts()
local manuscript_words = body_words + note_words
-- Format these different numbers
local total_words_out = string.format(
"%d total %s",
total_words, total_words == 1 and "word" or "words"
)
local manuscript_words_out = string.format(
"%d %s in body and notes",
manuscript_words, manuscript_words == 1 and "word" or "words"
)
local body_words_out = string.format(
"%d %s in text body",
body_words, body_words == 1 and "word" or "words"
)
local note_words_out = note_words > 0 and string.format(
"%d %s in notes",
note_words, note_words == 1 and "word" or "words"
) or ""
local ref_words_out = ref_words > 0 and string.format(
"%d %s in reference section",
ref_words, ref_words == 1 and "word" or "words"
) or ""
local appendix_words_out = appendix_words > 0 and string.format(
"%d %s in appendix section",
appendix_words, appendix_words == 1 and "word" or "words"
) or ""
local longest_out = math.max(
#total_words_out,
#manuscript_words_out,
#body_words_out,
#note_words_out,
#ref_words_out,
#appendix_words_out
)
print("Overall totals:")
print(string.rep("-", longest_out + 3))
print("- " .. total_words_out)
print("- " .. manuscript_words_out)
print("\nSection totals:")
print(string.rep("-", longest_out + 3))
print("- " .. body_words_out)
if note_words_out ~= "" then
print("- " .. note_words_out)
end
if ref_words_out ~= "" then
print("- " .. ref_words_out)
end
if appendix_words_out ~= "" then
print("- " .. appendix_words_out)
end
print()
end
body_count = {
Str = function(el)
-- we don't count a word if it's entirely punctuation:
if is_word(el.text) then
body_words = body_words + 1
end
end,
Code = function(el)
_, n = el.text:gsub("%S+", "")
body_words = body_words + n
end
}
ref_count = {
Str = function(el)
-- we don't count a word if it's entirely punctuation:
if is_word(el.text) then
ref_words = ref_words + 1
end
end
}
-- Count words in the appendix
appendix_count = {
Str = function(el)
if is_word(el.text) then
appendix_words = appendix_words + 1
end
end,
Code = function(el)
_, n = el.text:gsub("%S+", "")
appendix_words = appendix_words + n
end
}
note_count = {
Str = function(el)
if is_word(el.text) then
note_words = note_words + 1
end
end,
Code = function(el)
_, n = el.text:gsub("%S+", "")
note_words = note_words + n
end
}
-- Actual word counting
function Pandoc(el)
if PANDOC_VERSION == nil then -- if pandoc_version < 2.1
io.stderr:write("WARNING: pandoc >= 2.1 required for wordcount filter\n")
return el
end
-- Count code blocks in body, notes, and appendix if needed
if el.meta["count-code-blocks"] ~= nil then
count_code_blocks = el.meta["count-code-blocks"]
else
count_code_blocks = true
end
-- Add these functions to the respective section counting functions
if count_code_blocks then
body_count.CodeBlock = function(el)
_, n = el.text:gsub("%S+", "")
body_words = body_words + n
end
appendix_count.CodeBlock = function(el)
_, n = el.text:gsub("%S+", "")
appendix_words = appendix_words + n
end
note_count.CodeBlock = function(el)
_, n = el.text:gsub("%S+", "")
note_words = note_words + n
end
end
-- Get all notes
local all_notes = get_all_notes(el.blocks)
-- Count words in notes
pandoc.walk_block(pandoc.Div(all_notes), note_count)
-- Remove tables, images, and {.no-count} contents
local untabled = remove_all_tables_images(el.blocks)
-- Next, remove notes
local unnote = remove_all_notes(untabled)
refs_title = el.meta["reference-section-title"]
local unreffed = remove_all_refs(unnote)
-- Remove appendix divs from the blocks
local unappended = remove_all_appendix(unreffed)
-- Walk through the unappended blocks and count the words
pandoc.walk_block(pandoc.Div(unappended), body_count)
local refs = get_all_refs(unnote)
pandoc.walk_block(pandoc.Div(refs), ref_count)
-- Get all appendix divs
local appendix = get_all_appendix(unreffed)
-- Walk through the appendix divs and count the words
pandoc.walk_block(pandoc.Div(appendix), appendix_count)
-- Calculate total
total_words = body_words + note_words + ref_words + appendix_words
-- Show counts in terminal
print_word_counts()
-- Modify metadata for words.lua
el.meta = set_meta(el.meta)
return el
end