Skip to content

Commit

Permalink
handle isNode only ops
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Sep 1, 2024
1 parent 917f53f commit e3966be
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
9 changes: 6 additions & 3 deletions luax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ local originalRequire = require
local function tokenize(input)
local pos = 1
local output = ""
local isNode

while pos <= #input do
local char = input:sub(pos, pos)

if char == "<" then
if input:sub(pos + 1, pos + 1) == "/" then
local tagName = input:match("</(%w+)>", pos)
isNode = nil
pos = pos + #tagName + 3 -- skip "</tag>"
output = output .. ")" -- close the Lua function call
else
local tagName = input:match("<(%w+)", pos)
if tagName then isNode = true end
pos = pos + #tagName + 1 -- skip "<tag>"
output = output .. tagName .. "({" -- opening the Lua function call

Expand Down Expand Up @@ -45,15 +48,15 @@ local function tokenize(input)
output = output .. table.concat(attributes, ", ") -- add delimiter
end
end
elseif char == "{" then
elseif isNode and char == "{" then
-- handle content inside curly braces
local content = input:match("{(.-)}", pos)
output = output .. content:match("%s*(.*)%s*")
pos = pos + #content + 2 -- skip "{content}"
elseif char == "}" then
elseif isNode and char == "}" then
pos = pos + 1
else
if char == ">" then
if isNode and char == ">" then
pos = pos + 1
output = output .. "}, " -- opening the Lua function call
local text = input:match("([^<]+)", pos)
Expand Down
5 changes: 2 additions & 3 deletions test/element.luax
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local class = "container"
local val = 'value'
local myElement = <div id="foo" bar="bar" d="1" class={class} val={val}>Hello, world!</div>
local attr = { class = "container", val = 'value' }
local myElement = <div id="foo" bar="bar" d="1" class={attr.class} val={attr.val}>Hello, world!</div>
return myElement
2 changes: 2 additions & 0 deletions test/test_ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ print(h(foo))

local element = require('test.element')

print(element)

print(h(element))

local varin = require('test.varin')
Expand Down
14 changes: 8 additions & 6 deletions test/varin.luax
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local div_class = "container"
local p_class = "title"
local p_inner = "Hello, world!"
local span_style = "color: red;"
local span_inner = "This is a span"
local v = {
div_class = "container",
p_class = "title",
p_inner = "Hello, world!",
span_style = "color: red;",
span_inner = "This is a span",
}

local el = <div id="div_1" class={div_class}><p id="p_2" style="border: 1px solid red;" class={p_class}>{p_inner}</p></div>
local el = <div id="div_1" class={v.div_class}><p id="p_2" style="border: 1px solid red;" class={v.p_class}>{v.p_inner}</p></div>
return el

0 comments on commit e3966be

Please sign in to comment.