Skip to content

Commit 28af52d

Browse files
committed
subpages() can now take a path prefix.
1 parent f37c8c3 commit 28af52d

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

src/app.lua2p

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ local function setup()
398398
urlize = urlize,
399399

400400
chooseExistingFile = function(sitePathWithoutExt, exts)
401-
local pathWithoutExt = sitePathToPath(sitePathWithoutExt)
401+
local pathWithoutExt = sitePathToPath(sitePathWithoutExt, 2)
402402

403403
for _, ext in ipairs(exts) do
404404
local pathRel = pathWithoutExt.."."..ext
@@ -413,12 +413,12 @@ local function setup()
413413
end,
414414

415415
fileExists = function(sitePath)
416-
local pathRel = sitePathToPath(sitePath)
416+
local pathRel = sitePathToPath(sitePath, 2)
417417
return (isFile(DIR_CONTENT.."/"..pathRel))
418418
end,
419419

420420
fileExistsInOutput = function(sitePath, skipRewriting)
421-
local pathRel = sitePathToPath(sitePath)
421+
local pathRel = sitePathToPath(sitePath, 2)
422422
local pathOutputRel = skipRewriting and pathRel or rewriteOutputPath(pathRel)
423423
return (isFile(DIR_OUTPUT.."/"..pathOutputRel))
424424
end,
@@ -503,7 +503,7 @@ local function setup()
503503
thumbH, isLink = 0, thumbH
504504
end
505505

506-
local pathImageRel = sitePathToPath(sitePathImageRel)
506+
local pathImageRel = sitePathToPath(sitePathImageRel, 2)
507507
local thumbInfo = createThumbnail(pathImageRel, thumbW, thumbH, 2)
508508
local thumbUrl = toUrl("/"..thumbInfo.path)
509509

@@ -582,7 +582,7 @@ local function setup()
582582
onlyFilenames, filter = false, onlyFilenames
583583
end
584584

585-
local pathRel = sitePathToPath(sitePath)
585+
local pathRel = sitePathToPath(sitePath, 2)
586586
local dirPath = pathRel == "" and DIR_CONTENT or DIR_CONTENT.."/"..pathRel
587587
local sitePaths = {}
588588

@@ -625,7 +625,7 @@ local function setup()
625625
end,
626626

627627
getCompleteOutputPath = function(sitePath)
628-
local pathRel = sitePathToPath(sitePath)
628+
local pathRel = sitePathToPath(sitePath, 2)
629629
local pathOutputRel = rewriteOutputPath(pathRel)
630630
return DIR_OUTPUT.."/"..pathOutputRel
631631
end,
@@ -656,7 +656,7 @@ local function setup()
656656

657657
getImageDimensions = function(sitePath)
658658
local getImageDimensions_internal = getImageDimensions -- :BetterTraceback :BetterNameForRedirectedFunctionInErrorMessage
659-
local wOrNil, hOrErr = getImageDimensions_internal(sitePathToPath(sitePath))
659+
local wOrNil, hOrErr = getImageDimensions_internal(sitePathToPath(sitePath, 2))
660660
return wOrNil, hOrErr
661661
end,
662662

@@ -907,7 +907,7 @@ local function setup()
907907
assertContext("config", "generateFromTemplate")
908908
!ARGS "sitePathRel,template:string ? paramsOrInit:table,function"
909909

910-
local pathRel = sitePathToPath(sitePathRel)
910+
local pathRel = sitePathToPath(sitePathRel, 2)
911911
local page = newPage(pathRel, false)
912912

913913
if type(paramsOrInit) == "table" then
@@ -928,14 +928,14 @@ local function setup()
928928
assertContext("config", "outputRaw")
929929
!ARGS "sitePathRel,contents:string"
930930

931-
local pathRel = sitePathToPath(sitePathRel)
931+
local pathRel = sitePathToPath(sitePathRel, 2)
932932
writeOutputFile("raw", pathRel, sitePathRel, contents, nil, "")
933933
end,
934934

935935
preserveRaw = function(sitePathRel)
936936
assertContext("config", "preserveRaw")
937937

