|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Markdown.MAML.Model.Markdown; |
| 7 | + |
| 8 | +namespace Markdown.MAML.Renderer |
| 9 | +{ |
| 10 | + public class TextRenderer |
| 11 | + { |
| 12 | + private StringBuilder _stringBuilder = new StringBuilder(); |
| 13 | + |
| 14 | + private const string aboutIndentation = " "; |
| 15 | + |
| 16 | + private int _maxLineWidth { get; set; } |
| 17 | + |
| 18 | + public TextRenderer() : this(80) { } |
| 19 | + |
| 20 | + public TextRenderer(int maxLineWidth) |
| 21 | + { |
| 22 | + _maxLineWidth = maxLineWidth; |
| 23 | + } |
| 24 | + |
| 25 | + public string AboutMarkdownToString(DocumentNode document) |
| 26 | + { |
| 27 | + //ensure that all node types in the about topic are handeled. |
| 28 | + List<MarkdownNodeType> AcceptableNodeTypes = new List<MarkdownNodeType>(); |
| 29 | + AcceptableNodeTypes.Add(MarkdownNodeType.Heading); |
| 30 | + AcceptableNodeTypes.Add(MarkdownNodeType.Paragraph); |
| 31 | + AcceptableNodeTypes.Add(MarkdownNodeType.CodeBlock); |
| 32 | + if (document.Children.Any(c => (!AcceptableNodeTypes.Contains(c.NodeType)))) |
| 33 | + { |
| 34 | + throw new NotSupportedException("About Topics can only have heading, parapgrah or code block nodes in their Markdown Model."); |
| 35 | + } |
| 36 | + |
| 37 | + //processes all nodes in order |
| 38 | + foreach (var currentNode in document.Children) |
| 39 | + { |
| 40 | + switch (currentNode.NodeType) |
| 41 | + { |
| 42 | + case MarkdownNodeType.Paragraph: |
| 43 | + ParagraphNode paragraphNode = currentNode as ParagraphNode; |
| 44 | + AddAboutParagraph(paragraphNode); |
| 45 | + break; |
| 46 | + case MarkdownNodeType.Heading: |
| 47 | + HeadingNode headingNode = currentNode as HeadingNode; |
| 48 | + AddAboutHeading(headingNode, document); |
| 49 | + break; |
| 50 | + case MarkdownNodeType.CodeBlock: |
| 51 | + CodeBlockNode codeblockNode = currentNode as CodeBlockNode; |
| 52 | + AddAboutCodeBlock(codeblockNode); |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return RenderCleaner.FullNormalization(_stringBuilder.ToString()); |
| 58 | + } |
| 59 | + |
| 60 | + private void AddAboutCodeBlock(CodeBlockNode codeblockNode) |
| 61 | + { |
| 62 | + string[] lines = codeblockNode.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None); |
| 63 | + |
| 64 | + foreach (string line in lines) |
| 65 | + { |
| 66 | + _stringBuilder.Append(aboutIndentation); |
| 67 | + _stringBuilder.AppendLine(line); |
| 68 | + } |
| 69 | + |
| 70 | + _stringBuilder.AppendLine(); |
| 71 | + } |
| 72 | + |
| 73 | + private void AddAboutHeading(HeadingNode headingNode, DocumentNode document) |
| 74 | + { |
| 75 | + if (document.Children.ElementAt(0) as HeadingNode == headingNode) |
| 76 | + { |
| 77 | + _stringBuilder.AppendLine("TOPIC"); |
| 78 | + } |
| 79 | + else if (headingNode.HeadingLevel == 1) |
| 80 | + { |
| 81 | + _stringBuilder.AppendLine(headingNode.Text.ToUpper()); |
| 82 | + } |
| 83 | + else if (headingNode.HeadingLevel == 2 && document.Children.ElementAt(1) == headingNode) |
| 84 | + { |
| 85 | + _stringBuilder.AppendFormat("{0}{1}{2}{3}", |
| 86 | + aboutIndentation, |
| 87 | + headingNode.Text.ToLower(), |
| 88 | + Environment.NewLine, Environment.NewLine); |
| 89 | + } |
| 90 | + else if (headingNode.HeadingLevel == 2) |
| 91 | + { |
| 92 | + _stringBuilder.AppendLine(headingNode.Text); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + _stringBuilder.AppendFormat("{0}{1}{2}", |
| 97 | + aboutIndentation, |
| 98 | + headingNode.Text.ToUpper(),Environment.NewLine); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void AddAboutParagraph(ParagraphNode paragraphNode) |
| 103 | + { |
| 104 | + foreach (string lineContent in paragraphNode.Spans.Select(span => span.Text)) |
| 105 | + { |
| 106 | + //handles all paragraph lines over 80 characters long and not headers |
| 107 | + if (lineContent.Length > _maxLineWidth - 4) |
| 108 | + { |
| 109 | + WrapAndAppendLines(lineContent, _stringBuilder); |
| 110 | + _stringBuilder.AppendLine(); |
| 111 | + } |
| 112 | + else if (StringComparer.OrdinalIgnoreCase.Equals(lineContent,"\n")) |
| 113 | + { |
| 114 | + _stringBuilder.AppendLine(); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + _stringBuilder.AppendFormat("{0}{1}{2}", |
| 119 | + aboutIndentation, |
| 120 | + lineContent,Environment.NewLine); |
| 121 | + } |
| 122 | + } |
| 123 | + _stringBuilder.AppendLine(); |
| 124 | + } |
| 125 | + |
| 126 | + public void WrapAndAppendLines(string text, StringBuilder sb) |
| 127 | + { |
| 128 | + string SingleSpace = " "; |
| 129 | + |
| 130 | + string[] words = text.Split(' '); |
| 131 | + text = ""; |
| 132 | + |
| 133 | + foreach (string word in words) |
| 134 | + { |
| 135 | + if (word.Contains("\r\n")) |
| 136 | + { |
| 137 | + string[] breakLine = word.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); |
| 138 | + |
| 139 | + foreach (string part in breakLine) |
| 140 | + { |
| 141 | + if (part == breakLine.Last()) |
| 142 | + { |
| 143 | + text += part + SingleSpace; |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + text += part; |
| 148 | + sb.AppendFormat("{0}{1}{2}",aboutIndentation,text,Environment.NewLine); |
| 149 | + text = ""; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + else if (text.Length + word.Length > (this._maxLineWidth - 4)) |
| 154 | + { |
| 155 | + if (StringComparer.OrdinalIgnoreCase.Equals(text.Substring(text.Length - 1),SingleSpace)) |
| 156 | + { |
| 157 | + text = text.Substring(0, text.Length - 1); |
| 158 | + } |
| 159 | + sb.AppendFormat("{0}{1}{2}", aboutIndentation, text, Environment.NewLine); |
| 160 | + |
| 161 | + text = word + SingleSpace; |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + text += word + SingleSpace; |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + if (text.Length > 0 && !StringComparer.OrdinalIgnoreCase.Equals(text,SingleSpace)) |
| 171 | + { |
| 172 | + if (StringComparer.OrdinalIgnoreCase.Equals(text.Substring(text.Length - 1),SingleSpace)) |
| 173 | + { |
| 174 | + text = text.Substring(0, text.Length - 1); |
| 175 | + } |
| 176 | + |
| 177 | + sb.AppendFormat("{0}{1}",aboutIndentation, text); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments