Строка 35: |
Строка 35: |
| end | | end |
| | | |
| + | -- Return true, if node1 and node2 are tags ot the same type with exactly the same attributes: |
| local function same_tag_and_attributes (node1, node2) | | local function same_tag_and_attributes (node1, node2) |
| if type (node1) ~= 'table' or type (node2) ~= 'table' then | | if type (node1) ~= 'table' or type (node2) ~= 'table' then |
| + | return false |
| + | end |
| + | if table_tags [node1.__name] or table_tags [node2.__name] then |
| return false | | return false |
| end | | end |
Строка 52: |
Строка 56: |
| end | | end |
| | | |
| + | -- Merge two tags of the same type with the same attributes: |
| + | local function merge_tags (tag1, tag2) |
| + | for _, node in ipairs (tag2) do |
| + | tag1 [#tag1 + 1] = node |
| + | end |
| + | return tag1 |
| + | end |
| + | |
| local grammar = P { V'fragment' * -1, | | local grammar = P { V'fragment' * -1, |
| fragment = (V'tag' + C (V'char' ^ 1)) ^ 0, | | fragment = (V'tag' + C (V'char' ^ 1)) ^ 0, |
Строка 59: |
Строка 71: |
| tag [attr.name] = attr.value | | tag [attr.name] = attr.value |
| end | | end |
− | local previous | + | local accumulator |
| for _, fragment in ipairs (tbl) do | | for _, fragment in ipairs (tbl) do |
− | if same_tag_and_attributes (fragment, previous) then | + | --mw.log ('In glueing. fragment = ' .. mw.dumpObject (fragment) .. ', accumulator = ' .. mw.dumpObject (accumulators)) |
− | for __, subfragment in ipairs (fragment) do | + | -- If this tag is of the same type and with the same attributes as the previous one, merge them: |
− | previous [#previous + 1] = subfragment
| + | if same_tag_and_attributes (fragment, accumulator) then |
− | end | + | --mw.log ('The same. fragment = ' .. mw.dumpObject (fragment) .. ', accumulator = ' .. mw.dumpObject (accumulator)) |
| + | merge_tags (accumulator, fragment) |
| else | | else |
− | tag [#tag + 1] = fragment | + | --mw.log ('Not the same. fragment = ' .. mw.dumpObject (fragment) .. ', accumulator = ' .. mw.dumpObject (accumulator)) |
− | previous = fragment | + | tag [#tag + 1] = accumulator |
| + | accumulator = fragment |
| end | | end |
| end | | end |
| + | tag [#tag + 1] = accumulator |
| return tag | | return tag |
| end, | | end, |
Строка 104: |
Строка 119: |
| test = function (frame) | | test = function (frame) |
| return mw.dumpObject {grammar:match (test)} | | return mw.dumpObject {grammar:match (test)} |
− | end | + | end, same = same_tag_and_attributes |
| } | | } |