938-
local pathRel = sitePathToPath(sitePathRel)
938+
local pathRel = sitePathToPath(sitePathRel, 2)
939939
local pathOutputRel = rewriteOutputPath(pathRel)
940940
local path = DIR_OUTPUT.."/"..pathOutputRel
941941

@@ -956,20 +956,25 @@ local function setup()
956956
return getContext().page.url.v:sub(1, #urlPrefix) == urlPrefix
957957
end,
958958

959-
subpages = function(allowCurrentPage)
960-
assertContext("template", "subpages")
959+
subpages = function(pathPrefixOrAllowCurrentPage)
960+
local subpages = {}
961+
local pageCurrent, pathPrefix, allowCurrentPage
961962

962-
local pageCurrent = getContext().page
963-
local dir = getDirectory(pageCurrent._path)
964-
local subpages = {}
963+
if type(pathPrefixOrAllowCurrentPage) == "string" then
964+
pageCurrent = nil
965+
pathPrefix = sitePathToPath(pathPrefixOrAllowCurrentPage, 2)
966+
allowCurrentPage = false -- Doesn't matter.
967+
else
968+
assertContext("template", "subpages")
969+
pageCurrent = getContext().page
970+
pathPrefix = pageCurrent._path:gsub("[^/]+$", "")
971+
allowCurrentPage = pathPrefixOrAllowCurrentPage
972+
end
965973

966974
for _, page in ipairs(site._pages) do
967975
if (page ~= pageCurrent or allowCurrentPage) and (
968-
dir == ""
969-
or (
970-
page._path:byte(#dir+1) == !(string.byte"/") and
971-
page._path:find(dir, 1, true) == 1
972-
)
976+
pathPrefix == ""
977+
or page._path:find(pathPrefix, 1, true) == 1
973978
) then
974979
if not page._isGenerated then
975980
generateFromTemplateFile(page)
@@ -1204,7 +1209,7 @@ local function buildWebsite()
12041209
local function getT(kPath, default, kType, vType) -- Table
12051210
return get(
12061211
kPath, default, lineAssertTable, kType, vType,
1207-
"config.%s must be a table of [%s]=%s.",
1212+
"config.lua: config.%s must be a table of [%s]=%s.",
12081213
kPath, (kType or "value"), (vType or "value")
12091214
)
12101215
end

src/functions.lua2p

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,7 +2270,7 @@ end
22702270
do
22712271
local function removeIndexFilename(pathRel)
22722272
local sitePath = pathToSitePath(pathRel) :gsub("/index%.%w+$", "/")
2273-
return sitePathToPath(sitePath)
2273+
return (sitePathToPath(sitePath, 2))
22742274
end
22752275

22762276
function _G.newPage(pathRel, isRedirection)
@@ -2425,9 +2425,11 @@ function _G.pathToSitePath(pathRel)
24252425
return "/"..pathRel
24262426
end
24272427

2428-
function _G.sitePathToPath(sitePath)
2428+
-- sitePathToPath( sitePath, errorLevel=1 )
2429+
function _G.sitePathToPath(sitePath, errLevel)
24292430
if not sitePath:find"^/" then
2430-
errorf(2, "Path is not a valid site path - they must start with '/': %s", sitePath)
2431+
errLevel = 1 + (errLevel or 1)
2432+
errorf(errLevel, "Path is not a valid site path - they must start with '/': %s", sitePath)
24312433
end
24322434
return (sitePath:gsub("^/", ""))
24332435
end
@@ -2560,10 +2562,10 @@ function _G.rewriteOutputPath(pathRel)
25602562
errorf("config.rewriteOutputPath() returned an empty string. (%s)", sitePath)
25612563
end
25622564

2563-
return (sitePathToPath(sitePathNew))
2565+
return (sitePathToPath(sitePathNew, 2))
25642566

25652567
else
2566-
return (sitePathToPath(F(site._outputPathFormat, sitePath)))
2568+
return (sitePathToPath(F(site._outputPathFormat, sitePath), 2))
25672569
end
25682570
end
25692571

0 commit comments

Comments
 (0)