Markdown into Micron
RNGit has a utility for converting markdown into micron and it works really well. Better still, I don't have to maintain anything for it to work, I can just make use of the latest RNS library and I get it for free.
Here's my Python to call it:
T282828
T8b949e#!/usr/bin/env python
Tff7b72import T7ee787argparse
Tff7b72import T7ee787os
Tff7b72from T7ee787RNST7ee787.T7ee787UtilitiesT7ee787.T7ee787rngitT7ee787.T7ee787util Tff7b72import Te6edf3MarkdownToMicron
Tff7b72from T7ee787RNST7ee787.T7ee787UtilitiesT7ee787.T7ee787rngitT7ee787.T7ee787highlight Tff7b72import Te6edf3SyntaxHighlighter
Tff7b72def Td2a8ffparse_argsTb4b4b4(Tb4b4b4)Tb4b4b4:
Te6edf3parser Tff7b72= Te6edf3argparseTff7b72.Td2a8ffArgumentParserTb4b4b4(Te6edf3descriptionTff7b72=Ta5d6ff"Ta5d6ffConvert markdown to micron.Ta5d6ff"Tb4b4b4)
Te6edf3parserTff7b72.Td2a8ffadd_argumentTb4b4b4(Ta5d6ff"Ta5d6ffinput_fileTa5d6ff"Tb4b4b4, Tffa657typeTff7b72=Tffa657strTb4b4b4, Te6edf3helpTff7b72=Ta5d6ff"Ta5d6ffPath to the input markdown fileTa5d6ff"Tb4b4b4)
Te6edf3parserTff7b72.Td2a8ffadd_argumentTb4b4b4(Ta5d6ff"Ta5d6ffoutput_fileTa5d6ff"Tb4b4b4, Tffa657typeTff7b72=Tffa657strTb4b4b4, Te6edf3helpTff7b72=Ta5d6ff"Ta5d6ffPath to the output micron fileTa5d6ff"Tb4b4b4)
Te6edf3parserTff7b72.Td2a8ffadd_argumentTb4b4b4(
Ta5d6ff"Ta5d6ff--overwriteTa5d6ff"Tb4b4b4, Te6edf3actionTff7b72=Ta5d6ff"Ta5d6ffstore_trueTa5d6ff"Tb4b4b4, Te6edf3helpTff7b72=Ta5d6ff"Ta5d6ffOverwrite existing output fileTa5d6ff"
Tb4b4b4)
Tff7b72return Te6edf3parserTff7b72.Td2a8ffparse_argsTb4b4b4(Tb4b4b4)
Tff7b72if Tff7b72__name__ Tff7b72== Ta5d6ff"Ta5d6ff__main__Ta5d6ff"Tb4b4b4:
Te6edf3args Tff7b72= Te6edf3parse_argsTb4b4b4(Tb4b4b4)
Tff7b72if Tff7b72not Te6edf3osTff7b72.Td2a8ffpathTff7b72.Td2a8ffisfileTb4b4b4(Te6edf3argsTff7b72.Td2a8ffinput_fileTb4b4b4)Tb4b4b4:
Tff7b72raise Tf85149FileNotFoundErrorTb4b4b4(Ta5d6fffTa5d6ff"Ta5d6ffInput file Ta5d6ff'Tffd700{Te6edf3argsTff7b72.Td2a8ffinput_fileTffd700}Ta5d6ff'Ta5d6ff does not exist.Ta5d6ff"Tb4b4b4)
Tff7b72if Te6edf3osTff7b72.Td2a8ffpathTff7b72.Td2a8ffisfileTb4b4b4(Te6edf3argsTff7b72.Td2a8ffoutput_fileTb4b4b4) Tff7b72and Tff7b72not Te6edf3argsTff7b72.Td2a8ffoverwriteTb4b4b4:
Tff7b72raise Tf85149FileExistsErrorTb4b4b4(
Ta5d6fffTa5d6ff"Ta5d6ffOutput file Ta5d6ff'Tffd700{Te6edf3argsTff7b72.Td2a8ffoutput_fileTffd700}Ta5d6ff'Ta5d6ff already exists. Use --overwrite to overwrite it.Ta5d6ff"
Tb4b4b4)
Te6edf3converter Tff7b72= Te6edf3MarkdownToMicronTb4b4b4(Te6edf3max_widthTff7b72=T79c0ff80Tb4b4b4, Te6edf3syntax_highlighterTff7b72=Te6edf3SyntaxHighlighterTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3url_scopeTff7b72=Tff7b72NoneTb4b4b4)
Tff7b72with Tffa657openTb4b4b4(Te6edf3argsTff7b72.Td2a8ffinput_fileTb4b4b4) Tff7b72as Te6edf3input_fileTb4b4b4:
Tffa657printTb4b4b4(Te6edf3argsTff7b72.Td2a8ffoutput_fileTb4b4b4)
Tff7b72with Tffa657openTb4b4b4(Te6edf3argsTff7b72.Td2a8ffoutput_fileTb4b4b4, Ta5d6ff"Ta5d6ffwTa5d6ff"Tb4b4b4) Tff7b72as Te6edf3output_fileTb4b4b4:
Te6edf3output_fileTff7b72.Td2a8ffwriteTb4b4b4(Te6edf3converterTff7b72.Td2a8ffformat_blockTb4b4b4(Te6edf3input_fileTff7b72.Td2a8ffreadTb4b4b4(Tb4b4b4)Tb4b4b4)Tb4b4b4)
Edit: updated the script to include suggestions made by the creator of Reticulum and the parsers within. Now it actually does pygments in Reticulum via NomadNet and it looks amazing.
For dependencies: rns, ucwidth, pygments (last one is optional for syntax highlighting -- don't leave off ucwidth, despite that it's technically optional).
Remaining idiosyncrasies
YAML headers
It seems oblivious to YAML headers. They just get dumped in wholesale, but the T383838--- goes to the micron T383838- before and after. I forget, is that pre-formatted text?
Because I'm piping my markdown through pandoc as a preprocessing step, the YAML header has already been stripped before the content is processed with the markdown to mircon script. I apply a template to add the title from the YAML header to the page and I shift indent levels. So no big deal here for my stuff.
reference links
It doesn't support reference links, but pandoc can help here -- any invocation of pandoc with a markdown output type will strip reference links unless the options is explicitly passed to allow them.
Preformatted text
While it's valid to use four spaces to create a block of preformatted text, this isn't yet handled by the parser, so I'm still looking into ways that I could preformat with pandoc to remove these. For now I'm going to start removing preformatted blocks from my notes in favoring of just using fences.
Pandoc hangups
Pandoc will escape the T383838# if it's used in a link title. This can be fixed with a sed expression. If the sed expression is put in a Makefile, it gets a little tricker with the escaping, for example:
To accomplish this:
T282828
pandoc input -t markdown Tb4b4b4| sed -e Ta5d6ff's,\[\\\#,[#,g' > output
Would require something like the following:
T282828
Te6edf3UNESCAPE_HASHESTff7b72=sed -e Ta5d6ff's,\[\\\\\#,[#,g'
Td2a8fftargetTff7b72:
pandoc input -t markdown Tb4b4b4| Tff7b72$(UNESCAPE_HASHESTff7b72) > output
link extensions
Link targets are not touched by the conversion process, so any links that need to point at a micron page need to have the extension swapped. This is pretty much how all tools work, so I already had logic for swapping extensions that could be adapted.
Sed can help here.
In the Makefile example:
FMT_MU=sed 's,\(.*\)\.mu\>,\1.mu,g'
target:
pandoc input -t markdown | $(FMT_MU) > output
Tags: index, reticulum
Tags
Navigation
Backlinks