protected override void Write(HtmlRenderer renderer, HeadingBlock obj) { int index = obj.Level - 1; string headingText = (uint)index < (uint)HeadingTexts.Length ? HeadingTexts[index] : $"h{obj.Level.ToString(CultureInfo.InvariantCulture)}"; if (renderer.EnableHtmlForBlock) { renderer.Write("<").Write(headingText).WriteAttributes(obj).Write('>'); renderer.Write(HeaderTextTag); } renderer.WriteLeafInline(obj); if (renderer.EnableHtmlForBlock) { renderer.Write("</span>"); // Append Hash permalink to ID if one exists. string?id = obj.TryGetAttributes()?.Id; if (!string.IsNullOrEmpty(id)) { renderer.Write("<a class=\"header-permalink\" onclick=\"loadHash('#").Write(id).Write("')\">#</a>"); } renderer.Write("</").Write(headingText).WriteLine(">"); } renderer.EnsureLine(); }
private void OnHeadingBlockParsed(BlockProcessor processor, Block block) { if (!(block is HeadingBlock headingBlock) || block is BlogMetadataBlock) { return; } if (headingBlock.Level < 2) { return; // Ignore h1 since there's no point including it. } var document = processor.Document; var toc = document.Where(b => b is TableOfContentsBlock).FirstOrDefault() as TableOfContentsBlock; if (toc == null) { return; } ContainerBlock parent = toc; for (int i = 0; i < headingBlock.Level - 2; i++) // 2 is the minimum level we support, hence -2 { if (!(parent.LastChild is ContainerBlock childContainer)) { childContainer = new ListItemBlock(block.Parser); parent.Add(childContainer); } parent = (ContainerBlock)parent.LastChild; } var headingCopy = new HeadingBlock(block.Parser) { Column = headingBlock.Column, HeaderChar = headingBlock.HeaderChar, Inline = headingBlock.Inline, IsBreakable = headingBlock.IsBreakable, IsOpen = headingBlock.IsOpen, Level = headingBlock.Level, Line = headingBlock.Line, ProcessInlines = headingBlock.ProcessInlines, RemoveAfterProcessInlines = headingBlock.RemoveAfterProcessInlines, Span = headingBlock.Span }; headingCopy.Lines = new StringLineGroup(headingBlock.Lines.Lines.Length); headingCopy.SetAttributes(headingBlock.GetAttributes()); foreach (var line in headingBlock.Lines.Lines) { if (line.Slice.Text == null) { continue; } var textCopy = new StringSlice(line.Slice.Text, line.Slice.Start, line.Slice.End); var reffableLine = new StringLine(ref textCopy); headingCopy.Lines.Add(ref reffableLine); } parent.Add(headingCopy); }
protected override void Write(HtmlRenderer renderer, HeadingBlock obj) { string contentWrote; using (var writer = new StringWriter()) { base.Write(new HtmlRenderer(writer), obj); contentWrote = writer.ToString(); } if (!renderer.EnableHtmlForBlock) { renderer.Write(contentWrote); return; } var minStart = string.Format("<h{0}>", _minLevel); var minEnd = string.Format("</h{0}>", _minLevel); var currentLevel = 1; while (currentLevel < _minLevel) { var replaceStart = string.Format("<h{0}>", currentLevel); var replaceEnd = string.Format("</h{0}>", currentLevel); contentWrote = contentWrote.Replace(replaceStart, minStart).Replace(replaceEnd, minEnd); currentLevel++; } renderer.Write(contentWrote); }
private Phrase Render(HeadingBlock block) { var fonts = new iTextSharp.text.Font[] { Header1Font, Header2Font, Header3Font, Header4Font, Header5Font, Header6Font }; var colors = new iTextSharp.text.Color[] { new iTextSharp.text.Color(0x9B1C47), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0), new iTextSharp.text.Color(0, 0, 0) }; return(CreateFormatted(block.Inline, fonts[block.Level - 1], 0, colors[block.Level - 1])); }
/// <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>();
private void AssertHeading(HeadingBlock heading, int expectedLevel, string expectedText) { var headingText = GetBlockText(heading); heading.Level.Should().Be(expectedLevel); headingText.Should().Be(expectedText); }
public WikiPanelHeading(HeadingBlock headingBlock) : base(headingBlock) { Margin = new MarginPadding { Bottom = 40 }; }
public MarkdownHeading(HeadingBlock headingBlock) { this.headingBlock = headingBlock; AutoSizeAxes = Axes.Y; RelativeSizeAxes = Axes.X; }
public void Heading_should_construct_from_HeadingBlock_correctly() { var headingBlock = new HeadingBlock(new ParagraphBlockParser()); headingBlock.Inline = new ContainerInline(); var firstContent = new LiteralInline("myheading"); headingBlock.Inline.AppendChild(firstContent); var firstHeading = new Heading(headingBlock); Assert.AreEqual("myheading", firstHeading.Title); var secondContent = new LiteralInline("-myotherheading"); headingBlock.Inline.AppendChild(secondContent); var secondHeading = new Heading(headingBlock); Assert.AreEqual("myheading-myotherheading", secondHeading.Title); var codeContent = new CodeInline(); codeContent.Content = "awesomeclassname"; headingBlock.Inline.AppendChild(codeContent); var codeHeading = new Heading(headingBlock); Assert.AreEqual("myheading-myotherheadingawesomeclassname", codeHeading.Title); }
private Dictionary <string, List <string> > ReadReferenceDescription(MarkdownDocument document, ref int start) { Dictionary <string, List <string> > descriptions = new Dictionary <string, List <string> >(); while (start < document.Count) { string name = null; var list = new List <string>(); while (start < document.Count) { HeadingBlock heading = document[start++] as HeadingBlock; if (heading != null) { name = heading.Inline.FirstChild.ToString(); break; } } list = ReadDescription(document, ref start); if (name != null) { descriptions[name] = list; } } return(descriptions); }
/// <summary> /// Create tree item or several tree items in parent-child relation based on levels count. /// There might be situations where document is invalid and header2 is followed by header4 /// then to keep tree structure we need to create fake header3 in the middle. /// </summary> /// <param name="currentHeader">current header</param> /// <param name="levels">int representation of how many levels there is /// between curent header and last one added</param> /// <param name="headingBlock">data source for tree item</param> /// <param name="lastItemAdded">item added previously to tree</param> /// <returns>tree structure item for heading block</returns> private HeaderData CreateTreeItem(HeaderData currentHeader, int levels, HeadingBlock headingBlock, out HeaderData lastItemAdded) { var result = new List <HeaderData>(); for (var i = 0; i < levels; i++) { result.Add(new HeaderData { Level = currentHeader.Level + 1 + i }); if (i == levels - 1) { result[i].Text = headingBlock.Inline.FirstChild.ToString(); result[i].Level = headingBlock.Level; result[i].Id = headingBlock.GetAttributes().Id; } if (i > 0) { result[i - 1].Children = new List <HeaderData> { result[i] }; result[i].Parent = result[i - 1]; } else { result[i].Parent = currentHeader; } } lastItemAdded = result.Last(); return(result[0]); }
private void AssertHeading(HeadingBlock heading, int expectedLevel, string expectedText) { var inline = heading.Inline.ToList(); inline.Should().HaveCount(1); heading.Level.Should().Be(expectedLevel); inline[0].ToString().Should().Be(expectedText); }
protected override MarkdownHeading CreateHeading(HeadingBlock headingBlock) { var heading = base.CreateHeading(headingBlock); OnAddHeading(headingBlock, heading); return(heading); }
private void Render(HeadingBlock block) { MarkdownStyle style; switch (block.Level) { case 1: style = this.Theme.Heading1; break; case 2: style = this.Theme.Heading2; break; case 3: style = this.Theme.Heading3; break; case 4: style = this.Theme.Heading4; break; case 5: style = this.Theme.Heading5; break; default: style = this.Theme.Heading6; break; } 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), }; label.Margin = style.Margin; AttachLinks(label); if (style.BorderSize > 0) { var headingStack = new StackLayout(); headingStack.Children.Add(label); headingStack.Children.Add(new BoxView { HeightRequest = style.BorderSize, BackgroundColor = style.BorderColor, }); stack.Children.Add(headingStack); } else { stack.Children.Add(label); } }
void Render(HeadingBlock block) { MarkdownStyle style; switch (block.Level) { case 1: style = Theme.Heading1; break; case 2: style = Theme.Heading2; break; case 3: style = Theme.Heading3; break; case 4: style = Theme.Heading4; break; case 5: style = Theme.Heading5; break; default: style = Theme.Heading6; break; } 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); if (style.BorderSize > 0) { var headingStack = new StackLayout(); headingStack.Children.Add(label); headingStack.Children.Add(new BoxView { HeightRequest = style.BorderSize, BackgroundColor = style.BorderColor, }); stack.Children.Add(headingStack); } else { stack.Children.Add(label); } }
/// <summary> /// Try to start a new maml file. /// </summary> /// <param name="headingBlock">The heading block that might trigger the start of a new maml file..</param> /// <returns>True if a new Maml file was started; otherwise, false.</returns> public bool TryStartNewMamlFile(HeadingBlock headingBlock) { if (_indexOfAmlFile < 0 || (_indexOfAmlFile + 1 < _amlFileList.Count && _amlFileList[_indexOfAmlFile + 1].spanStart == headingBlock.Span.Start)) { if (null != Writer) { CloseCurrentMamlFile(); } ++_indexOfAmlFile; var mamlFile = _amlFileList[_indexOfAmlFile]; System.IO.Directory.CreateDirectory(Path.GetDirectoryName(mamlFile.fileName)); var tw = new System.IO.StreamWriter(mamlFile.fileName, false, Encoding.UTF8, 1024); Writer = tw; Push(MamlElements.topic, new[] { new KeyValuePair <string, string>("id", mamlFile.guid), new KeyValuePair <string, string>("revisionNumber", "1") }); Push(MamlElements.developerConceptualDocument, new[] { new KeyValuePair <string, string>("xmlns", "http://ddue.schemas.microsoft.com/authoring/2003/5"), new KeyValuePair <string, string>("xmlns:xlink", "http://www.w3.org/1999/xlink") }); Push(MamlElements.introduction); bool hasLinkOrOutlineElement = false; if (EnableLinkToPreviousSection && _indexOfAmlFile > 0) { Push(MamlElements.para); Write(LinkToPreviousSectionLabelText); var prevTopic = _amlFileList[_indexOfAmlFile - 1]; Push(MamlElements.link, new[] { new KeyValuePair <string, string>("xlink:href", prevTopic.guid) }); Write(prevTopic.title); PopTo(MamlElements.link); PopTo(MamlElements.para); hasLinkOrOutlineElement = true; } if (AutoOutline) { WriteLine("<autoOutline />"); hasLinkOrOutlineElement = true; } if (hasLinkOrOutlineElement) { Push(MamlElements.markup); Write("<hr/>"); PopTo(MamlElements.markup); } return(true); } else { return(false); } }
public void AddEntry(HeadingBlock headingBlock, MarkdownHeading heading) { switch (headingBlock.Level) { case 2: case 3: tableOfContents.AddEntry(getTitle(headingBlock.Inline), heading, headingBlock.Level == 3); break; } }
public void EnsureEmptyHeadingNotWritten() { HeadingBlock block = new HeadingBlock(new HeadingBlockParser()) { Level = 1, Inline = new ContainerInline() }; renderer.Write(pdfBuilder, block); Assert.AreEqual(0, document.LastSection.Elements.Count); }
public HeadingBlockView(DocumentEditorContextView root, HeadingBlock block) : base(root, block) { _block = block; _block.Tag = this; Padding = new Thickness(10); SyncTextSize(); //Text.TextFont = new Typeface("Segoe UI Semibold"); }
static string GetHeadingText(HeadingBlock heading) { var sb = new StringBuilder(); foreach (var item in heading.Inline) { sb.Append(item); } return(sb.ToString()); }
public Task <string> GetTitle() { HeadingBlock header = Markdown.FirstOrDefault(f => f is HeadingBlock) as HeadingBlock; if (header != null) { return(Task.FromResult(string.Join("", header.Inline))); } return(Task.FromResult(Name)); }
private void addTitle(string text, bool subtitle = false) { var headingBlock = new HeadingBlock(new HeadingBlockParser()) { Inline = new ContainerInline().AppendChild(new LiteralInline(text)), Level = subtitle ? 3 : 2, }; var heading = new OsuMarkdownHeading(headingBlock); sidebar.AddEntry(headingBlock, heading); }
private static HeadingBlock RemoveHtmlTag(HeadingBlock block) { var inlines = block.Inline.Skip(2); block.Inline = new ContainerInline(); foreach (var inline in inlines) { inline.Remove(); block.Inline.AppendChild(inline); } return(block); }
public void Verify_Serialization() { var paragraph = new HeadingBlock(); var caret = (TextCaret)paragraph.GetCaretAtStart(); caret.InsertText("This is some of the text") .InsertText("Some additional text"); var descriptorsLookup = new DescriptorsLookup((BlockDescriptor)HeadingBlock.Descriptor); // Act SerializationHelpers.VerifyDeserialization(paragraph, descriptorsLookup); }
public void ConversionTest(int headerLevel) { HeaderConverter converter = GetHeaderConverter(); var blockInput = new HeadingBlock(null); blockInput.Level = headerLevel; blockInput.Inline = new ContainerInline(); var result = (Header)converter.Execute(blockInput); Assert.Equal(result.Level, headerLevel); }
private Paragraph ConvertHeadingBlock(HeadingBlock headingBlock) { var inlineConverter = new InlineConverter(Manipulator, UserSettingStyleMap, BaseFolderPathForRelativePath); var oxmlInlineElements = inlineConverter.Convert(headingBlock.Inline); var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Heading, new UserSettingStyleMap.HeadingStyleMapArgs() { Level = headingBlock.Level }); var oxmlHeading = Manipulator.ElementCreator.CreateHeadingElement(oxmlInlineElements, styleId); Manipulator.AdjustImageDimension(oxmlHeading); return(oxmlHeading); }
/// <summary> /// Constructor for parsing the Markdig heading object. /// </summary> /// <param name="headingBlock">The Markdig heading object. Note that this is an AST object.</param> public Heading(HeadingBlock headingBlock) { Title = string.Empty; for (var inline = headingBlock.Inline.FirstChild; inline != null; inline = inline.NextSibling) { if (inline is CodeInline codeInline) { Title += codeInline.Content; } else { Title += inline.ToString(); } } SanitizeTitle(); }
private View Render(HeadingBlock block) { View heading; switch (block.Level) { case 1: heading = Heading1Template.CreateContent() as View; break; case 2: heading = Heading2Template.CreateContent() as View; break; case 3: heading = Heading3Template.CreateContent() as View; break; case 4: heading = Heading4Template.CreateContent() as View; break; case 5: heading = Heading5Template.CreateContent() as View; break; case 6: heading = Heading6Template.CreateContent() as View; break; default: throw new NotImplementedException("Header levels 7+ are not implemented."); } heading.BindingContext = new Templates.HeadingAstNode { FormattedText = CreateFormatted(block.Inline) }; AttachLinks(heading); return(heading); }
private static string TranslateHeadingBlock(HeadingBlock headingBlock, int nestLevel) { var headingLevelMarkText = new string('#', headingBlock.Level); string headingBlockText; (var plainInlineText, var shouldBeTranslate) = GetTextToTranslate(headingBlock.Inline); if (shouldBeTranslate) { var result = TranslatorClient.Translate(plainInlineText, "ja", "en"); headingBlockText = string.Format("{0} {1}", headingLevelMarkText, result.Result); } else { headingBlockText = string.Format("{0} {1}", headingLevelMarkText, plainInlineText); } return(GetIndentWhitespaces(nestLevel) + headingBlockText); }
protected override void Write(HtmlRenderer renderer, HeadingBlock heading) { // Clone the heading block, and increment the level var hackHeading = new HeadingBlock(heading.Parser) { Level = heading.Level + 1, Column = heading.Column, HeaderChar = heading.HeaderChar, Inline = heading.Inline, IsBreakable = heading.IsBreakable, IsOpen = heading.IsOpen, Line = heading.Line, Lines = heading.Lines, ProcessInlines = heading.ProcessInlines, RemoveAfterProcessInlines = heading.RemoveAfterProcessInlines, Span = heading.Span, }; hackHeading.SetAttributes(heading.GetAttributes()); base.Write(renderer, hackHeading); }