at master 3.8 kB view raw
1<# 2 This file is part of NKK. 3 4 NKK is free software: you can redistribute it and/or modify it under the 5 terms of the GNU Affero General Public License as published by the Free 6 Software Foundation, either version 3 of the License, or (at your option) 7 any later version. 8 9 NKK is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for 12 more details. 13 14 You should have received a copy of the GNU Affero General Public License 15 along with NKK. If not, see <http://www.gnu.org/licenses/>. 16#> 17 18Import-Module Pode 19Import-Module Mizumiya 20 21function _log { 22 param( 23 [Parameter(ValueFromPipeline)] 24 $Message, 25 [ValidateSet('Request', 'Information', 'Warning', 'Fatal')] 26 $Type 27 ) 28 29 switch ($Type) { 30 'Request' { 31 $Style = $PSStyle.Foreground.BrightBlack 32 } 33 34 'Information' { 35 $Style = '' 36 } 37 38 'Warning' { 39 $Style = $PSStyle.Formatting.Warning 40 } 41 42 'Fatal' { 43 $Style = $PSStyle.Formatting.Error 44 } 45 } 46 47 Write-Host "$Style[NKK/$Type] $Message$($PSStyle.Reset)" 48 if ($Type -eq 'Fatal') { throw $Message } 49} 50 51function _request { 52 param( 53 [Parameter(ValueFromPipeline)] 54 $Message 55 ) 56 57 _log -Type Request -Message $Message 58} 59 60function _info { 61 param( 62 [Parameter(ValueFromPipeline)] 63 $Message 64 ) 65 66 _log -Type Information -Message $Message 67} 68 69function _warn { 70 param( 71 [Parameter(ValueFromPipeline)] 72 $Message 73 ) 74 75 _log -Type Warning -Message $Message 76} 77 78function _fatal { 79 param ( 80 [Parameter(ValueFromPipeline)] 81 $Message 82 ) 83 84 _log -Type Fatal $Message 85} 86 87function _get_splash { 88 param ( $Index ) 89 90 if ($Index -and $Index -gt 0) { 91 return (Get-PodeConfig).NKK.Splashes | Select-Object -Index $Index | ConvertFrom-Markdown | % Html 92 } 93 94 return (Get-PodeConfig).NKK.Splashes | Get-Random | ConvertFrom-Markdown | % Html 95} 96 97function _format_size ([uint64] $size) { 98 switch ($Size) { 99 {$size -gt 1tb} { return '{0:n2} TiB' -f ($size/1tb) } 100 {$size -gt 1gb} { return '{0:n2} GiB' -f ($size/1gb) } 101 {$size -gt 1mb} { return '{0:n2} MiB' -f ($size/1mb) } 102 {$size -gt 1kb} { return '{0:n2} KiB' -f ($size/1kb) } 103 default { return "$size B" } 104 } 105} 106 107function _check_joinedpath_is_based ($Root, $JoinedPath) { 108 $Resolved = Get-Item -LiteralPath $JoinedPath -ErrorAction Ignore 109 110 return ($null -ne $Resolved -and $Resolved.FullName.StartsWith($Root)) 111} 112 113function _scripts { 114 script -Src /htmx.js 115 # meta -Name htmx-config -Content (@{ scrollIntoViewOnBoost=$false; defaultHideShowStrategy='twDisplay' }|ConvertTo-Json -Compress) 116 meta -Name htmx-config -Content (@{ scrollIntoViewOnBoost=$false }|ConvertTo-Json -Compress) 117} 118 119function _header { 120 header -Class "n-box clear-n-box flex flex-row! items-center mb-1!" { 121 # img -Class "rounded-md pfp" -Src https://avatars.githubusercontent.com/helpimnotdrowning -Alt "Profile picture" -Width 70 122 img -Class "rounded-md pfp" -Src /user.webp -Alt "Profile picture" -Width 70 123 124 div -Class "ms-6" { 125 span -Class "text-4xl" { 'helpimnotdrowning' } 126 div -Id splash -HxGet /random-splash ` 127 -HxSwap innerText ` 128 -HxTrigger 'load,click' ` 129 { p 'missingno' } 130 } 131 } 132 133 nav -Class 'n-box mt-2! w-full' { 134 div { 135 div -Class 'float-left flex gap-4' { 136 a -Href / { 'Home' } 137 a -Href /sayings { 'Posts' } 138 } 139 div -Class 'float-right' { 140 a ` 141 -Class 'text-blue-600 underline' ` 142 -Href 'https://git.helpimnotdrowning.net/helpimnotdrowning/NKK' ` 143 -Target _blank ` 144 { 145 'running helpimnotdrowning/NKK' 146 } 147 } 148 } 149 } 150} 151 152function _parse_saying_header { 153 param ([IO.FileInfo] $Path) 154 $Data = Get-Content $Path | Select-Object -First 1 | ConvertFrom-Json -AsHashtable 155 $Data.Path = (Join-Path /sayings $Path.BaseName) 156 157 return $Data 158}