Skip to content

Commit c2d325d

Browse files
committed
Added JSON parser.
1 parent bd553f2 commit c2d325d

File tree

8 files changed

+370
-29
lines changed

8 files changed

+370
-29
lines changed

build/README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ LuaWebGen uses this folder structure for a website project:
8585
my-website/ -- Root of the website project.
8686
content/ -- All website content, including pages, images, CSS and JavaScript files.
8787
index.(html|md) -- Homepage/root index page.
88-
data/ -- Optional data folder. Can contain Lua, TOML and XML files.
88+
data/ -- Optional data folder. Can contain Lua, TOML, JSON and XML files.
8989
layouts/ -- All HTML layout templates.
9090
page.html -- Default page layout template.
9191
output/ -- Where the built website ends up.

src/app.lua2p

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,13 @@ local function setup()
337337
table = table,
338338

339339
-- Lua libraries.
340-
lfs = lfs,
341-
xml = xmlLib,
340+
json = jsonLib,
341+
lfs = lfs,
342+
xml = xmlLib,
343+
344+
toml = { -- @Cleanup: Don't use 3rd party TOML library.
345+
parse = dataParsers.toml,
346+
},
342347

343348
-- Site objects. (Create at site generation.)
344349
site = nil,

src/functions.lua2p

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
assert
1414
attributeWith, attributeWithAny
1515
cleanupPath
16+
codepointToUtf8
1617
countString, countSubString
1718
createDirectory, isDirectoryEmpty, removeEmptyDirectories
1819
createThumbnail
@@ -1434,6 +1435,13 @@ _G.dataParserIsBinary = {}
14341435
-- return doc
14351436
-- end
14361437

1438+
dataParserIsBinary.json = false
1439+
function dataParsers.json(dataStr, path)
1440+
local v, err = jsonLib.parse(dataStr, path)
1441+
if not v then errorNoPos(err) end
1442+
return v
1443+
end
1444+
14371445
dataParserIsBinary.lua = false
14381446
function dataParsers.lua(dataStr, path)
14391447
local chunk, err = loadstring(dataStr, "@"..path)
@@ -2736,3 +2744,67 @@ function _G.countSubString(s, pos, posEnd, needle, plain)
27362744
end
27372745

27382746

2747+
2748+
local byteToString = string.char
2749+
local tableInsert = table.insert
2750+
2751+
-- string = codepointToUtf8( cp )
2752+
-- codepointToUtf8( cp, outputBuffer )
2753+
function _G.codepointToUtf8(cp, buffer)
2754+
if cp < 0 or cp > 0x10ffff then
2755+
errorf("Codepoint %d is outside the valid range (0..10FFFF).", cp)
2756+
end
2757+
2758+
if cp >= 128 then
2759+
-- void
2760+
elseif buffer then
2761+
tableInsert(buffer, byteToString(cp))
2762+
return
2763+
else
2764+
return byteToString(cp)
2765+
end
2766+
2767+
local suffix = cp % 64
2768+
local c4 = 128 + suffix
2769+
cp = (cp - suffix) / 64
2770+
2771+
if cp >= 32 then
2772+
-- void
2773+
elseif buffer then
2774+
tableInsert(buffer, byteToString(192+cp))
2775+
tableInsert(buffer, byteToString(c4))
2776+
return
2777+
else
2778+
return byteToString(192+cp, c4) -- @Speed @Memory
2779+
end
2780+
2781+
suffix = cp % 64
2782+
local c3 = 128 + suffix
2783+
cp = (cp - suffix) / 64
2784+
2785+
if cp >= 16 then
2786+
-- void
2787+
elseif buffer then
2788+
tableInsert(buffer, byteToString(224+cp))
2789+
tableInsert(buffer, byteToString(c3))
2790+
tableInsert(buffer, byteToString(c4))
2791+
return
2792+
else
2793+
return byteToString(224+cp, c3, c4) -- @Speed @Memory
2794+
end
2795+
2796+
suffix = cp % 64
2797+
cp = (cp - suffix) / 64
2798+
2799+
if buffer then
2800+
tableInsert(buffer, byteToString(240+cp))
2801+
tableInsert(buffer, byteToString(128+suffix))
2802+
tableInsert(buffer, byteToString(c3))
2803+
tableInsert(buffer, byteToString(c4))
2804+
return
2805+
else
2806+
return byteToString(240+cp, 128+suffix, c3, c4) -- @Speed @Memory
2807+
end
2808+
end
2809+
2810+

src/globals.lua2p

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ _G.AUTOBUILD_MIN_INTERVAL = 1.00
3232
_G.FILE_TYPE_SET = {["markdown"]=true, ["html"]=true, ["othertemplate"]=true}
3333
_G.OUTPUT_CATEGORY_SET = {["page"]=true, ["template"]=true, ["raw"]=true}
3434

35-
_G.DATA_FILE_EXTENSIONS = {"lua","toml","xml"}
35+
_G.DATA_FILE_EXTENSIONS = {"json","lua","toml","xml"}
3636
_G.IMAGE_EXTENSIONS = {"png","jpg","jpeg","gif"}
3737

3838
_G.NOOP = function()end
@@ -44,6 +44,7 @@ _G.NOOP = function()end
4444
_G.lfs = require"lfs"
4545

4646
_G.dateLib = require"date"
47+
_G.jsonLib = require"json"
4748
_G.markdownLib = require"markdown"
4849
_G.tomlLib = require"toml"
4950
_G.urlLib = require"url"

0 commit comments

Comments
 (0)