From d88c12c479ddbadc494d4967e6c4777f1afeb537 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sun, 30 Jan 2022 21:14:20 +0000 Subject: markdown (30jan22) git-svn-id: svn://tug.org/texlive/trunk@61807 c570f23f-e606-0410-a88d-b1316a301751 --- .../source/generic/markdown/markdown.dtx | 244 +++++++++++++++++++-- 1 file changed, 229 insertions(+), 15 deletions(-) (limited to 'Master/texmf-dist/source') diff --git a/Master/texmf-dist/source/generic/markdown/markdown.dtx b/Master/texmf-dist/source/generic/markdown/markdown.dtx index b624b048799..4b6b148082d 100644 --- a/Master/texmf-dist/source/generic/markdown/markdown.dtx +++ b/Master/texmf-dist/source/generic/markdown/markdown.dtx @@ -81,9 +81,11 @@ citations, definitionLists, fencedCode, + footnotes, headerAttributes, - hybrid, html, + hybrid, + inlineFootnotes, jekyllData, stripPercentSigns, underscores = false, @@ -7719,6 +7721,162 @@ following text: % % \begin{markdown} +#### Markdown Document Renderers +The \mdef{markdownRendererDocumentBegin} and \mdef{markdownRendererDocumentEnd} +macros represent the beginning and the end of a *markdown* document. The macros +receive no arguments. + +A \TeX{} document may contain any number of markdown documents. Additionally, +markdown documents may appear not only in a sequence, but several markdown +documents may also be *nested*. Redefinitions of the macros should take this +into account. + +% \end{markdown} +% +% \iffalse + +##### \LaTeX{} Example {.unnumbered} + +\def\markdownRendererDocumentEnd{% + \endgroup + This is the end of a document.\par +} +\def\markdownRendererDocumentBegin{% + This is the beginning of a document.\par + \begingroup + \def\markdownRendererDocumentBegin{% + This is the beginning of a nested document.\par + \begingroup + \def\markdownRendererDocumentBegin{% + This is the beginning of a nested document.\par + \begingroup + }% + \def\markdownRendererDocumentEnd{% + \endgroup + This is the end of a nested document.\par + }% + }% +} + +Using a text editor, create a text document named `nested.md` with the +following content: +``` md +This is a *nested* markdown document. +``` + +Using a text editor, create a text document named `document.tex` with the +following content: +``` tex +\documentclass{article} +\usepackage[contentBlocks]{markdown} +\markdownSetup{ + renderers = { + contentBlock = {% + \markdownInput{#3}% + }, + documentBegin = {% + \par + \emph{(The beginning of a document)} + \par + \begingroup + \markdownSetup{snippet=first-nesting-level}% + }, + documentEnd = {% + \endgroup + \par + \emph{(The end of a document)} + \par + }, + }, +} +\markdownSetupSnippet{first-nesting-level}{ + renderers = { + documentBegin = { + \par + \emph{(The beginning of a nested document)} + \par + \begingroup + \markdownSetup{snippet=second-nesting-level-and-below} + }, + }, +} +\markdownSetupSnippet{second-nesting-level-and-below}{ + renderers = { + documentBegin = { + \par + \emph{(The beginning of a nested document)} + \par + \begingroup + }, + documentEnd = { + \endgroup + \par + \emph{(The end of a nested document)} + \par + }, + }, +} +\begin{document} +\begin{markdown} +Hello *world*! + +/nested.md + +_Foo_ bar! +\end{markdown} +\begin{markdown} + +Bar baz! + +\end{markdown} +\end{document} +``````` +Next, invoke LuaTeX from the terminal: +``` sh +lualatex document.tex +`````` +A PDF document named `document.pdf` should be produced and contain the +following text: + +> *(The beginning of a document)* +> +> Hello *world*! +> +> *(The beginning of a nested document)* +> +> This is a *nested* markdown document. +> +> *(The end of a nested document)* +> +> _Foo_ bar! +> +> *(The end of a document)* +> +> *(The beginning of a document)* +> +> Bar baz! +> +> *(The end of a document)* + +% +%<*tex> +% \fi +% +% \begin{macrocode} +\def\markdownRendererDocumentBegin{% + \markdownRendererDocumentBeginPrototype}% +\def\markdownRendererDocumentEnd{% + \markdownRendererDocumentEndPrototype}% +% \end{macrocode} +% \par +% +% \iffalse +% +%<*manual-tokens> +% \fi +% +% \begin{markdown} + #### Interblock Separator Renderer The \mdef{markdownRendererInterblockSeparator} macro represents a separator between two markdown block elements. The macro receives no arguments. @@ -11899,6 +12057,8 @@ following text: % % \end{markdown} % \begin{macrocode} +\def\markdownRendererDocumentBeginPrototype{}% +\def\markdownRendererDocumentEndPrototype{}% \def\markdownRendererInterblockSeparatorPrototype{}% \def\markdownRendererLineBreakPrototype{}% \def\markdownRendererEllipsisPrototype{}% @@ -12946,6 +13106,10 @@ The following ordered list will be preceded by roman numerals: % % \end{markdown} % \begin{macrocode} +\define@key{markdownRenderers}{documentBegin}{% + \renewcommand\markdownRendererDocumentBegin{#1}}% +\define@key{markdownRenderers}{documentEnd}{% + \renewcommand\markdownRendererDocumentEnd{#1}}% \define@key{markdownRenderers}{interblockSeparator}{% \renewcommand\markdownRendererInterblockSeparator{#1}}% \define@key{markdownRenderers}{lineBreak}{% @@ -13117,6 +13281,10 @@ The following ordered list will be preceded by roman numerals: % % \end{markdown} % \begin{macrocode} +\define@key{markdownRendererPrototypes}{documentBegin}{% + \renewcommand\markdownRendererDocumentBeginPrototype{#1}}% +\define@key{markdownRendererPrototypes}{documentEnd}{% + \renewcommand\markdownRendererDocumentEndPrototype{#1}}% \define@key{markdownRendererPrototypes}{interblockSeparator}{% \renewcommand\markdownRendererInterblockSeparatorPrototype{#1}}% \define@key{markdownRendererPrototypes}{lineBreak}{% @@ -15989,15 +16157,6 @@ function M.writer.new(options) % \par % \begin{markdown} % -% Define \luamdef{writer->eof} as the end of file marker in the output format. -% -% \end{markdown} -% \begin{macrocode} - self.eof = [[\relax]] -% \end{macrocode} -% \par -% \begin{markdown} -% % Define \luamdef{writer->linebreak} as the output format of a forced line break. % % \end{markdown} @@ -16498,6 +16657,19 @@ function M.writer.new(options) % \par % \begin{markdown} % +% Define \luamdef{writer->document} as a function that will transform a +% document `d` to the output format. +% +% \end{markdown} +% \begin{macrocode} + function self.document(d) + return {"\\markdownRendererDocumentBegin\n", d, + "\\markdownRendererDocumentEnd"} + end +% \end{macrocode} +% \par +% \begin{markdown} +% % Define \luamdef{writer->jekyllData} as a function that will transform an % input \acro{yaml} table `d` to the output format. The table is the value for % the key `p` in the parent table; if `p` is nil, then the table has no parent. @@ -18276,7 +18448,7 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline * (P("---") + P("...")) / function(text) local tinyyaml = require("markdown-tinyyaml") - data = tinyyaml.parse(text) + data = tinyyaml.parse(text,{timestamps=false}) return writer.jekyllData(data, function(s) return parse_blocks(s) end, nil) @@ -18717,7 +18889,8 @@ larsers.PipeTable = Ct(larsers.table_row * parsers.newline % \end{markdown} % \begin{macrocode} local function convert(input) - return util.rope_to_string(parse_blocks_toplevel(input)) .. writer.eof + local document = parse_blocks_toplevel(input) + return util.rope_to_string(writer.document(document)) end if options.eagerCache or options.finalizeCache then local name = util.cache(options.cacheDir, input, salt, convert, ".md" .. writer.suffix) @@ -20031,7 +20204,25 @@ end dlBeginTight = {\markdownRendererDlBegin}, dlEndTight = {\markdownRendererDlEnd}}}} \RequirePackage{amsmath} -\RequirePackage{amssymb} +% \end{macrocode} +% \par +% \begin{markdown} +% +% Unless the \pkg{unicode-math} package has been loaded, load the \pkg{amssymb} +% package with symbols to be used for tickboxes. +% +% \end{markdown} +% \begin{macrocode} +\@ifpackageloaded{unicode-math}{ + \markdownSetup{rendererPrototypes={ + untickedBox = {$\mdlgwhtsquare$}, + }} +}{ + \RequirePackage{amssymb} + \markdownSetup{rendererPrototypes={ + untickedBox = {$\square$}, + }} +} \RequirePackage{csvsimple} \RequirePackage{fancyvrb} \RequirePackage{graphicx} @@ -20045,7 +20236,31 @@ end backslash = {\textbackslash}, tilde = {\textasciitilde}, pipe = {\textbar}, - codeSpan = {\texttt{#1}}, +% \end{macrocode} +% \par +% \begin{markdown} +% +% We can capitalize on the fact that the expansion of renderers is performed by +% \TeX{} during the typesetting. Therefore, even if we don't know whether a +% span of text is part of math formula or not when we are parsing markdown,% +% ^[This property may actually be undecidable. Suppose a span of text is a part +% of a macro definition. Then, whether the span of text is part of a math +% formula or not depends on where the macro is later used, which may easily +% be *both* inside and outside a math formula.] we can reliably detect math +% mode inside the renderer. +% +% Here, we will redefine the code span renderer prototype to typeset upright +% text in math formulae and typewriter text outside math formulae. +% +% \end{markdown} +% \begin{macrocode} + codeSpan = {% + \ifmmode + \text{#1}% + \else + \texttt{#1}% + \fi + }, contentBlock = {% \ifthenelse{\equal{#1}{csv}}{% \begin{table}% @@ -20088,7 +20303,6 @@ end emphasis = {\emph{#1}}, tickedBox = {$\boxtimes$}, halfTickedBox = {$\boxdot$}, - untickedBox = {$\square$}, blockQuoteBegin = {\begin{quotation}}, blockQuoteEnd = {\end{quotation}}, inputVerbatim = {\VerbatimInput{#1}}, -- cgit v1.2.3