Raku Land

Markdown::Grammar

zef:antononcube

Raku Markdown::Grammar

In brief

Markdown parser suitable for making converters of Markdown files into files of different kind of formats:

See the video "Markdown to Mathematica converter (CLI and StackExchange examples)", [AAv1], for a (quick, 7.5 min) demo. The video [AAv2] shows the conversion of a Jupyter notebook into a Mathematica notebook via Markdown representation.

Motivation

Mathematica notebooks

I am most interested in generating Mathematica notebooks from Markdown.

I have written a fair amount of Raku-related Markdown documents. Many of those Markdown documents were generated from Mathematica notebooks using M2MD, [JPp1]. But of course, most of the time, further changes and embellishments were made over those Markdown documents. Hence it would be very nice to be able to go back to Mathematica.

Remark: Raku can be used in Mathematica via the so called RakuMode -- see [AA1].

Remark: Markdown documents with Raku code cells can be evaluated with Command Line Interface (CLI) scripts of the package "Text::CodeProcessing", [AAp1]. For more details see the article "Connecting Mathematica and Raku", [AA1].

Here is a flowchart that gives better idea of the workflow outlined above:

graph TD
    Rnb[Make Raku notebook in Mathematica]-->LT
    LT[Literate programming]-->GE
    GE{Good enough?}-->|yes|CN
    CN[Convert notebook to Markdown]-->P
    P["Publish (GitHUb/WordPress)"]-->SP
    SP-->|yes|R
    R[Review and modify]-->P
    GE-->|no|LT
    SP{Needs refinement?}-->|no|SD
    SD{Significantly different?}-->|yes|CM
    SD-->|no|PC
    CM[Convert Markdown document to notebook]-->PC
    PC["Publish to notebook Community.wolfram.com"]   

Pod6

The notebook formats have syntax that makes it hard to evaluate the conversions visually. That is why I use Pod6 -- during development it is much easier to evaluate the Pod6 interpretations produced by the package. (I.e. no need to use another tool to open and evaluate the obtained conversion artifact.)


Installation

From Zef ecosystem:

zef install Markdown::Grammar

From GitHub:

zef install https://github.com/antononcube/Raku-Markdown-Grammar.git

Round trip translation

Consider the following round trip translation experiment:

  1. Make a Mathematica notebook

  2. Convert WL notebook into Markdown file with the Mathematica package M2MD

  3. Convert the obtained Markdown file into Mathematica notebook using the Raku package "Markdown::Grammar"

  4. Compare the notebooks

Here is the corresponding flowchart:

graph TD
    WL[Make a Mathematica notebook] --> E
    E["Examine notebook(s)"] --> M2MD
    M2MD["Convert to Markdown with M2MD"] --> MG
    MG["Convert to Mathematica with Markdown::Grammar"] --> |Compare|E

Here is a table of converters from- or to Markdown:

From \ ToHTMLJupyterMarkdownMathematicaPod6
HTMLpandocpandoc
JupyterJupyterJupyter, jupytext
Markdownpandoc, Markit, Text::MarkdownjupytextMarkdown2WL, Markdown::GrammarMarkdown::Grammar
MathematicaM2MD
Pod6Pod::To::Markdown

Remark: Pandoc attempts to be an universal converter, applicable for all couples of formats.

Remark: Except "this package" there are no other converters from Markdown to Pod6.

Remark: In general I like the idea of a Markdown-to-Mathematica converter written in Mathematica. The package "Markdown2WL" attempts that, but unfortunately it is fairly incomplete.


Command line interface

The package provides a Command Line Interface (CLI) script, from-markdown. Here is its usage message:

