Skip to content

Commit b1102dd

Browse files
committed
Added new Markdown parser (for GitHub Flavored Markdown). Phew!
Added xml.decodeEntities(). Added data parser for plain text files.
1 parent e866bb9 commit b1102dd

File tree

10 files changed

+3050
-93
lines changed

10 files changed

+3050
-93
lines changed

build/build.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ traverseDirectory("src", function(pathIn)
9393
makeDirectoryRecursive(dir)
9494
end
9595

96+
metaEnv.constants = {}
97+
metaEnv.statics = {}
98+
9699
pp.processFile{
97100
pathIn = pathIn,
98101
pathOut = pathOut,
@@ -102,6 +105,35 @@ traverseDirectory("src", function(pathIn)
102105
backtickStrings = true,
103106
canOutputNil = false,
104107

108+
onAfterMeta = function(lua)
109+
local header, _, rest = lua:match"^(#![^\n]*\n%-%-%[(=*)%[.-%]%2%]\n)(.*)"
110+
if not header then header, _, rest = lua:match "^(%-%-%[(=*)%[.-%]%2%]\n)(.*)"
111+
if not header then header, _, rest = "", nil, lua
112+
end end
113+
114+
local buffer = {}
115+
table.insert(buffer, header)
116+
117+
for _, constName in ipairs(metaEnv.constants) do
118+
table.insert(buffer, "local ")
119+
table.insert(buffer, constName)
120+
table.insert(buffer, " = ")
121+
table.insert(buffer, pp.toLua(constants[constName]))
122+
table.insert(buffer, "\n")
123+
end
124+
for _, staticName in ipairs(metaEnv.statics) do
125+
table.insert(buffer, "local ")
126+
table.insert(buffer, staticName)
127+
table.insert(buffer, " = ")
128+
table.insert(buffer, pp.toLua(statics[staticName]))
129+
table.insert(buffer, "\n")
130+
end
131+
132+
table.insert(buffer, rest)
133+
134+
return table.concat(buffer)
135+
end,
136+
105137
onError = function(err)
106138
os.exit(1)
107139
end,

build/meta.lua

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
--==============================================================
1111
1212
ARGS
13+
ASSERT
14+
BYTE
15+
CONST, CONSTSET, STATIC
1316
convertTextFileEncoding, addBomToUft16File
1417
copyFile, copyFilesInDirectory, copyDirectoryRecursive
1518
errorf
1619
execute, executeRequired
1720
F
1821
getReleaseVersion
22+
getStringBytes
1923
indexOf
24+
IS_CHAR
2025
isFile, isDirectory
2126
loadParams
2227
makeDirectory, makeDirectoryRecursive, removeDirectory, removeDirectoryRecursive
23-
NOSPACE
28+
NOSPACE, NOSPACE0
2429
PUSH_CONTEXT, POP_CONTEXT
2530
readFile, writeFile, writeTextFile
2631
Set
@@ -34,7 +39,8 @@
3439
3540
--============================================================]]
3641

37-
_G.F = string.format
42+
_G.BYTE = string.byte
43+
_G.F = string.format
3844

3945

4046

@@ -371,10 +377,16 @@ end
371377

372378

373379

380+
-- Remove spaces.
374381
function _G.NOSPACE(s)
375382
return (s:gsub(" +", ""))
376383
end
377384

385+
-- Remove spaces and replace NULL bytes with new spaces afterwards.
386+
function _G.NOSPACE0(s)
387+
return (s:gsub(" +", ""):gsub("%z", " "))
388+
end
389+
378390

379391

380392
do
@@ -401,7 +413,7 @@ do
401413

402414
__LUA(F(
403415
"if %s then errorf(%d, \"Bad argument #%d '%s'. (Expected %s, got %%s)\", type(%s)) end",
404-
ifFormat:format(argName, typesCode),
416+
F(ifFormat, argName, typesCode),
405417
errLevel + 1,
406418
n,
407419
argName,
@@ -431,7 +443,7 @@ end
431443

432444

433445
function errorf(s, ...)
434-
error(s:format(...), 2)
446+
error(F(s, ...), 2)
435447
end
436448

437449

@@ -476,3 +488,96 @@ function _G.Set(values)
476488
end
477489

478490

491+
492+
local function compare(v1, v2)
493+
if type(v1) ~= type(v2) then return false end
494+
if type(v1) ~= "table" then return v1 == v2 end
495+
496+
for k in pairs(v1) do
497+
if not compare(v1[k], v2[k]) then return false end
498+
end
499+
for k in pairs(v2) do
500+
if v1[k] == nil then return false end
501+
end
502+
503+
return true
504+
end
505+
506+
-- name = @@CONST{ ... }
507+
function _G.CONST(tCode)
508+
local t = assert(loadstring("return "..tCode))()
509+
510+
for _, constName in ipairs(constants) do
511+
if compare(constants[constName], t) then return constName end
512+
end
513+
514+
local constName = "CONST" .. (#constants+1)
515+
table.insert(constants, constName)
516+
constants[constName] = t
517+
518+
return constName
519+
end
520+
521+
-- name = @@CONSTSET{ value1, ... }
522+
function _G.CONSTSET(valuesCode)
523+
return CONST("Set("..valuesCode..")")
524+
end
525+
526+
-- name = @@STATIC( identifier )
527+
-- name = @@STATIC{ ... }
528+
function _G.STATIC(tCodeOrIdent)
529+
local staticName, t
530+
531+
if tCodeOrIdent:find"^{" then
532+
staticName = "STATIC" .. (#statics+1)
533+
t = assert(loadstring("return"..tCodeOrIdent))()
534+
else
535+
staticName = tCodeOrIdent
536+
t = {}
537+
end
538+
539+
assert(not statics[staticName], staticName)
540+
541+
table.insert(statics, staticName)
542+
statics[staticName] = t
543+
544+
return staticName
545+
end
546+
547+
548+
549+
-- @@ASSERT( condition [, message=auto ] )
550+
function ASSERT(condCode, messageCode)
551+
if not DEV then return "" end
552+
553+
messageCode = messageCode or F("%q", "Assertion failed: "..condCode)
554+
555+
return "if not ("..condCode..") then error("..messageCode..") end"
556+
end
557+
558+
559+
560+
-- bool = @@IS_CHAR( string, position, stringWithOneAsciiCharacter )
561+
-- true|nil = @@IS_CHAR( string, position, stringWithMultipleAsciiCharacters )
562+
function _G.IS_CHAR(sCode, posCode, cCode)
563+
local c = assert(loadstring("return "..cCode))()
564+
assert(type(c) == "string")
565+
assert(#c > 0)
566+
567+
if posCode == "1" then posCode = "" end
568+
569+
if #c == 1 then
570+
return F("((%s):byte(%s) == %d)", sCode, posCode, c:byte())
571+
else
572+
return F("%s[(%s):byte(%s)]", CONSTSET('{getStringBytes'..cCode..'}'), sCode, posCode)
573+
end
574+
end
575+
576+
577+
578+
-- byte1, ... = getStringBytes( string )
579+
function _G.getStringBytes(s)
580+
return s:byte(1, #s)
581+
end
582+
583+

examples/testsite/content/tests.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ One {{* "Space" }} Allowed
102102

103103
{{fori {5,99,- -8}}}
104104
- {{it}}
105-
{{end}}
105+
{{*end}}
106106

107107
URL: {{ /foo }}
108108
URL: {{ ./foo }}
@@ -196,26 +196,26 @@ Favorite fruit is neither apple nor banana. :(
196196

197197
{{ for i = 1, 3 }}
198198
- For {{ i }}
199-
{{ end }}
199+
{{* end }}
200200

201201
{{ for 3 }}
202-
- Short form {{ i }}
203-
{{ end }}
202+
* Short form {{ i }}
203+
{{* end }}
204204

205205
{{ for < 3 }}
206206
- Backwards {{ i }}
207-
{{ end }}
207+
{{* end }}
208208

209209
{{ local n = 3 }}
210-
{{ while n > 0 }}
211-
- Countdown #{{ n }}
212-
{{ n = n-1 }}
213-
{{ end }}
210+
{{* while n > 0 }}
211+
* Countdown #{{ n }}
212+
{{* n = n-1 }}
213+
{{* end }}
214214

215215
{{ repeat }}
216-
{{ n = n+1 }}
216+
{{* n = n+1 }}
217217
- Count #{{ n }}
218-
{{ until n >= 3 }}
218+
{{* until n >= 3 }}
219219

220220

221221

@@ -224,12 +224,12 @@ Favorite fruit is neither apple nor banana. :(
224224
Dogs:
225225
{{ fori dog in data.dogs }}
226226
- {{ i }}: {{ dog.name }} (age {{ dog.age }})
227-
{{ end }}
227+
{{* end }}
228228

229229
Cats, in reverse:
230230
{{ fori < data.cats.cats }}
231231
- {{ i }}: {{ it.name }} (age {{ it.age }})
232-
{{ end }}
232+
{{* end }}
233233

234234
{{ io.write("JSON: ") ; printObject(data.random) }}
235235
{{ io.write("XML: ") ; print(data.barf:getFirstElement()) }}

0 commit comments

Comments
 (0)