Sharing data between custom filters

9 views
Skip to first unread message

Luc Bourhis

unread,
Oct 30, 2022, 2:29:10 PM10/30/22
to nanoc
Hi, I found the need for a filter of mine to not just change @item content but also to pass some data back to the compile block. The following works:
```
compile ... do
    latex_stuff = {}
    filter :gather_latex_stuff, {latex_stuff: latex_stuff}
end
```
and elsewhere
```
class LaTeXStuff < Nanoc::Filter
  identifier :gather_latex_stuff
  def run(content, params)
    shared = params[:latex_stuff]
    shared[:something] = ...
```
but it sounds like a terrible hack, that could be defeated by future changes of Nanoc. What would be the cleanest way to do that?

My motivation comes from the following. I am trying to design a system to handle bibliography in articles. So I may have a hierarchy like
```
/references.bib
/first/references.bib
/first/second/article.md
```
with the *.bib files containing keyed entries with the bibliographic information (journal, author, pages, etc). Then article.md may features macros like [cite|somekey]. So I have one filter interpreting those citations, and then a layout producing the bibliography section. Both that filter and that layout need to get the information from the *.bib files and I want to gather that information only once.

Best wishes,

Luc J Bourhis

Luc Bourhis

unread,
Oct 30, 2022, 2:32:35 PM10/30/22
to nanoc
 And just to state the obvious, I want of course /first/second/article.md to be reprocessed if one of the .bib file changes. That requires accessing them from within a filter or a layout so that Nanoc dependencies are correctly computed. I can't access the .bib items from a preprocessor or a compile block, as Nanoc will then not create dependencies.

Luc Bourhis

unread,
Oct 31, 2022, 9:13:50 AM10/31/22
to nanoc
After all, I did the obvious: cram everything in one single filter, calling `render` directly there instead of from Rules. It makes that filter a bit of a black box but a comment in Rules as to what layout is used under the hood solves that. Still curious to know whether there is a more subtle way!
Reply all
Reply to author
Forward
0 new messages