> from-markdown --help
# Usage:
#  from-markdown [-t|--to=<Str>] [-l|--default-language=<Str>] [--raku-code-cell-name=<Str>] [-d|--docked-cells] [-o|--output=<Str>] <text> -- Converts Markdown files into Mathematica notebooks or Pod6 files.
#  
#    <text>                         Input file or Markdown text.
#    -t|--to=<Str>                  Format to convert to. (One of 'mathematica' or 'pod6'.) [default: 'mathematica']
#    -l|--default-language=<Str>    Default language. [default: 'Mathematica']
#    --raku-code-cell-name=<Str>    Raku code cell name. [default: 'Whatever']
#    -d|--docked-cells              Should formula conversion button be added as a docked cell or not? (Mathematica only.) [default: False]
#    -o|--output=<Str>              Output file; if an empty string then the result is printed to stdout. [default: '']
(

The CLI script from-markdown takes both file names and (Markdown) text. Here is an usage example for the latter:

> from-markdown -to=pod6 'Here is data wrangling code:

    obj = dfTitanic;
    obj = GroupBy[ obj, #["passengerSex"]& ];
    Echo[Map[ Length, obj], "counts:"]

## References'

# =begin
# =para
# Here is data wrangling code:
# =begin code
# obj = dfTitanic;
# obj = GroupBy[ obj, #["passengerSex"]& ];
# Echo[Map[ Length, obj], "counts:"]
# =end code
# =begin head2
# References
# =end head2
# =end pod

Remark: Using the CLI script with the option setting --raku-cell-code-name=RakuInputExecute produces Raku cells that require the Mathematica package "RakuMode.m", [AAp2], to be loaded and the command RakuMode[] to be executed in the result notebook.


Usage example

Consider the following Markdown text:

my $mtext = q:to/END/;
Here is data wrangling code:

    obj = dfTitanic;
    obj = GroupBy[ obj, #["passengerSex"]& ];
    Echo[Map[ Length, obj], "counts:"]

## References

### Articles

[AA1] Anton Antonov,
["Introduction to data wrangling with Raku"](https://rakuforprediction.wordpress.com/2021/12/31/introduction-to-data-wrangling-with-raku/),
(2021),
[RakuForPrediction at WordPress](https://rakuforprediction.wordpress.com).
END

say $mtext.chars;
# 410

Here is the corresponding Mathematica notebook:

use Markdown::Grammar;

from-markdown($mtext, to => 'mathematica')
# Notebook[{Cell[TextData[{"Here", " ", "is", " ", "data", " ", "wrangling", " ", "code:"}], "Text"], Cell[ BoxData["obj = dfTitanic;
# obj = GroupBy[ obj, #[\"passengerSex\"]& ];
# Echo[Map[ Length, obj], \"counts:\"]
# "], "Input"], Cell[TextData[{"References"}], "Section"], Cell[TextData[{"Articles"}], "Subsection"], Cell[TextData[{"[AA1]", " ", "Anton", " ", "Antonov,", " ", ButtonBox["\"Introduction to data wrangling with Raku\"", BaseStyle -> "Hyperlink", ButtonData -> { URL["https://rakuforprediction.wordpress.com/2021/12/31/introduction-to-data-wrangling-with-raku/"], None}], " ", ",", " ", "(2021),", " ", ButtonBox["RakuForPrediction at WordPress", BaseStyle -> "Hyperlink", ButtonData -> { URL["https://rakuforprediction.wordpress.com"], None}], " ", "."}], "Text"]}]

Here is the corresponding Pod6 text:

from-markdown($mtext, to => 'pod6')
# =begin
# =para
# Here is data wrangling code:
# =begin code
# obj = dfTitanic;
# obj = GroupBy[ obj, #["passengerSex"]& ];
# Echo[Map[ Length, obj], "counts:"]
# =end code
# =begin head2
# References
# =end head2
# 
# =begin head3
# Articles
# =end head3
# 
# =para
# [AA1] Anton Antonov,
# L<"Introduction to data wrangling with Raku"|https://rakuforprediction.wordpress.com/2021/12/31/introduction-to-data-wrangling-with-raku/> ,
# (2021),
# L<RakuForPrediction at WordPress|https://rakuforprediction.wordpress.com> .
# =end pod

TODOs

The most important items are placed first.


Acknowledgments

Many thanks to Jakub (Kuba) Podkalicki for programming the package "M2MD", and helping me to understand a fair amount of Mathematica's low-Level notebook programming.


References

Articles

[AA1] Anton Antonov, "Connecting Mathematica and Raku", (2021), RakuForPrediction at WordPress.

[FZ1] Faizon Zaman, "Parsing markdown files", (2021), Community.wolfram.com.

Guides

[JG1] John Gruber, Markdown: Syntax.

[MC1] Matt Cone, Markdown Guide.

[RC1] Raku Community, Raku Pod6.

Packages

[AAp1] Anton Antonov Text::CodeProcessing Raku package, (2021-2022), GitHub/antononcube.

[AAp2] Anton Antonov RakuMode Mathematica package, (2021), ConversationalAgents at GitHub/antononcube.

[FZp1] Faizon Zaman, Markdown2WL Mathematica package, (2021), GitHub/dishmint.

[JPp1] Jakub Podkalicki, M2MD, (2018-2022), GitHub/kubaPod.

Videos

[AAv1] Anton Antonov, "Markdown to Mathematica converter (CLI and StackExchange examples)", (2022), Anton A. Antonov's channel at YouTube.

[AAv2] Anton Antonov, "Markdown to Mathematica converter (Jupyter notebook example)", (2022), Anton A. Antonov's channel at YouTube.