title: Parsing and Expanding Markdown
body: <p>I'm currently trying to develop an templating engine for expanding my custom markdown into full HTML. </p>
  
  <p>I initially thought a recursive approach might work. This would use a tag stack to determine if you were inside another tag, and evaluating the logic of each tag when each are closed. I'm having trouble conceiving of a method that doesn't use indexOf over and over again, and that covers all custom directives I want to support.</p>
  
  <hr>
  
  <p>Syntactically, it uses open and closing tags for each directive. For example: </p>
  
  <pre><code>@foreach(item in list)
      &lt;p&gt;text&lt;/p&gt;
  @endforeach
  </code></pre>
  
  <p>Produces: </p>
  
  <pre><code>&lt;p&gt;text&lt;/p&gt;
  &lt;p&gt;text&lt;/p&gt;
  &lt;p&gt;text&lt;/p&gt;
  </code></pre>
  
  <p>(if there were 3 items in the list)</p>
  
  <hr>
  
  <pre><code>@foreach(item in list)
      &lt;p&gt;textA&lt;/p&gt;
  @endforeach
  @foreach(item in list)
      &lt;p&gt;textB&lt;/p&gt;
  @endforeach
  </code></pre>
  
  <p>Produces: </p>
  
  <pre><code>&lt;p&gt;textA&lt;/p&gt;
  &lt;p&gt;textA&lt;/p&gt;
  &lt;p&gt;textA&lt;/p&gt;
  &lt;p&gt;textA&lt;/p&gt;
  &lt;p&gt;textB&lt;/p&gt;
  &lt;p&gt;textB&lt;/p&gt;
  </code></pre>
  
  <p>(if there were 3 items in each list)  </p>
  
  <hr>
  
  <pre><code>@foreach(item in list)
      @foreach(item in list)
          &lt;p&gt;text_inside&lt;/p&gt;
      @endforeach
      &lt;p&gt;text_outside&lt;/p&gt;
  @endforeach
  </code></pre>
  
  <p>Produces:</p>
  
  <pre><code>&lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_outside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_outside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_inside&lt;/p&gt;
  &lt;p&gt;text_outside&lt;/p&gt;
  </code></pre>
  
  <hr>
  
  <p>Initially, I was thinking about a recursive approach that would use a stack to know whether or not it was inside or outside some other tag. It dawned on me that this is a common function for compilers and as such there should be some popular parsing/compilation algorithm.</p>
  
  <p>I came across the process that a compiler uses (Scanner -> Lexer -> Parser), and I assume that the process of tokenizing input is part of the lexical analysis, but I'm not sure if there is a common algorithm for breaking the code apart for the parser to perform the final logical operation.</p>
  
  <p>At this current point, I'm looking for direction in what method to research, not necessarily someone's implementation (although this might be useful to study).   </p>