Skip to content

Commit b229f28

Browse files
committed
Added commands 'new feed' and 'new sitemap'
1 parent 28af52d commit b229f28

File tree

3 files changed

+162
-54
lines changed

3 files changed

+162
-54
lines changed

build/meta.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
templateToString, templateToStringUtf16
2828
toWindowsPath
2929
traverseDirectory
30+
unindent
3031
utf16ToUtf8, utf8ToUtf16
3132
zipDirectory, zipFiles
3233
@@ -447,3 +448,16 @@ end
447448

448449

449450

451+
function _G.unindent(s)
452+
local indent = s:match"^\t+"
453+
if indent then
454+
s = s
455+
:gsub("\n"..indent, "\n")
456+
:sub(#indent+1)
457+
:gsub("\t+$", "")
458+
end
459+
460+
return s
461+
end
462+
463+

src/app.lua2p

Lines changed: 139 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,8 @@ local function setup()
4242
local kind = programArguments[i] or errorNoPos("[Arguments] Missing kind after 'new'.")
4343
i = i + 1
4444

45-
if kind == "page" then
46-
local pathRel = programArguments[i] or errorNoPos("[Arguments] Missing path after 'page'.")
47-
pathRel = pathRel:gsub("^/", "")
48-
i = i + 1
49-
50-
if programArguments[i] then
51-
errorNoPos("[Arguments] Unknown argument '%s'.", programArguments[i])
52-
end
53-
54-
local filename = getFilename(pathRel)
55-
local basename = getBasename(filename)
56-
local title = basename :gsub("%-+", " ") :gsub("^%a", string.upper) :gsub(" %a", string.upper)
57-
58-
local contents = formatTemplate(
59-
[=[
60-
{{
61-
page.title = :titleQuoted:
62-
page.date = ":date:"
63-
}}
64-
65-
:content:
66-
]=], {
67-
titleQuoted = F("%q", title),
68-
content = "",
69-
date = getDatetime(),
70-
}
71-
)
72-
73-
local path = DIR_CONTENT.."/"..pathRel
74-
if lfs.attributes(path, "mode") then
75-
errorNoPos("File or directory already exists: %s", path)
76-
end
77-
78-
createDirectory(getDirectory(path))
79-
80-
local file, err = io.open(path, "wb")
81-
if not file then errorNoPos(err) end
82-
83-
file:write(contents)
84-
file:close()
85-
86-
printf("Created page: %s", path)
87-
88-
elseif kind == "site" then
45+
----------------
46+
if kind == "site" then
8947
local pathToSite = programArguments[i] or errorNoPos("[Arguments] Missing path after 'site'.")
9048
i = i+1
9149

@@ -181,6 +139,140 @@ local function setup()
181139

182140
printf("Created site: %s", pathToSite)
183141

142+
----------------
143+
elseif kind == "page" then
144+
local pathRel = programArguments[i] or errorNoPos("[Arguments] Missing path after 'page'.")
145+
pathRel = pathRel:gsub("^/", "")
146+
i = i + 1
147+
148+
if programArguments[i] then
149+
errorNoPos("[Arguments] Unknown argument '%s'.", programArguments[i])
150+
end
151+
152+
local filename = getFilename(pathRel)
153+
local basename = getBasename(filename)
154+
local title = basename :gsub("%-+", " ") :gsub("^%a", string.upper) :gsub(" %a", string.upper)
155+
156+
local contents = formatTemplate(
157+
[=[
158+
{{
159+
page.title = :titleQuoted:
160+
page.date = ":date:"
161+
}}
162+
163+
:content:
164+
]=], {
165+
titleQuoted = F("%q", title),
166+
content = "",
167+
date = getDatetime(),
168+
}
169+
)
170+
171+
local path = DIR_CONTENT.."/"..pathRel
172+
if lfs.attributes(path, "mode") then
173+
errorNoPos("File or directory already exists: %s", path)
174+
end
175+
176+
createDirectory(getDirectory(path))
177+
178+
local file, err = io.open(path, "wb")
179+
if not file then errorNoPos(err) end
180+
181+
file:write(contents)
182+
file:close()
183+
184+
printf("Created page: %s", path)
185+
186+
----------------
187+
elseif kind == "feed" then
188+
local pathRel = "feed.xml"
189+
190+
if programArguments[i] then
191+
pathRel = programArguments[i]:gsub("^/", "")
192+
i = i + 1
193+
194+
if programArguments[i] then
195+
errorNoPos("[Arguments] Unknown argument '%s'.", programArguments[i])
196+
end
197+
end
198+
199+
local contents = !(unindent[=[
200+
<?xml version="1.0" encoding="UTF-8"?>
201+
<rss version="2.0">
202+
<channel>
203+
<title>{{ echo(site.title) }}</title>
204+
<link>{{ url(site.baseUrl) }}</link>
205+
<atom:link href="{{ url(page.permalink) }}" rel="self" type="application/rss+xml"/>
206+
<description>{{ echo(site.description) }}</description>
207+
<language>{{ site.languageCode }}</language>
208+
<lastBuildDate>{{ os.date"%Y-%m-%d" }}</lastBuildDate>
209+
210+
{{ fori subpage in clampArray(subpages(), 15) }}
211+
{{ if not subpage.isIndex }}
212+
<item>
213+
<title>{{ echo(subpage.title) }}</title>
214+
<description><![CDATA[{{ subpage.description ~= "" and subpage.description or summarize(subpage.content, 400, true) }}]]></description>
215+
<pubDate>{{ os.date("!%a, %d %b %Y %H:%M:%S GMT", toTime(subpage.publishDate)) }}</pubDate>
216+
<link>{{ url(subpage.permalink) }}</link>
217+
<guid>{{ url(subpage.permalink) }}</guid>
218+
</item>
219+
{{ end }}
220+
{{ end }}
221+
</channel>
222+
</rss>
223+
]=])
224+
225+
local path = DIR_CONTENT.."/"..pathRel
226+
if lfs.attributes(path, "mode") then
227+
errorNoPos("File or directory already exists: %s", path)
228+
end
229+
230+
createDirectory(getDirectory(path))
231+
232+
local file, err = io.open(path, "wb")
233+
if not file then errorNoPos(err) end
234+
235+
file:write(contents)
236+
file:close()
237+
238+
printf("Created page: %s", path)
239+
240+
----------------
241+
elseif kind == "sitemap" then
242+
if programArguments[i] then
243+
errorNoPos("[Arguments] Unknown argument '%s'.", programArguments[i])
244+
end
245+
246+
local contents = !(unindent[=[
247+
<?xml version="1.0" encoding="UTF-8"?>
248+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
249+
{{ fori subpage in subpages() }}
250+
<url>
251+
<loc>{{ url(subpage.permalink) }}</loc>
252+
<lastmod>{{ os.date("%Y-%m-%d", toTime(subpage.dateModified)) }}</lastmod>
253+
<changefreq>daily</changefreq>
254+
<priority>0.5</priority>
255+
</url>
256+
{{ end }}
257+
</urlset>
258+
]=])
259+
260+
local path = DIR_CONTENT.."/sitemap.xml"
261+
if lfs.attributes(path, "mode") then
262+
errorNoPos("File or directory already exists: %s", path)
263+
end
264+
265+
createDirectory(getDirectory(path))
266+
267+
local file, err = io.open(path, "wb")
268+
if not file then errorNoPos(err) end
269+
270+
file:write(contents)
271+
file:close()
272+
273+
printf("Created sitemap: %s", path)
274+
275+
----------------
184276
else
185277
errorNoPos("[Arguments] Unknown kind '%s' after 'new'.", kind)
186278
end
@@ -716,7 +808,7 @@ local function setup()
716808
return _dataParsers
717809
end,
718810

719-
-- array = function( array, length ) -- @Doc
811+
-- array = clampArray( array, length ) -- @Doc
720812
clampArray = function(t, len)
721813
for i = #t, len+1, -1 do
722814
t[i] = nil
@@ -813,8 +905,8 @@ local function setup()
813905
charsRemaining = charsRemaining - len
814906

815907
if charsRemaining < 0 then
816-
!local ELLIPSIS = "(...)"
817-
local nextCharPos = #node + 1
908+
!local ELLIPSIS = "..."
909+
local nextCharPos = #node + 1
818910

819911
for i = charsRemaining, !(#ELLIPSIS-1) do
820912
nextCharPos = utf8.getStartOfCharacter(node, nextCharPos-1)

testsite/content/feed.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
<lastBuildDate>{{ os.date"%Y-%m-%d" }}</lastBuildDate>
1010

1111
{{ fori subpage in clampArray(subpages(), 15) }}
12-
<item>
13-
<title>{{ echo(subpage.title) }}</title>
14-
<description><![CDATA[{{ subpage.description ~= "" and subpage.description or summarize(subpage.content, 400, true) }}]]></description>
15-
<pubDate>{{ os.date("!%a, %d %b %Y %H:%M:%S GMT", toTime(subpage.publishDate)) }}</pubDate>
16-
<link>{{ url(subpage.permalink) }}</link>
17-
<guid>{{ url(subpage.permalink) }}</guid>
18-
</item>
12+
{{ if not subpage.isIndex }}
13+
<item>
14+
<title>{{ echo(subpage.title) }}</title>
15+
<description><![CDATA[{{ subpage.description ~= "" and subpage.description or summarize(subpage.content, 400, true) }}]]></description>
16+
<pubDate>{{ os.date("!%a, %d %b %Y %H:%M:%S GMT", toTime(subpage.publishDate)) }}</pubDate>
17+
<link>{{ url(subpage.permalink) }}</link>
18+
<guid>{{ url(subpage.permalink) }}</guid>
19+
</item>
20+
{{ end }}
1921
{{ end }}
2022
</channel>
2123
</rss>

0 commit comments

Comments
 (0)