﻿<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text" xml:lang="en">MfGames Writing CIL</title>
  <link type="application/atom+xml" href="https://d.moonfire.us/tags/mfgames-writing-cil/atom.xml" rel="self" />
  <link type="text/html" href="https://d.moonfire.us/tags/mfgames-writing-cil/" rel="alternate" />
  <updated>2026-08-15T17:42:46Z</updated>
  <id>https://d.moonfire.us/tags/mfgames-writing-cil/</id>
  <author>
    <name>D. Moonfire</name>
  </author>
  <rights>Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International</rights>
  <entry>
    <title>Messing with Markdown</title>
    <link rel="alternate" href="https://d.moonfire.us/blog/2014/11/23/messing-with-markdown/" />
    <updated>2014-11-23T06:00:00Z</updated>
    <id>https://d.moonfire.us/blog/2014/11/23/messing-with-markdown/</id>
    <category term="programming" scheme="https://d.moonfire.us/categories/" label="Programming" />
    <category term="c-sharp" scheme="https://d.moonfire.us/tags/" label="C#" />
    <category term="markdown" scheme="https://d.moonfire.us/tags/" label="Markdown" />
    <category term="mfgames-text-markup-cil" scheme="https://d.moonfire.us/tags/" label="MfGames Text Markup CIL" />
    <category term="mfgames-writing-cil" scheme="https://d.moonfire.us/tags/" label="MfGames Writing CIL" />
    <category term="mfgames-writing-python" scheme="https://d.moonfire.us/tags/" label="MfGames Writing Python" />
    <summary type="html">I spent the last few weeks working on the beginning of an event-based Markdown parser. I know why, but I've fallen into a rabbit hole that threatens to swallow me.</summary>
    <content type="html">&lt;p&gt;I have a lot of projects in my head, which is one reason why I try to avoid starting new things while working on others. It got too easy to start a project and then never finish it when I got bored or just moved on.&lt;/p&gt;
