Skip to content

Commit 80d79fb

Browse files
committed
The scripts folder can have subfolders.
1 parent 95a9ba1 commit 80d79fb

File tree

6 files changed

+91
-7
lines changed

6 files changed

+91
-7
lines changed

src/app.lua2p

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,11 @@ local function setup()
927927
v = scriptFunctions[k]
928928
if v then return v end
929929

930-
local path = F("%s/%s.lua", DIR_SCRIPTS, k)
930+
local basePath = F("%s/%s", DIR_SCRIPTS, k)
931+
local path = basePath..".lua"
931932

932933
if isFile(path) then
934+
-- :ScriptFunctionInit
933935
local main_chunk, err = loadfile(path)
934936
if not main_chunk then
935937
error(err, 2)
@@ -943,6 +945,11 @@ local function setup()
943945

944946
scriptFunctions[k] = v
945947
return v
948+
949+
elseif isDirectory(basePath) then
950+
v = newScriptFolderReader(basePath)
951+
scriptFunctions[k] = v
952+
return v
946953
end
947954

948955
errorf(2, "Tried to get non-existent global or script '%s'.", tostring(k))

src/functions.lua2p

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
markdownToHtml
4444
newDataFolderReader, isDataFolderReader, preloadData
4545
newPage
46+
newScriptFolderReader, isScriptFolderReader, preloadScripts
4647
newStringBuilder
4748
pack
4849
pairsSorted
@@ -1562,6 +1563,76 @@ end
15621563

15631564

15641565

1566+
-- scriptFolderReader = newScriptFolderReader( directory )
1567+
function _G.newScriptFolderReader(dir)
1568+
local scriptFolderReader = {}
1569+
1570+
setmetatable(scriptFolderReader, {
1571+
__index = function(scriptFolderReader, k)
1572+
local scriptObj
1573+
1574+
if type(k) ~= "string" then
1575+
return nil
1576+
1577+
elseif k == "." or k == ".." then
1578+
errorf(2, "Bad script key '%s'.", k)
1579+
1580+
elseif isFile(F("%s/%s.lua", dir, k)) then
1581+
local path = F("%s/%s.lua", dir, k)
1582+
1583+
-- :ScriptFunctionInit
1584+
local main_chunk, err = loadfile(path)
1585+
if not main_chunk then
1586+
error(err, 2)
1587+
end
1588+
1589+
setfenv(main_chunk, scriptEnvironment)
1590+
scriptObj = main_chunk()
1591+
if type(scriptObj) ~= "function" then
1592+
errorNoPos("%s did not return a function.", path)
1593+
end
1594+
1595+
elseif isDirectory(F("%s/%s", dir, k)) then
1596+
scriptObj = newScriptFolderReader(F("%s/%s", dir, k))
1597+
1598+
else
1599+
errorf(2, "Bad script path '%s/%s'.", dir, k)
1600+
end
1601+
1602+
assert(scriptObj ~= nil)
1603+
rawset(scriptFolderReader, k, scriptObj)
1604+
return scriptObj
1605+
end,
1606+
})
1607+
1608+
scriptReaderPaths[scriptFolderReader] = dir
1609+
return scriptFolderReader
1610+
end
1611+
1612+
function _G.isScriptFolderReader(t)
1613+
return scriptReaderPaths[t] ~= nil
1614+
end
1615+
1616+
function _G.preloadScripts(scriptFolderReader)
1617+
if scriptIsPreloaded[scriptFolderReader] then return scriptFolderReader end
1618+
1619+
for filename in lfs.dir(scriptReaderPaths[scriptFolderReader]) do
1620+
if filename:find"%.lua$" then
1621+
local path = scriptReaderPaths[scriptFolderReader] .. "/" .. filename
1622+
local basename = getBasename(filename)
1623+
1624+
if not rawget(scriptFolderReader, basename) and isFile(path) then
1625+
local _ = scriptFolderReader[basename]
1626+
end
1627+
end
1628+
end
1629+
1630+
scriptIsPreloaded[scriptFolderReader] = true
1631+
return scriptFolderReader
1632+
end
1633+
1634+
1635+
15651636
function _G.getProtectionWrapper(obj, objName)
15661637
!ARGS "obj:table objName:string"
15671638

src/globals.lua2p

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ _G.scriptEnvironmentGlobals = nil
6969

7070
_G.dataReaderPaths = setmetatable({--[[ [dataFolderReader1]=path1, ... ]]}, {__mode="k"})
7171
_G.dataIsPreloaded = setmetatable({--[[ [dataFolderReader1]=true, ... ]]}, {__mode="k"})
72+
_G.scriptReaderPaths = setmetatable({--[[ [scriptFolderReader1]=path1, ... ]]}, {__mode="k"})
73+
_G.scriptIsPreloaded = setmetatable({--[[ [scriptFolderReader1]=true, ... ]]}, {__mode="k"})
7274
_G.protectionWrappers = setmetatable({--[[ [object1]=wrapper1, ... ]]}, {__mode="kv"})
7375
_G.protectionedObjects = setmetatable({--[[ [wrapper1]=object1, ... ]]}, {__mode="kv"})
7476

@@ -128,8 +130,8 @@ function _G.resetSiteVariables()
128130
_G.redirectionLayout = ""
129131

130132
-- Other values.
131-
_G.layoutTemplates = {--[[ [path1]=template1, ... ]]}
132-
_G.scriptFunctions = {--[[ [scriptName1]=function(...), ... ]]}
133+
_G.layoutTemplates = {--[[ [path1]=template1, ... ]]}
134+
_G.scriptFunctions = {--[[ [scriptName1]=function|scriptFolderReader, ... ]]} -- @Cleanup: Replace this table with a script folder reader.
133135

134136
_G.pages = {--[[ page1, ... ]]}
135137
_G.pagesGenerating = {--[[ [pathRelOut1]=true, ... ]]}

testsite/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ end
7070

7171

7272

73-
runTests()
73+
tests.runTests()
7474

7575
return config
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ return function()
2828

2929
--[[ XML module tests.
3030
-- require"pl.xml"
31-
timerStart("our") ; xmlTests(xml) ; timerEnd()
32-
-- timerStart("pl") ; xmlTests(require"pl.xml") ; timerEnd()
31+
timerStart("our") ; tests.xmlTests(xml) ; timerEnd()
32+
-- timerStart("pl") ; tests.xmlTests(require"pl.xml") ; timerEnd()
3333
--]]
3434

3535
--[==[ XML parsing.
@@ -73,8 +73,11 @@ return function()
7373
<head>
7474
<SCRIPT>function bitAnd(a, b) { return a && b; }</SCRIPT>
7575
</head>
76+
7677
<BODY id="foo & bar">
77-
<h1 super duper=yes>Hello, world &amp; all bananas!</h1>
78+
<h1>Hello, world &&amp; all bananas!</h1>
79+
<input type=text disabled>
80+
7881
<svg width="391" height="391" viewBox="-70.5 -70.5 391 391" foo="">
7982
<rect fill="#fff" stroke="#000" x="-70" y="-70" width="390" height="390"/>
8083
<g opacity="0.8">
@@ -84,6 +87,7 @@ return function()
8487
<line x1="50" y1="50" x2="200" y2="200" stroke="blue" stroke-width="4" />
8588
</g>
8689
</svg>
90+
8791
<math>
8892
<mi>&pi;</mi>
8993
<mo>&InvisibleTimes;</mo>

0 commit comments

Comments
 (0)