Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Allow to register custom XML parsers #104

Open
ajh123 opened this issue Dec 31, 2023 · 0 comments
Open

feature: Allow to register custom XML parsers #104

ajh123 opened this issue Dec 31, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@ajh123
Copy link

ajh123 commented Dec 31, 2023

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.

@ajh123 ajh123 added the enhancement New feature or request label Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant