Skip to content

Commit

Permalink
feat(packages): Support inline note, better definition lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Jul 23, 2022
1 parent 03d6780 commit 16a4f78
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions packages/markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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....)

Expand Down

0 comments on commit 16a4f78

Please sign in to comment.