Skip to content

Instantly share code, notes, and snippets.

@tarleb
Created May 22, 2020 06:28
Show Gist options
  • Save tarleb/afee1b1d97e52aca888f410e77b3624a to your computer and use it in GitHub Desktop.
Save tarleb/afee1b1d97e52aca888f410e77b3624a to your computer and use it in GitHub Desktop.
Filter to highlight some authors in the bibliography
local List = require 'pandoc.List'
local utils = require 'pandoc.utils'
local stringify = utils.stringify
function highlighter(given_name_pattern, family_name_pattern)
local highlight_author = function (author)
local given = author.given and stringify(author.given)
local family = author.family and stringify(author.family)
if given and given:match(given_name_pattern) and
family and family:match(family_name_pattern) then
author.given = {pandoc.Strong(setmetatable(author.given, nil))}
author.family = {pandoc.Strong(setmetatable(author.family, nil))}
end
return author
end
return function(reference)
if reference.author and reference.author.map then
reference.author = reference.author:map(highlight_author)
end
return reference
end
end
function Pandoc (doc)
local meta = doc.meta
local fh = io.popen(
"pandoc-citeproc --bib2yaml "
.. stringify(meta.bibliography)
)
if io.type(fh) ~= 'file' then return end
local bibyaml = fh:read('*a')
fh:close()
local references = pandoc.read(bibyaml).meta.references
meta.bibliography = nil
meta.references = references:map(
highlighter(stringify(meta['given-name-pattern']),
stringify(meta['family-name-pattern']))
)
return utils.run_json_filter(
pandoc.Pandoc(doc.blocks, meta),
'pandoc-citeproc'
)
end
@kjayhan
Copy link

kjayhan commented Sep 22, 2022

Thank you very much for very detailed instructions. I should have probably mentioned that I am a markdown novice. And this is first time touching a lua file. I tried adding test.lua to my folder and to _output.yml here, but it didn't produce any result either (probably I should change something more).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment