How to use lua-filter with gitbook output?

I have an example bookdown project with gitbook output

index.Rmd:

---
title: "An Example Project"
author: "Coderoo"
output:
  bookdown::gitbook: default
    pandoc_args: ["--lua-filter=hello.lua"]
documentclass: book
---

Some text {{helloworld}}

hello.lua is taken straight from the pandoc lua-filter doc:

return {
  {
    Str = function (elem)
      if elem.text == "{{helloworld}}" then
        return pandoc.Emph {pandoc.Str "Hello, World"}
      else
        return elem
      end
    end,
  }
}

But when I try to build the book:

Rscript -e "bookdown::render_book('index.Rmd','bookdown::gitbook')"

I get this error:

Error in yaml::yaml.load(..., eval.expr = TRUE) : 
  Scanner error: mapping values are not allowed in this context at line 5, column 16
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous> -> .Call
Please delete _main.Rmd after you finish debugging the error.
Execution halted

If I replace bookdown::gitbook default in the YAML metadata with html_document then everything works fine.

Crossposted here: https://stackoverflow.com/questions/59523765/how-to-use-lua-filter-with-bookdown-gitbook-output

Hi, and welcome!

The short answer is that the default template chokes on the lua argument, which you can see by commenting it out. The long answer is that if you want to hew as closely to the default template as possible, you are going to need to create a custom template, but see@yihui caution

Sometimes you may want to change the overall theme of the output, and usually this can be done through the in_header option described in the previous section, or the css option if the output is HTML. Some output formats have their unique themes, such as gitbook, tufte_html_book, and tufte_book2, and you may not want to customize these themes too much. By comparison, the output formats html_book() and pdf_book() are not tied to particular themes and more customizable.

1 Like

Thank you. Yes, that has fixed it.

If I remove default the lua-filter works but the output still looks like the default gitbook. So is it ok to leave out the word default so I don't have to deal with the hassle of creating a custom template?

1 Like

AFAIK, no reason not to do that if it's getting the results you want! If this works, please mark the solution for the benefit of those to follow.

Actually, YOU found the solution. Please mark it. No false modesty. Penny a point, ain’t no one keepin’ score!

Thank you and no false modesty indeed. But I think your reply is more comprehensive for others who may have the same question.

Also, I'm not 100% sure that removing default will work as intended. It just seems to work right now in my toy project.

Fair enough! Come back if you have other issues. We all learn, especially those who hazard answers! I've learned much of what I know here.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.