using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
namespace InABox.Core
{
public static class HTMLUtils
{
///
{(string.IsNullOrWhiteSpace(l) ? " " : l)}
").ToList(); while (string.Equals(lines.LastOrDefault(),$"")) lines.RemoveAt(lines.Count-1); result = $"{string.Join("\n",lines)}"; } var doc = new HtmlDocument(); doc.LoadHtml(result); HtmlNode? head = null; var htmlnode = doc.DocumentNode.SelectSingleNode("//html"); if (!htmlnode.ChildNodes.Any(x => string.Equals(x.Name, "head", StringComparison.CurrentCultureIgnoreCase))) { head = doc.CreateElement("head"); htmlnode.PrependChild(head); } head = doc.DocumentNode.SelectSingleNode("//head"); if (head != null) { HtmlNode? style = null; if (!head.ChildNodes.Any(x => string.Equals(x.Name, "style", StringComparison.CurrentCultureIgnoreCase))) { style = doc.CreateElement("style"); head.PrependChild(style); } else style = head.ChildNodes["style"]; style.InnerHtml = "p { margin: 0px 0px 0px 0px; }"; } return doc.DocumentNode.OuterHtml; } ///
]*>", "" }, { "
]*>", "\r\n" }, }; var doc = new HtmlDocument(); doc.LoadHtml(html); ProcessNode(doc.DocumentNode, (n) => ReplaceAttribute(n, "font-style","italic", "i")); ProcessNode(doc.DocumentNode, (n) => ReplaceAttribute(n, "text-decoration","underline", "u")); ProcessNode(doc.DocumentNode, (n) => ReplaceAttribute(n, "font-weight","bold", "b")); ProcessNode(doc.DocumentNode, (n) => ReplaceAttribute(n, "font-size","(.*?)", "")); ProcessNode(doc.DocumentNode, (n) => ReplaceAttribute(n, "font-family","(.*?)", "")); ProcessNode(doc.DocumentNode, (n) => ReplaceAttribute(n, "background","(.*?)", "")); string result = doc.DocumentNode.SelectSingleNode("//body").InnerHtml; foreach (var _key in _replacements.Keys) result = Regex.Replace(result, _key, _replacements[_key]); var lines = result.Split("\r\n").ToList(); while (string.IsNullOrWhiteSpace(lines.LastOrDefault())) lines.RemoveAt(lines.Count-1); return string.Join("\r\n", lines); } private static void ProcessNode(HtmlNode node, Action