Skip to content

Commit

Permalink
fix p, meta head
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Oct 11, 2024
1 parent 55b6e49 commit e8193f5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
15 changes: 8 additions & 7 deletions luax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@ local function decentParserAST(input)
local tagName = tagRange:match("<(%w+)", 0)
local tagNameEnd = tagRange:match("</(%w+)>", 0)
if tagName then deepNode = deepNode + 1 end
if tagNameEnd then deepNode = deepNode - 1 end
pos = pos + 1

if tagName and not deepString then
isTag = true
textNode = false
if textNodeStart then
textNodeStart = not textNodeStart
output = output .. "]]"
end
if deepNode > 1 then
output = output .. ", " .. tagName .. "({"
else
output = output .. tagName .. "({"
end
local step = 1
-- enclose attributes if it empty
if tagRange:sub(#tagRange - 1, #tagRange):gsub("[\r\n]", ""):match("^%s*(.-)$") == ">" then step = 0 end
pos = pos + #tagName + step
pos = pos + #tagName
elseif tagNameEnd then
deepNode = deepNode - 1
if isTag and not textNode then
isTag = not isTag
local trail = input:sub(0, pos - 2):gsub("[%s\r\n]", "")
if trail:sub(#trail - 1, #trail - 1) == '/' then
if trail:sub(#trail - 1, #trail - 1) == "/" then
output = output .. ")"
else
output = output .. "})"
Expand Down Expand Up @@ -104,7 +105,7 @@ local function decentParserAST(input)
end

if textNode and not textNodeStart then
local subNode = input:match("%s*<(%w+)", pos)
local subNode = input:match("^%s*<(%w+)", pos) or input:match("^%s*{(%w+)", pos)
if not isTag and not subNode and not var then
textNodeStart = not textNodeStart
output = output .. ", [["
Expand Down
61 changes: 61 additions & 0 deletions test/14_page.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local module = {}
local toggleMain = "toggleMain"
local todoList = "todoList"
local todoFooter = "todoFooter"

function module.Page(todos)
return <html lang="en" data-framework="htmx">
<head>
<meta charSet="utf-8" />
<title>HTMXTodoMVC</title>
<link rel="stylesheet" href="https://unpkg.com/todomvc-common@1.0.5/base.css" type="text/css" />
<link rel="stylesheet" href="https://unpkg.com/todomvc-app-css/index.css" type="text/css" />
<script type="text/hyperscript" src="/hs/start-me-up._hs"></script>
<script type="text/hyperscript" src="/hs/main._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/toggle-main._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/toggle-footer._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/toggle-show._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/add-todo._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/footer._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/toggle-all._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/clear-completed._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/destroy._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/todo-count._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/todo-dblclick._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/todo-check._hs"></script>
<script type="text/hyperscript" src="/hs/behaviors/todo-edit._hs"></script>
</head>
<body>
<section class="todoapp"
_="install ToggleMain end install ToggleFooter end install ToggleShow end"
>
<header class="header">
<h1>todos</h1>
<input
id="add-todo"
name="title"
class="new-todo"
placeholder="What needs to be done?"
_="install AddTodo"
/>
</header>
{toggleMain}
{todoList}
{todoFooter}
</section>
<footer class="info"
_="on load debounced at 10ms call startMeUp() hashCache()"
>
<p>Double-click to edit a todo</p>
<p>Created by <a href="http://github.com/syarul/">syarul</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
<img src="https://htmx.org/img/createdwith.jpeg" width="250" height="auto" />
</footer>
</body>
<script src="https://unpkg.com/todomvc-common@1.0.5/base.js"></script>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<script src="https://unpkg.com/hyperscript.org/dist/_hyperscript.js"></script>
</html>
end

return module
7 changes: 7 additions & 0 deletions test/15_title.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
return <html lang="en" data-framework="htmx">
<head>
<title>HTMXTodoMVC</title>
</head>
<body>
</body>
</html>
1 change: 1 addition & 0 deletions test/16_p.luax
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return <p>Created by <a href="http://github.com/syarul/">syarul</a></p>
12 changes: 12 additions & 0 deletions test/test_ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ local varin = require('test.13_varin')

h(varin)

local page = require('test.14_page')

print(h(page.Page("todos")))

local title = require('test.15_title')

print(h(title))

local p = require('test.16_p')

print(h(p))


7 changes: 6 additions & 1 deletion test/test_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,18 @@ describe("LuaX", function()
it("should return a HTML string with deep node tree", function()
local el = require("test.12_test")
assert.is.equal(
'<li _="on destroy my.querySelector(\'button\').click()" class="test" id="1"><div class="view">todo A<label _="install TodoDblclick" hx-patch="/edit-todo?id=1&foo=test" hx-swap="outerHTML" hx-target="next input" hx-trigger="dblclick">todo A Label</label><button _="install Destroy" class="destroy" hx-delete="/remove-todo?id=1" hx-swap="outerHTML" hx-target="closest <li/>" hx-trigger="click"></button></div>todo A Value</li>',
[[<li _="on destroy my.querySelector('button').click()" class="test" id="1"><div class="view">todo A<label _="install TodoDblclick" hx-patch="/edit-todo?id=1&foo=test" hx-swap="outerHTML" hx-target="next input" hx-trigger="dblclick">todo A Label </label><button _="install Destroy" class="destroy" hx-delete="/remove-todo?id=1" hx-swap="outerHTML" hx-target="closest <li/>" hx-trigger="click"></button></div>todo A Value</li>]],
h(el))
end)

it("should return a HTML string when given JSX like syntax with nested node", function()
local el = require("test.13_varin")
assert.is.equal('<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 with nested node", function()
local el = require("test.14_page")
assert.is.equal([[<html data-framework="htmx" lang="en"><head><meta charSet="utf-8"><title>HTMX • TodoMVC</title><link href="https://unpkg.com/todomvc-common@1.0.5/base.css" rel="stylesheet" type="text/css"><link href="https://unpkg.com/todomvc-app-css/index.css" rel="stylesheet" type="text/css"><script src="/hs/start-me-up._hs" type="text/hyperscript"></script><script src="/hs/main._hs" type="text/hyperscript"></script><script src="/hs/behaviors/toggle-main._hs" type="text/hyperscript"></script><script src="/hs/behaviors/toggle-footer._hs" type="text/hyperscript"></script><script src="/hs/behaviors/toggle-show._hs" type="text/hyperscript"></script><script src="/hs/behaviors/add-todo._hs" type="text/hyperscript"></script><script src="/hs/behaviors/footer._hs" type="text/hyperscript"></script><script src="/hs/behaviors/toggle-all._hs" type="text/hyperscript"></script><script src="/hs/behaviors/clear-completed._hs" type="text/hyperscript"></script><script src="/hs/behaviors/destroy._hs" type="text/hyperscript"></script><script src="/hs/behaviors/todo-count._hs" type="text/hyperscript"></script><script src="/hs/behaviors/todo-dblclick._hs" type="text/hyperscript"></script><script src="/hs/behaviors/todo-check._hs" type="text/hyperscript"></script><script src="/hs/behaviors/todo-edit._hs" type="text/hyperscript"></script> </head><body><section _="install ToggleMain end install ToggleFooter end install ToggleShow end" class="todoapp"><header class="header"><h1>todos</h1><input _="install AddTodo" class="new-todo" id="add-todo" name="title" placeholder="What needs to be done?"></header>toggleMaintodoListtodoFooter</section><footer _="on load debounced at 10ms call startMeUp() hashCache()" class="info"><p>Double-click to edit a todo</p><p>Created by <a href="http://github.com/syarul/">syarul</a></p><p>Part of <a href="http://todomvc.com">TodoMVC</a></p><img height="auto" src="https://htmx.org/img/createdwith.jpeg" width="250"></footer></body><script src="https://unpkg.com/todomvc-common@1.0.5/base.js"></script><script src="https://unpkg.com/htmx.org@1.9.10"></script><script src="https://unpkg.com/hyperscript.org/dist/_hyperscript.js"></script> </html>]], h(el.Page("todos")))
end)

end)

0 comments on commit e8193f5

Please sign in to comment.