-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate word count using wordcount.lua filter
- Loading branch information
Showing
6 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Release type: minor | ||
|
||
* Using [wordcount Lua Filter](https://github.com/pandoc/lua-filters/tree/master/wordcount) instead of the [markdown-word-count](https://github.com/gandreadis/markdown-word-count) Python package to calculate word count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-- counts words in a document | ||
|
||
words = 0 | ||
characters = 0 | ||
characters_and_spaces = 0 | ||
process_anyway = false | ||
|
||
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 | ||
characters = characters + utf8.len(el.text) | ||
characters_and_spaces = characters_and_spaces + utf8.len(el.text) | ||
end, | ||
|
||
Space = function(el) | ||
characters_and_spaces = characters_and_spaces + 1 | ||
end, | ||
|
||
Code = function(el) | ||
_,n = el.text:gsub("%S+","") | ||
words = words + n | ||
text_nospace = el.text:gsub("%s", "") | ||
characters = characters + utf8.len(text_nospace) | ||
characters_and_spaces = characters_and_spaces + utf8.len(el.text) | ||
end, | ||
|
||
CodeBlock = function(el) | ||
_,n = el.text:gsub("%S+","") | ||
words = words + n | ||
text_nospace = el.text:gsub("%s", "") | ||
characters = characters + utf8.len(text_nospace) | ||
characters_and_spaces = characters_and_spaces + utf8.len(el.text) | ||
end | ||
} | ||
|
||
-- check if the `wordcount` variable is set to `process-anyway` | ||
function Meta(meta) | ||
if meta.wordcount and (meta.wordcount=="process-anyway" | ||
or meta.wordcount=="process" or meta.wordcount=="convert") then | ||
process_anyway = true | ||
end | ||
end | ||
|
||
function Pandoc(el) | ||
-- skip metadata, just count body: | ||
pandoc.walk_block(pandoc.Div(el.blocks), wordcount) | ||
print(words .. " words in body") | ||
print(characters .. " characters in body") | ||
print(characters_and_spaces .. " characters in body (including spaces)") | ||
if not process_anyway then | ||
os.exit(0) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters