public static string Emit(this MarkDown markdown) { var code = new StringBuilder(); code.EmitImpl(markdown); return(code.ToString()); }
private static StringBuilder EmitImpl(this StringBuilder code, MarkDown markdown) { code.Line("<!DOCTYPE html>") .Begin("head") .Begin("meta", "charset".v("utf-8")) .Begin("meta", "name".v("viewport"), "content".v("width=device-width, initial-scale=1.0")) .Begin("tile") .Line(markdown.Title) .End("title") .Begin("link", "rel".v("stylesheet"), "href".v("./base.css")) .End("link") .Begin("script", "type".v("text/javascript"), "src".v("./math.js")) .End("script") .Begin("body") .Begin("div", "class".v("container")) .EmitElements(markdown.RootElements) .End("div") .End("body") .End("head") .Line(""); return(code); }
public static Element Block2Element(this MarkdownBlock block) { switch (block.Type) { case MarkdownBlockType.Root: var doc = block as MarkdownDocument; var md = new MarkDown(); md.Title = ""; md.RootElements.AddRange(doc.Blocks.Select(b => b.Block2Element())); return(md); case MarkdownBlockType.Header: var headerBlock = block as HeaderBlock; var section = new Section(); var titleLine = new Seq(); titleLine.Values.AddRange(headerBlock.Inlines.Select(l => l.Inline2Element())); section.Title = titleLine; section.Level = headerBlock.HeaderLevel; return(section); case MarkdownBlockType.Code: var code = block as CodeBlock; var lines = code.Lines.Select(l => new Line() { Value = l }); var c = new Code(); c.Lines.AddRange(lines); c.CodeKind = CodeKind.Block; return(c); case MarkdownBlockType.Quote: var quote = block as QuoteBlock; var bq = new Blockquote(); bq.Values.AddRange(quote.Blocks.Select(b => b.Block2Element())); return(bq); case MarkdownBlockType.Paragraph: var paraBlock = block as ParagraphBlock; var paragraph = new Paragraph(); paragraph.Values.AddRange(paraBlock.Inlines.Select(l => l.Inline2Element())); return(paragraph); case MarkdownBlockType.Table: var tableBlock = block as TableBlock; var table = new Table(); int ri = 0; foreach (var rowBlock in tableBlock.Rows) { var row = new Row(); var cd = 0; if (ri == 0) { row.RowKind = tableBlock.HasHeaderRow ? RowKind.Head : RowKind.Body; ri++; } else { row.RowKind = RowKind.Body; } foreach (var cellBlock in rowBlock.Cells) { var column = tableBlock.ColumnDefinitions[cd++]; var cell = new Cell(); var blockLine = new Seq(); blockLine.Values.AddRange(cellBlock.Inlines.Select(i => i.Inline2Element())); cell.Value = blockLine; cell.Align = column.Alignment.ColumnAlignToString(); cell.CellKind = row.RowKind == RowKind.Head ? CellKind.Head : CellKind.Cell; row.Cells.Add(cell); } table.Rows.Add(row); } return(table); case MarkdownBlockType.LinkReference: var link = block as LinkReferenceBlock; var hyperlink = new HyperLink(); hyperlink.Text = new Text { Value = link.ToString() }; hyperlink.Url = new Uri(link.Url); return(hyperlink); case MarkdownBlockType.HorizontalRule: var hr = block as HorizontalRuleBlock; return(new Rule { Value = hr.ToString() }); case MarkdownBlockType.List: var listBlock = block as ListBlock; var list = new List(); list.ListKind = listBlock.Style == ListStyle.Numbered ? ListKind.Order : ListKind.UnOrder; foreach (var listItemBlock in listBlock.Items) { foreach (var b in listItemBlock.Blocks) { var e = b.Block2Element(); list.Items.Add(e); } } return(list); default: return(null); } }