From 28527ca2a2336c371966cef441247f11a6805686 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 16 Nov 2021 17:43:15 +0100 Subject: Make variables accessible from content files --- filters/code.lua | 13 ++++++++++++ filters/common_actions.lua | 53 ---------------------------------------------- filters/headers.lua | 16 ++++++++++++++ filters/vars.lua | 38 +++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 filters/code.lua delete mode 100644 filters/common_actions.lua create mode 100644 filters/headers.lua create mode 100644 filters/vars.lua (limited to 'filters') diff --git a/filters/code.lua b/filters/code.lua new file mode 100644 index 0000000..aca3ce8 --- /dev/null +++ b/filters/code.lua @@ -0,0 +1,13 @@ +function CodeBlock(el) + if el.classes[1] == "plain" then + el = pandoc.Div({ el }, { class = 's-code' }) + elseif el.classes[1] then + local formatted = pandoc.pipe('pygmentize', { + '-l', el.classes[1], '-f', 'html', '-O', 'cssclass=s-code s-code--highlight', + }, el.text) + + if formatted then el = pandoc.RawBlock('html', formatted) end + end + + return el +end diff --git a/filters/common_actions.lua b/filters/common_actions.lua deleted file mode 100644 index cfe2d58..0000000 --- a/filters/common_actions.lua +++ /dev/null @@ -1,53 +0,0 @@ -function CodeBlock(el) - if el.classes[1] == "plain" then - el = pandoc.Div({ el }, { class = 's-code' }) - elseif el.classes[1] then - local formatted = pandoc.pipe('pygmentize', { - '-l', el.classes[1], '-f', 'html', '-O', 'cssclass=s-code s-code--highlight', - }, el.text) - - if formatted then el = pandoc.RawBlock('html', formatted) end - end - - return el -end - -function Header(el) - if el.level == 1 then - local newchildren = pandoc.List() - newchildren:insert(pandoc.Span(el.content, { class = 's-headlines__title-inner' })) - el.content = newchildren - end - - if el.level <= 3 and el.identifier ~= '' then - el.content:insert(pandoc.Space()) - el.content:insert(pandoc.Link(pandoc.RawInline('html', - ''), - '#' .. el.identifier, nil, { class = 's-headlines__link' })) - end - - return el -end - -function Div(el) - if el.attributes.macro == nil then return el end - - if el.attributes.macro == 'refs' and el.content[1].tag == 'BulletList' then - local newchildren = pandoc.List() - - newchildren:insert(pandoc.RawBlock('html', '')) - - el.content = newchildren - end - - return el.content -end diff --git a/filters/headers.lua b/filters/headers.lua new file mode 100644 index 0000000..4e3a689 --- /dev/null +++ b/filters/headers.lua @@ -0,0 +1,16 @@ +function Header(el) + if el.level == 1 then + local newchildren = pandoc.List() + newchildren:insert(pandoc.Span(el.content, { class = 's-headlines__title-inner' })) + el.content = newchildren + end + + if el.level <= 3 and el.identifier ~= '' then + el.content:insert(pandoc.Space()) + el.content:insert(pandoc.Link(pandoc.RawInline('html', + ''), + '#' .. el.identifier, nil, { class = 's-headlines__link' })) + end + + return el +end diff --git a/filters/vars.lua b/filters/vars.lua new file mode 100644 index 0000000..e60019e --- /dev/null +++ b/filters/vars.lua @@ -0,0 +1,38 @@ +local vars = {} + +function string.split(str, sep) + sep = sep or '%s' + + local parts = pandoc.List() + + for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do + parts:insert(field) + if s == "" then return parts end + end +end + +function meta(meta) vars = meta end + +function str(el) + local prefix, varref, suffix = el.text:match('^(.*)%%(.*)%%(.*)$') + + if varref then + local parts = varref:split(".") + local var = vars + + for i = 1, #parts do + local part = parts[i] + local v = var[part] + + if not v then return el end + + var = v + end + + if var then return pandoc.Str(prefix .. var .. suffix) end + end + + return el +end + +return { { Meta = meta }, { Str = str } } -- cgit v1.2.3-70-g09d2