public/Read-HelpExample.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

function Read-HelpExample() {
    Param(
        [Parameter(Position = 0)]
        [PsCustomObject] $Example
    )

   
    if(!$example) {
      
        $Example = [PSCustomObject]@{
            Code = ""
            Remarks = [PSCustomObject]@{
                Text = ""
            }
            IsEmpty = $true 
            Title = ""
        }
    } else {
        
    }

    $code = $Example.Code 
    $remarks = $Example.Remarks;
    if($remarks.Text) {
        $remarks = $remarks.Text;
    }

    if($code) {
        $nextCode = $code.TrimEnd("`n")
    } else {
        $nextCode = "";
    }
   
    
    $nextRemark = "";
    if($remarks) {

        # Powershell only allows one line examples.
        
        # PS C:\> can be used to extend example code blocks
        # where "PS C:\>" acts as a marker to instruct
        # the code below to be added to the code example
       
        foreach($line in $remarks.Split("`n")) {
            
            if($line.Trim().StartsWith("PS C:\>")) {
                
                $l = $line.SubString(7)
                $nextCode += "`n$l"
            } else {
                $nextRemark += "`n$line"
            }
        }
    }
    
    $Example.Code =  $nextCode
    $Example.Remarks = Write-EscapedMarkdownString $nextRemark
    return $Example
}