Skip to content

Commit 80aee47

Browse files
stigruneKeboo
authored andcommitted
Create GenerateThemesWikiMarkdown.ps1 (#1192)
* Create GenerateThemesWikiMarkdown.ps1 Created powershell script to display all control styles in markdown * Changing the name of the output file
1 parent e6ccae3 commit 80aee47

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
$defaultStyleText = "(default style)"
2+
$headerMarkdown = "##"
3+
$listMarkdown = "-"
4+
$themesDirectory = "..\MaterialDesignThemes.Wpf\Themes\"
5+
$outputFileName = "ControlStyleList.md"
6+
$themesFullDir = Join-Path $PSScriptRoot $themesDirectory
7+
$outputFullDir = Join-Path $PSScriptRoot $outputFileName
8+
$DebugPreference = 'Continue' # Log debug messages to terminal.
9+
10+
$discoverdStyles = New-Object System.Collections.ArrayList
11+
$defaults = New-Object System.Collections.ArrayList
12+
13+
Function Main {
14+
# Get xaml files and loop through.
15+
Get-ChildItem $themesFullDir -Filter *.xaml |
16+
Foreach-Object {
17+
$xamlString = Get-Content -Path $_.FullName
18+
$file = Select-ControlNameFromFile($_.Name)
19+
Read-XamlStyles -xamlString $xamlString -file $file
20+
}
21+
Set-Defaults
22+
Format-Output
23+
}
24+
25+
Function Format-Output {
26+
27+
foreach($style in $discoverdStyles | Sort-Object -Property Control,@{Expression = {$_.IsDefault}; Ascending = $false}) {
28+
if ($previousFile -ne $style.File) {
29+
Write-OutputFile "`n$headerMarkdown $($style.File)"
30+
}
31+
$previousFile = $style.File;
32+
33+
if ($style.IsDefault) {
34+
Write-OutputFile ("$listMarkdown $($style.Style) $defaultStyleText" -replace '\s+', ' ')
35+
}
36+
else {
37+
Write-OutputFile "$listMarkdown $($style.Style)"
38+
}
39+
}
40+
}
41+
42+
Function Write-OutputFile{
43+
Param ($output)
44+
Add-content $outputFullDir -value $output
45+
Write-Debug $output #debug
46+
}
47+
48+
Function Set-Defaults{
49+
$remainingDefaults = New-Object System.Collections.ArrayList
50+
ForEach ($default in $defaults) {
51+
$style = $discoverdStyles.Where({$_.style -match $default.style})
52+
if ($null -ne $style[0]) {
53+
$style[0].IsDefault = $true
54+
}
55+
else {
56+
$remainingDefaults.Add($default)
57+
}
58+
}
59+
$discoverdStyles | Format-Table #debug
60+
$remainingDefaults | Format-Table #debug
61+
}
62+
63+
Function Select-ControlNameFromFile {
64+
Param ($fileName)
65+
return $fileName -replace ".xaml" -replace "MaterialDesignTheme."
66+
}
67+
68+
Function Read-XamlStyles {
69+
Param ($xamlString, $file)
70+
[xml]$xaml = $xamlString
71+
$xaml.selectNodes("//*") | Where-Object {$_.LocalName -eq "Style"} |
72+
Foreach-Object {
73+
if ($file -eq "Defaults") {
74+
# Special handeling of Defaults
75+
New-Default -style $_ -file $file
76+
}
77+
elseif ($file -eq "Generic") {
78+
# Special handeling of Generic
79+
New-GenericDefault -style $_ -file $file
80+
}
81+
else{
82+
New-Style -style $_ -file $file
83+
}
84+
}
85+
}
86+
87+
Function New-GenericDefault {
88+
Param ($style, $file)
89+
$targetType = Read-TargetType($style | Select-Object TargetType)
90+
$basedOn = Read-BasedOn($style | Select-Object BasedOn)
91+
$styleNameValue = ($style | Select-Object Key).Key
92+
$defaultStyleName = if ($null -eq $styleNameValue) { $basedOn } else { $styleNameValue }
93+
Write-Debug "[$file] [Type: $targetType] [StyleNameValue: $styleNameValue] [BasedOn: $basedOn] [DefaultStyleName: $defaultStyleName]"
94+
Add-DefaultStyle -file $file -targetType $targetType -styleName $defaultStyleName
95+
}
96+
97+
98+
Function New-Default {
99+
Param ($style, $file)
100+
$targetType = Read-TargetType($style | Select-Object TargetType)
101+
$basedOn = Read-BasedOn($style | Select-Object BasedOn)
102+
$styleNameValue = ($style | Select-Object Key).Key
103+
$defaultStyleName = if ($null -eq $styleNameValue) { $basedOn } else { $styleNameValue }
104+
Write-Debug "[$file] [Type: $targetType] [StyleNameValue: $styleNameValue] [BasedOn: $basedOn] [DefaultStyleName: $defaultStyleName]"
105+
Add-DefaultStyle -file $file -targetType $targetType -styleName $defaultStyleName
106+
}
107+
108+
Function New-Style {
109+
Param ($style, $file)
110+
$targetType = Read-TargetType($style | Select-Object TargetType)
111+
$styleName = ($style | Select-Object Key).Key
112+
$splittedFile = $file.split('.') # Suport for "nested" file names like DataGrid.ComboBox
113+
114+
if ($targetType -eq $splittedFile[-1]) {
115+
Write-Debug "[Match ] [File: $file] [Type: $targetType] [Style: $styleName]"
116+
Add-Style -targetType $targetType -styleName $styleName -fileName $file
117+
}
118+
else {
119+
Write-Debug "[Skipped] [File: $file] [Type: $targetType] [Style: $styleName] "
120+
}
121+
}
122+
123+
Function Add-Style {
124+
Param ($targetType, $styleName, $fileName)
125+
$temp = "" | Select-Object "Control", "Style", "IsDefault", "File"
126+
$temp.Control = $targetType
127+
$temp.Style = $styleName
128+
$temp.IsDefault = !$styleName
129+
$temp.File = $fileName
130+
$discoverdStyles.Add($temp) | Out-Null
131+
}
132+
133+
Function Add-DefaultStyle {
134+
Param ($file, $targetType, $styleName)
135+
$temp = "" | Select-Object "File", "Type", "Style"
136+
$temp.File = $file
137+
$temp.Type = $targetType
138+
$temp.Style = $styleName
139+
$defaults.Add($temp) | Out-Null
140+
}
141+
142+
Function Read-TargetType {
143+
Param ($targetTypeText)
144+
return ($targetTypeText.TargetType -replace "{x:Type" -replace "{x:Type" -replace ".*:" -replace "}*" -replace "Base").Trim()
145+
}
146+
147+
Function Read-BasedOn {
148+
Param ($targetTypeText)
149+
return ($targetTypeText.BasedOn -replace "{StaticResource" -replace ".*:" -replace "}*").Trim()
150+
}
151+
152+
Main

0 commit comments

Comments
 (0)