Skip to content

Commit 5bd172c

Browse files
feat: psturtle.com includes ( Fixes #183, Fixes #184 )
Adding includes for Index.json and Index.rss and removing from build.
1 parent b335147 commit 5bd172c

File tree

3 files changed

+147
-127
lines changed

3 files changed

+147
-127
lines changed

psturtle.com/_includes/Index.json.ps1

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<#
2+
.SYNOPSIS
3+
Includes `index.json`
4+
.DESCRIPTION
5+
Includes content for an `index.json` file.
6+
7+
This should be called after the build to generate a list of all files.
8+
#>
9+
#region index.json
10+
if (-not $Site.NoIndex) {
11+
$fileIndex =
12+
if ($filePath) { Get-ChildItem -Recurse -File -Path $FilePath }
13+
else { Get-ChildItem -Recurse -File }
14+
15+
$replacement =
16+
if ($filePath) {
17+
"^" + ([regex]::Escape($filePath) -replace '\*','.{0,}?')
18+
} else {
19+
"^" + [regex]::Escape("$pwd")
20+
}
21+
22+
$indexObject = [Ordered]@{}
23+
$gitCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand('git', 'Application')
24+
foreach ($file in $fileIndex) {
25+
$gitDates =
26+
try {
27+
(& $gitCommand log --follow --format=%ci --date default $file.FullName *>&1) -as [datetime[]]
28+
} catch {
29+
$null
30+
}
31+
$LASTEXITCODE = 0
32+
33+
$indexObject[$file.FullName -replace $replacement] = [Ordered]@{
34+
Name = $file.Name
35+
Length = $file.Length
36+
Extension = $file.Extension
37+
CreatedAt =
38+
if ($gitDates) {
39+
$gitDates[-1]
40+
} else {
41+
$file.CreationTime
42+
}
43+
LastWriteTime =
44+
if ($gitDates) {
45+
$gitDates[0]
46+
} else {
47+
$file.LastWriteTime
48+
}
49+
}
50+
}
51+
52+
foreach ($indexKey in $indexObject.Keys) {
53+
if (-not $indexObject[$indexKey].CreatedAt) {
54+
if ($indexObject["$indexKey.ps1"].CreatedAt) {
55+
$indexObject[$indexKey].CreatedAt = $indexObject["$indexKey.ps1"].CreatedAt
56+
}
57+
}
58+
if (-not $indexObject[$indexKey].LastWriteTime) {
59+
if ($indexObject["$indexKey.ps1"].LastWriteTime) {
60+
$indexObject[$indexKey].LastWriteTime = $indexObject["$indexKey.ps1"].LastWriteTime
61+
}
62+
}
63+
}
64+
65+
$indexObject | ConvertTo-Json -Depth 4
66+
}
67+
#endregion index.json

psturtle.com/_includes/Index.rss.ps1

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<#
2+
.SYNOPSIS
3+
Includes `index.rss`
4+
.DESCRIPTION
5+
Includes the content for an `index.rss`.
6+
7+
This should be called after the build to generate an RSS feed for the site.
8+
#>
9+
#region index.rss
10+
if (-not $Site.NoRss) {
11+
$pagesByDate = @($site.PagesByUrl.GetEnumerator() |
12+
Sort-Object { $_.Value.Date } -Descending)
13+
$lastPubDate = if ($pagesByDate.Values.Date) {
14+
$pagesByDate[0].Value.Date.ToString('R')
15+
} else {
16+
$lastBuildTime.ToString('R')
17+
}
18+
$rssXml = @(
19+
'<rss version="2.0">'
20+
'<channel>'
21+
"<title>$([Security.SecurityElement]::Escape($(
22+
if ($site.Title) { $site.Title } else { $site.CNAME }
23+
)))</title>"
24+
"<link>$($site.RootUrl)</link>"
25+
"<description>$([Security.SecurityElement]::Escape($(
26+
if ($site.Description) { $site.Description } else { $site.Title }
27+
)))</description>"
28+
"<pubDate>$($lastPubDate)</pubDate>"
29+
"<lastBuildDate>$($lastBuildTime.ToString('R'))</lastBuildDate>"
30+
"<language>$([Security.SecurityElement]::Escape($site.Language))</language>"
31+
:nextPage foreach ($keyValue in $pagesByDate) {
32+
$key = $keyValue.Key
33+
$keyUri = $key -as [Uri]
34+
$page = $keyValue.Value
35+
if ($site.Disallow) {
36+
foreach ($disallow in $site.Disallow) {
37+
if ($keyUri.LocalPath -like "*$disallow*") { continue nextPage }
38+
if ($keyUri.AbsoluteUri -like "*$disallow*") { continue nextPage }
39+
}
40+
}
41+
if ($site.PagesByUrl[$key].NoIndex) { continue }
42+
if ($site.PagesByUrl[$key].NoSitemap) { continue }
43+
if ($site.PagesByUrl[$key].OutputFile.Extension -ne '.html') { continue }
44+
"<item>"
45+
"<title>$([Security.SecurityElement]::Escape($(
46+
if ($page.Title) { $page.Title }
47+
elseif ($site.Title) { $site.Title }
48+
else { $site.CNAME }
49+
)))</title>"
50+
if ($site.PagesByUrl[$key].Date -is [DateTime]) {
51+
"<pubDate>$($site.PagesByUrl[$key].Date.ToString('R'))</pubDate>"
52+
}
53+
"<description>$([Security.SecurityElement]::Escape($(
54+
if ($page.Description) { $page.Description }
55+
elseif ($site.Description) { $site.Description }
56+
)))</description>"
57+
"<link>$key</link>"
58+
"<guid isPermaLink='true'>$key</guid>"
59+
"</item>"
60+
}
61+
'</channel>'
62+
'</rss>'
63+
) -join ' ' -as [xml]
64+
65+
if ($rssXml) {
66+
$stringWriter = [IO.StringWriter]::new()
67+
$rssXml.Save($stringWriter)
68+
"$stringWriter"
69+
}
70+
}
71+
#endregion index.rss
72+

