From bb8cb326a156c736ec9aa01d76c5f64c89988a2e Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Thu, 1 Feb 2024 12:18:46 -0800 Subject: [PATCH] feat: Export-4BitSvg ( Fixes #34 ) Also updating build --- 4bitcss.build.ps1 | 14 +++++++- Export-4BitSVG.ps1 | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 Export-4BitSVG.ps1 diff --git a/4bitcss.build.ps1 b/4bitcss.build.ps1 index f77def8167..af7bdebcf4 100644 --- a/4bitcss.build.ps1 +++ b/4bitcss.build.ps1 @@ -126,4 +126,16 @@ $4bitJS = Export-4BitJS -ColorSchemeName $allColorSchemes -DarkColorSchemeName $ $4bitJSDocsPath = Join-Path $docsPath "js" | Join-Path -ChildPath "4bit.js" New-Item -ItemType File -Path $4bitJSDocsPath -Force -Value $4bitJS -New-Item -ItemType File -Path ".\4bit.js" -Force -Value $4bitJS \ No newline at end of file +New-Item -ItemType File -Path ".\4bit.js" -Force -Value $4bitJS + + +#region Icons +$IncludesPath = Join-Path $docsPath "_includes" +if (-not (Test-Path $IncludesPath)) { + $null = New-Item -ItemType Directory -Path $IncludesPath +} + +Export-4BitSVG -SVG https://raw.githubusercontent.com/feathericons/feather/master/icons/download.svg -Stroke "ansi6" -OutputPath (Join-Path $IncludesPath "download-icon.svg") +Export-4BitSVG -SVG https://raw.githubusercontent.com/feathericons/feather/master/icons/download-cloud.svg -Stroke "ansi6" -OutputPath (Join-Path $IncludesPath "download-cloud-icon.svg") +Export-4BitSVG -SVG https://raw.githubusercontent.com/feathericons/feather/master/icons/shuffle.svg -Stroke "ansi6" -OutputPath (Join-Path $IncludesPath "shuffle-icon.svg") +#endregion Icons \ No newline at end of file diff --git a/Export-4BitSVG.ps1 b/Export-4BitSVG.ps1 new file mode 100644 index 0000000000..1384826fc5 --- /dev/null +++ b/Export-4BitSVG.ps1 @@ -0,0 +1,82 @@ +function Export-4BitSVG +{ + <# + .SYNOPSIS + Exports an SVG that uses a 4 bit palette + .DESCRIPTION + Exports an SVG with a slight modifications that make it use a specific color palette. + .NOTES + SVGs can use CSS styles to dynamically change color. + #> + param( + # The SVG content, or a URL to the SVG + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $SVG, + + # The color used for strokes within the SVG. + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $Stroke, + + # The color used for fill within the SVG. + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $Fill, + + # If provided, will output the SVG to a file + [Parameter(ValueFromPipelineByPropertyName)] + [string] + $OutputPath + ) + + process { + $svgXML = $null + if ($svg -match "^https?://") { + $svgRest = Invoke-RestMethod -Uri $svg + if ($svgRest -isnot [xml]) { + Write-Error "$Svg was not XML" + return + } + $svgXML = $svgRest + $svg = $svgXML.OuterXML + } elseif ($svg -notmatch '[\n\r]' -and (Test-path $svg)) { + $svg = Get-Content -Path $svg -Raw + } + + + if (-not ($svg -as [xml])) { + try { + $svgXML = [xml]$svg + } catch { + $ex = $_ + Write-Error -Exception $ex.Exception -Message "Could not convert to SVG: $($ex.Exception)" -TargetObject $svg + } + } + + if (-not $svgXML) { return } + + $svgClass = @( + if ($stroke) { + $stroke -replace "(?:-stroke)?$", '-stroke' + } + if ($Fill) { + $fill -replace "(?:-fill)?$", '-fill' + } + ) -join ' ' + + if ($svgClass) { + $svgXML.svg.SetAttribute("class", "$svgClass") + } + + if ($OutputPath) { + $svgXML.svg.OuterXML | Set-Content -Path $OutputPath + if ($?) { + Get-Item -Path $OutputPath + } + } else { + $svgXML.svg.OuterXML + } + + } +} \ No newline at end of file