-
-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds process-anyway option #162
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
# wordcount | ||
|
||
This filter counts the words and characters in the body of a document (omitting | ||
metadata like titles and abstracts), including words in code. | ||
It should be more accurate than `wc -w` or `wc -m` run directly on a | ||
Markdown document, since `wc` will also count markup | ||
characters, like the `#` in front of an ATX header, or | ||
tags in HTML documents. | ||
This filter counts the words and characters in the body of a document | ||
(omitting metadata like titles and abstracts), including words in | ||
code. It should be more accurate than `wc -w` or `wc -m` run directly | ||
on a Markdown document, since `wc` will also count markup characters, | ||
like the `#` in front of an ATX header, or tags in HTML documents. | ||
|
||
To run it, `pandoc --lua-filter wordcount.lua myfile.md`. | ||
The word count will be printed to stdout. | ||
|
||
If you want to process the document as well as printing the word count | ||
set the variable `wordcount` to `process` (or `process-anyway` or `convert`). | ||
This works only in conjunction with the standalone document option (`-s`). | ||
This can be done through the command line: | ||
|
||
``` | ||
pandoc -s -L wordcount.lua -M wordcount=process sample.md -o output.html | ||
``` | ||
|
||
Or the document's metadata block: | ||
|
||
``` | ||
--- | ||
title: My Long Book | ||
wordcount: process-anyway | ||
--- | ||
``` | ||
|
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 |
---|---|---|
@@ -1,45 +1,56 @@ | ||
-- counts words in a document | ||
-- counts words in a document | ||
|
||
words = 0 | ||
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 | ||
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, | ||
end, | ||
|
||
Space = function(el) | ||
characters_and_spaces = characters_and_spaces + 1 | ||
end, | ||
|
||
Code = function(el) | ||
_,n = el.text:gsub("%S+","") | ||
words = words + n | ||
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, | ||
end, | ||
|
||
CodeBlock = function(el) | ||
_,n = el.text:gsub("%S+","") | ||
words = words + n | ||
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 | ||
} | ||
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") | ||
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)") | ||
os.exit(0) | ||
if not process_anyway then | ||
os.exit(0) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with this