psturtle.com/build.ps1

Lines changed: 8 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -117,79 +117,10 @@ if ($PSScriptRoot -and "$PSScriptRoot" -ne "$pwd") {
117117
Push-Location $psScriptRoot
118118
}
119119

120-
if ($site.includes.'LastBuild.json' -is [Management.Automation.ExternalScriptInfo]) {
120+
if ($site.includes.'LastBuild.json' -is [Management.Automation.ExternalScriptInfo]) {
121121
. $site.includes.'LastBuild.json' > ./lastBuild.json
122122
}
123123

124-
#region index.rss
125-
if (-not $Site.NoRss) {
126-
$pagesByDate = @($site.PagesByUrl.GetEnumerator() |
127-
Sort-Object { $_.Value.Date } -Descending)
128-
$lastPubDate = if ($pagesByDate.Values.Date) {
129-
$pagesByDate[0].Value.Date.ToString('R')
130-
} else {
131-
$lastBuildTime.ToString('R')
132-
}
133-
$rssXml = @(
134-
'<rss version="2.0">'
135-
'<channel>'
136-
"<title>$([Security.SecurityElement]::Escape($(
137-
if ($site.Title) { $site.Title } else { $site.CNAME }
138-
)))</title>"
139-
"<link>$($site.RootUrl)</link>"
140-
"<description>$([Security.SecurityElement]::Escape($(
141-
if ($site.Description) { $site.Description } else { $site.Title }
142-
)))</description>"
143-
"<pubDate>$($lastPubDate)</pubDate>"
144-
"<lastBuildDate>$($lastBuildTime.ToString('R'))</lastBuildDate>"
145-
"<language>$([Security.SecurityElement]::Escape($site.Language))</language>"
146-
:nextPage foreach ($keyValue in $pagesByDate) {
147-
$key = $keyValue.Key
148-
$keyUri = $key -as [Uri]
149-
$page = $keyValue.Value
150-
if ($site.Disallow) {
151-
foreach ($disallow in $site.Disallow) {
152-
if ($keyUri.LocalPath -like "*$disallow*") { continue nextPage }
153-
if ($keyUri.AbsoluteUri -like "*$disallow*") { continue nextPage }
154-
}
155-
}
156-
if ($site.PagesByUrl[$key].NoIndex) { continue }
157-
if ($site.PagesByUrl[$key].NoSitemap) { continue }
158-
if ($site.PagesByUrl[$key].OutputFile.Extension -ne '.html') { continue }
159-
"<item>"
160-
"<title>$([Security.SecurityElement]::Escape($(
161-
if ($page.Title) { $page.Title }
162-
elseif ($site.Title) { $site.Title }
163-
else { $site.CNAME }
164-
)))</title>"
165-
if ($site.PagesByUrl[$key].Date -is [DateTime]) {
166-
"<pubDate>$($site.PagesByUrl[$key].Date.ToString('R'))</pubDate>"
167-
}
168-
"<description>$([Security.SecurityElement]::Escape($(
169-
if ($page.Description) { $page.Description }
170-
elseif ($site.Description) { $site.Description }
171-
)))</description>"
172-
"<link>$key</link>"
173-
"<guid isPermaLink='true'>$key</guid>"
174-
"</item>"
175-
}
176-
'</channel>'
177-
'</rss>'
178-
) -join ' ' -as [xml]
179-
180-
if ($rssXml) {
181-
$rssOutputPath = Join-Path $site.PSScriptRoot 'RSS' | Join-Path -ChildPath 'index.rss'
182-
if (-not (Test-Path $rssOutputPath)) {
183-
# Create the file if it doesn't exist
184-
$null = New-Item -ItemType File -Force $rssOutputPath
185-
}
186-
$rssXml.Save($rssOutputPath)
187-
}
188-
}
189-
190-
191-
#endregion index.rss
192-
193124
if ($site.includes.'Sitemap.xml' -is [Management.Automation.ExternalScriptInfo]) {
194125
. $site.includes.'Sitemap.xml' > sitemap.xml
195126
}
@@ -198,65 +129,15 @@ if ($site.includes.'Robots.txt' -is [Management.Automation.ExternalScriptInfo])
198129
. $site.includes.'Robots.txt' > robots.txt
199130
}
200131

201-
#region index.json
202-
if (-not $Site.NoIndex) {
203-
$fileIndex =
204-
if ($filePath) { Get-ChildItem -Recurse -File -Path $FilePath }
205-
else { Get-ChildItem -Recurse -File }
206-
207-
$replacement =
208-
if ($filePath) {
209-
"^" + ([regex]::Escape($filePath) -replace '\*','.{0,}?')
210-
} else {
211-
"^" + [regex]::Escape("$pwd")
212-
}
132+
if ($site.includes.'Index.rss' -is [Management.Automation.ExternalScriptInfo]) {
133+
New-Item -ItemType File -Path ./RSS/index.rss -Force -Value (
134+
. $site.includes.'Index.rss'
135+
)
136+
}
213137

214-
$indexObject = [Ordered]@{}
215-
$gitCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand('git', 'Application')
216-
foreach ($file in $fileIndex) {
217-
$gitDates =
218-
try {
219-
(& $gitCommand log --follow --format=%ci --date default $file.FullName *>&1) -as [datetime[]]
220-
} catch {
221-
$null
222-
}
223-
$LASTEXITCODE = 0
224-
225-
$indexObject[$file.FullName -replace $replacement] = [Ordered]@{
226-
Name = $file.Name
227-
Length = $file.Length
228-
Extension = $file.Extension
229-
CreatedAt =
230-
if ($gitDates) {
231-
$gitDates[-1]
232-
} else {
233-
$file.CreationTime
234-
}
235-
LastWriteTime =
236-
if ($gitDates) {
237-
$gitDates[0]
238-
} else {
239-
$file.LastWriteTime
240-
}
241-
}
242-
}
243-
244-
foreach ($indexKey in $indexObject.Keys) {
245-
if (-not $indexObject[$indexKey].CreatedAt) {
246-
if ($indexObject["$indexKey.ps1"].CreatedAt) {
247-
$indexObject[$indexKey].CreatedAt = $indexObject["$indexKey.ps1"].CreatedAt
248-
}
249-
}
250-
if (-not $indexObject[$indexKey].LastWriteTime) {
251-
if ($indexObject["$indexKey.ps1"].LastWriteTime) {
252-
$indexObject[$indexKey].LastWriteTime = $indexObject["$indexKey.ps1"].LastWriteTime
253-
}
254-
}
255-
}
256-
257-
$indexObject | ConvertTo-Json -Depth 4 > index.json
138+
if ($site.includes.'Index.json' -is [Management.Automation.ExternalScriptInfo]) {
139+
. $site.includes.'Index.json' > index.json
258140
}
259-
#endregion index.json
260141

261142
#region archive.zip
262143
if ($site.Archive) {

0 commit comments

Comments
 (0)