Meltdown: Coding
The previous posts on Meltdown, hopefully demonstrate that it is easy to generate an email message that only requires basic formatting, that is HTML formatted . Surely far simpler that writing the message directly using HTML tags and somewhat simpler for simple formatting than Markdown. That was the first “acid test”. But what about simplicity with coding of a Meltdown to HTML parser for a 201 level student ? The conclusion is that a Meltdown parser for most functionality is easy to code but some functionality is a little more complex when trying to “dot the Is and cross the Ts”.
GitHub Code Repository
This implements the Meltdown to HTML parser as well as some sample code:
djaus2/Meltdown
Meltdown V2.1.0 is now available as Nuget package and can be used with any .NET 5.0 apps.
https://www.nuget.org/packages/Meltdown/
About Meltdown
Meltdown is a simple Markup language envisaged as being of use with web apps that send HTML formatted messages. It is line based in that it passes a string made up of lines, all parsing is on one line at a time and no state is passed between lines. Except for headings, lists and tables, each line is interpreted as a paragraph. Within a line, sections can be bracketed as bold, italics, underline and with a font color. Meltdown parallels Markup, but its syntax is somewhat simpler. Markup has more features but the features of Meltdown are sufficient to create well structured and formatted emails.
This Version 2.0
With strict adherence to precedence, pairs of brackets can have multiple meanings depending upon context. This version limits all markup to only use pairs, no triplets, except in one case:
- ColorPattern is based upon 2 brackets, not 3 …
"*((*|*))*"
- Link Patterns use greater than and less than symbols …
"*<<*|*>>*"
and"*<<*>>*"
- The bold-italic-underline format is the only triplet used:
[({ ... })]
Coding
The Meltdown library is implemented as a C# .NET class, although other platforms and languages, such as Java, could be used. To generate HTML from Meltdown text, the Parse method is called parsing the text and returning the HTML text. The parser splits the text into lines. Each aspect of Meltdown is implemented as a separate class method which are called in turn for each line. Lines are first checked for beginning monikers( Headings, Lists and Tables) then the formatting methods are called.
For basic formatting, bold, italics and underline within a line, direct substitution of their opening bracket pairs is made for their HTML equivalents. The closing bracket pairs are similarly translated. Note that it is assumed that for each opening bracket pair there is a suitably placed closing pair in the line. There is no attempt to validate this.
A pivotal aspect of the coding is the searching for Meltdown string patterns in the line string and translating them into HTML when found. Whilst one could, or possibly should, use System.Text.RegularExpressions.Regex string searches, it was decided to avoid the Regex “mumbo-jumbo” and keep it simple for the 201 level student programmer, a simpler wild-star search method is used:
The start of line monikers are searched for using
System.IO.Enumeration.FileSystemName.MatchesSimpleExpression(searchpattern, line)
The search pattern "[[?]]*"
is used to check if a line is a heading, the ? character being the heading level id a digit. Similarly “((?))*” is used to check if a line is part of an (extended) list with ? being the list list item level (if a digit). A match of "((T))*"
at the start of a line indicates its a table heading row whereas "((t))*"
means its a table row.
Comment: I suppose Markdown ### is simpler than [[3]] for headings ??? but ((n)) is simpler for multi-level lists and ((T/t)) with csv content is simpler for tables.
UPDATE Markdown format"###spaceHeading Text"
for Headings is now accepted, as is Markdown format"*[Link text](Url)*"
for web links!
Similarly, searching for font color specifications the following search query is used in a while loop:
while (System.IO.Enumeration.FileSystemName.MatchesSimpleExpression(ColorPattern, line))
{
<process line>
}
where
ColorPattern = "*((*|*))*"
This will search for substrings in the line beginning with ((
, ending with ))
and that have a vertical bar in the middle, returning true if found. If true then the substring location is determined and converted to <font=”color”>inner text</font> where color is the text in the substring between ((
and |
, and the inner text is the text between |
and ))
. The wild-stars in the pattern outside of the brackets represent the rest of the line. if there is more more than one such pattern, the while loop will continue until there aren’t any.
The search patterns for links are "*<<*|*>>*"
and "*<<*>>*"
and are similarly processed. The first pattern has link text before the vertical bar and the Url after. The second pattern just gets a Url within the brackets which in generating the HTML is also used as the link text. Note that the Markdown syntax for links is included.
Precedence
There is a definite order in the parsing chain.
[[ ... ]]
is used for both headings and bold. In processing a line a check is made for"[[?]]*"
in the first 5 characters, with ? being a single digit. If found then the line is a heading. After that processing the checks for [[ … ]] in the rest of the line are then is interpreted as bold format.(( ... )))
is used for italics, tables and font color. A check is made of the first 5 characters in the line and if((T))
or((t))
the line is part of a table and processed accordingly. The line is then searched for pattern matches with"*((*|*))*"
and if found it is processed. Any remaining ```((*))`` in the line is italics format.
Extensibility
Note that the delimiters used are in a separate file and can be easily changed there. There is also a method that permits them to be set programmatically. Other features could be included by specifying their delimiters and adding a method to be called by teh parser that seeks out instances of that feature in the Meltdown code.
Topic | Subtopic | |
This Category Links | ||
Category: | Web Sites Index: | Web Sites |
Next: > | Meltdown | CS201 Exrecises |
< Prev: | Meltdown | Blazor Test App |