Skip to content

Commit

Permalink
DRAFT: HTML support
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlcctrlv committed Nov 12, 2022
1 parent 2f69ba1 commit 1e1a582
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
11 changes: 10 additions & 1 deletion inputters/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,18 @@ local function SileAstWriter (options)
writer.blockquote = simpleCommandWrapper("markdown:internal:blockquote")
writer.verbatim = simpleCommandWrapper("verbatim")
writer.listitem = simpleCommandWrapper("item")
writer.linebreak = simpleCommandWrapper("cr")
writer.singlequoted = simpleCommandWrapper("singlequoted")
writer.doublequoted = simpleCommandWrapper("doublequoted")

writer.inline_html = function(args)
local htmlroot = htmlparser.parse(args)
if #htmlroot.nodes > 0 then
local rootname = htmlroot.nodes[1].name
SU.debug("markdown", "Got raw HTML w/root tag: " .. rootname)
return utils.createCommand("markdown:html:" .. rootname)
end
end

-- Special case for hrule (simple too, but arguments from lunamark has to be ignored)
writer.hrule = function () return utils.createCommand("fullrule") end

Expand Down Expand Up @@ -305,6 +313,7 @@ end
function inputter.parse (_, doc)
local lunamark = require("lunamark")
local reader = lunamark.reader.markdown
htmlparser = require("htmlparser")
local writer = SileAstWriter({
layout = "minimize" -- The default layout is to output \n\n as inter-block separator
-- Let's cancel it completely, and insert our own \par where needed.
Expand Down
3 changes: 2 additions & 1 deletion markdown.sile-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dependencies = {
"lua >= 5.1",
"ptable.sile",
"textsubsuper.sile",
"smartquotes.sile"
"smartquotes.sile",
"htmlparser",
}

build = {
Expand Down
7 changes: 5 additions & 2 deletions packages/markdown/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
--
local utils = require("packages.markdown.utils")
local base = require("packages.base")
local html = require("packages.markdown.html")

local package = pl.class(base)
package._name = "markdown.commands"
Expand Down Expand Up @@ -364,7 +365,10 @@ function package:registerCommands ()
SILE.call("smallskip")
end, "A fallback command for Markdown to insert a captioned table")

-- C. Customizable hooks
-- C. HTML commands
html.registerHTMLcommands(self)

-- D. Customizable hooks

self:registerCommand("markdown:custom-style:hook", function (options, content)
-- Default implementation for the custom-style hook:
Expand All @@ -386,7 +390,6 @@ function package:registerCommands ()
end
end
end, "Default hook for custom style support in Markdown")

end

package.documentation = [[\begin{document}
Expand Down
44 changes: 44 additions & 0 deletions packages/markdown/html.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local base = require("packages.base")
local utils = require("packages.markdown.utils")

local package = pl.class(base)
package._name = "markdown.html"

local passthroughs = {
"sup",
"sub",
"em",
"strong",
{ ["div"] = "hbox" },
{ ["b"] = "strong" },
{ ["i"] = "em" },
}

package.registerHTMLcommands = function(self)
self:registerCommand("markdown:html:br", function (_, _)
SU.debug("markdown", "warning: manual linebreak (\\cr) inserted")
SILE.call("cr")
end, "HTML <br/> in Markdown")

for _, p in pairs(passthroughs) do
local html_name, sile_name
if type(p) == "table" then
html_name, sile_name = next(p), p[next(p)]
else
html_name, sile_name = p, p
end

self:registerCommand("markdown:html:" .. html_name, function(options, content)
SU.debug("markdown", "info: " .. html_name .. " replaced by \\" .. sile_name)
return utils.createCommand(sile_name, options, content)
end)
end
end

package.documentation = [[\begin{document}
A helper package for Markdown processing, providing HTML support.
It is not intended to be used alone.
\end{document}]]

return package
1 change: 1 addition & 0 deletions packages/markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package._name = "markdown"
function package:_init (_)
base._init(self)
self.class:loadPackage("markdown.commands")
self.class:loadPackage("markdown.html")

-- Extend inputters if needed.
-- Chicken and eggs... This just forces the inputter to be loaded!
Expand Down

0 comments on commit 1e1a582

Please sign in to comment.