Skip to content

Commit

Permalink
fix parser
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Oct 3, 2024
1 parent fb5a784 commit bffd3aa
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
46 changes: 11 additions & 35 deletions h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ local function createElement(tag, atts, children)
}
end

local function flattenChildren(children)
local flattened = {}
for _, child in ipairs(children) do
if type(child) == "table" and child.isArray then
for _, subChild in ipairs(child) do
table.insert(flattened, subChild)
end
else
table.insert(flattened, child)
end
end
return flattened
end

local function isAtts(tbl)
for k, v in pairs(tbl) do
if type(k) ~= "string" or type(v) == "table" then
Expand All @@ -49,43 +35,33 @@ setmetatable(_G, {
atts = children[1]
children = { select(2, ...) }
end
children = flattenChildren(children)
if atts == nil and isAtts(children[1]) then
atts = children[1]
children = { select(2, children) }
end
atts = atts or children[1]
return createElement(tag, atts, children)
end
end
})

local function printTable(t, indent)
indent = indent or 0 -- Default to zero if no indent is provided
local tab = string.rep(" ", indent) -- Create indentation for readability

for key, value in pairs(t) do
if type(value) == "table" then
-- If the value is a table, recursively print it
print(tab .. tostring(key) .. ": ")
printTable(value, indent + 1) -- Increase indentation for nested tables
else
-- Print the key and value
print(tab .. tostring(key) .. ": " .. tostring(value))
end
end
end


local function h(element)
if type(element) ~= "table" then return element or "" end
if element.tag == nil then return element.children or "" end
local tkeys = {}
for k in pairs(element.atts) do table.insert(tkeys, k) end
if #tkeys then table.sort(tkeys) end
local atts = ""
local children = ""
for _, k in ipairs(tkeys) do
local v = element.atts[k]
if type(v) ~= "table" then
atts = atts .. " " .. k .. "=\"" .. v .. "\""
if k ~= "children" then
atts = atts .. " " .. k .. "=\"" .. v .. "\""
else
children = v
end
end
end
local children = ""
for _, child in ipairs(element.children) do
if type(child) == "table" then
children = children .. h(child)
Expand Down
18 changes: 15 additions & 3 deletions luax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local function decentParserAST(input)
local output = ""
local isTag = 0
local isTextNode = 0
local deepNode = 0

while pos <= #input do
local char = input:sub(pos, pos)
Expand All @@ -20,11 +21,15 @@ local function decentParserAST(input)
output = output .. ", "
end
if tagName then
deepNode = deepNode + 1
isTag = 1
output = output .. tagName .. "({"
pos = pos + #tagName + 1
elseif tagNameEnd then
isTag = 0
deepNode = deepNode - 1
if deepNode == 0 then
isTag = 0
end
if isTextNode == 2 then
isTextNode = 0
output = output .. "\")"
Expand All @@ -42,8 +47,11 @@ local function decentParserAST(input)
end
pos = pos + 1
elseif char == "/" then
deepNode = deepNode - 1
-- self closing tag
isTag = 0
if deepNode == 0 then
isTag = 0
end
output = output .. " })"
pos = pos + 1
else
Expand All @@ -64,9 +72,13 @@ local function decentParserAST(input)
elseif char == "{" or char == "}" then
skip = true
end

end

if isTag ~= 0 then
-- add bracket to all attributes key
if input:match("%=") then
output = output:gsub('([%w%-_]+)%=(".-")', '["%1"]=%2')
end
if isTextNode == 1 and char == "{" or char == "}" then
skip = true
isTextNode = 3
Expand Down
1 change: 1 addition & 0 deletions test/3_comment.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return <!--ccc-->
2 changes: 2 additions & 0 deletions test/content.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local content = "foo" .. "bar"
return <footer class="footer" _="install Footer"><span class="todo-count" hx-trigger="load" _="install TodoCount"></span>{content}</footer>
4 changes: 4 additions & 0 deletions test/test_ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ print(h(varin))
local foo = require('test.foo')

print(h(foo))

local content = require('test.content')

print(h(content))
7 changes: 7 additions & 0 deletions test/test_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ describe("LuaX", function()
'<div class="container" id="div_1"><p class="title" id="p_2" style="border: 1px solid red;">Hello, world!</p></div>',
h(el))
end)

it("should return a HTML string when given JSX like syntax 4", function()
local el = require("test.content")
assert.is.equal(
'<footer _="install Footer" class="footer"><span _="install TodoCount" class="todo-count" hx-trigger="load"></span>foobar</footer>',
h(el))
end)
end)

0 comments on commit bffd3aa

Please sign in to comment.