Skip to content

Commit

Permalink
Add inlineHtmlComment renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Witiko committed Jun 21, 2021
1 parent b0ff44a commit bf54b2f
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 87 deletions.
120 changes: 100 additions & 20 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -10454,8 +10454,6 @@ following content:
: Demonstration of pipe table syntax
\end{markdown}
\end{document}
\end{markdown}
\end{document}
```````
Next, invoke LuaTeX from the terminal:
``` sh
Expand Down Expand Up @@ -10494,6 +10492,69 @@ following text:
%
% \begin{markdown}

#### Inline HTML Comment Renderer
The \mdef{markdownRendererInlineHtmlComment} macro represents the contents of an
inline \acro{HTML} comment. This macro will only be produced, when the
\Opt{html} option is enabled. The macro receives a single argument that
corresponds to the contents of the \acro{HTML} comment.

% \end{markdown}
%
% \iffalse

##### \LaTeX{} Example {.unnumbered}

Using a text editor, create a text document named `document.tex` with the
following content:
``` tex
\documentclass{article}
\usepackage[html]{markdown}
\usepackage{marginnote}
\markdownSetup{
renderers = {
inlineHtmlComment = {\marginnote{#1}},
},
}
\begin{document}
\begin{markdown}
A useful use of HTML comments are side notes.
<!-- Side notes are displayed in the horizontal margins next to the relevant
passages, which makes them easier for the reader to find than footnotes. -->
\end{markdown}
\end{document}
```````
Next, invoke LuaTeX from the terminal:
``` sh
lualatex document.tex
lualatex document.tex
``````
A PDF document named `document.pdf` should be produced and contain the
following body text:

> A useful use of HTML comments are side notes.

The horizontal margins should contain the following text:

> Side notes are displayed in the horizontal margins next to the relevant
> passages, which makes them easier for the reader to find than footnotes.

%</manual-tokens>
%<*tex>
% \fi
%
% \begin{macrocode}
\def\markdownRendererInlineHtmlComment{%
\markdownRendererInlineHtmlCommentPrototype}%
% \end{macrocode}
% \par
%
% \iffalse
%</tex>
%<*manual-tokens>
% \fi
%
% \begin{markdown}

### Token Renderer Prototypes

% \label{sec:texrendererprototypes}
Expand Down Expand Up @@ -10684,6 +10745,7 @@ following text:
\def\markdownRendererCitePrototype#1{}%
\def\markdownRendererTextCitePrototype#1{}%
\def\markdownRendererTablePrototype#1#2#3{}%
\def\markdownRendererInlineHtmlCommentPrototype#1{}%
% \end{macrocode}
% \par
% \begin{markdown}
Expand Down Expand Up @@ -11595,6 +11657,8 @@ following text, where the middot (`·`) denotes a non-breaking space:
\renewcommand\markdownRendererTextCite[1]{#1}}%
\define@key{markdownRenderers}{table}{%
\renewcommand\markdownRendererTable[3]{#1}}%
\define@key{markdownRenderers}{inlineHtmlComment}{%
\renewcommand\markdownRendererInlineHtmlComment[1]{#1}}%
% \end{macrocode}
% \par
% \begin{markdown}
Expand Down Expand Up @@ -11738,6 +11802,8 @@ following text, where the middot (`·`) denotes a non-breaking space:
\renewcommand\markdownRendererTextCitePrototype[1]{#1}}%
\define@key{markdownRendererPrototypes}{table}{%
\renewcommand\markdownRendererTablePrototype[3]{#1}}%
\define@key{markdownRendererPrototypes}{inlineHtmlComment}{%
\renewcommand\markdownRendererInlineHtmlCommentPrototype[1]{#1}}%
% \end{macrocode}
% \par
% \begin{markdown}
Expand Down Expand Up @@ -14818,15 +14884,15 @@ function M.writer.new(options)
% \par
% \begin{markdown}
%
% Define \luamdef{writer->inline_html} and \luamdef{writer->display_html}
% as functions that will transform an inline or block \acro{html} element
% respectively to the output format, where `html` is the \acro{html}
% input.
% Define \luamdef{writer->inline_html_comment} as a function that will
% transform the contents of an inline \acro{HTML} comment, to the output
% format, where `contents` are the contents of the \acro{HTML} comment.
%
% \end{markdown}
% \begin{macrocode}
function self.inline_html(html) return "" end
function self.display_html(html) return "" end
function self.inline_html_comment(contents)
return {"\\markdownRendererInlineHtmlComment{",contents,"}"}
end
% \end{macrocode}
% \par
% \begin{markdown}
Expand Down Expand Up @@ -15717,9 +15783,13 @@ parsers.htmlattribute = parsers.spacing^1
* parsers.sp * parsers.equal * parsers.sp
* parsers.htmlattributevalue

parsers.htmlcomment = P("<!--") * (parsers.any - P("-->"))^0 * P("-->")
parsers.htmlcomment = P("<!--")
* parsers.optionalspace
* Cs((parsers.any - parsers.optionalspace * P("-->"))^0)
* parsers.optionalspace
* P("-->")

parsers.htmlinstruction = P("<?") * (parsers.any - P("?>" ))^0 * P("?>" )
parsers.htmlinstruction = P("<?") * (parsers.any - P("?>"))^0 * P("?>")

parsers.openelt_any = parsers.less * parsers.keyword * parsers.htmlattribute^0
* parsers.sp * parsers.more
Expand Down Expand Up @@ -15766,17 +15836,11 @@ end
parsers.in_matched_block_tags = parsers.less
* Cmt(#parsers.openelt_block, parse_matched_tags)

parsers.displayhtml = parsers.htmlcomment
parsers.displayhtml = parsers.htmlcomment / ""
+ parsers.emptyelt_block
+ parsers.openelt_exact("hr")
+ parsers.in_matched_block_tags
+ parsers.htmlinstruction

parsers.inlinehtml = parsers.emptyelt_any
+ parsers.htmlcomment
+ parsers.htmlinstruction
+ parsers.openelt_any
+ parsers.closeelt_any
% \end{macrocode}
% \par
% \begin{markdown}
Expand Down Expand Up @@ -16105,6 +16169,12 @@ function M.reader.new(writer, options)
return larsers.inlines_no_inline_note
end, false)

local parse_inlines_no_html
= create_parser("parse_inlines_no_html",
function()
return larsers.inlines_no_html
end, false)

local parse_inlines_nbsp
= create_parser("parse_inlines_nbsp",
function()
Expand Down Expand Up @@ -16486,7 +16556,12 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline

larsers.EscapedChar = parsers.backslash * C(parsers.escapable) / writer.string

larsers.InlineHtml = C(parsers.inlinehtml) / writer.inline_html
larsers.InlineHtml = parsers.emptyelt_any
+ (parsers.htmlcomment / parse_inlines_no_html)
/ writer.inline_html_comment
+ parsers.htmlinstruction
+ parsers.openelt_any
+ parsers.closeelt_any

larsers.HtmlEntity = parsers.hexentity / entities.hex_entity / writer.string
+ parsers.decentity / entities.dec_entity / writer.string
Expand All @@ -16504,8 +16579,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline
* parsers.contentblock_tail
/ writer.contentblock

larsers.DisplayHtml = C(parsers.displayhtml)
/ expandtabs / writer.display_html
larsers.DisplayHtml = parsers.displayhtml

larsers.Verbatim = Cs( (parsers.blanklines
* ((parsers.indentedline - parsers.blankline))^1)^1
Expand Down Expand Up @@ -16889,6 +16963,12 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline
inlines_no_inline_note_t.InlineNote = parsers.fail
larsers.inlines_no_inline_note = Ct(inlines_no_inline_note_t)

local inlines_no_html_t = util.table_copy(inlines_t)
inlines_no_html_t.DisplayHtml = parsers.fail
inlines_no_html_t.InlineHtml = parsers.fail
inlines_no_html_t.HtmlEntity = parsers.fail
larsers.inlines_no_html = Ct(inlines_no_html_t)

local inlines_nbsp_t = util.table_copy(inlines_t)
inlines_nbsp_t.Endline = larsers.NonbreakingEndline
inlines_nbsp_t.Space = larsers.NonbreakingSpace
Expand Down
66 changes: 34 additions & 32 deletions tests/support/markdownthemewitiko_markdown_test.sty
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,39 @@
\TYPE{pipe}%
\GOBBLE},
codeSpan = {%
\TYPE{codeSpan: #1}},
\TYPE{codeSpan: #1}},
link = {%
\TYPE{BEGIN link}%
\TYPE{- label: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{- label: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{END link}},
image = {%
\TYPE{BEGIN image}%
\TYPE{- label: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{- label: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{END image}},
contentBlock = {%
\TYPE{BEGIN contentBlock}%
\TYPE{- suffix: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{- suffix: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{END contentBlock}%
},
contentBlockOnlineImage = {%
\TYPE{BEGIN contentBlockOnlineImage}%
\TYPE{- suffix: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{- suffix: #1}%
\TYPE{- URI: #2}%
\TYPE{- title: #4}%
\TYPE{END contentBlockOnlineImage}%
},
contentBlockCode = {%
\TYPE{BEGIN contentBlockCode}%
\TYPE{- suffix: #1}%
\TYPE{- language: #2}%
\TYPE{- URI: #3}%
\TYPE{- title: #5}%
\TYPE{- suffix: #1}%
\TYPE{- language: #2}%
\TYPE{- URI: #3}%
\TYPE{- title: #5}%
\TYPE{END contentBlockCode}%
},
ulBegin = {%
Expand All @@ -102,7 +102,7 @@
olItemEnd = {%
\TYPE{olItemEnd}},
olItemWithNumber = {%
\TYPE{olItemWithNumber: #1}},
\TYPE{olItemWithNumber: #1}},
olEnd = {%
\TYPE{olEnd}},
olEndTight = {%
Expand All @@ -112,7 +112,7 @@
dlBeginTight = {%
\TYPE{dlBeginTight}},
dlItem = {%
\TYPE{dlItem: #1}},
\TYPE{dlItem: #1}},
dlItemEnd = {%
\TYPE{dlItemEnd}},
dlDefinitionBegin = {%
Expand All @@ -124,41 +124,43 @@
dlEndTight = {%
\TYPE{dlEndTight}},
emphasis = {%
\TYPE{emphasis: #1}},
\TYPE{emphasis: #1}},
strongEmphasis = {%
\TYPE{strongEmphasis: #1}},
\TYPE{strongEmphasis: #1}},
blockQuoteBegin = {%
\TYPE{blockQuoteBegin}},
blockQuoteEnd = {%
\TYPE{blockQuoteEnd}},
inputVerbatim = {%
\TYPE{inputVerbatim: #1}},
\TYPE{inputVerbatim: #1}},
inputFencedCode = {%
\TYPE{BEGIN fencedCode}%
\TYPE{- src: #1}%
\TYPE{- infostring: #2}%
\TYPE{- src: #1}%
\TYPE{- infostring: #2}%
\TYPE{END fencedCode}},
headingOne = {%
\TYPE{headingOne: #1}},
\TYPE{headingOne: #1}},
headingTwo = {%
\TYPE{headingTwo: #1}},
\TYPE{headingTwo: #1}},
headingThree = {%
\TYPE{headingThree: #1}},
\TYPE{headingThree: #1}},
headingFour = {%
\TYPE{headingFour: #1}},
\TYPE{headingFour: #1}},
headingFive = {%
\TYPE{headingFive: #1}},
\TYPE{headingFive: #1}},
headingSix = {%
\TYPE{headingSix: #1}},
\TYPE{headingSix: #1}},
horizontalRule = {%
\TYPE{horizontalRule}},
footnote = {%
\TYPE{footnote: #1}},
\TYPE{footnote: #1}},
cite = {%
\CITATIONS{#1}},
textCite = {%
\TEXTCITATIONS{#1}},
table = {%
\TABLE{#1}{#2}{#3}}
\TABLE{#1}{#2}{#3}},
inlineHtmlComment = {%
\TYPE{inlineHtmlComment: #1}},
}%
}%
Loading

0 comments on commit bf54b2f

Please sign in to comment.