Skip to content

Commit 95be31d

Browse files
feat: psturtle.com Palette Settings ( Fixes #204, Fixes #205 )
1 parent 55b59da commit 95be31d

File tree

2 files changed

+89
-23
lines changed

2 files changed

+89
-23
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<#
2+
.SYNOPSIS
3+
Includes a palette selector
4+
.DESCRIPTION
5+
Includes a palette selector in a page. This allows the page to use multiple color palettes.
6+
#>
7+
param(
8+
[uri]
9+
$PaletteListSource = 'https://4bitcss.com/Palette-List.json',
10+
11+
# The Palette CDN. This is the root URL of all palettes.
12+
[uri]
13+
$PaletteCDN = 'https://cdn.jsdelivr.net/gh/2bitdesigns/4bitcss@latest/css/',
14+
15+
# The identifier for the palette `<select>`.
16+
[string]
17+
$SelectPaletteId = 'SelectPalette',
18+
19+
# The identifier for the stylesheet. By default, palette.
20+
[string]
21+
$PaletteId = 'palette'
22+
)
23+
24+
25+
$JavaScript = @"
26+
function SetPalette() {
27+
var palette = document.getElementById('$PaletteId')
28+
if (! palette) {
29+
palette = document.createElement('link')
30+
palette.rel = 'stylesheet'
31+
palette.id = 'palette'
32+
document.head.appendChild(palette)
33+
}
34+
var selectedPalette = document.getElementById('$SelectPaletteId').value
35+
palette.href = '$PaletteCDN' + selectedPalette + '.css'
36+
}
37+
"@
38+
39+
$paletteSelector = @"
40+
<select id='$SelectPaletteId' onchange='SetPalette()'>
41+
$(
42+
if (-not $script:PaletteList) {
43+
$script:PaletteList = Invoke-RestMethod $PaletteListSource
44+
}
45+
foreach ($paletteName in $script:PaletteList) {
46+
"<option value='$([Web.HttpUtility]::HtmlAttributeEncode($paletteName))'>$([Web.HttpUtility]::HtmlEncode($paletteName))</option>"
47+
}
48+
)
49+
</select>
50+
"@
51+
52+
53+
$HTML = @"
54+
<script>
55+
$JavaScript
56+
</script>
57+
$PaletteSelector
58+
"@
59+
60+
$HTML
61+

psturtle.com/config.ps1

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ param()
1616
#region Core
1717
if ($psScriptRoot) {Push-Location $psScriptRoot}
1818
if (-not $Site) { $Site = [Ordered]@{} }
19-
$parentFolderManifest =
20-
Get-ChildItem -File -Path .. |
21-
Where-Object Extension -eq .psd1 |
19+
$parentFolderManifest =
20+
Get-ChildItem -File -Path .. |
21+
Where-Object Extension -eq .psd1 |
2222
Select-String 'ModuleVersion\s{0,}='
2323

2424
if ($parentFolderManifest) {
@@ -65,23 +65,27 @@ if ($site.PSScriptRoot) {
6565
}
6666
$pointer = $pointer[$subdirectory]
6767
}
68-
68+
6969
$propertyName = $hierarchy[-1] -replace '_' -replace "$([Regex]::Escape($underbarFile.Extension))$"
7070

71-
$fileData =
72-
switch ($underbarFile.Extension) {
73-
'.ps1' { $ExecutionContext.SessionState.InvokeCommand.GetCommand($underbarFile.FullName, 'ExternalScript') }
74-
'.txt' { Get-Content -LiteralPath $underbarFile.FullName }
75-
'.json' { Get-Content -LiteralPath $underbarFile.FullName -Raw | ConvertFrom-Json }
76-
'.psd1' { Get-Content -LiteralPath $underbarFile.FullName -Raw | ConvertFrom-StringData }
77-
'.yaml' {
78-
'YaYaml' | RequireModule
79-
Get-Content -LiteralPath $underbarFile.FullName -Raw | ConvertFrom-Yaml
80-
}
81-
'.svg' { (Get-Content -LiteralPath $underbarFile -Raw) -as [xml] }
82-
'.csv' { Import-Csv -LiteralPath $underbarFile.FullName }
83-
'.tsv' { Import-Csv -LiteralPath $underbarFile.FullName -Delimiter "`t" }
84-
}
71+
$getFile = @{LiteralPath=$underbarFile.FullName}
72+
$fileData =
73+
switch -regex ($underbarFile.Extension) {
74+
'\.ps1$' { $ExecutionContext.SessionState.InvokeCommand.GetCommand($underbarFile.FullName, 'ExternalScript') }
75+
'\.(css|html|txt)$' { Get-Content @getFile }
76+
'\.json$' { Get-Content @getFile | ConvertFrom-Json }
77+
'\.jsonl$' { Get-Content @getFile | ConvertFrom-Json }
78+
'\.psd1$' { Get-Content @getFile -Raw | ConvertFrom-StringData }
79+
'\.(?>ps1xml|xml|svg)$' { (Get-Content @getFile -Raw) -as [xml] }
80+
'\.(?>yaml|toml)$' { Get-Content @getFile -Raw }
81+
'\.csv$' { Import-Csv @getFile }
82+
'\.tsv$' { Import-Csv @getFile -Delimiter "`t" }
83+
}
84+
if (-not $fileData) { continue }
85+
switch ($underbarFile.Extension) {
86+
.toml { RequireModule PSToml; $fileData = $fileData | ConvertFrom-Toml }
87+
.yaml { RequireModule YaYaml; $fileData = $fileData | ConvertFrom-Yaml }
88+
}
8589

8690
if ($fileData) {
8791
$pointer[$propertyName] = $fileData
@@ -138,13 +142,14 @@ if ($site.Module) {
138142
"<pre><code>Import-Module $($site.Module)</code></pre>"
139143
"<h3>Basics</h3>"
140144
"<pre><code>turtle polygon 42 6</code></pre>"
141-
"$(turtle polygon 42 6)"
142-
"<h3>Fractals</h3>"
143-
"<pre><code>turtle SierpinskiTriangle 42 3</code></pre>"
144-
"$(turtle SierpinskiTriangle 42 3)"
145-
"<h3><a href='/Commands/Get-Turtle'>More Examples</a></h3>"
145+
"$(turtle polygon 42 6)"
146+
"<h3><a href='/Commands/Get-Turtle'>More Examples</a></h3>"
146147
}
147148
) -join [Environment]::NewLine
149+
'Settings' = @(
150+
. $site.includes.SelectPalette
151+
. $site.includes.GetRandomPalette
152+
)
148153
}
149154

150155
<#$site.HeaderMenu = [Ordered]@{

0 commit comments

Comments
 (0)