private bool TryParse(string markdown, out LinkInline link, bool?includeTrailingCharacters = null, char?whiteSpaceUrlChar = null) { var pr = BuidProcessor(); var sl = new StringSlice(markdown); var p = new WikiLinkParser() { Extension = ".html" }; if (includeTrailingCharacters != null) { p.IncludeTrailingCharacters = includeTrailingCharacters.Value; } if (whiteSpaceUrlChar != null) { p.WhiteSpaceUrlChar = whiteSpaceUrlChar.Value; } var b = p.Match(pr, ref sl); if (b) { link = (LinkInline)pr.Inline; } else { link = null; } return(b); }
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); }
public UserLinkText(string text, LinkInline linkInline) : base(text, linkInline) { Padding = new MarginPadding { Bottom = 2 }; }
public void ExtractCiteUrl_ExtractsCiteUrlIfSuccessful() { // Arrange const string dummyUrl = "dummyLink"; var dummyAuthorLinkInline = new LinkInline(); var dummyCiteLinkInline = new LinkInline(dummyUrl, null); var dummyContainerInline = new ContainerInline(); dummyContainerInline. AppendChild(dummyAuthorLinkInline). AppendChild(dummyCiteLinkInline); LeafBlock dummyCitationBlock = _mockRepository.Create <LeafBlock>(null).Object; InlineProcessor dummyInlineProcessor = MarkdigTypesFactory.CreateInlineProcessor(); dummyInlineProcessor.ProcessInlineLeaf(dummyCitationBlock); // Sets InlineProcessor.Block to dummyCitationBlock dummyCitationBlock.Inline = dummyContainerInline; // Replace container created in ProcessInlineLeaf FlexiQuoteBlock dummyFlexiQuoteBlock = CreateFlexiQuoteBlock(); dummyFlexiQuoteBlock.Add(dummyCitationBlock); Mock <FlexiQuoteBlockFactory> mockTestSubject = CreateMockFlexiQuoteBlockFactory(); mockTestSubject.Setup(m => m.NormalizeCiteLinkIndex(2, dummyFlexiQuoteBlock)).Returns(1); // Act mockTestSubject.Object.ExtractCiteUrl(dummyInlineProcessor, null); // Assert _mockRepository.VerifyAll(); Assert.Equal(dummyUrl, dummyFlexiQuoteBlock.CiteUrl); }
/// <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 MarkdownLinkText(string text, LinkInline linkInline) { this.text = text; url = linkInline.Url ?? string.Empty; AutoSizeAxes = Axes.Both; }
private OpenXmlElement ConvertLinkInline(LinkInline linkInline, bool isBoldInherited = false, bool isItalicInherited = false) { if (linkInline.IsImage) { var explicitAbsoluteImagePath = GetExplicitAbsoluteImagePath(linkInline.Url, BaseFolderPathForRelativePath); var relationshipId = AddImagePartFromFile(explicitAbsoluteImagePath); // At this time, temporary uses the original image dimension. // In later, adjust the image dimension using the page settings and inherited indentation. (var originalImageWidthInch, var originalImageHeightInch) = Manipulator.GetImageDimensionInInch(relationshipId); var originalImageWidthEmu = WordDocumentManipulator.UnitConverter.InchToEmu(originalImageWidthInch); var originalImageHeightEmu = WordDocumentManipulator.UnitConverter.InchToEmu(originalImageHeightInch); var fileName = Path.GetFileName(explicitAbsoluteImagePath); var altText = GetLinkText(linkInline); return(Manipulator.ElementCreator.CreateImageElement(relationshipId, (long)originalImageWidthEmu, (long)originalImageHeightEmu, fileName, altText)); } else { var linkText = GetLinkText(linkInline); var hyperlinkUri = GetLinkTargetUri(linkInline.Url); var hyperlinkRelationshipId = Manipulator.AddHyperlinkRelationship(hyperlinkUri); var styleId = UserSettingStyleMap.GetStyleId(UserSettingStyleMap.StyleMapKeyType.Hyperlink, null); return(Manipulator.ElementCreator.CreateHyperlinkElement(linkText, hyperlinkRelationshipId, isBoldInherited, isItalicInherited, styleId)); } }
public TextLinkX(LinkInline linkInline) { this.HAnchor = HAnchor.Fit; this.VAnchor = VAnchor.Fit; this.Cursor = Cursors.Hand; this.linkInline = linkInline; }
private bool TryGuessAudioVideoFile(Uri uri, HtmlRenderer renderer, LinkInline linkInline) { var path = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped); // Otherwise try to detect if we have an audio/video from the file extension string mimeType; var lastDot = path.LastIndexOf('.'); if (lastDot >= 0 && Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out mimeType)) { var htmlAttributes = GetHtmlAttributes(linkInline); var isAudio = mimeType.StartsWith("audio"); var tagType = isAudio ? "audio" : "video"; renderer.Write($"<{tagType}"); htmlAttributes.AddPropertyIfNotExist("width", Options.Width); if (!isAudio) { htmlAttributes.AddPropertyIfNotExist("height", Options.Height); } htmlAttributes.AddPropertyIfNotExist("controls", null); renderer.WriteAttributes(htmlAttributes); renderer.Write($"><source type=\"{mimeType}\" src=\"{linkInline.Url}\"></source></{tagType}>"); return(true); } return(false); }
private static string GetTextToTranslateFromLinkInline(LinkInline linkInline, bool needOriginalMarkdown) { var plainInlineText = new StringBuilder(); foreach (var inline in linkInline) { switch (inline) { case LiteralInline literalInline: plainInlineText.Append(GetTextToTranslateFromLiteralInline(literalInline)); break; default: throw new NotImplementedException(string.Format("Unknown inline type: {0}", inline.GetType().FullName)); } } if (needOriginalMarkdown) { return(string.Format("{0}[{1}]({2})", linkInline.IsImage ? "!" : "", plainInlineText.ToString(), linkInline.Url)); } else { return(plainInlineText.ToString()); } }
private static void TransformUrl(MarkdownObject markdownObject, string currentDir) { foreach (MarkdownObject child in markdownObject.Descendants()) { // LinkInline can be both an image or a <a href="..."> LinkInline link = child as LinkInline; if (link != null) { string url = link.Url; if (!urlRegex.IsMatch(url)) { try { if (!Path.IsPathRooted(url) && currentDir != null) { url = Path.GetFullPath(Path.Combine(currentDir, url)).Replace('\\', '/'); } url = "file:///" + url; link.Url = url; } catch (Exception) { return; } } } TransformUrl(child, currentDir); } }
private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline) { if (!linkInline.IsImage || linkInline.Url == null) { return(false); } Uri uri; // Only process absolute Uri if (!Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out uri) || !uri.IsAbsoluteUri) { return(false); } if (TryRenderIframeFromKnownProviders(uri, renderer, linkInline)) { return(true); } if (TryGuessAudioVideoFile(uri, renderer, linkInline)) { return(true); } return(false); }
public void ParseLink_should_return_RemoteLink_when_url_starts_with_www() { var linkInline = new LinkInline("www.google.com", "Title"); var link = SimplifiedMarkdownDoc.ParseLink(linkInline); Assert.IsInstanceOf <RemoteLink>(link); }
public void ParseLink_should_return_LocalLink_when_not_a_remote_url() { var linkInline = new LinkInline("../README.md", "Title"); var link = SimplifiedMarkdownDoc.ParseLink(linkInline); Assert.IsInstanceOf <LocalLink>(link); }
public void RemoteLink_should_assign_the_url_correctly() { var linkInline = new LinkInline("https://www.google.com", "Title"); var remoteLink = new RemoteLink(linkInline); Assert.AreEqual(linkInline.Url, remoteLink.Url); }
private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline) { if (!linkInline.IsImage || linkInline.Url == null) { return(false); } var figureElement = new XElement( "figure", new XElement( "img", new XAttribute("src", _options.Host + linkInline.Url)), new XElement("figcaption", linkInline.Title)); renderer.Write(figureElement.ToString()); _options.RenderedLinks.Add(linkInline); return(true); /* * * * * <figure> * <img src="http://example.com/pic1.jpg" width="1200" height="900"> * <figcaption> * Первый андроид-фермер смотрит на свои угодья * </figcaption> * </figure> */ }
protected override void Write(HtmlRenderer renderer, LinkInline link) { if (link.Url != null && link.Url.EndsWith(".md")) { link.Url = link.Url.Substring(0, link.Url.Length - 3) + ".html"; } base.Write(renderer, link); }
public void LocalLink_should_correctly_parse_other_markdown_file_reference() { var linkInline = new LinkInline("../README.md#my-heading", "Title"); var localLink = new LocalLink(linkInline); Assert.IsTrue(localLink.Heading.HasValue); Assert.IsNotNull(localLink.FilePath); }
public void LocalLink_should_correctly_parse_in_file_reference() { var linkInline = new LinkInline("#other-heading-in-doc", "Title"); var localLink = new LocalLink(linkInline); Assert.IsTrue(localLink.Heading.HasValue); Assert.IsNull(localLink.FilePath); }
public TextLinkX(AggRenderer renderer, string url, LinkInline linkInline) { this.HAnchor = HAnchor.Fit; this.VAnchor = VAnchor.Fit; this.Cursor = Cursors.Hand; this.linkInline = linkInline; this.url = url; this.aggRenderer = renderer; }
protected override void Write(Renderers.WpfRenderer renderer, LinkInline link) { if (link.IsImage) { link.Url = _linkpath + link.Url; } base.Write(renderer, link); }
protected override void Write(Renderers.WpfRenderer renderer, LinkInline link) { if (link?.IsImage ?? throw new ArgumentNullException(nameof(link))) { link.Url = _linkpath + link.Url; } base.Write(renderer, link); }
public WikiMarkdownImageBlock(LinkInline linkInline) { this.linkInline = linkInline; RelativeSizeAxes = Axes.X; AutoSizeAxes = Axes.Y; Direction = FillDirection.Vertical; Spacing = new Vector2(0, 3); }
/// <summary> /// A helper function that parses the Markdig link object and returns the correct type of link - remote or local. /// </summary> /// <param name="linkInline">The object from Markdig to parse.</param> /// <returns></returns> public static ILink ParseLink(LinkInline linkInline) { if (linkInline.Url.StartsWith("http") || linkInline.Url.StartsWith("www")) { return(new RemoteLink(linkInline)); } return(new LocalLink(linkInline)); }
protected override void AddLinkText(string text, LinkInline linkInline) { if (githubUrls.ContainsKey(text)) { var baseUri = new Uri(githubUrls[text]); // Get hash tag with number const string pattern = @"(\#[0-9]+\b)(?!;)"; var issueOrRequests = Regex.Matches(linkInline.Url, pattern, RegexOptions.IgnoreCase); if (!issueOrRequests.Any()) { return; } AddText("("); foreach (var issue in issueOrRequests.Select(x => x.Value)) { AddDrawable(new MarkdownLinkText($"{text}{issue}", new LinkInline { Url = new Uri(baseUri, $"pull/{issue.Replace("#", "")}").AbsoluteUri })); if (issue != issueOrRequests.LastOrDefault()?.Value) { AddText(", "); } } AddText(")"); // add use name if has user var user = linkInline.Url.Split('@').LastOrDefault(); if (!string.IsNullOrEmpty(user)) { var textScale = new Vector2(0.7f); AddText(" by:", text => { text.Scale = textScale; }); AddDrawable(new MarkdownLinkText(user, new LinkInline { Url = $"https://github.com/{user}" }) { Scale = textScale, }); } } else { base.AddLinkText(text, linkInline); } }
public void LocalLink_should_correctly_parse_other_file_reference() { var linkInline = new LinkInline("../README.md", "Title"); var localLink = new LocalLink(linkInline); Assert.IsFalse(localLink.Heading.HasValue); Assert.IsNotNull(localLink.FilePath); Assert.AreEqual(linkInline.Url, localLink.FilePath); }
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 LinkInlineRenderer(null); inline = new LinkInline(); inline.Url = "sample link"; }
private static void AddClass(LinkInline link, string cssClass) { HtmlAttributes attributes = link.GetAttributes(); if (!attributes.Classes.Any(x => x == cssClass)) { attributes.Classes.Add(cssClass); link.SetAttributes(attributes); } }
private static void AddAttribute(LinkInline link, string name, string value) { HtmlAttributes attributes = link.GetAttributes(); if (!attributes.Properties.Any(x => x.Key == name)) { attributes.AddPropertyIfNotExist(name, value); link.SetAttributes(attributes); } }
private static bool IsOffHostLink(LinkInline link, IReadonlyRuntimeSettings RuntimeConfig) { if (RuntimeConfig.Configuration != null) { return(!link.Url?.StartsWith(RuntimeConfig.Configuration.HostName) ?? false); } else { return(true); } }