public void CanBeInsertedInto() { ContainerBlock container = new MockContainerBlock(); var one = new ParagraphBlock(); container.Insert(0, one); Assert.AreEqual(1, container.Count); Assert.AreSame(container[0], one); var two = new ParagraphBlock(); container.Insert(1, two); Assert.AreEqual(2, container.Count); Assert.AreSame(container[0], one); Assert.AreSame(container[1], two); var three = new ParagraphBlock(); container.Insert(0, three); Assert.AreEqual(3, container.Count); Assert.AreSame(container[0], three); Assert.AreSame(container[1], one); Assert.AreSame(container[2], two); Assert.Throws <ArgumentNullException>(() => container.Insert(0, null)); Assert.Throws <ArgumentOutOfRangeException>(() => container.Insert(4, new ParagraphBlock())); Assert.Throws <ArgumentOutOfRangeException>(() => container.Insert(-1, new ParagraphBlock())); Assert.Throws <ArgumentException>(() => container.Insert(0, one)); // one already has a parent }
public void WriteBlock_OnlyWritesChildrenIfEnableHtmlForBlockIsFalse() { // Arrange const string dummyChildText = "dummyChildText"; var dummyContainerInline = new ContainerInline(); dummyContainerInline.AppendChild(new LiteralInline(dummyChildText)); var dummyParagraphBlock = new ParagraphBlock() { Inline = dummyContainerInline }; FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock(); dummyFlexiSectionBlock.Add(dummyParagraphBlock); var dummyStringWriter = new StringWriter(); var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter) { EnableHtmlForBlock = false }; ExposedFlexiSectionBlockRenderer testSubject = CreateExposedFlexiSectionBlockRenderer(); // Act testSubject.ExposedWriteBlock(dummyHtmlRenderer, dummyFlexiSectionBlock); string result = dummyStringWriter.ToString(); // Assert Assert.Equal(dummyChildText + "\n", result, ignoreLineEndingDifferences: true); }
private static Block CreateParagraph(ParagraphBlock paragraphBlock) { var paragraph = new Paragraph(); paragraph.Inlines.AddRange(CreateInlines(paragraphBlock.Inlines)); return(paragraph); }
/// <summary> /// Convert a Markdown element to a Forkdown one. /// </summary> /// <param name="mdo">Markdown object to convert.</param> public static Element ToForkdown(IMarkdownObject mdo) { Element result = mdo switch { MarkdownDocument _ => new Document(), HeadingBlock h => new Heading(h), ListBlock l => new Listing(l), ListItemBlock li => new ListItem(li), ParagraphBlock p => new Paragraph(p), CustomContainer c => new Section(c), CustomContainerInline c => new ExplicitInlineContainer(c), CodeInline c => new Code(c), FencedCodeBlock c => new CodeBlock(c), LinkInline l => new Link(l), LiteralInline t => new Text(t), LineBreakInline _ => new LineBreak(), ThematicBreakBlock _ => new Separator(), Tables.Table t => new Table(t), Tables.TableRow tr => new TableRow(tr), Tables.TableCell tc => new TableCell(tc), EmphasisInline e => new Emphasis(e), HtmlBlock b => new Html(b), _ => new Placeholder(mdo), }; var subs = mdo switch { LeafBlock b => b.Inline, IEnumerable <MarkdownObject> e => e, _ => null } ?? Nil.E <MarkdownObject>();
public void Hyperlink_MailtoHostPart(string input, string hyperLinkUrl, string text, string superScriptText) { ParagraphBlock expected; if (hyperLinkUrl is null) { if (superScriptText != null) { expected = new ParagraphBlock().AddChildren(new TextRunInline { Text = text }, new SuperscriptTextInline().AddChildren(new TextRunInline { Text = superScriptText })); } else { expected = new ParagraphBlock().AddChildren(new TextRunInline { Text = text }); } } else { expected = new ParagraphBlock().AddChildren(new HyperlinkInline { Url = hyperLinkUrl, Text = text, LinkType = HyperlinkType.Email }); } AssertEqual(input, expected); }
public static CellData Parse(ParagraphBlock paragraph) { CellData data = new CellData(); StringBuilder buffer = new StringBuilder(); foreach (var segment in paragraph.Inline) { LinkInline link = segment as LinkInline; if (link != null) { data.Link = link.Url; buffer = new StringBuilder(); foreach (var text in link) { buffer.Append(text); } data.Text = buffer.ToString(); break; } buffer.Append(segment.ToString()); } data.Text = buffer.ToString(); return(data); }
private List <string> ReadDescription(MarkdownDocument document, ref int start) { List <string> descriptions = new List <string>(); while (start < document.Count) { ParagraphBlock paragraph = document[start++] as ParagraphBlock; if (paragraph == null) { break; } StringBuilder builder = new StringBuilder(); foreach (var jj in paragraph.Inline) { var linebreak = jj is Markdig.Syntax.Inlines.LineBreakInline; if (linebreak) { descriptions.Add(builder.ToString()); builder.Length = 0; continue; } builder.Append(jj.ToString()); } descriptions.Add(builder.ToString()); } return(descriptions); }
/// <summary> /// Replaces a <see cref="ProxyTableBlock"/> with a <see cref="ParagraphBlock"/>. /// </summary> /// <param name="blockProcessor">The <see cref="BlockProcessor"/> processing the <see cref="ProxyTableBlock"/> to undo.</param> /// <param name="proxyTableBlock">The <see cref="ProxyTableBlock"/> to undo.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="blockProcessor"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="proxyTableBlock"/> is <c>null</c>.</exception> protected virtual void Undo(BlockProcessor blockProcessor, ProxyTableBlock proxyTableBlock) { if (blockProcessor == null) { throw new ArgumentNullException(nameof(blockProcessor)); } if (proxyTableBlock == null) { throw new ArgumentNullException(nameof(proxyTableBlock)); } // Discard proxyTableBlock ContainerBlock parent = proxyTableBlock.Parent; blockProcessor.Discard(proxyTableBlock); // Replace with paragraph block ParagraphBlockParser parser = blockProcessor.Parsers.FindExact <ParagraphBlockParser>(); var paragraphBlock = new ParagraphBlock(parser) { Lines = proxyTableBlock.Lines, }; parent.Add(paragraphBlock); blockProcessor.Open(paragraphBlock); }
static ParagraphBlockViewModel CreateParagraphViewModel(ParagraphBlock paragraph, string prefix = null) { var viewModel = new ParagraphBlockViewModel(); if (!string.IsNullOrEmpty(prefix)) { viewModel.Spans.Add(new SpanInline { Text = prefix }); } foreach (var inline in paragraph.Inlines) { var span = GetInlineTextWithFlags(inline); if (span == null) { continue; } viewModel.Spans.Add(span); } viewModel.GenerateFormattedString(); return(viewModel); }
/// <summary> /// Renders a paragraph element. /// </summary> protected override void RenderParagraph(ParagraphBlock element, IRenderContext context) { if (!(context is BlockCollectionRenderContext localContext)) { throw new RenderContextIncorrectException(); } var blockCollection = localContext.BlockCollection; var paragraph = new Paragraph { Margin = ParagraphMargin, LineHeight = ParagraphLineHeight, Foreground = localContext.Foreground }; var childContext = new InlineRenderContext(paragraph.Inlines, context) { Parent = paragraph }; RenderInlineChildren(element.Inlines, childContext); blockCollection.Add(paragraph); }
public void TestParse() { const string markdown = "This is a text with some *emphasis*"; var pipeline = new MarkdownPipelineBuilder() .UsePreciseSourceLocation() .Build(); MarkdownDocument document = Markdown.Parse(markdown, pipeline); Assert.AreEqual(1, document.LineCount); Assert.AreEqual(markdown.Length, document.Span.Length); Assert.AreEqual(1, document.LineStartIndexes.Count); Assert.AreEqual(0, document.LineStartIndexes[0]); Assert.AreEqual(1, document.Count); ParagraphBlock paragraph = document[0] as ParagraphBlock; Assert.NotNull(paragraph); Assert.AreEqual(markdown.Length, paragraph.Span.Length); LiteralInline literal = paragraph.Inline.FirstChild as LiteralInline; Assert.NotNull(literal); Assert.AreEqual("This is a text with some ", literal.ToString()); EmphasisInline emphasis = literal.NextSibling as EmphasisInline; Assert.NotNull(emphasis); Assert.AreEqual("*emphasis*".Length, emphasis.Span.Length); LiteralInline emphasisLiteral = emphasis.FirstChild as LiteralInline; Assert.NotNull(emphasisLiteral); Assert.AreEqual("emphasis", emphasisLiteral.ToString()); Assert.Null(emphasisLiteral.NextSibling); Assert.Null(emphasis.NextSibling); }
// Root, --- Done // Paragraph, --- Done // Header,// #(1-6)、===、--- --- Done // HorizontalRule,// ---、*** // List, // +、-、*、number. --- Done // Code, // Four spaces or \t // Quote, // > // ListItemBuilder, // Table, // 表 | 1 | 2 | // LinkReference, // [name](url) internal static MarkdownBlock ParseBlock(string markdownText, int start, int end, out int actualEnd) { actualEnd = start; MarkdownBlock block = null; char nonSpaceChar = GetNonSpaceChar(markdownText, start, end, out int nonSpacePos); if (nonSpaceChar == '#' && nonSpacePos == start) { block = HeaderBlock.Parse(markdownText, start, end, out actualEnd); } if (block == null && (nonSpaceChar == '*' || nonSpaceChar == '-' || nonSpaceChar == '_')) { block = HorizontalRuleBlock.Parse(markdownText, start, end, out actualEnd); } if (block == null && (nonSpaceChar == '*' || nonSpaceChar == '+' || nonSpaceChar == '-' || (nonSpaceChar >= '0' && nonSpaceChar <= '9'))) { block = ListElement.Parse(markdownText, start, end, out actualEnd); } if (block == null) { block = ParagraphBlock.Parse(markdownText, start, end, out actualEnd); } return(block); }
public MarkdownParagraph(ParagraphBlock paragraphBlock) { this.paragraphBlock = paragraphBlock; RelativeSizeAxes = Axes.X; AutoSizeAxes = Axes.Y; }
private void RenderParagraph(ParagraphBlock block, UIElementCollection blockUIElementCollection) { var paragraph = new Paragraph(); RenderInlines(block.Inlines, paragraph.Inlines); var textBlock = CreateOrReuseRichTextBlock(blockUIElementCollection); textBlock.Blocks.Add(paragraph); }
private ParagraphBlock CreateBlock() { var paragraph = new ParagraphBlock(); var cursor = (TextCaret)paragraph.GetCaretAtStart(); cursor.InsertText("This is some of the text") .InsertText("Some additional text"); return(paragraph); }
private TableData ParseGenericTable(Table table) { var data = new TableData(); bool body = false; List <string> columns = new List <string>(); List <List <CellData> > rows = new List <List <CellData> >(); foreach (TableRow row in table) { if (!body) { foreach (TableCell cell in row) { foreach (var content in cell) { ParagraphBlock paragraph = content as ParagraphBlock; if (paragraph != null) { var cellData = CellData.Parse(paragraph); columns.Add(cellData.Text); } } } body = true; continue; } List <CellData> newRow = new List <CellData>(); foreach (TableCell cell in row) { foreach (var content in cell) { ParagraphBlock paragraph = content as ParagraphBlock; if (paragraph != null) { var cellData = CellData.Parse(paragraph); newRow.Add(cellData); } } } rows.Add(newRow); } data.Columns = columns; data.Rows = rows; return(data); }
public void Setup() { document = new Document(); // Workaround for a quirk in the migradoc API. _ = document.AddSection().Elements; pdfBuilder = new PdfBuilder(document, PdfOptions.Default); renderer = new ParagraphBlockRenderer(); paragraph = new ParagraphBlock(); paragraphText = "some text"; paragraph.Inline = new ContainerInline().AppendChild(new LiteralInline(paragraphText)); }
/// <summary> /// Renders a paragraph element. /// </summary> /// <param name="element"></param> /// <param name="blockUIElementCollection"></param> /// <param name="context"></param> private void RenderParagraph(ParagraphBlock element, UIElementCollection blockUIElementCollection, RenderContext context) { var paragraph = new Paragraph(); paragraph.Margin = ParagraphMargin; context.TrimLeadingWhitespace = true; RenderInlineChildren(paragraph.Inlines, element.Inlines, paragraph, context); var textBlock = CreateOrReuseRichTextBlock(blockUIElementCollection, context); textBlock.Blocks.Add(paragraph); }
private Paragraph ConvertParagraphBlock(ParagraphBlock paragraphBlock, int numberingId, int nestLevel) { var inlineConverter = new InlineConverter(Manipulator, UserSettingStyleMap, BaseFolderPathForRelativePath); var oxmlInlineElements = inlineConverter.Convert(paragraphBlock.Inline); var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Paragraph, null); var oxmlParagraph = Manipulator.ElementCreator.CreateParagraphElement(oxmlInlineElements, styleId, numberingId, nestLevel); Manipulator.AdjustImageDimension(oxmlParagraph); return(oxmlParagraph); }
private void Render(ParagraphBlock block) { var style = this.Theme.Paragraph; var foregroundColor = isQuoted ? this.Theme.Quote.ForegroundColor : style.ForegroundColor; var label = new Label { FormattedText = CreateFormatted(block.Inline, style.FontFamily, style.Attributes, foregroundColor, style.BackgroundColor, style.FontSize), }; AttachLinks(label); this.stack.Children.Add(label); }
private IEnumerable <View> Render(ParagraphBlock block) { var style = this.Theme.Paragraph; var foregroundColor = isQuoted ? this.Theme.Quote.ForegroundColor : style.ForegroundColor; var label = new Label { FormattedText = CreateFormatted(block.Inline, style.FontFamily, style.Attributes, foregroundColor, style.BackgroundColor, style.FontSize), }; AttachLinks(label); return(new View[] { label }); }
public void ParagraphConverterCallsInlineConverterTest() { var testInlineParser = new TestInlineParser(); var logger = loggerMockFactory.Get <ParagraphConverter>().Object; ParagraphConverter converter = new ParagraphConverter(testInlineParser, logger); var paragraph = new ParagraphBlock(); converter.Execute(paragraph); Assert.True(testInlineParser.ParseCalled); }
protected override void Write(HtmlRenderer renderer, ParagraphBlock obj) { if (!renderer.IsFirstInContainer) { renderer.EnsureLine(); } renderer.WriteLeafInline(obj); if (!renderer.IsLastInContainer) { renderer.WriteLine("<br />"); renderer.WriteLine("<br />"); } }
private View Render(ParagraphBlock block) { var paragraph = ParagraphTemplate.CreateContent() as View; paragraph.BindingContext = new Templates.ParagraphAstNode { FormattedText = CreateFormatted(block.Inline) }; AttachLinks(paragraph); return(paragraph); }
protected override void Write(RoundtripRenderer renderer, QuoteBlock quoteBlock) { renderer.RenderLinesBefore(quoteBlock); renderer.Write(quoteBlock.TriviaBefore); var indents = new string[quoteBlock.QuoteLines.Count]; for (int i = 0; i < quoteBlock.QuoteLines.Count; i++) { var quoteLine = quoteBlock.QuoteLines[i]; var wsb = quoteLine.TriviaBefore.ToString(); var quoteChar = quoteLine.QuoteChar ? ">" : ""; var spaceAfterQuoteChar = quoteLine.HasSpaceAfterQuoteChar ? " " : ""; var wsa = quoteLine.TriviaAfter.ToString(); indents[i] = (wsb + quoteChar + spaceAfterQuoteChar + wsa); } bool noChildren = false; if (quoteBlock.Count == 0) { noChildren = true; // since this QuoteBlock instance has no children, indents will not be rendered. We // work around this by adding empty LineBreakInlines to a ParagraphBlock. // Wanted: a more elegant/better solution (although this is not *that* bad). foreach (var quoteLine in quoteBlock.QuoteLines) { var emptyLeafBlock = new ParagraphBlock { NewLine = quoteLine.NewLine }; var newLine = new LineBreakInline { NewLine = quoteLine.NewLine }; var container = new ContainerInline(); container.AppendChild(newLine); emptyLeafBlock.Inline = container; quoteBlock.Add(emptyLeafBlock); } } renderer.PushIndent(indents); renderer.WriteChildren(quoteBlock); renderer.PopIndent(); if (!noChildren) { renderer.RenderLinesAfter(quoteBlock); } }
void Render(ParagraphBlock block) { var style = Theme.Paragraph; var foregroundColor = isQuoted ? Theme.Quote.ForegroundColor : style.ForegroundColor; var label = new Label { FormattedText = CreateFormatted(block.Inline, style.FontFamily, style.Attributes, style.TextDecorations, foregroundColor, style.BackgroundColor, style.FontSize, style.LineHeight), HorizontalTextAlignment = style.HorizontalTextAlignment, VerticalTextAlignment = style.VerticalTextAlignment, }; AttachLinks(label); stack.Children.Add(label); }
protected override void RenderParagraph(ParagraphBlock element, IRenderContext context) { // Adding new paragraphs to the root of the xml document var doc = context.Parent as XDocument; var paragraph = new XElement("p"); doc.Root.Add(paragraph); // Render the paragraph's children RenderInlineChildren(element.Inlines, new RenderContext() { Parent = paragraph }); }
private static void Walk(MarkdownObject markdownObject) { var links = markdownObject .Descendants() .Where(o => o is ParagraphBlock) .Cast <ParagraphBlock>(); var p = new ParagraphBlock(); p.Inline = new ContainerInline(); //p.Inline.AppendChild(new Url) int y0 = 2; }
private static void Undo(BlockProcessor processor, GridTableState tableState, Table gridTable) { var parser = processor.Parsers.FindExact <ParagraphBlockParser>(); // Discard the grid table var parent = gridTable.Parent; processor.Discard(gridTable); var paragraphBlock = new ParagraphBlock(parser) { Lines = tableState.Lines, }; parent.Add(paragraphBlock); processor.Open(paragraphBlock); }
public void WriteBlock_OnlyWritesChildrenIfEnableHtmlForBlockIsFalse() { // Arrange // Dummy title part const string dummyTitle = "dummyTitle"; var dummyTitleContainerInline = new ContainerInline(); dummyTitleContainerInline.AppendChild(new LiteralInline(dummyTitle)); var dummyTitlePartBlock = new PlainLeafBlock(null); dummyTitlePartBlock.Inline = dummyTitleContainerInline; // Dummy content part const string dummyContent = "dummyContent"; var dummyContentContainerInline = new ContainerInline(); dummyContentContainerInline.AppendChild(new LiteralInline(dummyContent)); var dummyContentParagraphBlock = new ParagraphBlock() { Inline = dummyContentContainerInline }; var dummyContentPartBlock = new PlainContainerBlock(null); dummyContentPartBlock.Add(dummyContentParagraphBlock); // Dummy footnote part const string dummyFootnote = "dummyFootnote"; var dummyFootnoteContainerInline = new ContainerInline(); dummyFootnoteContainerInline.AppendChild(new LiteralInline(dummyFootnote)); var dummyFootnotePartBlock = new PlainLeafBlock(null); dummyFootnotePartBlock.Inline = dummyFootnoteContainerInline; FlexiCardsBlock dummyFlexiCardsBlock = CreateFlexiCardsBlock(children: new List <FlexiCardBlock> { CreateFlexiCardBlock(titlePart: dummyTitlePartBlock, contentPart: dummyContentPartBlock, footnotePart: dummyFootnotePartBlock) }); var dummyStringWriter = new StringWriter(); var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter) { EnableHtmlForBlock = false }; ExposedFlexiCardsBlockRenderer testSubject = CreateExposedFlexiCardsBlockRenderer(); // Act testSubject.ExposedWriteBlock(dummyHtmlRenderer, dummyFlexiCardsBlock); string result = dummyStringWriter.ToString(); // Assert Assert.Equal($"{dummyTitle}\n{dummyContent}\n{dummyFootnote}\n", result, ignoreLineEndingDifferences: true); }