Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move \markdownSetup from LaTeX layer to plain TeX + expl3 layer #275

Closed
6 tasks
Witiko opened this issue Mar 8, 2023 · 0 comments · Fixed by #349
Closed
6 tasks

Move \markdownSetup from LaTeX layer to plain TeX + expl3 layer #275

Witiko opened this issue Mar 8, 2023 · 0 comments · Fixed by #349
Labels
context Related to the ConTeXt interface and implementation expl3 Related to the high-level programming language expl3 latex Related to the LaTeX interface and implementation plaintex Related to the plain TeX interface and implementation technical debt Related to code smells and refactoring
Milestone

Comments

@Witiko
Copy link
Owner

Witiko commented Mar 8, 2023

The \markdownSetup and \setupmarkdown commands of LaTeX and ConTeXt are close duplicates and mostly just wrappers for functionality provided by the plain TeX + expl3 layers (see a recent layer diagram of the Markdown package):

markdown/markdown.dtx

Lines 17860 to 17869 in 36290d5

\ExplSyntaxOn
\cs_new:Nn
\@@_setup:n
{
\keys_set:nn
{ markdown/latex-options }
{ #1 }
}
\let\markdownSetup=\@@_setup:n
\ExplSyntaxOff

markdown/markdown.dtx

Lines 19031 to 19044 in 36290d5

\ExplSyntaxOn
\cs_new:Nn
\@@_setup:n
{
\keys_set:nn
{ markdown/context-options }
{ #1 }
}
\long\def\setupmarkdown[#1]
{
\@@_setup:n
{ #1 }
}
\ExplSyntaxOff

We should move most of the heavy lifting to the plain TeX + expl3 layer and make the \markdownSetup command available from plain TeX. This will deduplicate the code and make it possible to use the \markdownSetup command uniformly across all formats with all its benefits (#232).

Tasks

  • Replace markdown/latex-options and markdown/context-options keyvals with a single markdown/options keyval defined in the plain TeX layer.
  • In ConTeXt, redefine \@@_set_option_value:nn to include the special handling of values yes and no:

    markdown/markdown.dtx

    Lines 19114 to 19124 in 36290d5

    \tl_set:Nx
    \l_tmpa_tl
    {
    \str_case:nnF
    { ##1 }
    {
    { yes } { true }
    { no } { false }
    }
    { ##1 }
    }
  • In ConTeXt, add new entries to the markdown/options keyval using just the \@@_caseless:N function:

    markdown/markdown.dtx

    Lines 19093 to 19102 in 36290d5

    \cs_new:Nn \@@_caseless:N
    {
    \regex_replace_all:nnN
    { ([a-z])([A-Z]) }
    { \1 \c { str_lowercase:n } \cB\{ \2 \cE\} }
    #1
    \tl_set:Nx
    #1
    { #1 }
    }
  • In LaTeX, define theme and snippet keys for the markdown/options keyval:

    markdown/markdown.dtx

    Lines 18120 to 18124 in 36290d5

    \keys_define:nn
    { markdown/latex-options }
    {
    theme .code:n = { \@@_set_latex_theme:n { #1 } },
    }

    markdown/markdown.dtx

    Lines 18451 to 18462 in 36290d5

    snippet .code:n = {
    \markdownIfSnippetExists{#1}
    {
    \expandafter\markdownSetup\expandafter{
    \the\csname markdownLaTeXSetupSnippet
    \markdownLaTeXThemeName#1\endcsname}
    }{
    \markdownError
    {Can't~invoke~setup~snippet~#1}
    {The~setup~snippet~is~undefined}
    }
    }
  • Move code for defining renderers and renderer prototypes from LaTeX layer to the plain TeX + expl3 layer:

    markdown/markdown.dtx

    Lines 18713 to 18845 in 36290d5

    %#### Plain \TeX{} Markdown Token Renderers {#latexrenderers}
    %
    % The \LaTeX{} interface recognizes an option with the `renderers` key,
    % whose value must be a list of options that map directly to the markdown token
    % renderer macros exposed by the plain \TeX{} interface (see Section
    % <#sec:texrenderersuser>).
    %
    % \end{markdown}
    % \begin{macrocode}
    \ExplSyntaxOn
    \cs_new:Nn \@@_latex_define_renderers:
    {
    \seq_map_function:NN
    \g_@@_renderers_seq
    \@@_latex_define_renderer:n
    }
    \cs_new:Nn \@@_latex_define_renderer:n
    {
    \@@_renderer_tl_to_csname:nN
    { #1 }
    \l_tmpa_tl
    \prop_get:NnN
    \g_@@_renderer_arities_prop
    { #1 }
    \l_tmpb_tl
    \@@_latex_define_renderer:ncV
    { #1 }
    { \l_tmpa_tl }
    \l_tmpb_tl
    }
    \cs_new:Nn \@@_renderer_tl_to_csname:nN
    {
    \tl_set:Nn
    \l_tmpa_tl
    { \str_uppercase:n { #1 } }
    \tl_set:Nx
    #2
    {
    markdownRenderer
    \tl_head:f { \l_tmpa_tl }
    \tl_tail:n { #1 }
    }
    }
    \cs_new:Nn \@@_latex_define_renderer:nNn
    {
    \@@_with_various_cases:nn
    { #1 }
    {
    \keys_define:nn
    { markdown/latex-options/renderers }
    {
    ##1 .code:n = {
    \cs_generate_from_arg_count:NNnn
    #2
    \cs_set:Npn
    { #3 }
    { ####1 }
    },
    }
    }
    }
    \cs_generate_variant:Nn
    \@@_latex_define_renderer:nNn
    { ncV }
    \ExplSyntaxOff
    % \end{macrocode}
    % \par
    % \begin{markdown}
    %
    % The following example \LaTeX{} code showcases a possible configuration of the
    % \mref{markdownRendererLink} and \mref{markdownRendererEmphasis} markdown token
    % renderers.
    % ``` tex
    % \markdownSetup{
    % renderers = {
    % link = {#4}, \% Render links as the link title.
    % emphasis = {\emph{#1}}, \% Render emphasized text via `\emph`.
    % }
    % }
    % ```````
    %
    %#### Plain \TeX{} Markdown Token Renderer Prototypes {#latexrendererprototypes}
    %
    % The \LaTeX{} interface recognizes an option with the `rendererPrototypes`
    % key, whose value must be a list of options that map directly to the markdown
    % token renderer prototype macros exposed by the plain \TeX{} interface (see
    % Section <#sec:texrendererprototypes>).
    %
    % \end{markdown}
    % \begin{macrocode}
    \ExplSyntaxOn
    \cs_new:Nn \@@_latex_define_renderer_prototypes:
    {
    \seq_map_function:NN
    \g_@@_renderers_seq
    \@@_latex_define_renderer_prototype:n
    }
    \cs_new:Nn \@@_latex_define_renderer_prototype:n
    {
    \@@_renderer_prototype_tl_to_csname:nN
    { #1 }
    \l_tmpa_tl
    \prop_get:NnN
    \g_@@_renderer_arities_prop
    { #1 }
    \l_tmpb_tl
    \@@_latex_define_renderer_prototype:ncV
    { #1 }
    { \l_tmpa_tl }
    \l_tmpb_tl
    }
    \cs_new:Nn \@@_latex_define_renderer_prototype:nNn
    {
    \@@_with_various_cases:nn
    { #1 }
    {
    \keys_define:nn
    { markdown/latex-options/renderer-prototypes }
    {
    ##1 .code:n = {
    \cs_generate_from_arg_count:NNnn
    #2
    \cs_set:Npn
    { #3 }
    { ####1 }
    },
    }
    }
    }
    \cs_generate_variant:Nn
    \@@_latex_define_renderer_prototype:nNn
    { ncV }
    \ExplSyntaxOff
  • Rewrite unit tests to use \markdownSetup exclusively.
@Witiko Witiko added plaintex Related to the plain TeX interface and implementation latex Related to the LaTeX interface and implementation context Related to the ConTeXt interface and implementation technical debt Related to code smells and refactoring expl3 Related to the high-level programming language expl3 labels Mar 8, 2023
@Witiko Witiko added this to the 3.1.0 milestone Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
context Related to the ConTeXt interface and implementation expl3 Related to the high-level programming language expl3 latex Related to the LaTeX interface and implementation plaintex Related to the plain TeX interface and implementation technical debt Related to code smells and refactoring
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant