/// <summary>
 /// Converts the given Markdown text into a HTML page.
 /// </summary>
 /// <param name="source">The input Markdown text.</param>
 /// <returns>A string with the HTML markup of the generated page.</returns>
 public string ConvertToHtml(TextReader source)
 {
     Output = new StringBuilder();
     LineNo = 0;
     CodePreamble = null;
     Anchors = new List<MdAnchor>();
     Context = MdContext.Text;
     string line = null;
     while ((line = source.ReadLine()) != null)
     {
         ProcessLine(line);
         LineNo++;
     }
     ProcessLine("");
     return ConvertToHtml(Output.ToString());
 }
 private void ProcessLine(string line)
 {
     switch (Context)
     {
         case MdContext.Inactive:
             if (MdSyntax.IsYamlHeaderStart(LineNo, line))
                 Context = MdContext.YamlHeader;
             else if (MdSyntax.IsHtmlCommentStart(line))
                 Context = MdContext.HtmlComment;
             else if (MdSyntax.IsCodeBlockStart(line, ref CodePreamble))
                 Context = MdContext.CodeBlock;
             else if (IsActivationCue(line))
                 Context = MdContext.Text;
             break;
         case MdContext.YamlHeader:
             if (MdSyntax.IsYamlHeaderEnd(LineNo, line))
                 Context = MdContext.Text;
             break;
         case MdContext.HtmlComment:
             if (MdSyntax.IsHtmlCommentEnd(line))
                 Context = MdContext.Text;
             break;
         case MdContext.CodeBlock:
             if (MdSyntax.IsCodeBlockEnd(line, ref CodePreamble))
                 Context = MdContext.Text;
             break;
         case MdContext.Text:
             if (MdSyntax.IsYamlHeaderStart(LineNo, line))
                 Context = MdContext.YamlHeader;
             else if (MdSyntax.IsHtmlCommentStart(line))
                 Context = MdContext.HtmlComment;
             else if (MdSyntax.IsCodeBlockStart(line, ref CodePreamble))
                 Context = MdContext.CodeBlock;
             else if (IsDeactivationCue(line))
                 Context = MdContext.Inactive;
             else if (IsListStart(line, ref ListKey))
                 Context = MdContext.List;
             else
                 ProcessTextLine(line);
             break;
         case MdContext.List:
             if (!IsListElement(line, ListItems))
             {
                 var listValue = string.Join(ListSeparatorStr, ListItems.ToArray());
                 ProcessTextLine(string.Format(MdListFormat, ListKey, listValue));
                 ListKey = null;
                 ListItems.Clear();
                 Context = MdContext.Text;
                 ProcessLine(line);
             }
             break;
         default:
             throw new ArgumentOutOfRangeException();
     }
 }
 /// <summary>
 /// Parses the given text input and recognizes configuration properties.
 /// </summary>
 /// <param name="source">The text input.</param>
 public void Parse(TextReader source)
 {
     LineNo = 0;
     CodePreamble = null;
     CurrentGroup = null;
     Context = ActivationCue == null ? MdContext.Text : MdContext.Inactive;
     string line = null;
     while ((line = source.ReadLine()) != null)
     {
         ProcessLine(line);
         LineNo++;
     }
     ProcessLine("");
 }
 private void ProcessLine(string line)
 {
     switch (Context)
     {
         case MdContext.Inactive:
             if (MdSyntax.IsYamlHeaderStart(LineNo, line))
                 Context = MdContext.YamlHeader;
             else if (MdSyntax.IsHtmlCommentStart(line))
                 Context = MdContext.HtmlComment;
             else if (MdSyntax.IsCodeBlockStart(line, ref CodePreamble))
                 Context = MdContext.CodeBlock;
             break;
         case MdContext.YamlHeader:
             if (MdSyntax.IsYamlHeaderEnd(LineNo, line))
                 Context = MdContext.Text;
             break;
         case MdContext.HtmlComment:
             if (MdSyntax.IsHtmlCommentEnd(line))
                 Context = MdContext.Text;
             break;
         case MdContext.CodeBlock:
             if (MdSyntax.IsCodeBlockEnd(line, ref CodePreamble))
                 Context = MdContext.Text;
             break;
         case MdContext.Text:
             if (MdSyntax.IsYamlHeaderStart(LineNo, line))
                 Context = MdContext.YamlHeader;
             else if (MdSyntax.IsHtmlCommentStart(line))
                 Context = MdContext.HtmlComment;
             else if (MdSyntax.IsCodeBlockStart(line, ref CodePreamble))
                 Context = MdContext.CodeBlock;
             else
                 ProcessTextLine(line);
             break;
         default:
             throw new ArgumentOutOfRangeException();
     }
     Output.AppendLine(line);
 }