Skip to content

Commit

Permalink
refactor, use snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Oct 18, 2024
1 parent 1849ae9 commit 195685d
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 154 deletions.
6 changes: 3 additions & 3 deletions .rockspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package = "luax"
version = "1.0.6-1"
version = "1.0.7-1"

source = {
url = "https://github.com/syarul/luax/archive/refs/tags/v1.0.6.tar.gz",
dir = "luax-1.0.6"
url = "https://github.com/syarul/luax/archive/refs/tags/v1.0.7.tar.gz",
dir = "luax-1.0.7"
}
description = {
summary = "HTML parse in Lua",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

LuaX is Lua + XML Syntax extension with built-in decent parse. In retrospect it's akin to React JSX.

<a href="https://luarocks.org/modules/syarul/luax" rel="nofollow"><img alt="Luarocks Package" src="https://img.shields.io/badge/Luarocks-1.0.6-blue.svg" style="max-width:100%;"></a>
<a href="https://luarocks.org/modules/syarul/luax" rel="nofollow"><img alt="Luarocks Package" src="https://img.shields.io/badge/Luarocks-1.0.7-blue.svg" style="max-width:100%;"></a>
[![Lua CI](https://github.com/syarul/luax/actions/workflows/lua.yml/badge.svg)](https://github.com/syarul/luax/actions/workflows/lua.yml)

## Decent Parser
Expand Down
28 changes: 14 additions & 14 deletions h.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
local voidTags = {
local void_tags = {
"area", "base", "basefont", "br", "col",
"frame", "hr", "img", "input", "link",
"meta", "param", "embed", "command", "keygen",
"source", "track", "wbr"
}

local function isVoidTag(tag)
for _, voidTag in ipairs(voidTags) do
if voidTag == tag then
local function is_void_tag(tag)
for _, void_tag in ipairs(void_tags) do
if void_tag == tag then
return true
end
end
return false
end

local function kebabCase(tag)
local function kebab_case(tag)
if not tag:match("^[a-z]") and tag:match("%u%u") and tag:match("[^%w]") then
return tag
end
local kebab = tag:gsub("(%u)", "-%1"):lower()
return kebab:gsub("^%-", "")
end

local function createElement(tag, atts, children)
local function create_element(tag, atts, children)
return {
tag = tag,
atts = atts or {},
children = children or {}
}
end

local function isAtts(tbl, tag)
local function is_atts(tbl, tag)
if tag:lower() == "doctype" then
return true
end
Expand All @@ -47,16 +47,16 @@ setmetatable(_G, {
return function(...)
local atts
local children = { ... }
if type(children[1]) == "table" and isAtts(children[1], tag) and #children ~= 1 then
if type(children[1]) == "table" and is_atts(children[1], tag) and #children ~= 1 then
atts = children[1]
children = { select(2, ...) }
end
if atts == nil and isAtts(children[1], tag) then
if atts == nil and is_atts(children[1], tag) then
atts = children[1]
children = { select(2, children) }
end
atts = atts or children[1]
return createElement(tag, atts, children)
return create_element(tag, atts, children)
end
end
})
Expand Down Expand Up @@ -86,11 +86,11 @@ local function h(element)
end
end
if element.tag:lower() == "doctype" then
return "<!" .. kebabCase(element.tag:lower()) .. " " .. table.concat(element.atts, " ") .. ">" .. children
elseif isVoidTag(element.tag) then
return "<" .. kebabCase(element.tag) .. atts .. ">"
return "<!" .. kebab_case(element.tag:lower()) .. " " .. table.concat(element.atts, " ") .. ">" .. children
elseif is_void_tag(element.tag) then
return "<" .. kebab_case(element.tag) .. atts .. ">"
else
return "<" .. kebabCase(element.tag) .. atts .. ">" .. children .. "</" .. kebabCase(element.tag) .. ">"
return "<" .. kebab_case(element.tag) .. atts .. ">" .. children .. "</" .. kebab_case(element.tag) .. ">"
end
end

Expand Down
Loading

0 comments on commit 195685d

Please sign in to comment.