aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-09-19 22:24:20 -0400
committerfrosty <gabriel@bwaaa.monster>2026-09-19 22:24:20 -0400
commit3bff1316d1fc22f81b542d614c4bc90953bfa322 (patch)
treea3b19fdabf3f913edd8ecea55f6566ea1045c69b
parent4c8d764d171ec77ff626cec273c44ec7e7a995fc (diff)
feat: add html.element lua helper
-rw-r--r--examples/site/build.lua12
-rw-r--r--src/Dom/Dom.c44
-rw-r--r--src/Dom/Dom.h1
-rw-r--r--src/LuaEngine/LuaEngine.c1
4 files changed, 50 insertions, 8 deletions
diff --git a/examples/site/build.lua b/examples/site/build.lua
index 0b9e67a..4d0939a 100644
--- a/examples/site/build.lua
+++ b/examples/site/build.lua
@@ -1,9 +1,5 @@
-- External scripts are resolved relative to the HTML file that includes them :)
-local function element(tag, attributes, children)
- return { tag = tag, attributes = attributes or {}, children = children or {} }
-end
-
local function find_by_class(node, wanted)
if type(node) ~= "table" then return nil end
local classes = node.attributes and node.attributes.class or ""
@@ -27,7 +23,7 @@ local features = {
local feature_list = assert(find_by_class(page, "feature-list"))
for _, feature in ipairs(features) do
- table.insert(feature_list.children, element("li", {}, { feature }))
+ table.insert(feature_list.children, html.element("li", nil, { feature }))
end
local entries = {}
@@ -41,8 +37,8 @@ summary.text = tostring(#entries) .. " entries"
local file_list = assert(find_by_class(page, "file-list"))
for _, entry in ipairs(entries) do
- table.insert(file_list.children, element("li", {}, {
- element("code", {}, { entry.path }),
+ table.insert(file_list.children, html.element("li", nil, {
+ html.element("code", nil, { entry.path }),
" (", entry.type, ")"
}))
end
@@ -54,7 +50,7 @@ local mixed = assert(find_by_class(page, "mixed-content"))
mixed.children = {
{ kind = "comment", text = "generated by Lua" },
"Text with ",
- element("strong", {}, { "an element" }),
+ html.element("strong", nil, { "an element" }),
"."
}
diff --git a/src/Dom/Dom.c b/src/Dom/Dom.c
index ede7ddd..6dc6760 100644
--- a/src/Dom/Dom.c
+++ b/src/Dom/Dom.c
@@ -6,6 +6,50 @@
#include <string.h>
#include <strings.h>
+static int html_element(lua_State *lua) {
+ const char *tag;
+
+ luaL_checktype(lua, 1, LUA_TSTRING);
+ tag = lua_tostring(lua, 1);
+ if (tag[0] == '\0') {
+ return luaL_argerror(lua, 1, "tag cannot be empty");
+ }
+ if (!lua_isnoneornil(lua, 2)) {
+ luaL_checktype(lua, 2, LUA_TTABLE);
+ }
+ if (!lua_isnoneornil(lua, 3)) {
+ luaL_checktype(lua, 3, LUA_TTABLE);
+ }
+
+ lua_newtable(lua);
+
+ lua_pushvalue(lua, 1);
+ lua_setfield(lua, -2, "tag");
+
+ if (lua_istable(lua, 2)) {
+ lua_pushvalue(lua, 2);
+ } else {
+ lua_newtable(lua);
+ }
+ lua_setfield(lua, -2, "attributes");
+
+ if (lua_istable(lua, 3)) {
+ lua_pushvalue(lua, 3);
+ } else {
+ lua_newtable(lua);
+ }
+ lua_setfield(lua, -2, "children");
+
+ return 1;
+}
+
+void dom_register_html(lua_State *lua) {
+ lua_newtable(lua);
+ lua_pushcfunction(lua, html_element);
+ lua_setfield(lua, -2, "element");
+ lua_setglobal(lua, "html");
+}
+
static int absolute_index(lua_State *lua, int index) {
return index < 0 ? lua_gettop(lua) + index + 1 : index;
}
diff --git a/src/Dom/Dom.h b/src/Dom/Dom.h
index 06bd1ff..bee6cd1 100644
--- a/src/Dom/Dom.h
+++ b/src/Dom/Dom.h
@@ -6,5 +6,6 @@
void dom_push_page(lua_State *lua, const xmlNode *root);
xmlNodePtr dom_page_from_lua(lua_State *lua, int table_index);
+void dom_register_html(lua_State *lua);
#endif
diff --git a/src/LuaEngine/LuaEngine.c b/src/LuaEngine/LuaEngine.c
index 319f408..c34fd68 100644
--- a/src/LuaEngine/LuaEngine.c
+++ b/src/LuaEngine/LuaEngine.c
@@ -257,6 +257,7 @@ int process_html_document(htmlDocPtr document, const char *input_path) {
luaL_openlibs(lua);
register_yapssg(lua);
+ dom_register_html(lua);
lua_logger_register(lua);
directory_walker_register(lua, input_path);
dom_push_page(lua, root);