lua-gumbo
is a HTML5 parser and DOM library for Lua. It
originally started out as a set of Lua bindings for the Gumbo C
library, but has now absorbed an improved fork of it.
To install the latest release via LuaRocks use the command:
luarocks install gumbo
The gumbo
module provides a parse
function and a parseFile
function, which both return a Document
node containing a tree of
descendant nodes. The structure and API of this tree mostly follows
the DOM Level 4 Core specification.
For full API documentation, see: https://craigbarnes.gitlab.io/lua-gumbo/.
The following is a simple demonstration of how to find an element by ID and print the contents of it's first child text node.
local gumbo = require "gumbo"
local document = gumbo.parse('<div id="foo">Hello World</div>')
local foo = document:getElementById("foo")
local text = foo.childNodes[1].data
print(text) --> Hello World
Note: this example omits error handling for the sake of simplicity.
Production code should wrap each step with assert()
or some other,
application-specific error handling.
See also: https://craigbarnes.gitlab.io/lua-gumbo/#examples.