Mini Markdown
January 21, 2020
The first step is to create an input file to be used for testing:
This is a text paragraph. # Heading level 1 This is another text paragraph. - List item 1 - List item 2 - List item 3 This is still another text paragraph. ### Heading level 3 This is the last text paragraph.
I’ll write the program in Awk, which is rapidly becoming my second-favorite language, because it has a “paragraph mode” that is very useful. From the Posix specification, in the definition of RS
:
If RS is null, then records are separated by sequences consisting of a <newline> plus one or more blank lines, leading or trailing blank lines shall not result in empty records at the beginning or end of the input, and a <newline> shall always be a field separator, no matter what the value of FS is.
It pays to know the dark corners of your language. Here’s the code:
BEGIN { FS = OFS = "\n"; RS = "" } # paragraph mode $1 ~ /^[#]{1,6} / { if (inlist) { inlist = 0; print "
” } len = index($1, ” “) – 1 print “<h” len “>” substr($1,len+2) “</h” len “>” next } $1 ~ /^[-] / { if (! inlist) { inlist = 1; print ”
-
- ” } print ”
- ” substr($1,3) “
” next } { if (inlist) { inlist = 0; print ”
” } print ”
” $0 ”
” }
Variable inlist
keeps track of whether or not the input is currently in a list, and writes list headers and trailers as needed. Here’s the output:
This is a text paragraph.
Heading level 1
This is another text paragraph.
-
- List item 1
- List item 2
- List item 3
This is still another text paragraph.
Heading level 3
This is the last text paragraph.
You can run the program at https://ideone.com/lat9aL.
Here’s a solution in Python.
@programmingpraxis, your solution seemingly does not add a closing
for list items occurring as the last elements of the input text.
Example Usage:
Here’s my same comment included above, this time with HTML escaping to try preventing dropped text.
@programmingpraxis, your solution seemingly does not add a closing
</ul>
for list items occurring as the last elements of the input text.