Skip to content
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 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions wordcount/README.md
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
---
```

57 changes: 34 additions & 23 deletions wordcount/wordcount.lua
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

function Meta(meta)
  local keyword_list = pandoc.List {'process-anyway', 'process', 'convert', 'keep-going'}
  process_anyway = keyword_list:includes(meta.wordcount)
end

Copy link
Member

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

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