Created
June 11, 2022 13:43
-
-
Save nathanlesage/ccd99ec89c0152d1930e7b79903cc43f to your computer and use it in GitHub Desktop.
A Pandoc filter for exporting HTML comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Default: Don't export comments | |
local comments = nil | |
function Meta (meta) | |
-- If there is a "comments" field in the YAML frontmatter, extract this | |
if meta.comments then | |
comments = pandoc.utils.stringify(meta.comments) | |
end | |
return meta | |
end | |
function RawBlock (raw) | |
-- Only export if the input format is HTML (== HTML comment) | |
-- and if we have a comments value | |
if not comments then return end | |
if raw.format ~= 'html' then return end | |
-- Let's see if we actually have a comment | |
local comment = raw.text:match '%<%!%-%-%s*(.*)%s*%-%-%>' | |
if not comment then return end | |
-- Export the comment always with the correct class, and as an element depending | |
-- on the comments value. If the comments value is not supported, this is the same | |
-- as not exporting the comments. | |
if comments == "paragraph" then return pandoc.Div(comment, {class='comment'}) | |
elseif comments == "bold" then return pandoc.Strong(comment, {class='comment'}) | |
elseif comments == "italic" then return pandoc.Emph(comment, {class='comment'}) | |
elseif comments == "footnote" then return pandoc.Note(comment, {class='comment'}) | |
elseif comments == "blockquote" then return pandoc.Quoted(comment, {class='comment'}) | |
elseif comments == "small-caps" then return pandoc.SmallCaps(comment, {class='comment'}) | |
elseif comments == "strikeout" then return pandoc.Strikeout(comment, {class='comment'}) | |
elseif comments == "underline" then return pandoc.Underline(comment, {class='comment'}) | |
end | |
end | |
-- This line is required so that the meta values are read in first and THEN the | |
-- RawBlocks are traversed | |
return { | |
{ Meta = Meta }, | |
{ RawBlock = RawBlock } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment