papaja icon indicating copy to clipboard operation
papaja copied to clipboard

[Feature request] Automate YAML wordcount

Open levolz opened this issue 3 years ago • 5 comments

Hi, it would be great if there's a way to automate the wordcount specified in the YAML. It's always a small pierce in the heart to knit a document, see the Lua filter wordcount, and then having to go back to insert that just to knit again.

Cheers, Leonhard [this issue was supported by the communicative power of Twitter]

levolz avatar Aug 11 '20 06:08 levolz

Could the lua filter be modified to replace a string like '{{wordcount}}' with the calculated count? Or is the header containing the word count not yet appended to the rest of the document?

conig avatar Aug 27 '20 00:08 conig

Yes, something like this is would be possible. Another possibility would be to check if the meta field wordcount is missing and if so insert the result of the word count. I think this would be fairly straight forward.

What makes this more difficult to implement, is that, for historical reasons, keywords and word count are currently handled by the R-based preprocessor. So an implementation of this feature would need to translate this bit of code to Lua as well:

https://github.com/crsh/papaja/blob/1f124807b6acad09aaae156f0fb9d59590e6650c/R/apa6_formats.R#L458-L464

This is definitely doable and I'd be happy to accept a PR if anyone wants to take a crack a this (either just adding the word count to the meta field or both).

crsh avatar Aug 27 '20 09:08 crsh

This may be a useful approach: https://twitter.com/pandoc_tips/status/1357102818298130435

crsh avatar Apr 17 '21 20:04 crsh

This isn't a final solution, but if anyone is itching for this you can add this code to 'wordcount.lua' at line 105

  file = io.open("wordcount.tmp", "w")
  file:write(body_words)
  file:close()

This will output the wordcount to a file in your working directory. Then you can set your wordcount value to:

`r readLines('wordcount.tmp', warn = FALSE)`

Unfortunately this will result in your document having the previous version's wordcount, so you'll need to run knit twice when you need the word count to be brought up to date.

conig avatar Jul 06 '21 01:07 conig

Thanks for sharing this solution. I'll be sure to revisit it when I find the time to tackle this issue.

crsh avatar Jul 06 '21 07:07 crsh