Строка 1: |
Строка 1: |
| local sort, concat, clone = table.sort, table.concat, mw.clone | | local sort, concat, clone = table.sort, table.concat, mw.clone |
− | local match, gmatch, gsub = mw.ustring.match, mw.ustring.gmatch, mw.ustring.gsub | + | local match, find, gmatch, gsub = mw.ustring.match, mw.ustring.find, mw.ustring.gmatch, mw.ustring.gsub |
| local lpeg = lpeg | | local lpeg = lpeg |
| local P, S, V, C, Cg, Cb, Ct, Cf, Cmt = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Cg, lpeg.Cb, lpeg.Ct, lpeg.Cf, lpeg.Cmt | | local P, S, V, C, Cg, Cb, Ct, Cf, Cmt = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Cg, lpeg.Cb, lpeg.Ct, lpeg.Cf, lpeg.Cmt |
Строка 26: |
Строка 26: |
| end | | end |
| return set | | return set |
| + | end |
| + | |
| + | -- Serialise a parsed tag back: |
| + | local function serialise (tag) |
| + | if not tag then |
| + | return '' |
| + | end |
| + | if type (tag) == 'string' then |
| + | return tag |
| + | end |
| + | local serialised = {} |
| + | if tag.__name then |
| + | serialised [#serialised + 1] = '<' .. tag.__name |
| + | for attr, value in pairs (tag) do |
| + | if type (attr) ~= 'number' and attr ~= '__name' and value ~= '' then |
| + | serialised [#serialised + 1] = attr .. '="' .. gsub (value, '"', '\\"', 1, true) .. '"' |
| + | end |
| + | end |
| + | serialised [#serialised + 1] = '>' |
| + | end |
| + | |
| + | for _, node in ipairs (tag) do |
| + | serialised [#serialised + 1] = serialise (node) |
| + | end |
| + | |
| + | if tag.__name then |
| + | serialised [#serialised + 1] = '</' .. tag.__name .. '>' |
| + | end |
| + | |
| + | return concat (serialised, ' ') |
| end | | end |
| | | |
Строка 125: |
Строка 155: |
| end | | end |
| return node | | return node |
− | end
| |
− |
| |
− | -- Serialise a parsed tag back:
| |
− | local function serialise (tag)
| |
− | if not tag then
| |
− | return ''
| |
− | end
| |
− | if type (tag) == 'string' then
| |
− | return tag
| |
− | end
| |
− | local serialised = {}
| |
− | if tag.__name then
| |
− | serialised [#serialised + 1] = '<' .. tag.__name
| |
− | for attr, value in pairs (tag) do
| |
− | if type (attr) ~= 'number' and attr ~= '__name' and value ~= '' then
| |
− | serialised [#serialised + 1] = attr .. '="' .. gsub (value, '"', '\\"', 1, true) .. '"'
| |
− | end
| |
− | end
| |
− | serialised [#serialised + 1] = '>'
| |
− | end
| |
− |
| |
− | for _, node in ipairs (tag) do
| |
− | serialised [#serialised + 1] = serialise (node)
| |
− | end
| |
− |
| |
− | if tag.__name then
| |
− | serialised [#serialised + 1] = '</' .. tag.__name .. '>'
| |
− | end
| |
− |
| |
− | return concat (serialised, ' ')
| |
| end | | end |
| | | |
Строка 167: |
Строка 167: |
| fragment = (V'tag' + C (V'char' ^ 1)) ^ 0, | | fragment = (V'tag' + C (V'char' ^ 1)) ^ 0, |
| tag = Ct (V'open' * V'fragment' * V'close' + V'void') / function (tbl) | | tag = Ct (V'open' * V'fragment' * V'close' + V'void') / function (tbl) |
− | --local tag = setmetatable ({ __name = tbl.__name }, { __tostring = serialise })
| |
| local tag = { __name = tbl.__name } | | local tag = { __name = tbl.__name } |
| for _, attr in ipairs (tbl.__attrs) do | | for _, attr in ipairs (tbl.__attrs) do |
Строка 173: |
Строка 172: |
| end | | end |
| for _, node in ipairs (tbl) do | | for _, node in ipairs (tbl) do |
− | if #tag == 0 then | + | --if not (type (node) == 'string' and gsub (node, '%s+', '') == '') then |
− | tag [1] = node
| + | if #tag == 0 then |
− | else
| + | tag [1] = node |
− | tag [#tag], tag [#tag + 1] = merge_nodes (tag [#tag], node)
| + | else |
− | end | + | tag [#tag], tag [#tag + 1] = merge_nodes (tag [#tag], node) |
| + | end |
| + | --end |
| end | | end |
| tag = assimilate (tag) -- try to assimilate the only child. | | tag = assimilate (tag) -- try to assimilate the only child. |
− | if not empty [tag.__name] and (#tag == 0 or type (tag [1]) == 'string' and match (tag [1], '^%s*$')) then | + | if not empty [tag.__name] and #tag == 0 then |
| -- An empty tag of this type can be safely dropped: | | -- An empty tag of this type can be safely dropped: |
| return nil | | return nil |