Diferencia entre revisiones de «Módulo:HtmlBuilder»
De Familia Sanchez Arjona
m (7 revisiones importadas) |
(←Created page with '-- Experimental module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface local HtmlBuilder = {} local metatable = {} metatable.__i...') |
||
(No se muestran 25 ediciones intermedias de 8 usuarios) | |||
Línea 1: | Línea 1: | ||
− | -- | + | -- Experimental module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface |
− | + | ||
local HtmlBuilder = {} | local HtmlBuilder = {} | ||
Línea 23: | Línea 23: | ||
metatable.__tostring = function(t) | metatable.__tostring = function(t) | ||
local ret = {} | local ret = {} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
if t.tagName then | if t.tagName then | ||
− | table.insert(ret, '<' .. t.tagName | + | table.insert(ret, '<' .. t.tagName .. '>') |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
for i, node in ipairs(t.nodes) do | for i, node in ipairs(t.nodes) do | ||
− | + | table.insert(ret, tostring(node)) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | if t.tagName | + | if t.tagName then |
table.insert(ret, '</' .. t.tagName .. '>') | table.insert(ret, '</' .. t.tagName .. '>') | ||
end | end | ||
+ | return table.concat(ret, '') | ||
end | end | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
metatable.wikitext = function(t, ...) | metatable.wikitext = function(t, ...) | ||
local vals = {...} | local vals = {...} | ||
Línea 77: | Línea 42: | ||
end | end | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
return t | return t | ||
end | end | ||
Línea 91: | Línea 51: | ||
table.insert(t.nodes, builder) | table.insert(t.nodes, builder) | ||
return builder | return builder | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Línea 171: | Línea 69: | ||
setmetatable(builder, metatable) | setmetatable(builder, metatable) | ||
builder.nodes = {} | builder.nodes = {} | ||
− | |||
− | |||
builder.tagName = tagName | builder.tagName = tagName | ||
builder.parent = args.parent | builder.parent = args.parent | ||
− | |||
− | |||
return builder | return builder | ||
end | end | ||
return HtmlBuilder | return HtmlBuilder |
Revisión del 08:48 28 feb 2013
Este módulo crea html usando una interfaz fluida Lua.
Uso
para empezar, es necesario cargar el módulo:
HtmlBuilder local = require ('Módule:HtmlBuilder')
A continuación, puede crear html usando los siguientes sub-funciones:
-
create()
- la función básica. Todas las sub-funciones se denominan de esta. -
wikitext()
- wikitexto texto normal que se produce entre las etiquetas html, o fuera de las etiquetas HTML. -
allDone()
- esto cierra cualquier etiqueta html abierta. Cada llamada a HtmlBuilder debe finalizar con esto, independientemente de si se utiliza cualquier etiqueta o no. -
tag()
- especifica que etiqueta utilizar. Puede ser utilizado para crear etiquetas no cerradas utilizando el unclosed {true} = parámetro. -
attr()
- especifica los atributos utilizados en la etiqueta html. -
css()
- especifica estilos CSS usado en un "estilo" atributo en la etiqueta html. Esta función sub-acepta dos parámetros - la primera es la propiedad css, y el segundo es el valor. Por ejemplo, ('background', 'rojo') . -
cssText()
- establece este estilo css como una cadena de texto que contiene la propiedad css y el valor. Por ejemplo, ('background: red; ") -
addClass()
- esto se suma un valor de más de clase a la etiqueta.
-- Experimental module for building complex HTML (e.g. infoboxes, navboxes) using a fluent interface local HtmlBuilder = {} local metatable = {} metatable.__index = function(t, key) local ret = rawget(t, key) if ret then return ret end ret = metatable[key] if type(ret) == 'function' then return function(...) return ret(t, ...) end else return ret end end metatable.__tostring = function(t) local ret = {} if t.tagName then table.insert(ret, '<' .. t.tagName .. '>') end for i, node in ipairs(t.nodes) do table.insert(ret, tostring(node)) end if t.tagName then table.insert(ret, '</' .. t.tagName .. '>') end return table.concat(ret, '') end metatable.wikitext = function(t, ...) local vals = {...} for i = 1, #vals do if vals[i] then table.insert(t.nodes, vals[i]) end end return t end metatable.tag = function(t, tagName, args) args = args or {} args.parent = t local builder = HtmlBuilder.create(tagName, args) table.insert(t.nodes, builder) return builder end metatable.done = function(t) return t.parent or t end metatable.allDone = function(t) while t.parent do t = t.parent end return t end function HtmlBuilder.create(tagName, args) args = args or {} local builder = {} setmetatable(builder, metatable) builder.nodes = {} builder.tagName = tagName builder.parent = args.parent return builder end return HtmlBuilder