functions/Read-EBCssStyleSheet.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
function Read-EBCssStyleSheet
{
<#
    .SYNOPSIS
        Parse a stylesheet and convert it into an object model.
     
    .DESCRIPTION
        Parse a stylesheet and convert it into an object model.
     
    .PARAMETER CssData
        CSS data provided as a string.
     
    .PARAMETER Path
        Path to CSS stylesheets to parse.
     
    .PARAMETER Merge
        For all styles bound to a class, merge in the settings of the base styles for the tag the class applies to.
     
    .EXAMPLE
        PS C:\> Read-EBCssStyleSheet -CssData $content
     
        Parses the CSS styles contained as string in $content
     
    .EXAMPLE
        PS C:\> Get-ChildItem *.css | Read-EBCssStyleSheet
     
        Parses all CSS stylesheets in the current folder.
#>

    [OutputType([EbookBuilder.StyleObject])]
    [CmdletBinding(DefaultParameterSetName = 'Text')]
    param (
        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Text')]
        [string]
        $CssData,
        
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'File')]
        [Alias('FullName')]
        [string[]]
        $Path,
        
        [switch]
        $Merge
    )
    
    begin{
        #region Functions
        function Convert-StyleSheet {
            [CmdletBinding()]
            param (
                [Parameter(ValueFromPipeline = $true)]
                [string[]]
                $Text,
                
                [hashtable]
                $ResultHash
            )
            
            begin {
                $currentTag = ''
                $currentClass = '_default'
                $inComment = $false
            }
            process {
                foreach ($line in $Text | Get-SubString -Trim " " | Split-String "`n" | Get-SubString -Trim " " | Remove-PSFNull) {
                    Write-PSFMessage -Level InternalComment -Message ' {0}' -StringValues $line
                    #region Comments
                    if ($line -match "^/\*") { $inComment = $true }
                    if ($line -match "\*/") { $inComment = $false }
                    if ($inComment -or $line -match "^/\*" -or $line -match "\*/") { continue }
                    #endregion Comments
                    
                    #region Close CSS
                    if ($line -eq "}") {
                        $currentTag = ''
                        $currentClass = '_default'
                        
                        continue
                    }
                    #endregion Close CSS
                    
                    #region Open CSS
                    if ($line -like "*{") {
                        $tempLine = $line | Get-SubString -TrimEnd " {"
                        $segments = $tempLine -split "\.",2
                        $currentTag = $segments[0]
                        if ($segments[1]) { $currentClass = $segments[1] }
                        else { $currentClass = '_default' }
                        continue
                    }
                    #endregion Open CSS
                    
                    #region Content
                    if (-not $ResultHash[$currentTag]) { $ResultHash[$currentTag] = @{ } }
                    if (-not $ResultHash[$currentTag][$currentClass]) { $ResultHash[$currentTag][$currentClass] = @{ } }
                    
                    $key, $value = $line -split ":", 2
                    if (-not $key -or -not $value) { continue }
                    Write-PSFMessage -Level InternalComment -Message ' {0} : {1}' -StringValues $key, $value
                    $ResultHash[$currentTag][$currentClass][$key.Trim()] = $value.Trim(" ;")
                    #endregion Content
                }
            }
        }
        
        function Resolve-StyleHash {
            [OutputType([EbookBuilder.StyleObject])]
            [CmdletBinding()]
            param (
                [hashtable]
                $ResultHash,
                
                [bool]
                $Merge
            )
            
            foreach ($pair in $ResultHash.GetEnumerator()) {
                $tagName = $pair.Key
                
                $defaultStyle = @{ }
                if ($Merge -and $pair.Value._Default) {
                    $defaultStyle = $pair.Value._Default
                }
                
                foreach ($entry in $pair.Value.GetEnumerator()) {
                    $style = [EbookBuilder.StyleObject]::new()
                    $style.Tag = $tagName
                    if ($entry.Key -ne '_default') { $style.Class = $entry.Key }
                    
                    $hash = $defaultStyle.Clone()
                    foreach ($attribute in $entry.Value.GetEnumerator()) { $hash[$attribute.Key] = $attribute.Value }
                    foreach ($attribute in $hash.GetEnumerator()) { $style.Attributes[$attribute.Key] = $attribute.Value }
                    $style
                }
            }
        }
        #endregion Functions
        
        $resultHash = @{ }
    }
    process
    {
        if ($CssData) { $CssData | Convert-StyleSheet -ResultHash $resultHash }
        foreach ($filePath in $Path) {
            Write-PSFMessage -Message 'Loading style file: {0}' -StringValues $filePath
            Get-Content -Path $filePath | Convert-StyleSheet -ResultHash $resultHash
        }
    }
    end
    {
        Resolve-StyleHash -ResultHash $resultHash -Merge $Merge
    }
}