Skip to content

Translation from Lua tables to XML using metamethods

Notifications You must be signed in to change notification settings

seblindfors/XMLua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

XMLua

Create standalone element

local elem = XML.Element .id 'root' .value (2) .enabled (true);

print(elem) or tostring(elem) has the following output:

<Element id="root" value="2" enabled="true" />

Create document

-- @param noGlobals:  forbid global lookups in factory, default false
-- @param stackLevel: local stack level, default 2
-- @return XMLDoc(s): objects which will convert to XML when printed

local doc = XML(noGlobals, stackLevel) { elem, elem, ... }

Example

local paragraph = XML.p .style 'color:red' 'This is a paragraph';
local doc = XML() {
    html .lang 'en' {
        head {
            meta .charset 'utf-8';
            meta .name 'description' .content 'This is a description';
            title ('Document title');
        };
        body {
            h1 ['data-parent'] 'root' ('This is a header');
            paragraph;
        };
    };
};

tostring(doc) outputs the following:

<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <meta name="description" content="This is a description"/>
        <title>Document title</title>
    </head>
    <body>
        <h1 data-parent="root">This is a header</h1>
        <p style="color:red">This is a paragraph</p>
    </body>
</html>

About

Translation from Lua tables to XML using metamethods

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages