/// <summary> /// Gets the font attribute and combine with the style, size and family. /// </summary> public HtmlFont GetAsFont(String name) { HtmlFont font = HtmlFont.Parse(this[name]); string attrValue = this[name + "-style"]; if (attrValue != null) { var style = Converter.ToFontStyle(attrValue); if (style.HasValue) { font.Style = style.Value; } } attrValue = this[name + "-variant"]; if (attrValue != null) { var variant = Converter.ToFontVariant(attrValue); if (variant.HasValue) { font.Variant = variant.Value; } } attrValue = this[name + "-weight"]; if (attrValue != null) { var weight = Converter.ToFontWeight(attrValue); if (weight.HasValue) { font.Weight = weight.Value; } } attrValue = this[name + "-family"]; if (attrValue != null) { font.Family = Converter.ToFontFamily(attrValue); } Unit unit = this.GetAsUnit(name + "-size"); if (unit.IsValid) { font.Size = unit; } return(font); }
public static string Process(string html) { // Check for web parts - these cannot be migrated HtmlHelper.FindWebParts(html); // TODO: Check for references of /Documents folder - these files are not migrated //Replace space characters string result = html.Replace("\u200B", "").Replace(" ", " "); result = result.Replace("size=\"+0\"", ""); result = result.Replace("<s>", "~~").Replace("</s>", "~~"); //Convert Span Highlights result = HtmlSpan.Process(result); //Convert embedded YouTube videos to markdown result = Youtube.convertYoutubeVideos(result); result = Greybox.Process(result); //Remove unhandled tags result = HtmlDescriptionList.Process(result); result = HtmlDescriptionDetails.Process(result); //Remove nodes, but keep the child nodes result = HtmlHelper.RemoveNode(result, "dl", true); result = HtmlHelper.RemoveNode(result, "dt", true); result = HtmlHelper.RemoveNode(result, "dd", true); result = HtmlHelper.ConvertTagsInPre(result); result = HtmlFont.Process(result); //Remove leading and trailing whitespace result = TrimWhitespaceAroundBoldText(result); result = HtmlHelper.RemoveNode(result, "dl", true); return(result); }
/// <summary> /// Converts some common styling attributes to their OpenXml equivalence. /// </summary> /// <param name="en">The Html parser.</param> /// <param name="styleAttributes">The collection of attributes where to store new discovered attributes.</param> public void ProcessCommonAttributes(HtmlEnumerator en, IList <OpenXmlElement> styleAttributes) { if (en.Attributes.Count == 0) { return; } var colorValue = en.StyleAttributes.GetAsColor("color"); if (colorValue.IsEmpty) { colorValue = en.Attributes.GetAsColor("color"); } if (!colorValue.IsEmpty) { styleAttributes.Add(new Color { Val = colorValue.ToHexString() }); } colorValue = en.StyleAttributes.GetAsColor("background-color"); if (!colorValue.IsEmpty) { // change the way the background-color renders. It now uses Shading instead of Highlight. // Changes brought by Wude on http://html2openxml.codeplex.com/discussions/277570 styleAttributes.Add(new Shading { Val = ShadingPatternValues.Clear, Fill = colorValue.ToHexString() }); } var decorations = Converter.ToTextDecoration(en.StyleAttributes["text-decoration"]); if ((decorations & TextDecoration.Underline) != 0) { styleAttributes.Add(new Underline { Val = UnderlineValues.Single }); } if ((decorations & TextDecoration.LineThrough) != 0) { styleAttributes.Add(new Strike()); } String[] classes = en.Attributes.GetAsClass(); if (classes != null) { for (int i = 0; i < classes.Length; i++) { string className = documentStyle.GetStyle(classes[i], StyleValues.Character, ignoreCase: true); if (className != null) // only one Style can be applied in OpenXml and dealing with inheritance is out of scope { styleAttributes.Add(new RunStyle() { Val = className }); break; } } } HtmlFont font = en.StyleAttributes.GetAsFont("font"); if (!font.IsEmpty) { if (font.Style == FontStyle.Italic) { styleAttributes.Add(new Italic()); } if (font.Weight == FontWeight.Bold || font.Weight == FontWeight.Bolder) { styleAttributes.Add(new Bold()); } if (font.Variant == FontVariant.SmallCaps) { styleAttributes.Add(new SmallCaps()); } if (font.Family != null) { styleAttributes.Add(new RunFonts() { Ascii = font.Family, HighAnsi = font.Family }); } // size are half-point font size if (font.Size.IsFixed) { styleAttributes.Add(new FontSize() { Val = (font.Size.ValueInPoint * 2).ToString(CultureInfo.InvariantCulture) }); } } }