diff --git a/src/System.Management.Automation/help/HelpCommentsParser.cs b/src/System.Management.Automation/help/HelpCommentsParser.cs index 8b6fc93527d..3bfa0181297 100644 --- a/src/System.Management.Automation/help/HelpCommentsParser.cs +++ b/src/System.Management.Automation/help/HelpCommentsParser.cs @@ -495,47 +495,25 @@ private void BuildSyntaxForParameterSet(XmlElement command, XmlElement syntax, M private static void GetExampleSections(string content, out string prompt_str, out string code_str, out string remarks_str) { - prompt_str = code_str = string.Empty; - StringBuilder builder = new StringBuilder(); string default_prompt_str = "PS > "; - int collectingPart = 1; - foreach (char c in content) + var promptMatch = Regex.Match(content, "^.*?>"); + prompt_str = promptMatch.Success ? promptMatch.Value : default_prompt_str; + if (promptMatch.Success) { - if (c == '>' && collectingPart == 1) - { - builder.Append(c); - prompt_str = builder.ToString().Trim(); - builder = new StringBuilder(); - ++collectingPart; - continue; - } - - if (c == '\n' && collectingPart < 3) - { - if (collectingPart == 1) - { - prompt_str = default_prompt_str; - } - - code_str = builder.ToString().Trim(); - builder = new StringBuilder(); - collectingPart = 3; - continue; - } - - builder.Append(c); + content = content.Substring(prompt_str.Length); } - if (collectingPart == 1) + var codeAndRemarksMatch = Regex.Match(content, "^(?.*?)\r?\n\r?\n(?.*)$", RegexOptions.Singleline); + if (codeAndRemarksMatch.Success) { - prompt_str = default_prompt_str; - code_str = builder.ToString().Trim(); - remarks_str = string.Empty; + code_str = codeAndRemarksMatch.Groups["code"].Value.Trim(); + remarks_str = codeAndRemarksMatch.Groups["remarks"].Value; } else { - remarks_str = builder.ToString(); + code_str = content.Trim(); + remarks_str = string.Empty; } } diff --git a/test/powershell/Language/Scripting/ScriptHelp.Tests.ps1 b/test/powershell/Language/Scripting/ScriptHelp.Tests.ps1 index 95217a2bcf4..49935865923 100644 --- a/test/powershell/Language/Scripting/ScriptHelp.Tests.ps1 +++ b/test/powershell/Language/Scripting/ScriptHelp.Tests.ps1 @@ -627,4 +627,63 @@ Describe 'get-help other tests' -Tags "CI" { (Get-Help foo -Examples).examples.example.introduction.Text | Should -BeExactly "PS > " } } + + Context 'get-help -Examples multi-line code block should be handled' { + function foo { + <# + .EXAMPLE + $a = Get-Service + $a | group Status + + .EXAMPLE + PS> $a = Get-Service + PS> $a | group Status + + Explanation. + + .EXAMPLE + + PS> Get-Service + Output + + .EXAMPLE + PS> Get-Service + Output + + Explanation. + Second line. + + .EXAMPLE + PS> Get-Service + Output + + Explanation. + + Next section. + #> + param() + } + + $x = get-help foo + It '$x.examples.example[0].introduction[0].text' { $x.examples.example[0].introduction[0].text | Should -BeExactly "PS > " } + It '$x.examples.example[0].code' { $x.examples.example[0].code | Should -BeExactly "`$a = Get-Service`n`$a | group Status" } + It '$x.examples.example[0].remarks[0].text' { $x.examples.example[0].remarks[0].text | Should -BeNullOrEmpty } + It '$x.examples.example[0].remarks.length' { $x.examples.example[0].remarks.length | Should -Be 5 } + It '$x.examples.example[1].introduction[0].text' { $x.examples.example[1].introduction[0].text | Should -BeExactly "PS>" } + It '$x.examples.example[1].code' { $x.examples.example[1].code | Should -BeExactly "`$a = Get-Service`nPS> `$a | group Status" } + It '$x.examples.example[1].remarks[0].text' { $x.examples.example[1].remarks[0].text | Should -BeExactly "Explanation." } + It '$x.examples.example[1].remarks.length' { $x.examples.example[1].remarks.length | Should -Be 5 } + It '$x.examples.example[2].introduction[0].text' { $x.examples.example[2].introduction[0].text | Should -BeExactly "PS>" } + It '$x.examples.example[2].code' { $x.examples.example[2].code | Should -BeExactly "Get-Service`nOutput" } + It '$x.examples.example[2].remarks[0].text' { $x.examples.example[2].remarks[0].text | Should -BeNullOrEmpty } + It '$x.examples.example[2].remarks.length' { $x.examples.example[2].remarks.length | Should -Be 5 } + It '$x.examples.example[3].introduction[0].text' { $x.examples.example[3].introduction[0].text | Should -BeExactly "PS>" } + It '$x.examples.example[3].code' { $x.examples.example[3].code | Should -BeExactly "Get-Service`nOutput" } + It '$x.examples.example[3].remarks[0].text' { $x.examples.example[3].remarks[0].text | Should -BeExactly "Explanation.`nSecond line." } + It '$x.examples.example[3].remarks.length' { $x.examples.example[3].remarks.length | Should -Be 5 } + It '$x.examples.example[4].introduction[0].text' { $x.examples.example[4].introduction[0].text | Should -BeExactly "PS>" } + It '$x.examples.example[4].code' { $x.examples.example[4].code | Should -BeExactly "Get-Service`nOutput" } + It '$x.examples.example[4].remarks[0].text' { $x.examples.example[4].remarks[0].text | Should -BeExactly "Explanation.`n`nNext section." } + It '$x.examples.example[4].remarks.length' { $x.examples.example[4].remarks.length | Should -Be 5 } + } }