Skip to content

pessoa736/DaviLuaXML

Repository files navigation

DaviLuaXML

XML syntax for Lua - write XML directly in your Lua code, similar to JSX in JavaScript.

LuaRocks

🌐 Language / Idioma / Idioma: English | Português | Español

Installation

luarocks install daviluaxml

Basic Usage

-- Register the loader for .lx files
require("DaviLuaXML")

-- Now you can use require() with .lx files
local App = require("my_component")

Example .lx file

-- component.lx
local function Button(props, children)
    return string.format('<button class="%s">%s</button>', 
        props.class, 
        children[1]
    )
end

local function App()
    return <div class="container">
        <h1>Hello World!</h1>
        <Button class="primary">Click here</Button>
    </div>
end

return App

How It Works

DaviLuaXML transforms XML tags into Lua function calls:

-- This:
local el = <div class="container">Hello</div>

-- Becomes:
local el = div({class = "container"}, {"Hello"})

The function receives two arguments:

  • props - table with the attributes
  • children - table with the children (text, numbers or other elements)

XML Syntax

Simple tags

<div/>                          -- Self-closing tag
<p>text</p>                     -- Tag with content

Attributes

<btn class="primary"/>          -- String
<input value={variable}/>       -- Lua expression
<comp enabled/>                 -- Boolean (true)

Expressions in braces

<sum>{1}{2}{3}</sum>            -- Multiple values
<p>{name .. " " .. surname}</p> -- Lua expressions

Nested tags

<div>
    <span>text</span>
    <ul>
        <li>item 1</li>
        <li>item 2</li>
    </ul>
</div>

Tags with dot (namespaces)

<html.div class="x"/>           -- Becomes: html.div({class = "x"}, {})

API

require("DaviLuaXML")

Registers the loader for .lx files. After that, require() works with .lx files.

require("DaviLuaXML.core")

local lx = require("DaviLuaXML.core")
local result, err = lx("file.lx")

Directly executes an .lx file by path.

require("DaviLuaXML.help")

local help = require("DaviLuaXML.help")
help()              -- General help
help("syntax")      -- Specific topic
help.list()         -- List topics
help.lang("en")     -- Set language (en, pt, es)

Logging (Debug)

DaviLuaXML uses loglua for logging. Debug logs are in the XMLRuntime section:

require("DaviLuaXML")
require("my_module")

-- Show runtime debug logs
log.show("XMLRuntime")

Modules

Module Description
init Registers the searcher for require()
core Directly executes .lx files
parser Parses XML tags
transform Transforms XML to Lua
elements Creates elements (tables)
props Processes attributes
errors Error formatting
help Help system

Tests

lua DaviLuaXML/test/run_all.lua

Dependencies

License

MIT

About

XML syntax for Lua - write XML directly in your Lua code, similar to JSX in JavaScript.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published