From 16a4f78cf06025fc8dbb5ede928a7c1457c38803 Mon Sep 17 00:00:00 2001 From: Omikhleia Date: Fri, 22 Jul 2022 12:17:13 +0200 Subject: [PATCH] feat(packages): Support inline note, better definition lists --- packages/markdown/init.lua | 61 +++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/packages/markdown/init.lua b/packages/markdown/init.lua index 8edb6a858..84f930339 100644 --- a/packages/markdown/init.lua +++ b/packages/markdown/init.lua @@ -53,7 +53,7 @@ end local function split (source, delimiters) local elements = {} local pattern = '([^'..delimiters..']+)' - string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end); + string.gsub(source, pattern, function (value) elements[#elements + 1] = value; end); return elements end @@ -207,7 +207,7 @@ local function lunamarkAST2SILE (options) return createCommand("markdown:internal:paragraph", {}, out) end - writer.fenced_code = function(s, _, _) -- s, infostring, attr + writer.fenced_code = function (s, _, _) -- s, infostring, attr return createCommand("verbatim", {}, s) end @@ -271,15 +271,27 @@ local function lunamarkAST2SILE (options) return createStructuredCommand(tablecmd, {}, captioned) end + writer.definitionlist = function (items, _) -- items, tight + -- HACK. SILE doesn't have a good definition list package yet. + -- (N.B. This could be a welcome addition, with terms going to an index, etc.) + -- Here, very minimalist approach. + local buffer = {} + for _, item in ipairs(items) do + buffer[#buffer + 1] = createCommand("font", { weight = 600 }, item.term) + buffer[#buffer + 1] = createStructuredCommand("markdown:internal:definition", {}, item.definitions) + end + return createStructuredCommand("markdown:internal:paragraph", {}, buffer) + end + -- Final AST conversion logic. - -- The lunamark AST is made of "ropes". - -- "A rope is an array whose elements may be ropes, strings, numbers, - -- or functions." - -- The default implementation flattens that to a string, so we overrride it, - -- in order to extract the AST. - -- The methods that were overriden above actually started to introduce SILE - -- AST command structures in place of some ropes. - -- Therefore, we now walk the AST and merge it, flattened to some degree. + -- The lunamark AST is made of "ropes". + -- "A rope is an array whose elements may be ropes, strings, numbers, + -- or functions." + -- The default implementation flattens that to a string, so we overrride it, + -- in order to extract the AST. + -- The methods that were overriden above actually started to introduce SILE + -- AST command structures in place of some ropes. + -- Therefore, we now walk the AST and merge it, flattened to some degree. function writer.merge (rope) local function walk(node) @@ -367,7 +379,9 @@ local function init (class, _) }) local parse = reader.new(writer, { smart = true, + definition_lists = true, notes = true, + inline_notes = true, fenced_code_blocks = true, pandoc_extensions = true, startnum = true, @@ -396,11 +410,27 @@ local function registerCommands (_) -- \par was not necessary... We switched to "compact" layout, to decide -- how to handle our own paragraphing. SILE.call("par") - end, "Paragraphing in Markdown") + end, "Paragraphing in Markdown (internal)") SILE.registerCommand("markdown:internal:rawcontent", function (_, content) SILE.doTexlike(content[1]) - end) + end, "Raw SILE content in Markdown (internal)") + + SILE.registerCommand("markdown:internal:definition", function (_, content) + -- HACK. + -- I hesitated between a fallback and an internal... + -- Kind of a hack in the writer too, anyway. + -- Just do something quite minimalist here, and reconsider later? + SILE.typesetter:leaveHmode() + SILE.settings:temporarily(function () + local indent = SILE.measurement("2em"):absolute() + local lskip = SILE.settings:get("document.lskip") or SILE.nodefactory.glue() + SILE.settings:set("document.lskip", SILE.nodefactory.glue(lskip.width + indent)) + SILE.process(content) + SILE.typesetter:leaveHmode() + end) + SILE.call("smallskip") + end, "Definition block in markdown (internal") -- Fallback commands applied when some other command is not found -- The rationale is that we don't want this package to load some higher level @@ -454,10 +484,13 @@ local function registerCommands (_) end, "A fallback command for Markdown to insert a captioned table") SILE.registerCommand("markdown:footnote:fallback", function (_, _) + -- What to do if we have no \footnote support? + -- Fallback to something visible where the note call would have been, + -- and drop the content. SILE.call("color", { color = "red" }, function () SILE.call("textsuperscript", {}, { "#" }) end) - end, "FIXME") + end, "A fallback command for Markdown when foonotes do not seem to be available") -- Customizable hooks @@ -478,7 +511,7 @@ local function registerCommands (_) SILE.process(content) if options.scope == "block" then SILE.call("par") end end - end) + end, "Default hook for custom style support in Markdown") -- Temporary stuff (expectedly....)