Skip to content

Commit 4b3d6ad

Browse files
committed
Added global 'scripts' object (just like 'data').
1 parent 772c6df commit 4b3d6ad

File tree

3 files changed

+78
-93
lines changed

3 files changed

+78
-93
lines changed

src/app.lua2p

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -944,32 +944,12 @@ local function setup()
944944
v = getContext()._scriptEnvironmentGlobals[k]
945945
if v ~= nil then return v end
946946

947-
v = site._scriptFunctions[k]
947+
v = rawget(site._scripts, k)
948948
if v then return v end
949949

950-
local basePath = F("%s/%s", DIR_SCRIPTS, k)
951-
local path = basePath..".lua"
952-
953-
if isFile(path) then
954-
-- :ScriptFunctionInit
955-
local main_chunk, err = loadfile(path)
956-
if not main_chunk then
957-
error(err, 2)
958-
end
959-
960-
setfenv(main_chunk, env)
961-
v = main_chunk()
962-
if type(v) ~= "function" then
963-
errorNoPos("%s did not return a function.", path)
964-
end
965-
966-
site._scriptFunctions[k] = v
967-
return v
968-
969-
elseif isDirectory(basePath) then
970-
v = newScriptFolderReader(basePath)
971-
site._scriptFunctions[k] = v
972-
return v
950+
local scriptPath = F("%s/%s", DIR_SCRIPTS, k)
951+
if isFile(scriptPath..".lua") or isDirectory(scriptPath) then
952+
return site._scripts[k] -- Should succeed (unless there's a parsing error or something, of course).
973953
end
974954

975955
errorf(2, "Tried to get non-existent global or script '%s'.", tostring(k))
@@ -1009,8 +989,9 @@ local function buildWebsite()
1009989
_G.oncePrints = {}
1010990
_G.warningCount = 0
1011991

1012-
scriptEnvironmentGlobals.site = getProtectionWrapper(site, "site")
1013-
scriptEnvironmentGlobals.data = newDataFolderReader(DIR_DATA, true)
992+
scriptEnvironmentGlobals.site = getProtectionWrapper(site, "site")
993+
scriptEnvironmentGlobals.data = newDataFolderReader(DIR_DATA, true)
994+
scriptEnvironmentGlobals.scripts = site._scripts
1014995

1015996

1016997

src/functions.lua2p

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,6 @@ function _G.newScriptFolderReader(dir)
15891589
elseif isFile(F("%s/%s.lua", dir, k)) then
15901590
local path = F("%s/%s.lua", dir, k)
15911591

1592-
-- :ScriptFunctionInit
15931592
local main_chunk, err = loadfile(path)
15941593
if not main_chunk then
15951594
error(err, 2)
@@ -2213,8 +2212,9 @@ function _G.newSite()
22132212
--
22142213

22152214
-- Other values.
2216-
_layoutTemplates = {--[[ [path1]=template1, ... ]]},
2217-
_scriptFunctions = {--[[ [scriptName1]=function|scriptFolderReader, ... ]]}, -- @Cleanup: Replace this table with a script folder reader.
2215+
_layoutTemplates = {--[[ [path1]=template1, ... ]]},
2216+
2217+
_scripts = newScriptFolderReader(DIR_SCRIPTS),
22182218

22192219
_pages = {--[[ page1, ... ]]},
22202220
_pagesGenerating = {--[[ [pathRelOut1]=true, ... ]]},

testsite/content/tests.md

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ P.foo = "bar"
1010

1111
Paragraph with [a link](http://foo.example.com/).
1212

13-
Paragraph with [a link]({{url"/relative-link"}}).
13+
Paragraph with [a link]({{ url"/relative-link" }}).
1414

15-
Paragraph with [a link]({{/relative-link}}).
15+
Paragraph with [a link]({{ /relative-link }}).
1616

17-
Paragraph with [a link]({{urlAbs"/absolute-link"}}).
17+
Paragraph with [a link]({{ urlAbs"/absolute-link" }}).
1818

19-
An image: ![the alt]({{/images/head.png}})
19+
An image: ![the alt]({{ /images/head.png }})
2020

21-
An image < <img src="{{/images/head.png}}"> > inside markdown.
21+
An image < <img src="{{ /images/head.png }}"> > inside markdown.
2222

23-
An image < {{'<img src="'..url'/images/head.png'..'">'}} > inside markdown.
23+
An image < {{ '<img src="'..url'/images/head.png'..'">' }} > inside markdown.
2424

2525
{{-- Line comment. {{"nope"}}
2626
}}
@@ -32,7 +32,7 @@ An image < {{'<img src="'..url'/images/head.png'..'">'}} > inside markdown.
3232
local localVar = function()end
3333
-- localVar = globalVar -- Error: Cannot access non-existing globals!
3434
}}
35-
{{localVar}}
35+
{{ localVar }}
3636

3737
{{
3838
-- function()end -- Error: Invalid expression!
@@ -55,7 +55,7 @@ for i = 1, 3 do
5555
end
5656
}}
5757

58-
foo < {{P.foo}} == "baz" > 0? <hr>
58+
foo < {{ P.foo }} == "baz" > 0? <hr>
5959

6060
Two variations of control structures:
6161

@@ -67,6 +67,10 @@ for i = 1, 3 do
6767
end
6868
}}
6969

