|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace QuickHelp.Converters |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Provides methods to convert help topics into HTML format. |
| 9 | + /// </summary> |
| 10 | + public class HtmlConverter |
| 11 | + { |
| 12 | + public static readonly HtmlConverter Default = new HtmlConverter |
| 13 | + { |
| 14 | + AutoFixHyperlinks = true, |
| 15 | + }; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Gets or sets a flag indicating whether to automatically fix |
| 19 | + /// hyperlinks according to one of the following rules: |
| 20 | + /// - If the hyperlink ends with ►, does not start with ◄, and is not |
| 21 | + /// a single ►, the ending ► is excluded from the hyperlink. |
| 22 | + /// </summary> |
| 23 | + public bool AutoFixHyperlinks { get; set; } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Converts the given help topic into HTML format. |
| 27 | + /// </summary> |
| 28 | + /// <param name="topic">The help topic to convert.</param> |
| 29 | + /// <returns>The HTML source for the topic.</returns> |
| 30 | + public string ConvertTopic(HelpTopic topic) |
| 31 | + { |
| 32 | + if (topic == null) |
| 33 | + throw new ArgumentNullException("topic"); |
| 34 | + |
| 35 | + StringBuilder html = new StringBuilder(); |
| 36 | + html.AppendFormat("<html><head><title>{0}</title></head>\r\n", |
| 37 | + Escape(topic.Title)); |
| 38 | + html.AppendLine("<body><pre>"); |
| 39 | + |
| 40 | + foreach (HelpLine line in topic.Lines) |
| 41 | + { |
| 42 | + FormatLine(html, topic, line); |
| 43 | + } |
| 44 | + |
| 45 | + html.AppendLine("</pre></body>"); |
| 46 | + html.AppendLine("</html>"); |
| 47 | + return html.ToString(); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Converts a help line into HTML format. |
| 52 | + /// </summary> |
| 53 | + /// <returns>HTML representation of the line.</returns> |
| 54 | + /// <remarks> |
| 55 | + /// This method does not guarantee that the generated html is XML |
| 56 | + /// conformant. Consider the following example: |
| 57 | + /// ...<b>...<i>...</b>...</i>... |
| 58 | + /// If XML conformance is required, this would have to be changed |
| 59 | + /// into |
| 60 | + /// ...<b>...<i>...</i></b><i>...</i>... |
| 61 | + /// While this could be done, it adds complexity and increases output |
| 62 | + /// size with no added value, since every web browser can handle the |
| 63 | + /// previous markup as expected. |
| 64 | + /// |
| 65 | + /// For a formal discussion about this issue, see the HTML 5 section |
| 66 | + /// http://www.w3.org/html/wg/drafts/html/master/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser |
| 67 | + /// </remarks> |
| 68 | + private void FormatLine(StringBuilder html, HelpTopic topic, HelpLine line) |
| 69 | + { |
| 70 | + TextAttribute oldAttrs = TextAttribute.Default; |
| 71 | + |
| 72 | + for (int i = 0; i < line.Text.Length; i++) |
| 73 | + { |
| 74 | + TextAttribute newAttrs = line.Attributes[i]; |
| 75 | + TextStyle addedStyles = newAttrs.Style & ~oldAttrs.Style; |
| 76 | + TextStyle removedStyles = oldAttrs.Style & ~newAttrs.Style; |
| 77 | + |
| 78 | + if (removedStyles != TextStyle.None) |
| 79 | + FormatRemovedStyles(html, removedStyles); |
| 80 | + if (addedStyles != TextStyle.None) |
| 81 | + FormatAddedStyles(html, addedStyles); |
| 82 | + |
| 83 | + if (AutoFixHyperlinks) |
| 84 | + { |
| 85 | + if (line.Text[i] == '►' && |
| 86 | + newAttrs.Link != null && |
| 87 | + (i == line.Length - 1 || line.Attributes[i + 1].Link == null)) |
| 88 | + { |
| 89 | + newAttrs = new TextAttribute(newAttrs.Style, null); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (newAttrs.Link != oldAttrs.Link) |
| 94 | + { |
| 95 | + if (oldAttrs.Link != null) |
| 96 | + html.Append("</a>"); |
| 97 | + if (newAttrs.Link != null) |
| 98 | + { |
| 99 | + html.AppendFormat("<a href=\"{0}\">", |
| 100 | + ConvertLink(topic, newAttrs.Link)); |
| 101 | + } |
| 102 | + } |
| 103 | + html.Append(Escape("" + line.Text[i])); |
| 104 | + oldAttrs = line.Attributes[i]; |
| 105 | + } |
| 106 | + |
| 107 | + // Reset styles and links at end of line. |
| 108 | + if (oldAttrs.Link != null) |
| 109 | + html.Append("</a>"); |
| 110 | + if (oldAttrs.Style != TextStyle.None) |
| 111 | + FormatRemovedStyles(html, oldAttrs.Style); |
| 112 | + |
| 113 | + html.AppendLine(); |
| 114 | + } |
| 115 | + |
| 116 | + protected virtual string ConvertLink(HelpTopic topic, HelpUri link) |
| 117 | + { |
| 118 | + return "?" + Escape(link.ToString()); |
| 119 | + } |
| 120 | + |
| 121 | + private static void FormatAddedStyles( |
| 122 | + StringBuilder html, TextStyle change) |
| 123 | + { |
| 124 | + if ((change & TextStyle.Bold) != 0) |
| 125 | + html.Append("<b>"); |
| 126 | + if ((change & TextStyle.Italics) != 0) |
| 127 | + html.Append("<i>"); |
| 128 | + if ((change & TextStyle.Underline) != 0) |
| 129 | + html.Append("<u>"); |
| 130 | + } |
| 131 | + |
| 132 | + private static void FormatRemovedStyles( |
| 133 | + StringBuilder html, TextStyle change) |
| 134 | + { |
| 135 | + if ((change & TextStyle.Bold) != 0) |
| 136 | + html.Append("</b>"); |
| 137 | + if ((change & TextStyle.Italics) != 0) |
| 138 | + html.Append("</i>"); |
| 139 | + if ((change & TextStyle.Underline) != 0) |
| 140 | + html.Append("</u>"); |
| 141 | + } |
| 142 | + |
| 143 | + public static string Escape(string s) |
| 144 | + { |
| 145 | + return System.Security.SecurityElement.Escape(s); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments