|
| 1 | +// "Therefore those skilled at the unorthodox |
| 2 | +// are infinite as heaven and earth, |
| 3 | +// inexhaustible as the great rivers. |
| 4 | +// When they come to an end, |
| 5 | +// they begin again, |
| 6 | +// like the days and months; |
| 7 | +// they die and are reborn, |
| 8 | +// like the four seasons." |
| 9 | +// |
| 10 | +// - Sun Tsu, |
| 11 | +// "The Art of War" |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.IO; |
| 16 | +using System.Text; |
| 17 | +using HtmlKit; |
| 18 | + |
| 19 | +namespace TheArtOfDev.HtmlRenderer.Demo.Common |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Utility to format HTML into a readable representation while preserving semantic structure. |
| 23 | + /// </summary> |
| 24 | + public static class HtmlPrettyPrinter |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// html tags that should be written with indentation on separate lines |
| 28 | + /// </summary> |
| 29 | + private static readonly HashSet<string> _blockTags = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| 30 | + { |
| 31 | + "article", "aside", "blockquote", "body", "caption", "colgroup", "dd", "div", "dl", "dt", "fieldset", "figcaption", |
| 32 | + "figure", "footer", "form", "head", "header", "html", "li", "main", "nav", "ol", "p", "section", "table", "tbody", |
| 33 | + "td", "tfoot", "th", "thead", "tr", "ul", "title" |
| 34 | + }; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// html tags that don't have a closing tag and should not change indentation depth |
| 38 | + /// </summary> |
| 39 | + private static readonly HashSet<string> _voidTags = new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| 40 | + { |
| 41 | + "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr" |
| 42 | + }; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Format the given html into a readable left-to-right comparable layout. |
| 46 | + /// </summary> |
| 47 | + public static string Format(string html) |
| 48 | + { |
| 49 | + if (string.IsNullOrWhiteSpace(html)) |
| 50 | + return string.Empty; |
| 51 | + |
| 52 | + var builder = new StringBuilder(html.Length * 2); |
| 53 | + using (var reader = new StringReader(html)) |
| 54 | + using (var writer = new StringWriter(builder)) |
| 55 | + { |
| 56 | + var tokenizer = new HtmlTokenizer(reader); |
| 57 | + HtmlToken token; |
| 58 | + int indent = 0; |
| 59 | + |
| 60 | + while (tokenizer.ReadNextToken(out token)) |
| 61 | + { |
| 62 | + switch (token.Kind) |
| 63 | + { |
| 64 | + case HtmlTokenKind.DocType: |
| 65 | + case HtmlTokenKind.Comment: |
| 66 | + AppendTokenLine(builder, writer, token, indent); |
| 67 | + break; |
| 68 | + |
| 69 | + case HtmlTokenKind.Tag: |
| 70 | + var tag = (HtmlTagToken)token; |
| 71 | + var isBlockTag = _blockTags.Contains(tag.Name); |
| 72 | + var isVoidTag = tag.IsEmptyElement || _voidTags.Contains(tag.Name); |
| 73 | + |
| 74 | + if (tag.IsEndTag) |
| 75 | + { |
| 76 | + if (isBlockTag) |
| 77 | + { |
| 78 | + indent = Math.Max(0, indent - 1); |
| 79 | + AppendTokenLine(builder, writer, token, indent); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + token.WriteTo(writer); |
| 84 | + } |
| 85 | + } |
| 86 | + else if (isBlockTag) |
| 87 | + { |
| 88 | + AppendTokenLine(builder, writer, token, indent); |
| 89 | + if (!isVoidTag) |
| 90 | + indent++; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + token.WriteTo(writer); |
| 95 | + } |
| 96 | + break; |
| 97 | + |
| 98 | + case HtmlTokenKind.CData: |
| 99 | + case HtmlTokenKind.Data: |
| 100 | + case HtmlTokenKind.ScriptData: |
| 101 | + var data = ((HtmlDataToken)token).Data; |
| 102 | + if (!string.IsNullOrWhiteSpace(data)) |
| 103 | + { |
| 104 | + if (IsAtLineStart(builder)) |
| 105 | + AppendIndent(builder, indent); |
| 106 | + builder.Append(data); |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return builder.ToString().Trim(); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Append the given html token on its own line using the given indentation level. |
| 118 | + /// </summary> |
| 119 | + private static void AppendTokenLine(StringBuilder builder, StringWriter writer, HtmlToken token, int indent) |
| 120 | + { |
| 121 | + if (!IsAtLineStart(builder)) |
| 122 | + builder.AppendLine(); |
| 123 | + |
| 124 | + AppendIndent(builder, indent); |
| 125 | + token.WriteTo(writer); |
| 126 | + builder.AppendLine(); |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Append indentation spaces to the string builder. |
| 131 | + /// </summary> |
| 132 | + private static void AppendIndent(StringBuilder builder, int indent) |
| 133 | + { |
| 134 | + builder.Append(' ', indent * 2); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Return if the builder currently points at the start of a new line. |
| 139 | + /// </summary> |
| 140 | + private static bool IsAtLineStart(StringBuilder builder) |
| 141 | + { |
| 142 | + return builder.Length == 0 || builder[builder.Length - 1] == '\n'; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments