From e7bb9d8e8a85f4b48731b5c418639a4abab6158e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 18 Sep 2020 15:50:44 +0200 Subject: [PATCH] add useful lua filters --- format-column.lua | 8 +++++++ format-link.lua | 51 ++++++++++++++++++++++++++++++++++++++++++ format-quote.lua | 25 +++++++++++++++++++++ format-semi-column.lua | 8 +++++++ 4 files changed, 92 insertions(+) create mode 100644 format-column.lua create mode 100644 format-link.lua create mode 100644 format-quote.lua create mode 100644 format-semi-column.lua diff --git a/format-column.lua b/format-column.lua new file mode 100644 index 0000000..19d5c5b --- /dev/null +++ b/format-column.lua @@ -0,0 +1,8 @@ +-- Add a latex insecable space before ":" for words ending with ":" +function Str(elem) + if elem.text:sub(-1) == ":" then + return pandoc.RawInline("latex", elem.text:gsub(":", "~:")) + else + return elem + end +end diff --git a/format-link.lua b/format-link.lua new file mode 100644 index 0000000..8f28ec5 --- /dev/null +++ b/format-link.lua @@ -0,0 +1,51 @@ +if FORMAT:match 'latex' then + function Link (elem) + + local abbrev = '' + if elem.c[3][1]:sub(2, 4) == 'fig' then + abbrev = 'fig.' + end + + if elem.c[3][1]:sub(2, 4) == 'sec' then + abbrev = 'section' + end + + if elem.c[1][2][1] == 'uri' then + return elem + else + + if elem.c[2][1] ~= nil then + + if elem.c[2][1].text == 'x' then + return { + pandoc.Str(abbrev), + pandoc.RawInline('latex', '~\\nameref{'), + pandoc.RawInline('latex', elem.c[3][1]:sub(2)), + pandoc.RawInline('latex', '}'), + pandoc.Str(', page'), + pandoc.RawInline('latex', '~\\pageref{'), + pandoc.RawInline('latex', elem.c[3][1]:sub(2)), + pandoc.RawInline('latex', '}') + } + end + + return { + elem, + pandoc.Str(' ('), + pandoc.Str(abbrev), + pandoc.RawInline('latex', '~\\ref{'), + pandoc.RawInline('latex', elem.c[3][1]:sub(2)), + pandoc.RawInline('latex', '}'), + pandoc.Str(', page'), + pandoc.RawInline('latex', '~\\pageref{'), + pandoc.RawInline('latex', elem.c[3][1]:sub(2)), + pandoc.RawInline('latex', '}'), + pandoc.Str(')'), + } + end + end + end +end + + + diff --git a/format-quote.lua b/format-quote.lua new file mode 100644 index 0000000..5d7712c --- /dev/null +++ b/format-quote.lua @@ -0,0 +1,25 @@ +-- fully copied from https://gist.github.com/niklaskorz/c9d55b157078dd1e1f5badad15093a72 +local double_quotes = {"»", "«"} +local single_quotes = {"›", "‹"} + +function get_preferences(m) + if m.double_quotes and m.double_quotes[1] and m.double_quotes[2] then + double_quotes = { m.double_quotes[1][1].c, m.double_quotes[2][1].c } + end + if m.single_quotes and m.single_quotes[1] and m.single_quotes[2] then + single_quotes = { m.single_quotes[1][1].c, m.single_quotes[2][1].c } + end +end + +function replace_quotes(elem) + local quotes = double_quotes + if elem.quotetype == "SingleQuote" then + quotes = single_quotes + end + return {pandoc.Str(quotes[2]), pandoc.RawInline('latex', '~')} .. elem.content .. {pandoc.RawInline('latex', '~'), pandoc.Str(quotes[1])} +end + +return { + { Meta = get_preferences }, + { Quoted = replace_quotes }, +} diff --git a/format-semi-column.lua b/format-semi-column.lua new file mode 100644 index 0000000..578d587 --- /dev/null +++ b/format-semi-column.lua @@ -0,0 +1,8 @@ +-- Add a latex insecable space before ";" for words ending with ";" +function Str(elem) + if elem.text:sub(-1) == ";" then + return pandoc.RawInline("latex", elem.text:gsub(";", "~;")) + else + return elem + end +end