Pyroxenium / Basalt

A UI Framework for CC:Tweaked
MIT License
194 stars 38 forks source link

feature: Allow to register custom XML parsers #104

Open ajh123 opened 11 months ago

ajh123 commented 11 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Parsing XML (or HTML) can be a challenge in Lua. Having the ability to register custom XML parsers will allow people to do this.

Describe the solution you'd like A clear and concise description of what you want to happen.

The creation of a system that allows people to make custom XML parsers which support custom tags. Each custom tag should be predefined.

Example lua:

-- This function will allow people to do what they want if a tag is parsed for example add it to a layout.
-- This function will be called for all tags. Parent tags (tags with child tags) will have this function run first on them, this could allow custom data to be passed to child tags.
local function parse(tag)
    local name = tag.name -- the name of the tag
    local attrs = tag.attrs -- the tag's attributes
    local children = tag.children -- the tag's children (a table of other tags in the same format) (or a string if it is just text e.g. `<p>text here</p>`)
    if name == "p" then
        if not tag.inDiv then
            frame:addLabel():setText(children) -- amusing `frame` is a `Frame` and `children` is a `string`
        end
    end
    if name == "div" then
        for k,v in pairs(children) do
            v.inDiv = true -- set a custom value on the children of this div
        end
    end
end

local tags = {
    p = { -- define a tag called "p"
        children = "string" -- this will allow only strings inside a `p` if other types are found (like and other tag) then there will be an error. Single numbers should be parsed as a string.
    },
    div = { -- define a tag called "div"
        children = "any" -- allow anything inside a div
    }
}

local parser = basalt.makeParser(tags, parse)
-- The parser object shall work like the normal XML parser with the same `:loadLayout`. In addition there should be a `:loadString` which takes the XML code as a string instead of a file name.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Copying your existing XML parser and modifying it.

Additional context Add any other context or screenshots about the feature request here.

This will allow me to make a better HTML parser for my CC Browser and have a very nice UI.