70+
{{if(1+2==3)}}
71+
No spaces.
72+
{{end}}
73+
7074
{{ if 1+2 == 3 }}
7175
Extra spaces.
7276
{{ end }}
@@ -112,93 +116,93 @@ Markdown parsing, solution: [Snake!](<https://en.wikipedia.org/wiki/Snake_(video
112116

113117
## Value Expressions
114118

115-
String: {{"Water"}}
119+
String: {{ "Water" }}
116120

117-
Operations: {{1+2*3}}
121+
Operations: {{ 1+2*3 }}
118122

119-
Operations: {{"a-".."-b" -- Inline comment.
123+
Operations: {{ "a-".."-b" -- Inline comment.
120124
}}
121125

122-
Operations: {{1 + --[[ Inline comment. ]] 2}}
126+
Operations: {{ 1 + --[[ Inline comment. ]] 2 }}
123127

124-
Value from function: {{date"%Y-%m-%d"}}
128+
Value from function: {{ date"%Y-%m-%d" }}
125129

126-
No value from function: {{print("Just a print to console.")}}
130+
No value from function: {{ print("Just a print to console.") }}
127131

128-
Field: {{page.title}}
132+
Field: {{ page.title }}
129133

130-
Table: {{site}}
134+
Table: {{ site }}
131135

132-
Param: {{P.foo}}
136+
Param: {{ P.foo }}
133137

134-
Text: {{"foo <img>"}}
138+
Text: {{ "foo <img>" }}
135139

136-
Html: {{"<img>"}}
140+
Html: {{ "<img>" }}
137141

138-
echo: {{echo('<img src="/images/head.png">')}}
142+
echo: {{ echo('<img src="/images/head.png">') }}
139143

140-
echoRaw: {{echoRaw('<img src="/images/head.png">')}}
144+
echoRaw: {{ echoRaw('<img src="/images/head.png">') }}
141145

142146

143147

144148
## Control Structures
145149

146-
{{local depth = 0}}
147-
{{do}}
148-
{{local depth = 1}}
149-
{{do}}
150-
{{local depth = 2}}
151-
Inner do...end at {{depth}}.
152-
{{end}}
153-
Outer do...end at {{depth}}.
154-
{{end}}
155-
Outside do...end at {{depth}}.
156-
157-
{{local favoriteFruit = "banana"}}
158-
{{if favoriteFruit == "apple"}}
150+
{{ local depth = 0 }}
151+
{{ do }}
152+
{{ local depth = 1 }}
153+
{{ do }}
154+
{{ local depth = 2 }}
155+
Inner do...end at {{ depth }}.
156+
{{ end }}
157+
Outer do...end at {{ depth }}.
158+
{{ end }}
159+
Outside do...end at {{ depth }}.
160+
161+
{{ local favoriteFruit = "banana" }}
162+
{{ if favoriteFruit == "apple" }}
159163
Favorite fruit is apple!
160-
{{elseif favoriteFruit == "banana"}}
164+
{{ elseif favoriteFruit == "banana" }}
161165
Favorite fruit is banana!
162-
{{else}}
166+
{{ else }}
163167
Favorite fruit is neither apple nor banana. :(
164-
{{end}}
168+
{{ end }}
165169

166-
{{for i = 1, 3}}
167-
- For {{i}}
168-
{{end}}
170+
{{ for i = 1, 3 }}
171+
- For {{ i }}
172+
{{ end }}
169173

170-
{{for 3}}
171-
- Short form {{i}}
172-
{{end}}
174+
{{ for 3 }}
175+
- Short form {{ i }}
176+
{{ end }}
173177

174-
{{for < 3}}
175-
- Backwards {{i}}
176-
{{end}}
178+
{{ for < 3 }}
179+
- Backwards {{ i }}
180+
{{ end }}
177181

178-
{{local n = 3}}
179-
{{while n > 0}}
180-
- Countdown #{{n}}
181-
{{n = n-1}}
182-
{{end}}
182+
{{ local n = 3 }}
183+
{{ while n > 0 }}
184+
- Countdown #{{ n }}
185+
{{ n = n-1 }}
186+
{{ end }}
183187

184-
{{repeat}}
185-
{{n = n+1}}
186-
- Count #{{n}}
187-
{{until n >= 3}}
188+
{{ repeat }}
189+
{{ n = n+1 }}
190+
- Count #{{ n }}
191+
{{ until n >= 3 }}
188192

189193

190194

191195
## Data
192196

193197
Dogs:
194-
{{fori dog in data.dogs}}
195-
- {{i}}: {{dog.name}} (age {{dog.age}})
196-
{{end}}
198+
{{ fori dog in data.dogs }}
199+
- {{ i }}: {{ dog.name }} (age {{ dog.age }})
200+
{{ end }}
197201

198202
Cats, in reverse:
199-
{{fori < data.cats.cats}}
200-
- {{i}}: {{it.name}} (age {{it.age}})
201-
{{end}}
203+
{{ fori < data.cats.cats }}
204+
- {{ i }}: {{ it.name }} (age {{ it.age }})
205+
{{ end }}
202206

203207
{{ io.write("JSON: ") ; printObject(data.random) }}
204208
{{ io.write("XML: ") ; print(data.barf:getFirstElement()) }}
@@ -207,11 +211,11 @@ Cats, in reverse:
207211

208212
## Scripts
209213

210-
ipsum: {{ipsum()}}
214+
ipsum: {{ ipsum() }}
211215

212-
echoOgres: {{echoOgres()}}
216+
echoOgres: {{ scripts.echoOgres() --[[ echoOgres() and scripts.echoOgres() refer to the same script. ]] }}
213217

214-
fullscreenImage: {{fullscreenImage"/images/head.png"}}
218+
fullscreenImage: {{ fullscreenImage"/images/head.png" }}
215219

216220

217221

0 commit comments

Comments
 (0)