0

Can someone help me to write a lua filter that runs over all div in a html page, extracts the one with class "bibliographie" and insert the processed bibliography (content of index.bib) ?

I've tried that, but I'm nowhere near where I want to be. Many thanks in advance !

part of YAML :

bibliography: index.bib

part of template.html :

<div class="bibliographie">
<h2 class="sources-def-bib-title">Bibliographie</h2>
</div>

and my lua script :

function Pandoc(doc)
    local hblocks = {}
    for i,el in pairs(doc.blocks) do
        if (el.t == "Div" and el.classes[1] == "bibliographie") then
           table.insert(meta.bibliography, value)
        end
    end
    return pandoc.Pandoc(hblocks, doc.meta)
end

EDIT

we are developing an R package and here are the pandoc_args we used :

pandoc_args <- c(pandoc_metadata_arg("lang", "fr"), pandoc_args)
# use the non-breaking space pandoc lua filter
pandoc_args <- c(nbsp_filter_pandoc_args(), pandoc_args)
# hyphenations
pandoc_args <- c(pandoc_metadata_arg("hyphenopoly"), pandoc_args)
Damien Dotta
  • 771
  • 3
  • 13

1 Answers1

1

Looking at this step by step, we first want to extract the div contents by filtering all div elements; a Div filter function can be used for that, storing the contents in a local variable. Then after that, we need to add the extracted content to the metadata; Lua filters can use a Meta function to access and modify just the metdata. If no explicit order is given, pandoc will filter the blocks first, then metadata, which is just what we want here.

-- This will contain the contents of the div with id "bibliographie".
local bibliographie

function Div (div)
  if div.classes[1] == 'bibliographie' then
    bibliographie = div.content
    return {} -- Removes the item from the document.
              -- Drop these lines to keep the div.
  end
end

function Meta (meta)
  meta.bibliographie = bibliographie
  return meta
end

This assumes that pandoc-citeproc has already run, so the Lua filter must be given after the pandoc-citeproc filter: --filter pandoc-citeproc --lua-filter bibliographie-to-meta.lua

The official docs have all the details if needed.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • Many thanks @tarleb for the explanation ! If I want to insert the processed bibliography (ie the content of file index.bib instead of 'index.bib") ? – Damien Dotta Jun 07 '20 at 12:51
  • Ah, slightly misunderstood your question. What's the format of `index.bib`? – tarleb Jun 07 '20 at 13:21
  • Your answer already helps me a lot ! `@article{floch2017standards, title={Standards of living and segregation in twelve French metropolises}, author={Floch, Jean-Michel}, journal={Economie et Statistique}, volume={497}, number={497}, pages={73--96}, year={2017}, publisher={Pers{\'e}e-Portail des revues scientifiques en SHS} }` – Damien Dotta Jun 07 '20 at 13:42
  • Understand, so index.bib contains BibTeX entries. Do could add `cite: '@*'` to your YAML metadata to force the inclusion of all entries in your bibliography. – tarleb Jun 08 '20 at 06:16
  • I tried this but my div with classes "bibliographie" is stil empty. Maybe I have to parse index.bib ? – Damien Dotta Jun 08 '20 at 07:55
  • Can you link your project or provide a more complete example? I'm having trouble understanding the setup. – tarleb Jun 08 '20 at 12:12