fix: proper printing of document
parent
e94cdc1260
commit
76d7f2e67b
48
html.lua
48
html.lua
|
@ -523,23 +523,40 @@ function M.clean_text_nodes(node)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function M.print_document(node, indent)
|
function M.tostring(node, indent, include_internal_pseudoelements)
|
||||||
-- Default indentation is 0 (root level)
|
-- Default indentation is 0 (root level)
|
||||||
indent = indent or 0
|
indent = indent or 0
|
||||||
|
include_internal_pseudoelements = include_internal_pseudoelements or false
|
||||||
|
|
||||||
|
local is_pseudo_element = (node.tag_name or ":root"):sub(1,1) == ":"
|
||||||
|
|
||||||
|
|
||||||
local indent_level_str = " "
|
local indent_level_str = " "
|
||||||
-- Create the indentation string (e.g., " " for each level)
|
-- Create the indentation string (e.g., " " for each level)
|
||||||
local indent_str = string.rep(indent_level_str, indent)
|
local indent_str = string.rep(indent_level_str, indent)
|
||||||
|
|
||||||
if node.tag_name == ":text" then
|
if node.tag_name == ":text" then
|
||||||
print(indent_str .. "<:text>\n" .. node.content .. "\n" .. indent_str .. "</:text>")
|
local str = ""
|
||||||
return
|
|
||||||
|
if include_internal_pseudoelements then
|
||||||
|
str = str .. "<:text>"
|
||||||
|
end
|
||||||
|
|
||||||
|
str = str .. node.content
|
||||||
|
|
||||||
|
if include_internal_pseudoelements then
|
||||||
|
str = str .. "</:text>"
|
||||||
|
end
|
||||||
|
|
||||||
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
local node_name = ""
|
local node_name = ""
|
||||||
|
|
||||||
|
if not is_pseudo_element or include_internal_pseudoelements then
|
||||||
-- Print the current node's tag name
|
-- Print the current node's tag name
|
||||||
node_name = node_name .. indent_str .. "<" .. (node.tag_name or ":root")
|
node_name = node_name .. "\n" .. indent_str .. "<" .. (node.tag_name or ":root")
|
||||||
|
end
|
||||||
|
|
||||||
-- Print attributes if any
|
-- Print attributes if any
|
||||||
if next(node.attributes) ~= nil then
|
if next(node.attributes) ~= nil then
|
||||||
|
@ -557,19 +574,36 @@ function M.print_document(node, indent)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not is_pseudo_element or include_internal_pseudoelements then
|
||||||
node_name = node_name .. ">"
|
node_name = node_name .. ">"
|
||||||
|
end
|
||||||
|
|
||||||
print( node_name )
|
--print( node_name )
|
||||||
|
|
||||||
|
local next_indent = indent + 1
|
||||||
|
if is_pseudo_element and not include_internal_pseudoelements then
|
||||||
|
next_indent = indent
|
||||||
|
end
|
||||||
|
|
||||||
-- Recursively print children
|
-- Recursively print children
|
||||||
for _, child in ipairs(node.children) do
|
for _, child in ipairs(node.children) do
|
||||||
M.print_document(child, indent + 1)
|
node_name = node_name .. M.tostring(child, next_indent, include_internal_pseudoelements)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not VOID_TAGS[node.tag_name] and ( not is_pseudo_element or include_internal_pseudoelements ) then
|
||||||
-- Print the closing tag
|
-- Print the closing tag
|
||||||
print(indent_str .. "</" .. (node.tag_name or ":root") .. ">")
|
local end_indent = ""
|
||||||
|
local closing_text_tag = "</:text>"
|
||||||
|
if node_name:sub(#node_name, #node_name) == ">" and node_name:sub(#node_name - #closing_text_tag + 1, #node_name) ~= closing_text_tag then
|
||||||
|
end_indent = "\n" .. indent_str
|
||||||
end
|
end
|
||||||
|
node_name = node_name .. end_indent .. "</" .. (node.tag_name or ":root") .. ">"
|
||||||
|
end
|
||||||
|
|
||||||
|
return node_name
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue