diff --git a/README.qmd b/README.qmd index bea8445..915333f 100644 --- a/README.qmd +++ b/README.qmd @@ -201,6 +201,39 @@ format: But that doesn't work yet. +## Appendices + +In academic writing, it's often helpful to have a separate word count for content in the appendices, since things there don't typically count against journal word limits. [Quarto has a neat feature for automatically creating an appendix section](https://quarto.org/docs/authoring/appendices.html) and moving content there automatically as needed. It does this (I think) with a fancy Lua filter. + +However, Quarto's appendix-generating process comes *after* any custom Lua filters, so even though the final rendered document creates a div with the id "appendix", that div isn't accessible when counting words (since it doesn't exist yet), so there's no easy way to extract the appendix words from the rest of the text. + +So, as a temporary workaround until I can figure out how to make this Lua filter run after the creation of the appendix div, you can get a separate word count for the appendix by creating your own div with the id `appendix-count`: + +````markdown +# Introduction + +Regular text goes here. + +::: {#appendix-count} + +# Appendix {.appendix} + +More words here + +::: +```` + +That will create this word count: + +``` +5 in the main text + references, with 4 in the appendix +------------------------------------------------------- +5 words in text body +0 words in reference section +4 words in appendix section +``` + + ## Example You can see a minimal sample document at [`template.qmd`](template.qmd). diff --git a/_extensions/wordcount/wordcount.lua b/_extensions/wordcount/wordcount.lua index 627f2f0..2f9e0fd 100644 --- a/_extensions/wordcount/wordcount.lua +++ b/_extensions/wordcount/wordcount.lua @@ -11,6 +11,7 @@ local body_words = 0 local ref_words = 0 +local appendix_words = 0 function is_table (blk) return (blk.t == "Table") @@ -62,6 +63,33 @@ function remove_all_refs (blks) 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 + body_count = { Str = function(el) -- we don't count a word if it's entirely punctuation: @@ -90,6 +118,16 @@ ref_count = { end } +-- Count words in the appendix +appendix_count = { + Str = function(el) + -- we don't count a word if it's entirely punctuation: + if el.text:match("%P") then + appendix_words = appendix_words + 1 + end + end +} + 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") @@ -100,14 +138,31 @@ function Pandoc(el) refs_title = el.meta["reference-section-title"] local unreffed = remove_all_refs(untabled) - pandoc.walk_block(pandoc.Div(unreffed), body_count) + + -- 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 body_words_out = body_words .. " words in text body" local refs = get_all_refs(untabled) pandoc.walk_block(pandoc.Div(refs), ref_count) local ref_words_out = ref_words .. " words in reference section" - local total_words_out = body_words + ref_words .. " total words" + -- 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) + -- Print out the count of words in the appendix + local appendix_words_out = appendix_words .. " words in appendix section" + + local total_words_out = "" + if appendix_words > 0 then + total_words_out = body_words + ref_words .. " in the main text + references, with " .. appendix_words .. " in the appendix" + else + total_words_out = body_words + ref_words + appendix_words .. " total words" + end local longest_out = math.max(string.len(body_words_out), string.len(ref_words_out), @@ -117,6 +172,11 @@ function Pandoc(el) print(string.rep("-", longest_out)) print(body_words_out) print(ref_words_out) + + if appendix_words > 0 then + print(appendix_words_out) + end + print() return el diff --git a/template.qmd b/template.qmd index 1a28d10..fc25310 100644 --- a/template.qmd +++ b/template.qmd @@ -55,4 +55,15 @@ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. -## Refereneces +## References + +::: {#refs} +::: + +::: {#appendix-count} + +## Appendix {.appendix} + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + +:::