Adding additional lua-filter into YAML

Viewed 132

I'm trying to add an additional lua-filter into the YAML:

---
title: "TITLE"
subtitle: "SUBTITLE"
date: "07/04/2022" 
output:
  pdf_document:
    pandoc_args: 
      - --lua-filter=/Users/user/.local/share/pandoc/filters/wordcount.lua
    latex_engine: pdflatex
    includes:
      in_header: style.sty
    #keep_tex:  true
  bookdown::pdf_book:
    citation_package: biblatex
bibliography: literature.bib
csl: my.csl
lang: de
fontsize: 12pt
---

ok, now when I try to knit the document I'm getting this erorr:

/usr/local/bin/pandoc +RTS -K512m -RTS mother.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output mother.tex --lua-filter /Users/user/Library/R/x86_64/4.1/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Users/user/Library/R/x86_64/4.1/library/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine pdflatex --include-in-header style.sty --variable graphics --lua-filter=/Users/user/.local/share/pandoc/filters/wordcount.lua --variable 'geometry:margin=1in' --citeproc
655 words in body
4171 characters in body
4791 characters in body (including spaces)
Error in readLines(con, warn = FALSE) : cannot open the connection
In addition: Warning message:
In readLines(con, warn = FALSE) :
  cannot open file 'mother.tex': No such file or directory

What is wrong with the mother.tex? Why it's not been created?

Hope someone can help. Thanks already.

2 Answers

The problem is that the filter stops the conversion process, so no output is created.

I'm assuming you are using the wordcount filter from https://github.com/pandoc/lua-filters. That filter will abort the document conversion unless the metadata field wordcount is set to the string process or convert.

So you can add --metadata=wordcount=process this to pandoc_args setting:

    pandoc_args: 
      - --lua-filter=/Users/user/.local/share/pandoc/filters/wordcount.lua
      - --metadata=wordcount=process

or modify the filter to always process the document regardless of whether the metadata field is set or not.

Ok, I struggled a little bit to find the right position for the wordcout: process argument.

But a solution for those one who try to achive a workaround could be the following:

edit the following file from https://github.com/pandoc/lua-filters/blob/04e2d663dcb374d2c79ad1a42a30714843cb4e40/wordcount/wordcount.lua

notice the comment-out lines in line 39-42 and 43-45

-- counts words in a document

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
    characters = characters + utf8.len(el.text)
    characters_and_spaces = characters_and_spaces + utf8.len(el.text)
  end,

  Space = function(el)
    characters_and_spaces = characters_and_spaces + 1
  end,

  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,

  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
}

-- 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")
    print(characters .. " characters in body")
    print(characters_and_spaces .. " characters in body (including spaces)")
    if not process_anyway then
      os.exit(0)
    end
  end

Related