&lt;p&gt;At the same time, many of my ideas don't &amp;ldquo;stick.&amp;rdquo; They seem like they will, but then it either doesn't work out, the ideas don't really gel, or simply I lose the passion. Some of them, like my &lt;a href="https://mfgames.com/comics/glorious-saber/"&gt;old Exalted webcomic&lt;/a&gt;, haunt me for years with the nagging voice of &amp;ldquo;finish me&amp;rdquo; but I keep not doing it. Others end up being bigger projects, but suffer through constant revisions as I try to figure it out.&lt;/p&gt;
&lt;p&gt;I do have some major projects that I haven continued to update and maintain quite a few years after I start them. One of the biggest is &lt;a href="https://github.com/dmoonfire/mfgames-writing-python"&gt;MfGames Writing Python&lt;/a&gt; which I started in 2010 when my father said I'd love Python. Even though I've long-since decided that &lt;a href="/blog/2012/10/17/two-years-of-python/"&gt;I don't like Python&lt;/a&gt;, I've still been writing updates to the tools in support of my own writing efforts.&lt;/p&gt;
&lt;p&gt;Like &lt;em&gt;Glorious Saber&lt;/em&gt;, I've been thinking about converting my Python writing tools over to C#, but never really got to it. I had a couple stabs at it but nothing really &amp;ldquo;stuck.&amp;rdquo;&lt;/p&gt;
&lt;h1&gt;The Side Project&lt;/h1&gt;
&lt;p&gt;After ICON, something &lt;a href="http://www.jimchines.com/"&gt;Jim C. Hines&lt;/a&gt; and &lt;a href="http://www.scottlynch.us/"&gt;Scott Lynch&lt;/a&gt; said during their &amp;ldquo;Beyond SF 101&amp;rdquo; panel was still echoing in my head.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Write more words and don't be a dick.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now, &lt;a href="/tags/sand-and-ash/"&gt;Sand and Ash&lt;/a&gt; is still stuck in limbo, so I couldn't really do anything with that. So I had this idea for a writing project that would be complimentary to my &lt;a href="https://fedran.com/"&gt;Fedran&lt;/a&gt; world and let me get different ideas out. It involved smaller pieces, short stories and essays and lessons, so I was trying to figure out how to pull it all together.&lt;/p&gt;
&lt;p&gt;For some reason, my Python tools just choked. They are optimized toward writing novels and single-file &lt;a href="http://www.docbook.org/tdg5/en/html/docbook.html"&gt;DocBook 5&lt;/a&gt; files, but not a multitude of smaller DocBook XML files which would these individual pieces.&lt;/p&gt;
&lt;p&gt;I started to look into my C# version, which had a different &amp;ldquo;gather&amp;rdquo; utility and, once again, realized that I probably wasn't &lt;em&gt;that&lt;/em&gt; far off from getting the C# version working with its flaws and maybe move away from the Python.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;One of my major difficulties with Python is that it doesn't handle UTF-8 characters natively. I seem to have a lot of non-ANSI characters in my desert world (macros are a killer) and it kept choking on them.&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;MfGames Writing CIL&lt;/h1&gt;
&lt;p&gt;So I shifted from my side project to &lt;a href="https://github.com/dmoonfire/mfgames-writing-cil"&gt;MfGames Writing CIL&lt;/a&gt; which is the C# version of the Python tools &amp;ldquo;plus&amp;rdquo; additional functionality. The biggest is that I wrote the Python around &lt;a href="http://www.wikicreole.org/"&gt;Creole&lt;/a&gt; instead of &lt;a href="http://en.wikipedia.org/wiki/Markdown"&gt;Markdown&lt;/a&gt;, which is the markup language I've migrated my writing to.&lt;/p&gt;
&lt;p&gt;While I was working with writing up a Markup conversion utility for the tools, I came upon &lt;a href="http://commonmark.org/"&gt;CommonMark&lt;/a&gt; which is an attempt at a well-documented specification. I figured I could use that to help guide my effort on the conversion utility.&lt;/p&gt;
&lt;p&gt;It didn't take long before I realized that I was duplicating my work with &lt;a href="/tags/author-intrusion/"&gt;Author Intrusion&lt;/a&gt; for handling Markdown. Usually when that happens, I figure I should start up a new project to handle the common logic and write it once.&lt;/p&gt;
&lt;h1&gt;MfGames Text Markup CIL&lt;/h1&gt;
&lt;p&gt;And then I moved from &lt;em&gt;MfGames Writing CIL&lt;/em&gt; to &lt;a href="https://github.com/dmoonfire/mfgames-text-markup-cil"&gt;MfGames Text Markup CIL&lt;/a&gt;. This is an attempt to create a single, centralized reader (and eventually writer) of markup languages in general and Markdown in specific.&lt;/p&gt;
&lt;p&gt;There were a couple reasons I went this project instead of another library:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Most Markdown libraries only convert to HTML, which meant I was parsing HTML to get into Author Intrusion or DocBook format. I wanted something that had an intermediate output that was ideal for converting to other formats.&lt;/li&gt;
&lt;li&gt;Again, most libraries seem to load the entire Markdown file into memory at once (mainly because of the deferred links) and then writing it out. I haven't tested this completely, but I already know that I have 640k word series that have to be parsed; I do not want to have this loaded into memory. This implies a callback interface (SAX verses DOM).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I decided to write this in a similar style to C#'s XmlReader. Instead of loading everything into memory and then writing out the results, it just translates the Markdown file into element types.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Loop through the Markdown and process each one.
while (markdown.Read())
{
    switch (markdown.ElementType)
    {
        case MarkupElementType.BeginDocument:
            this.WriteBeginDocument(xml);
            break;

        case MarkupElementType.EndDocument:
            xml.WriteEndElement();
            xml.WriteEndDocument();
            break;

        case MarkupElementType.BeginMetadata:
        case MarkupElementType.EndMetadata:
        case MarkupElementType.BeginContent:
        case MarkupElementType.EndContent:
            break;

        case MarkupElementType.BeginCodeSpan:
            this.WriteForeignPhrase(markdown, xml);
            break;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The system seemed to work out pretty well for my test cases, but when I started to throw &amp;ldquo;real&amp;rdquo; chapters at it, it started to crumble. Not from the foundation, but simply because I didn't write a good enough Markdown parser to translate them.&lt;/p&gt;
&lt;h1&gt;CommonMark&lt;/h1&gt;
&lt;p&gt;And here is where I started down the rabbit hole. The callback system worked great, but I needed to get my parser to be competent enough to handle what I wrote. Once I get that, converting to DocBook is trivial (as the above example probably shows).&lt;/p&gt;
&lt;p&gt;A few days ago, I noticed that the &lt;a href="https://github.com/jgm/CommonMark/blob/master/spec.txt"&gt;CommonMark spec&lt;/a&gt; was a Markdown file with some magic for handling the input/output examples. Well, I could write a bunch of unit tests or&amp;hellip; I could write a program that converted the 500+ examples into unit tests for me.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Why write things out by hand when I can write a program to do it for me?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Last night, I finished converting most of the unit tests over. Now, I just have to solve 508 unit tests, or at least 300 of them.&lt;/p&gt;
&lt;h1&gt;Rabbit Holes&lt;/h1&gt;
&lt;p&gt;I'm pretty sure this is going to be overwhelming but I think it will still further my goal of finishing up my writing tools and get back to my side project. We'll see how it ends up, but I still have a mountain to climb.&lt;/p&gt;
&lt;p&gt;This is also the reason I haven't really posted for a few weeks to. I was lost in a rabbit hole.&lt;/p&gt;
</content>
  </entry>
</feed>
