Пример #1
0
 private static StringBuilder EmitBlockQuote(this StringBuilder code, Blockquote quote)
 {
     code.Begin("blockquote");
     foreach (var q in quote.Values)
     {
         code.EmitElement(q);
     }
     code.End("blockquote");
     return(code);
 }
Пример #2
0
        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);
            }
        }