functions/ConvertFrom-EBMarkdown.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function ConvertFrom-EBMarkdown {
<#
    .SYNOPSIS
        A limited convertion from markdown to html.
     
    .DESCRIPTION
        A limited convertion from markdown to html.
        This command will process multiple lines into useful html.
        It is however limited in scope:
     
        + Paragraphs
        + Italic/emphasized text
        + Bold text
        + Bullet Points
     
        Other elements, such as comments (">") or headers ("#") are being ignored.
     
        This is due to this command being scoped not to converting whole pages, but instead for fairly small passages of markdown.
        Especially as a tool used within Blocks.
     
    .PARAMETER Line
        The lines of markdown string to convert.
     
    .PARAMETER EmphasisClass
        Which class to use for emphasized pieces of text.
        This is particularly intended for emphasis in text that is in italics by default.
     
        By default, emphasized text is wrapped into "<i>" and "</i>".
        However, when offerign a class instead a span tag is used:
        '<span class="EmphasisClass">' and '</span>'.
     
    .PARAMETER ClassFirstParagraph
        Which class to use for the first paragraph found.
        This affects the very first paragraph as well as any first paragraph after bulletpoints.
        Defaults to the same class as used for the ClassParagraph parameter.
     
    .PARAMETER ClassParagraph
        Which class to use for all paragraph but the first one.
        Defaults to: No class at all.
     
    .PARAMETER Classes
        A hashtable for mapping html tags to class names.
        Ignored for paragraphs, italic and bold, but can be used for example to add a class to "<li>" items.
     
    .PARAMETER AlwaysBreak
        By default, common markdown practice is to build a paragraph from multiple lines of text.
        Only on an empty line would a new paragraph be created.
        This can be disabled with this switch, causing every end of line to be treated as the end of a paragraph.
     
    .EXAMPLE
        PS C:\> ConvertFrom-EBMarkdown -Line $Data.Lines
     
        Converts all the lines of text in $Data.Lines without assigning special classes to any text.
     
    .EXAMPLE
        PS C:\> ConvertFrom-EBMarkdown -Line $Data.Lines -ClassParagraph blockOther -ClassFirstParagraph blockFirst -EmphasisClass blockEmphasis
     
        Converts all the lines of text in $Data.Lines, assigning the specified classes as applicable.
#>

    [OutputType([string])]
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [AllowEmptyCollection()]
        [AllowEmptyString()]
        [string[]]
        $Line,
        
        [string]
        $EmphasisClass,
        
        [string]
        $ClassFirstParagraph,
        
        [string]
        $ClassParagraph,
        
        [hashtable]
        $Classes = @{ },
        
        [switch]
        $AlwaysBreak
    )
    
    begin {
        #region Utility Functions
        function Get-ClassString {
            [OutputType([string])]
            [CmdletBinding()]
            param (
                [string]
                $Name,
                
                [hashtable]
                $Classes
            )
            
            if (-not $Classes.$Name) { return '' }
            
            ' class="{0}"' -f $Classes.$Name
        }
        
        function Write-Paragraph {
            [OutputType([string])]
            [CmdletBinding()]
            param (
                [string[]]
                $Text,
                
                [string]
                $FirstParagraph = $ClassFirstParagraph,
                
                [string]
                $Paragraph = $ClassParagraph,
                
                [bool]
                $First = $isFirstParagraph
            )
            
            Set-Variable -Name isFirstParagraph -Scope 1 -Value $false
            Set-Variable -Name currentParagraph -Scope 1 -Value @()
            
            $class = $Paragraph
            if ($First -and $FirstParagraph) { $class = $FirstParagraph }
            $classString = Get-ClassString -Name p -Classes @{ p = $class }
            
            "<p$($classString)>$($Text -join " ")</p>"
        }
        #endregion Utility Functions
        
        $currentParagraph = @()
        $inBullet = $false
        $isFirstParagraph = $true
        
        $convertParam = $PSBoundParameters | ConvertTo-PSFHashtable -Include EmphasisClass
    }
    process {
        foreach ($string in $Line) {
            #region Empty Line
            if (-not $string) {
                if ($currentParagraph) { Write-Paragraph -Text $currentParagraph }
                if ($AlwaysBreak -and -not $inBullet) { Write-Paragraph -Text '&nbsp;' }
                if ($inBullet) {
                    '</ul>'
                    $inBullet = $false
                }
                continue
            }
            #endregion Empty Line
            
            #region Bullet Lists
            if ($string -match '^- |^\+ ') {
                $isFirstParagraph = $true
                if (-not $inBullet) {
                    if ($currentParagraph) { Write-Paragraph -Text $currentParagraph }
                    "<ul$(Get-ClassString -Name ul -Classes $Classes)>"
                    $inBullet = $true
                }
                "<li$(Get-ClassString -Name li -Classes $Classes)>$($string | Set-String -OldValue '^- |^\+ ' | ConvertFrom-EBMarkdownLine @convertParam)</li>"
                continue
            }
            #endregion Bullet Lists
            
            #region Default: paragraph
            $currentParagraph += $string | ConvertFrom-EBMarkdownLine @convertParam
            
            if ($AlwaysBreak) { Write-Paragraph -Text $currentParagraph }
            #endregion Default: paragraph
        }
    }
    end {
        if ($inBullet) {
            '</ul>'
        }
        if ($currentParagraph) { Write-Paragraph -Text $currentParagraph }
    }
}