Skip to content

Commit 772c6df

Browse files
committed
Moved site variables to site object.
Fixed scripts not getting preloaded when they should've.
1 parent 2fbfe31 commit 772c6df

File tree

3 files changed

+199
-196
lines changed

3 files changed

+199
-196
lines changed

src/app.lua2p

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ local function setup()
280280

281281
_G.scriptEnvironmentGlobals = {
282282
_WEBGEN_VERSION = WEBGEN_VERSION, -- @Deprecated
283-
DATA_FILE_EXTENSIONS = DATA_FILE_EXTENSIONS,
284-
IMAGE_EXTENSIONS = IMAGE_EXTENSIONS,
283+
DATA_FILE_EXTENSIONS = {unpack(DATA_FILE_EXTENSIONS)},
284+
IMAGE_EXTENSIONS = {unpack(IMAGE_EXTENSIONS)},
285285
WEBGEN_VERSION = WEBGEN_VERSION,
286286

287287
-- Lua globals.
@@ -314,16 +314,16 @@ local function setup()
314314
xpcall = xpcall,
315315

316316
next = function(t)
317-
if isDataFolderReader(t) then preloadData(t) end
317+
if isDataFolderReader(t) then preloadData(t)
318+
elseif isScriptFolderReader(t) then preloadScripts(t) end
318319

319320
return next(t)
320321
end,
321322

322323
pairs = function(t)
323-
if isDataFolderReader(t) then
324-
return (pairsSorted(preloadData(t)))
325-
end
326-
return pairs(t)
324+
if isDataFolderReader(t) then return pairsSorted(preloadData(t)), t, nil end
325+
if isScriptFolderReader(t) then return pairsSorted(preloadScripts(t)), t, nil end
326+
return next, t, nil
327327
end,
328328

329329
-- Lua modules.
@@ -628,8 +628,11 @@ local function setup()
628628
end,
629629

630630
pairsSorted = function(t)
631+
if isDataFolderReader(t) then preloadData(t)
632+
elseif isScriptFolderReader(t) then preloadScripts(t) end
633+
631634
local pairsSorted_internal = pairsSorted -- :BetterTraceback :BetterNameForRedirectedFunctionInErrorMessage
632-
return (pairsSorted_internal(isDataFolderReader(t) and preloadData(t) or t))
635+
return (pairsSorted_internal(t))
633636
end,
634637

635638
validateUrls = function(urls)
@@ -658,7 +661,7 @@ local function setup()
658661
-- There's no reason to restrict context for this function, I don't think. 2021-04-27
659662
local fileInfos = {}
660663

661-
for _, entry in ipairs(writtenOutputEntries) do
664+
for _, entry in ipairs(site._writtenOutputEntries) do
662665
table.insert(fileInfos, {
663666
sourcePath = entry.path ~= "" and pathToSitePath(entry.path) or "",
664667
path = pathToSitePath(entry.pathOut),
@@ -677,7 +680,7 @@ local function setup()
677680
-- Just like getOutputtedFiles(), there's no reason to restrict context for this function, I don't think. 2021-05-18
678681
local generatedPages = {}
679682

680-
for _, page in ipairs(pages) do
683+
for _, page in ipairs(site._pages) do
681684
if page._isGenerated then
682685
table.insert(generatedPages, getProtectionWrapper(page, "page"))
683686
end
@@ -773,7 +776,7 @@ local function setup()
773776
generateFromTemplateString(page, template, nil, onPageInit)
774777

775778
if page.isPage.v and not page._isSkipped then
776-
table.insert(pages, page)
779+
table.insert(site._pages, page)
777780
end
778781

779782
return (getProtectionWrapper(page, "page"))
@@ -818,7 +821,7 @@ local function setup()
818821
local dir = getDirectory(pageCurrent._path)
819822
local subpages = {}
820823

821-
for _, page in ipairs(pages) do
824+
for _, page in ipairs(site._pages) do
822825
if (page ~= pageCurrent or allowCurrentPage) and page._path:sub(1, #dir) == dir then
823826
if not page._isGenerated then
824827
generateFromTemplateFile(page)
@@ -941,7 +944,7 @@ local function setup()
941944
v = getContext()._scriptEnvironmentGlobals[k]
942945
if v ~= nil then return v end
943946

944-
v = scriptFunctions[k]
947+
v = site._scriptFunctions[k]
945948
if v then return v end
946949

947950
local basePath = F("%s/%s", DIR_SCRIPTS, k)
@@ -960,12 +963,12 @@ local function setup()
960963
errorNoPos("%s did not return a function.", path)
961964
end
962965

963-
scriptFunctions[k] = v
966+
site._scriptFunctions[k] = v
964967
return v
965968

966969
elseif isDirectory(basePath) then
967-
v = newScriptFolderReader(basePath)
968-
scriptFunctions[k] = v
970+
v = newScriptFolderReader(basePath)
971+
site._scriptFunctions[k] = v
969972
return v
970973
end
971974

@@ -1002,9 +1005,9 @@ if done then return true end
10021005
local function buildWebsite()
10031006
local startTime = getTimeBetter()
10041007

1008+
_G.site = newSite()
10051009
_G.oncePrints = {}
10061010
_G.warningCount = 0
1007-
resetSiteVariables()
10081011

10091012
scriptEnvironmentGlobals.site = getProtectionWrapper(site, "site")
10101013
scriptEnvironmentGlobals.data = newDataFolderReader(DIR_DATA, true)
@@ -1090,35 +1093,35 @@ local function buildWebsite()
10901093

10911094
site.redirections.v = getT("redirections", site.redirections.v, "string", "string")
10921095

1093-
_G.ignoreFiles = getA("ignoreFiles", ignoreFiles, "string")
1094-
_G.ignoreFolders = getA("ignoreFolders", ignoreFolders, "string")
1095-
_G.ignorePaths = getA("ignorePaths", ignorePaths, "string")
1096+
site._ignoreFiles = getA("ignoreFiles", site._ignoreFiles, "string")
1097+
site._ignoreFolders = getA("ignoreFolders", site._ignoreFolders, "string")
1098+
site._ignorePaths = getA("ignorePaths", site._ignorePaths, "string")
10961099

1097-
_G.fileTypes = getT("types", fileTypes, "string", "string")
1098-
_G.fileProcessors = getT("processors", fileProcessors, "string", "function")
1100+
site._fileTypes = getT("types", site._fileTypes, "string", "string")
1101+
site._fileProcessors = getT("processors", site._fileProcessors, "string", "function")
10991102
local customDataTextParsers = getT("dataTextParsers", {}, "string", "function") -- @Doc
11001103
local customDataBinaryParsers = getT("dataBinaryParsers", {}, "string", "function") -- @Doc
11011104

11021105
local htaRedirect = getV("htaccess.redirect", false, "boolean")
11031106
local htaWww = getV("htaccess.www", false, "boolean")
1104-
_G.htaErrors = getT("htaccess.errors", htaErrors, "number", "string")
1107+
site._htaErrors = getT("htaccess.errors", site._htaErrors, "number", "string")
11051108
local htaNoIndexes = getV("htaccess.noIndexes",false, "boolean")
11061109
local htaPrettyUrlDir = getV("htaccess.XXX_prettyUrlDirectory", "", "string")
11071110
local htaDenyAccess = getA("htaccess.XXX_denyDirectAccess", {}, "string")
11081111
local htaccess = getT("htaccess", nil, "string")
11091112
local handleHtaccess = htaccess ~= nil
11101113

1111-
_G.outputPathFormat = get("rewriteOutputPath", "%s", NOOP)
1112-
_G.rewriteExcludes = getA("rewriteExcludes", rewriteExcludes, "string")
1114+
site._outputPathFormat = get("rewriteOutputPath", "%s", NOOP)
1115+
site._rewriteExcludes = getA("rewriteExcludes", site._rewriteExcludes, "string")
11131116

11141117
local onBefore = getV("before", nil, "function")
11151118
local onAfter = getV("after", nil, "function")
11161119
local onValidate = getV("validate", nil, "function")
11171120

1118-
_G.autoLockPages = getV("autoLockPages", false, "boolean")
1119-
_G.noTrailingSlash = getV("removeTrailingSlashFromPermalinks", false, "boolean") -- @Doc? @Hack? Also, this affects page.url too now. 2021-04-28
1121+
site._autoLockPages = getV("autoLockPages", site._autoLockPages, "boolean")
1122+
site._noTrailingSlash = getV("removeTrailingSlashFromPermalinks", site._noTrailingSlash, "boolean") -- @Doc? @Hack? Also, this affects page.url too now. 2021-04-28
11201123

1121-
_G.redirectionLayout = getV("redirectionLayout", redirectionLayout, "string")
1124+
site._redirectionLayout = getV("redirectionLayout", site._redirectionLayout, "string")
11221125

11231126
for k in pairs(config) do
11241127
timestampPrintWarning("Unknown config field '%s'.", k)
@@ -1127,12 +1130,12 @@ local function buildWebsite()
11271130
timestampPrintWarning("Unknown config.htaccess field '%s'.", k)
11281131
end
11291132

1130-
if not isAny(type(outputPathFormat), "string","function") then
1133+
if not isAny(type(site._outputPathFormat), "string","function") then
11311134
errorNoPos("config.lua: config.rewriteOutputPath must be a string or a function.")
11321135
end
11331136

11341137
-- Validate config.types
1135-
for ext, fileType in pairs(fileTypes) do
1138+
for ext, fileType in pairs(site._fileTypes) do
11361139
if ext ~= ext:lower() then
11371140
errorNoPos("config.lua: File extensions must be lower case: config.types[\"%s\"]", ext)
11381141
elseif not FILE_TYPE_SET[fileType] then
@@ -1141,7 +1144,7 @@ local function buildWebsite()
11411144
end
11421145

11431146
-- Validate config.processors
1144-
for ext in pairs(fileProcessors) do
1147+
for ext in pairs(site._fileProcessors) do
11451148
if ext ~= ext:lower() then
11461149
errorNoPos("config.lua: File extensions must be lower case: config.processors[\"%s\"]", ext)
11471150
end
@@ -1201,7 +1204,7 @@ local function buildWebsite()
12011204
timestampPrint("Generating website...")
12021205

12031206
for category in pairs(OUTPUT_CATEGORY_SET) do
1204-
outputFileCounts[category] = 0
1207+
site._outputFileCounts[category] = 0
12051208
end
12061209

12071210
if onBefore then
@@ -1216,18 +1219,18 @@ local function buildWebsite()
12161219
--
12171220
local nonPagePages = {}
12181221

1219-
traverseFiles(DIR_CONTENT, ignoreFolders, function(path, pathRel, filename, extLower)
1222+
traverseFiles(DIR_CONTENT, site._ignoreFolders, function(path, pathRel, filename, extLower)
12201223
-- Ignored.
12211224
if
1222-
(ignoreFiles[1] and isStringMatchingAnyPattern(filename, ignoreFiles)) or
1223-
(ignorePaths[1] and isStringMatchingAnyPattern(pathToSitePath(pathRel), ignorePaths))
1225+
(site._ignoreFiles[1] and isStringMatchingAnyPattern(filename, site._ignoreFiles)) or
1226+
(site._ignorePaths[1] and isStringMatchingAnyPattern(pathToSitePath(pathRel), site._ignorePaths))
12241227
then
12251228
-- void
12261229

12271230
-- Template.
1228-
elseif fileTypes[extLower] then
1231+
elseif site._fileTypes[extLower] then
12291232
-- Generate these later so e.g. urlExists() works better.
1230-
local pagesArray = (fileTypes[extLower] == "othertemplate" and nonPagePages or pages)
1233+
local pagesArray = (site._fileTypes[extLower] == "othertemplate" and nonPagePages or site._pages)
12311234
table.insert(pagesArray, newPage(pathRel, false))
12321235

12331236
-- Special.
@@ -1241,7 +1244,7 @@ local function buildWebsite()
12411244

12421245
-- Non-templates should be OK to preserve (if there's no file processor).
12431246
local oldModTime = (
1244-
not ignoreModificationTimes and not fileProcessors[extLower]
1247+
not ignoreModificationTimes and not site._fileProcessors[extLower]
12451248
and lfs.attributes(DIR_OUTPUT.."/"..pathOutputRel, "modification")
12461249
or nil
12471250
)
@@ -1259,7 +1262,7 @@ local function buildWebsite()
12591262
generateFromTemplateFile(page)
12601263
end
12611264

1262-
for _, page in ipairs(pages) do
1265+
for _, page in ipairs(site._pages) do
12631266
if not (page._isGenerated or page._isSkipped) then
12641267
generateFromTemplateFile(page)
12651268
end
@@ -1284,7 +1287,7 @@ local function buildWebsite()
12841287
allRedirections[url] = targetUrl
12851288
end
12861289

1287-
for _, page in ipairs(pages) do
1290+
for _, page in ipairs(site._pages) do
12881291
if page.isPage.v and not page._isSkipped then
12891292
assert(page._isGenerated)
12901293

@@ -1322,13 +1325,13 @@ local function buildWebsite()
13221325

13231326
-- Validate targets.
13241327
for url, targetUrl in pairsSorted(allRedirections) do
1325-
if targetUrl:find"^/" and not writtenOutputUrls[targetUrl] then
1328+
if targetUrl:find"^/" and not site._writtenOutputUrls[targetUrl] then
13261329
errorNoPos("Missing target page for redirection: %s -> %s", url, targetUrl)
13271330
end
13281331
end
13291332

13301333
-- Write redirections.
1331-
for _, page in ipairs(pages) do
1334+
for _, page in ipairs(site._pages) do
13321335
if page.isPage.v and not page._isSkipped then
13331336
for _, url in ipairs(page.aliases.v) do
13341337
generateRedirection(url, page.url.v, page._path)
@@ -1340,7 +1343,7 @@ local function buildWebsite()
13401343
local sourcePath = ""
13411344

13421345
if targetUrl:find"^/" then
1343-
for _, page in ipairs(pages) do
1346+
for _, page in ipairs(site._pages) do
13441347
if page.url.v == targetUrl then
13451348
sourcePath = page._path
13461349
break
@@ -1368,10 +1371,10 @@ local function buildWebsite()
13681371
contents = contents.."\nOptions -Indexes\n"
13691372
end
13701373

1371-
if next(htaErrors) then
1374+
if next(site._htaErrors) then
13721375
local b = newStringBuilder()
13731376

1374-
for errCode, target in pairsSorted(htaErrors) do
1377+
for errCode, target in pairsSorted(site._htaErrors) do
13751378
-- The target may be a URL or HTML code.
13761379
--
13771380
-- @Incomplete: Does Apache rewrite URLs to error documents? I don't think v2.2 did, but maybe
@@ -1417,11 +1420,11 @@ local function buildWebsite()
14171420
b("\n")
14181421
end
14191422

1420-
if htaRedirect and (next(writtenRedirects) or next(unwrittenRedirects)) then
1423+
if htaRedirect and (next(site._writtenRedirects) or next(site._unwrittenRedirects)) then
14211424
handlingSpecialRedirections = true
14221425
b("\t# Redirect moved resources.\n")
14231426

1424-
for url, targetUrl in pairsSorted(writtenRedirects) do
1427+
for url, targetUrl in pairsSorted(site._writtenRedirects) do
14251428
if targetUrl:sub(1, #site.baseUrl.v) == site.baseUrl.v then
14261429
targetUrl = targetUrl:sub(#site.baseUrl.v) -- Note: We keep the initial '/'.
14271430
end
@@ -1430,7 +1433,7 @@ local function buildWebsite()
14301433
b('\tRewriteRule .* "%s" [R=301,L]\n', escapeRuleSub(targetUrl))
14311434
end
14321435

1433-
for url, targetUrl in pairsSorted(unwrittenRedirects) do
1436+
for url, targetUrl in pairsSorted(site._unwrittenRedirects) do
14341437
if targetUrl:sub(1, #site.baseUrl.v) == site.baseUrl.v then
14351438
targetUrl = targetUrl:sub(#site.baseUrl.v) -- Note: We keep the initial '/'.
14361439
end
@@ -1454,7 +1457,7 @@ local function buildWebsite()
14541457
b("\n")
14551458
end
14561459

1457-
if noTrailingSlash then
1460+
if site._noTrailingSlash then
14581461
b("\t# Remove trailing slash.\n")
14591462
b("\tRewriteCond %{REQUEST_FILENAME} !-d\n")
14601463
b("\tRewriteCond %{REQUEST_URI} ./$\n")
@@ -1513,11 +1516,11 @@ local function buildWebsite()
15131516
writeOutputFile("raw", ".htaccess", "/.htaccess", contents, nil, (fileExisted and ".htaccess" or ""))
15141517
end--if handleHtaccess
15151518

1516-
if not handlingSpecialRedirections and next(unwrittenRedirects) then
1517-
local count = #getKeys(unwrittenRedirects)
1519+
if not handlingSpecialRedirections and next(site._unwrittenRedirects) then
1520+
local count = #getKeys(site._unwrittenRedirects)
15181521
timestampPrintWarning("%d URL redirection%s could not be satisfied:", count, (count == 1 and "" or "s"))
15191522

1520-
for url, targetUrl in pairsSorted(unwrittenRedirects) do
1523+
for url, targetUrl in pairsSorted(site._unwrittenRedirects) do
15211524
local s = F(" %s", url, targetUrl)
15221525
io.stderr:write(s, "\n")
15231526
log(s)
@@ -1535,7 +1538,7 @@ local function buildWebsite()
15351538
-- Clean up old generated files and folders
15361539
--
15371540
traverseFiles(DIR_OUTPUT, nil, function(path, pathOutputRel, filename, extLower)
1538-
if not writtenOutputFiles[pathOutputRel] then
1541+
if not site._writtenOutputFiles[pathOutputRel] then
15391542
timestampPrintVerbose("Removing: %s", path)
15401543
assert(os.remove(path))
15411544
end
@@ -1552,14 +1555,14 @@ local function buildWebsite()
15521555
local endTime = getTimeBetter()
15531556

15541557
print!(SEPARATOR)
1555-
printf("Files: %d", outputFileCount)
1556-
printf(" Pages: %d (Skipped: %d)", outputFileCounts["page"], outputFileSkippedPageCount)
1557-
printf(" OtherTemplates: %d", outputFileCounts["template"])
1558+
printf("Files: %d", site._outputFileCount)
1559+
printf(" Pages: %d (Skipped: %d)", site._outputFileCounts["page"], site._outputFileSkippedPageCount)
1560+
printf(" OtherTemplates: %d", site._outputFileCounts["template"])
15581561
printf(" OtherFiles: %d (Preserved: %d, %.1f%%)",
1559-
outputFileCounts["raw"], outputFilePreservedCount,
1560-
outputFileCounts["raw"] == 0 and 100 or outputFilePreservedCount/outputFileCounts["raw"]*100
1562+
site._outputFileCounts["raw"], site._outputFilePreservedCount,
1563+
site._outputFileCounts["raw"] == 0 and 100 or site._outputFilePreservedCount/site._outputFileCounts["raw"]*100
15611564
)
1562-
printf("TotalSize: %s", formatBytes(outputFileByteCount))
1565+
printf("TotalSize: %s", formatBytes(site._outputFileByteCount))
15631566
printf("Time: %.2f seconds", endTime-startTime)
15641567
print!(SEPARATOR)
15651568

@@ -1595,13 +1598,13 @@ if autobuild then
15951598
end
15961599

15971600
local function checkDirectory(dir, silent)
1598-
traverseDirectory(dir, ignoreFolders, function(path, pathRel, filename, itemType)
1601+
traverseDirectory(dir, site._ignoreFolders, function(path, pathRel, filename, itemType)
15991602
if itemType == "directory" then
16001603
dirTree[path] = -1
16011604

16021605
elseif itemType == "file" then
1603-
if ignoreFiles[1] and isStringMatchingAnyPattern(filename, ignoreFiles) then return end
1604-
if ignorePaths[1] and isStringMatchingAnyPattern(pathToSitePath(pathRel), ignorePaths) then return end
1606+
if site._ignoreFiles[1] and isStringMatchingAnyPattern(filename, site._ignoreFiles) then return end
1607+
if site._ignorePaths[1] and isStringMatchingAnyPattern(pathToSitePath(pathRel), site._ignorePaths) then return end
16051608
checkFile(path, silent)
16061609
end
16071610

0 commit comments

Comments
 (0)