Skip to content

Commit cdd5890

Browse files
committed
Update readme
1 parent ca3d6ed commit cdd5890

File tree

2 files changed

+206
-236
lines changed

2 files changed

+206
-236
lines changed

README.md

+118-128
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11

2-
<!-- README.md is generated from README.qmd. Please edit that file -->
32

43
# Quarto word count
54

6-
- <a href="#why-counting-words-is-hard"
7-
id="toc-why-counting-words-is-hard">Why counting words is hard</a>
8-
- <a href="#using-the-word-count-script"
9-
id="toc-using-the-word-count-script">Using the word count script</a>
10-
- <a href="#using-as-an-extension" id="toc-using-as-an-extension">Using
11-
as an extension</a>
12-
- <a href="#using-without-an-extension"
13-
id="toc-using-without-an-extension">Using without an extension</a>
14-
- <a href="#how-this-all-works" id="toc-how-this-all-works">How this all
15-
works</a>
16-
- <a href="#appendices" id="toc-appendices">Appendices</a>
17-
- <a href="#example" id="toc-example">Example</a>
18-
- <a href="#credits" id="toc-credits">Credits</a>
5+
6+
- [Why counting words is hard](#why-counting-words-is-hard)
7+
- [Using the word count script](#using-the-word-count-script)
8+
- [Installing](#installing)
9+
- [Usage](#usage)
10+
- [Terminal output](#terminal-output)
11+
- [Shortcodes](#shortcodes)
12+
- [Appendices](#appendices)
13+
- [Example](#example)
14+
- [Credits](#credits)
15+
- [How this all works](#how-this-all-works)
16+
17+
<!-- README.md is generated from README.qmd. Please edit that file -->
1918

2019
## Why counting words is hard
2120

@@ -61,8 +60,6 @@ bunch of reasons:
6160
footnote styles), and more importantly, it will not count any of the
6261
automatically generated references in the final bibliography list.
6362

64-
## Using the word count script
65-
6663
This extension fixes all three of these issues by relying on a [Lua
6764
filter](_extensions/wordcount/wordcount.lua) to count the words after
6865
the document has been rendered and before it has been converted to its
@@ -73,40 +70,25 @@ just copied and slightly expanded [that package’s
7370
`inst/wordcount.lua`](https://github.com/crsh/rmdfiltr/blob/master/inst/wordcount.lua)).
7471
The filter works really well and [is generally comparable to Word’s word
7572
count](https://cran.r-project.org/web/packages/rmdfiltr/vignettes/wordcount.html).
73+
You should definitely glance through the [“How this all works”
74+
section](#how-this-all-works) to understand… um… how it works.
7675

77-
The word count will appear in the terminal output when rendering the
78-
document. It shows three different values: (1) the total count, (2) the
79-
count for the document sans references, and (3) the count for the
80-
reference list alone.
81-
82-
``` text
83-
133 total words
84-
-----------------------------
85-
76 words in text body
86-
57 words in reference section
87-
```
88-
89-
There are two ways to use the filter: (1) as a formal Quarto format
90-
extension and (2) as a set of pandoc filters. You should definitely
91-
glance through the [“How this all works” section](#how-this-all-works)
92-
to understand… um… how it works.
93-
94-
### Using as an extension
76+
## Using the word count script
9577

96-
Install the extension in an already-existing project by running this in
97-
your terminal:
78+
### Installing
9879

9980
``` bash
10081
quarto add andrewheiss/quarto-wordcount
10182
```
10283

103-
Or create a brand new project by running this:
84+
Using {quarto-wordcount} requires Quarto version \>= 1.3.0
10485

105-
``` bash
106-
quarto use template andrewheiss/quarto-wordcount
107-
```
86+
This will install the extension under the `_extensions` subdirectory. If
87+
you’re using version control, you will want to check in this directory.
88+
89+
### Usage
10890

109-
You can then specify one of three different output formats in your YAML
91+
You can specify one of three different output formats in your YAML
11092
settings: `wordcount-html`, `wordcount-pdf`, and `wordcount-docx`:
11193

11294
``` yaml
@@ -128,43 +110,106 @@ format:
128110
cap-location: margin
129111
```
130112

131-
### Using without an extension
113+
### Terminal output
132114

133-
Alternatively, if you don’t want to install the extension, download the
134-
two Lua scripts [`wordcount.lua`](_extensions/wordcount/wordcount.lua)
135-
and [`citeproc.lua`](_extensions/wordcount/citeproc.lua), put them
136-
somewhere in your project, and reference them in the YAML front matter
137-
of your document. Make sure you also disable citeproc so that it doesn’t
138-
run twice.
115+
The word count will appear in the terminal output when rendering the
116+
document. It shows multiple values: (1) the total count, (2) the count
117+
for the document sans references, and (3) the count for the reference
118+
list alone.
139119

140-
``` yaml
141-
title: Something
142-
format:
143-
html:
144-
citeproc: false
145-
filters:
146-
- at: pre-render
147-
path: "citeproc.lua"
148-
- at: pre-render
149-
path: "wordcount.lua"
120+
``` text
121+
133 total words
122+
-----------------------------
123+
76 words in text body
124+
57 words in reference section
150125
```
151126

152-
Or as another option, install the extension like normal, but don’t use
153-
the `wordcount-html` format and instead refer to the Lua filters that
154-
live in the extension folder:
127+
### Shortcodes
128+
129+
There are also multiple shortcodes you can use to include different word
130+
counts directly in the document:
131+
132+
- Use `{{< words-total >}}` to include a count of all words
133+
134+
- Use `{{< words-body >}}` to include a count of the words in the text
135+
body only, omitting the references, notes, and appendix
136+
137+
- Use `{{< words-ref >}}` to include a count of the words in the
138+
reference section
139+
140+
- Use `{{< words-append >}}` to include a count of the words in the
141+
appendix, which must be wrapped in a div with the `#appendix-count` id
142+
([see below for more details](#appendices))
143+
144+
- Use `{{< words-note >}}` to include a count of the words in the notes:
145+
146+
- Use `{{< words-sum ARG >}}` where `ARG` is some concatenation of the
147+
four countable areas: `body`, `ref`, `append`, and `note`.
148+
149+
For example, `{{< words-sum body-note >}}` includes a count of the
150+
words in the body and notes; `{{< words-sum ref-append >}}` includes a
151+
count of the words in the references and appendix
152+
153+
You can use shortcodes in your YAML metadata too:
155154

156155
``` yaml
157156
title: Something
158-
format:
159-
html:
160-
citeproc: false
161-
filters:
162-
- at: pre-render
163-
path: "_extensions/andrewheiss/wordcount/citeproc.lua"
164-
- at: pre-render
165-
path: "_extensions/andrewheiss/wordcount/wordcount.lua"
157+
subtitle: "{{< words-total >}} words"
158+
```
159+
160+
### Appendices
161+
162+
In academic writing, it’s often helpful to have a separate word count
163+
for content in the appendices, since things there don’t typically count
164+
against journal word limits. [Quarto has a neat feature for
165+
automatically creating an appendix
166+
section](https://quarto.org/docs/authoring/appendices.html) and moving
167+
content there automatically as needed. It does this (I think) with a
168+
fancy Lua filter.
169+
170+
However, Quarto’s appendix-generating process comes *after* any custom
171+
Lua filters, so even though the final rendered document creates a div
172+
with the id “appendix”, that div isn’t accessible when counting words
173+
(since it doesn’t exist yet), so there’s no easy way to extract the
174+
appendix words from the rest of the text.
175+
176+
So, as a (temporary?) workaround (until I can figure out how to make
177+
this Lua filter run after the creation of the appendix div?), you can
178+
get a separate word count for the appendix by creating your own div with
179+
the id `appendix-count`:
180+
181+
``` markdown
182+
# Introduction
183+
184+
Regular text goes here.
185+
186+
::: {#appendix-count}
187+
188+
# Appendix {.appendix}
189+
190+
More words here
191+
192+
:::
166193
```
167194

195+
That will create this word count:
196+
197+
5 in the main text + references, with 4 in the appendix
198+
-------------------------------------------------------
199+
5 words in text body
200+
0 words in reference section
201+
4 words in appendix section
202+
203+
## Example
204+
205+
You can see a minimal sample document at [`template.qmd`](template.qmd).
206+
207+
## Credits
208+
209+
The original [`wordcount.lua`](_extensions/wordcount/wordcount.lua)
210+
filter came from [Frederik Aust’s (@crsh)](https://github.com/crsh)
211+
[{rmdfiltr}](https://github.com/crsh/rmdfiltr) package.
212+
168213
## How this all works
169214

170215
Behind the scenes, pandoc typically converts a Markdown document to an
@@ -230,8 +275,7 @@ format:
230275
html:
231276
citeproc: false
232277
filters:
233-
- at: pre-render
234-
path: "/path/to/wordcount.lua"
278+
- "/path/to/wordcount.lua"
235279
```
236280

237281
However, there’s no obvious way to reposition the `--citeproc` argument
@@ -259,10 +303,9 @@ format:
259303
html:
260304
citeproc: false
261305
filters:
262-
- at: pre-render
263-
path: "/path/to/citeproc.lua"
264-
- at: pre-render
265-
path: "/path/to/wordcount.lua"
306+
- "/path/to/citeproc.lua"
307+
- "/path/to/wordcount.lua"
308+
- quarto
266309
```
267310
268311
This creates a pandoc command that looks something like this, feeding
@@ -288,56 +331,3 @@ format:
288331
```
289332
290333
But that doesn’t work yet.
291-
292-
## Appendices
293-
294-
In academic writing, it’s often helpful to have a separate word count
295-
for content in the appendices, since things there don’t typically count
296-
against journal word limits. [Quarto has a neat feature for
297-
automatically creating an appendix
298-
section](https://quarto.org/docs/authoring/appendices.html) and moving
299-
content there automatically as needed. It does this (I think) with a
300-
fancy Lua filter.
301-
302-
However, Quarto’s appendix-generating process comes *after* any custom
303-
Lua filters, so even though the final rendered document creates a div
304-
with the id “appendix”, that div isn’t accessible when counting words
305-
(since it doesn’t exist yet), so there’s no easy way to extract the
306-
appendix words from the rest of the text.
307-
308-
So, as a temporary workaround until I can figure out how to make this
309-
Lua filter run after the creation of the appendix div, you can get a
310-
separate word count for the appendix by creating your own div with the
311-
id `appendix-count`:
312-
313-
``` markdown
314-
# Introduction
315-
316-
Regular text goes here.
317-
318-
::: {#appendix-count}
319-
320-
# Appendix {.appendix}
321-
322-
More words here
323-
324-
:::
325-
```
326-
327-
That will create this word count:
328-
329-
5 in the main text + references, with 4 in the appendix
330-
-------------------------------------------------------
331-
5 words in text body
332-
0 words in reference section
333-
4 words in appendix section
334-
335-
## Example
336-
337-
You can see a minimal sample document at [`template.qmd`](template.qmd).
338-
339-
## Credits
340-
341-
The [`wordcount.lua`](_extensions/wordcount/wordcount.lua) filter comes
342-
from [Frederik Aust’s (@crsh)](https://github.com/crsh)
343-
[{rmdfiltr}](https://github.com/crsh/rmdfiltr) package.

0 commit comments

Comments
 (0)