Skip to content

Commit 48e4640

Browse files
committed
Adding a test to confirm help exists on machine.
Making updates based on feedback from @vors
1 parent a41c8ad commit 48e4640

File tree

9 files changed

+324
-291
lines changed

9 files changed

+324
-291
lines changed

build.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mkdir out -ErrorAction SilentlyContinue > $null
2020
cp -Rec -Force src\platyPS out
2121
if (-not (Test-Path out\platyPS\Markdown.MAML.dll) -or
2222
(ls out\platyPS\Markdown.MAML.dll).LastWriteTime -lt (ls $assemblyPath).LastWriteTime)
23-
{
23+
{
2424
cp $assemblyPath out\platyPS
2525
} else {
2626
Write-Host -Foreground Yellow 'Skip Markdown.MAML.dll copying'

src/Markdown.MAML/Markdown.MAML.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="Parser\MarkdownPatternList.cs" />
6868
<Compile Include="Renderer\RenderCleaner.cs" />
6969
<Compile Include="Renderer\MarkdownV2Renderer.cs" />
70+
<Compile Include="Renderer\TextRenderer.cs" />
7071
<Compile Include="Resources\MarkdownStrings.Designer.cs">
7172
<AutoGen>True</AutoGen>
7273
<DesignTime>True</DesignTime>

src/Markdown.MAML/Renderer/Markdownv2Renderer.cs

+1-161
Original file line numberDiff line numberDiff line change
@@ -77,101 +77,7 @@ private string MamlModelToString(MamlCommand mamlCommand, Hashtable yamlHeader,
7777
RenderCleaner.NormalizeQuotesAndDashes(
7878
_stringBuilder.ToString())));
7979
}
80-
81-
public string AboutMarkdownToString(DocumentNode document)
82-
{
83-
//ensure that all node types in the about topic are handeled.
84-
List<MarkdownNodeType> AcceptableNodeTypes = new List<MarkdownNodeType>();
85-
AcceptableNodeTypes.Add(MarkdownNodeType.Heading);
86-
AcceptableNodeTypes.Add(MarkdownNodeType.Paragraph);
87-
AcceptableNodeTypes.Add(MarkdownNodeType.CodeBlock);
88-
if (document.Children.Any(c => (!AcceptableNodeTypes.Contains(c.NodeType)) ))
89-
{
90-
throw new NotSupportedException("About Topics can only have heading, parapgrah or code block nodes in their Markdown Model.");
91-
}
92-
93-
//processes all nodes in order
94-
foreach (var currentNode in document.Children)
95-
{
96-
switch (currentNode.NodeType)
97-
{
98-
case MarkdownNodeType.Paragraph:
99-
ParagraphNode paragraphNode = currentNode as ParagraphNode;
100-
AddAboutParagraph(paragraphNode);
101-
break;
102-
case MarkdownNodeType.Heading:
103-
HeadingNode headingNode = currentNode as HeadingNode;
104-
AddAboutHeading(headingNode, document);
105-
break;
106-
case MarkdownNodeType.CodeBlock:
107-
CodeBlockNode codeblockNode = currentNode as CodeBlockNode;
108-
AddAboutCodeBlock(codeblockNode);
109-
break;
110-
}
111-
}
112-
113-
return RenderCleaner.FullNormalization(_stringBuilder.ToString());
114-
}
115-
116-
private void AddAboutCodeBlock(CodeBlockNode codeblockNode)
117-
{
118-
string[] lines = codeblockNode.Text.Split(new string[] {"\r\n"}, StringSplitOptions.None);
119-
120-
foreach (string line in lines)
121-
{
122-
_stringBuilder.Append(" ");
123-
_stringBuilder.AppendLine(line);
124-
}
125-
126-
_stringBuilder.AppendLine();
127-
}
128-
129-
private void AddAboutHeading(HeadingNode headingNode, DocumentNode document)
130-
{
131-
if (document.Children.ElementAt(0) as HeadingNode == headingNode)
132-
{
133-
_stringBuilder.AppendLine("TOPIC");
134-
}
135-
else if (headingNode.HeadingLevel == 1)
136-
{
137-
_stringBuilder.AppendLine(headingNode.Text.ToUpper());
138-
}
139-
else if (headingNode.HeadingLevel == 2 && document.Children.ElementAt(1) == headingNode)
140-
{
141-
_stringBuilder.AppendLine(" " + headingNode.Text.ToLower() + "\r\n");
142-
}
143-
else if (headingNode.HeadingLevel == 2)
144-
{
145-
_stringBuilder.AppendLine(headingNode.Text);
146-
}
147-
else
148-
{
149-
_stringBuilder.AppendLine(" " + headingNode.Text.ToUpper());
150-
}
151-
}
152-
153-
private void AddAboutParagraph(ParagraphNode paragraphNode)
154-
{
155-
foreach (string lineContent in paragraphNode.Spans.Select(span => span.Text))
156-
{
157-
//handles all paragraph lines over 80 characters long and not headers
158-
if (lineContent.Length > _maxLineWidth - 4)
159-
{
160-
WrapAndAppendLines(lineContent, _stringBuilder);
161-
_stringBuilder.AppendLine();
162-
}
163-
else if (lineContent == "\n")
164-
{
165-
_stringBuilder.AppendLine();
166-
}
167-
else
168-
{
169-
_stringBuilder.AppendLine(" " + lineContent);
170-
}
171-
}
172-
_stringBuilder.AppendLine();
173-
}
174-
80+
17581
private void AddYamlHeader(Hashtable yamlHeader)
17682
{
17783
_stringBuilder.AppendFormat("---{0}", Environment.NewLine);
@@ -629,71 +535,5 @@ public static string GetEscapedMarkdownText(string text)
629535

630536
;
631537
}
632-
633-
public string ReplaceMarkdownHeaders(ref int headerLevel, string text)
634-
{
635-
if (text.Substring(0, 1) == "#")
636-
{
637-
headerLevel++;
638-
text = ReplaceMarkdownHeaders(ref headerLevel, text.Substring(1));
639-
}
640-
641-
return text;
642-
}
643-
644-
public void WrapAndAppendLines(string text, StringBuilder sb)
645-
{
646-
string[] words = text.Split(' ');
647-
648-
text = "";
649-
650-
foreach (string word in words)
651-
{
652-
if (word.Contains("\r\n"))
653-
{
654-
string[] breakLine = word.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
655-
656-
foreach (string part in breakLine)
657-
{
658-
if (part == breakLine.Last())
659-
{
660-
text += part + " ";
661-
}
662-
else
663-
{
664-
text += part;
665-
sb.AppendLine(" " + text);
666-
text = "";
667-
}
668-
}
669-
}
670-
else if (text.Length + word.Length > (this._maxLineWidth - 4))
671-
{
672-
if (text.Substring(text.Length - 1) == " ")
673-
{
674-
text = text.Substring(0, text.Length - 1);
675-
}
676-
sb.AppendLine(" " + text);
677-
678-
679-
text = word + " ";
680-
}
681-
else
682-
{
683-
text += word + " ";
684-
}
685-
686-
}
687-
688-
if (text.Length > 0 && text != " ")
689-
{
690-
if (text.Substring(text.Length - 1) == " ")
691-
{
692-
text = text.Substring(0, text.Length - 1);
693-
}
694-
695-
sb.Append(" " + text);
696-
}
697-
}
698538
}
699539
}
+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

src/platyPS/platyPS.psm1

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ function New-ExternalHelp
634634
{
635635
foreach($About in $AboutFiles)
636636
{
637-
$r = New-Object -TypeName 'Markdown.MAML.Renderer.MarkdownV2Renderer' -ArgumentList(80)
637+
$r = New-Object -TypeName 'Markdown.MAML.Renderer.TextRenderer' -ArgumentList(80)
638638
$Content = Get-Content -Raw $About.FullName
639639
$p = NewMarkdownParser
640640
$model = $p.ParseString($Content)

test/Markdown.MAML.Test/Markdown.MAML.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Compile Include="EndToEnd\EndToEndTests.cs" />
6565
<Compile Include="Renderer\MamlRendererTests.cs" />
6666
<Compile Include="Renderer\MarkdownV2RendererTests.cs" />
67+
<Compile Include="Renderer\TextRendererTests.cs" />
6768
<Compile Include="Transformer\MamlModelMergerTests.cs" />
6869
<Compile Include="Transformer\ParserAndTransformerTestsV1.cs" />
6970
<Compile Include="Parser\ParserTests.cs" />

0 commit comments

Comments
 (0)