private static Inline GenerateItalic(HtmlNode node) { Italic i = new Italic(); AddChildren(i, node); return i; }
public virtual void RenderElement(IElement element, ITextContainer parent, RenderContextBase context) { var italic = new Italic(); parent.Add(italic); context.RenderNode(element, new SpanContainer(italic)); }
private static Inline GenerateItalic(HtmlNode node) { Debug.WriteLine("Properties >>>>>> GenerateItalic"); Italic i = new Italic(); AddChildren(i, node); return i; }
private static void ParseElement(XmlElement element, ITextContainer parent) { foreach (var child in element.ChildNodes) { var tagNameToUpper = (child as XmlElement)?.TagName?.ToUpper(); if (!isInP && !(child is XmlElement && (tagNameToUpper == "P" || tagNameToUpper == "SPAN" || tagNameToUpper == "DIV" || tagNameToUpper == "IMG"))) { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; parent = new ParagraphTextContainer(parent, paragraph); } if (child is XmlText) { var text = child.InnerText.Replace("\n", "").Trim(); if (string.IsNullOrEmpty(text)) continue; text = text.Replace("</form>", ""); parent.Add(new Run { Text = System.Net.WebUtility.HtmlDecode(text), Foreground = new SolidColorBrush(Windows.UI.Colors.Black) }); } else if (child is XmlElement) { XmlElement e = (XmlElement)child; switch (tagNameToUpper) { case "P": case "DIV": case "SPAN": if(tagNameToUpper == "DIV") { var classVal = e.Attributes.FirstOrDefault(a => a.NodeName == "class")?.NodeValue?.ToString() ?? ""; if(classVal.Contains("open-list-comments") || classVal.Contains("open-list-footer") || classVal.Contains("modal-body")) { return; } } if (isInP) { if (tagNameToUpper == "P") parent.Add(new LineBreak()); ParseElement(e, parent); } else { var paragraph = new Paragraph(); parent.Add(paragraph); isInP = true; ParseElement(e, new ParagraphTextContainer(parent, paragraph)); isInP = false; } break; case "B": case "STRONG": case "H1": case "H2": case "H3": case "H4": case "H5": case "H6": var bold = new Bold(); parent.Add(bold); ParseElement(e, new SpanTextContainer(parent, bold)); break; case "I": case "EM": var italic = new Italic(); parent.Add(italic); ParseElement(e, new SpanTextContainer(parent, italic)); break; case "U": var underline = new Underline(); parent.Add(underline); ParseElement(e, new SpanTextContainer(parent, underline)); break; case "A": var url = e.Attributes.FirstOrDefault(a => a.NodeName == "href")?.NodeValue; Uri uri; if (url != null && Uri.TryCreate(url.ToString(), UriKind.Absolute, out uri)) { var link = new Hyperlink(); link.NavigateUri = uri; parent.Add(link); ParseElement(e, new SpanTextContainer(parent, link)); } else { ParseElement(e, parent); } break; case "BR": parent.Add(new LineBreak()); break; case "IMG": var src = e.Attributes.FirstOrDefault(a => a.NodeName == "src")?.NodeValue; Uri srcUri; if (src != null && Uri.TryCreate(src.ToString(), UriKind.Absolute, out srcUri)) { var img = new InlineUIContainer { Child = new Windows.UI.Xaml.Controls.Image() { Source = new BitmapImage(srcUri) } }; parent.AddToParentParagraph(img); } break; } } } }
/// <summary> /// Converts an HTML node into an Inline. /// </summary> private static Inline ToInline( HtmlNode node ) { switch ( node.Name ) { case "br": return new LineBreak(); case "a": string url = node.GetAttributeValue( "href", "" ); if ( string.IsNullOrWhiteSpace( url ) ) // happens sometimes... broken HTML... { break; } string text = node.InnerText; if ( string.IsNullOrWhiteSpace( text ) ) { text = url; } var link = new Hyperlink { Inlines = { new Run { Text = text, Foreground = new SolidColorBrush( Colors.Blue ) } } }; link.Click += ( _, __ ) => LauncherEx.Launch( new Uri( url, UriKind.Absolute ) ); return new Span { Inlines = { new Run { Text = " " }, link, new Run { Text = " " }, } }; case "strong": case "b": var bold = new Bold(); foreach ( var child in node.ChildNodes ) { bold.Inlines.Add( ToInline( child ) ); } return bold; case "em": case "i": var italic = new Italic(); foreach ( var child in node.ChildNodes ) { italic.Inlines.Add( ToInline( child ) ); } return italic; case "ul": var unorderedList = new Span(); foreach ( var child in node.ChildNodes ) { var listElem = new Span(); listElem.Inlines.Add( new Run { Text = "● " } ); listElem.Inlines.Add( ToInline( child ) ); unorderedList.Inlines.Add( listElem ); unorderedList.Inlines.Add( new LineBreak() ); } return unorderedList; case "ol": var orderedList = new Span(); for ( int n = 0; n < node.ChildNodes.Count; n++ ) { var listElem = new Span(); listElem.Inlines.Add( new Run { Text = n.ToString() + " " } ); listElem.Inlines.Add( ToInline( node.ChildNodes[n] ) ); orderedList.Inlines.Add( listElem ); orderedList.Inlines.Add( new LineBreak() ); } return orderedList; case "h1": return new Run { Text = node.InnerText + Environment.NewLine, FontWeight = FontWeights.Bold, FontSize = 32 }; case "h2": return new Run { Text = node.InnerText + Environment.NewLine, FontWeight = FontWeights.SemiBold, FontSize = 24 }; case "h3": return new Run { Text = node.InnerText + Environment.NewLine, FontWeight = FontWeights.SemiBold, FontSize = 19 }; case "h4": return new Run { Text = node.InnerText + Environment.NewLine, FontWeight = FontWeights.Medium, FontSize = 17 }; case "h5": return new Run { Text = node.InnerText + Environment.NewLine, FontWeight = FontWeights.Medium, FontSize = 16 }; case "div": case "p": case "blockquote": var container = new Span(); foreach ( var child in node.ChildNodes ) { container.Inlines.Add( ToInline( child ) ); } container.Inlines.Add( new LineBreak() ); container.Inlines.Add( new LineBreak() ); return container; } return new Run { Text = node.PreviousSibling == null ? node.InnerText.TrimStart() : node.InnerText }; }