Skip to content

ReplicatorLabs/lua-elementtree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-elementtree

Lua element tree serialization library.

  • No third-party dependencies.
  • Support common hierarchial markup languages:
    • XML
    • SVG
    • HTML5
  • Just enough serialization logic for each markup language to express the most common patterns (ex: HTML5 web sites, SVG diagrams, etc).
  • Just enough deserialization logic to parse markup generated by this library.

This library explicitly does not aim to support 100% of the markup language standards and there is no expectation it can correctly parse or recreate arbitrary third-party documents.

Usage

Parse, manipulate, create, and dump documents:

local et <const> = require('elementtree')

local input_file <close> = assert(io.open('input.html', 'r'))
local input_data <const> = assert(input_file:read('a'))
assert(input_file:close())

local document <const> = et.HTML5.load_string(input_data)

-- manipulate element attributes
local html_element <const> = document.root
assert(html_element.tag == 'html')
html_element:set_attribute('lang', 'en')
assert(html_element:get_attribute('lang') == 'en')

-- manipulate element children
local head_node <const> = html_element.children[1]
assert(head_node.tag == 'head')

while #head_node.children > 0 do
  head_node:remove_child(1)
end

-- create new elements
head_node:insert_child(et.Node('title', {}, {'A Modified Document'}))
head_node:insert_child(et.Node('link', {rel='stylesheet', href='style.css'}, {}))

-- prevent any future changes to this element and it's children
document.root:freeze()

local output_data <const> = et.HTML5.dump_string(document)
local output_file <close> = assert(io.open('output.html', 'w'))
assert(output_file:write(output_data))
assert(output_file:close())

See the unit and integration tests for more in-depth examples. The project script also has a command for loading and dumping a local file if you want to quickly check whether this library can interpret it:

git submodule update --init --recursive
./project.lua check ./tests/html/hello_world.html

XML

You may be able to preprocess XML documents using Tidy with the provided configuration file in order to parse them with this library:

tidy -config ./tidy-config --input-xml --output-xml yes -o clean.xml original.xml

SVG

You may be able to preprocess SVG documents using Tidy with the provided configuration file in order to parse them with this library:

tidy -config ./tidy-config --input-xml yes --output-xml yes -o clean.svg original.svg

HTML5

You may be able to preprocess HTML5 documents using Tidy with the provided configuration file in order to parse them with this library:

tidy -config ./tidy-config --output-html yes -o clean.html original.html

Tests

Run unit and integration tests:

env LUA_ELEMENTTREE_LEAK_INTERNALS=TRUE ./project.lua test

Parse a local file to validate the library can correctly interpret it:

./project.lua check ./tests/html/hello_world.html

Roadmap

Planned:

  • Lua 5.4 support.
    • Unit tests.
    • Integration tests.
  • LuaRocks package.

Open to consideration:

  • LuaJIT support.
    • Integration tests.
  • Lua 5.3 support.
    • Integration tests.
  • Lua 5.2 support.
    • Integration tests.
  • Lua 5.1 support.
    • Integration tests.

References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •