Строка 55: |
Строка 55: |
| return subset (node1, node2) and subset (node2, node1) | | return subset (node1, node2) and subset (node2, node1) |
| end | | end |
− |
| + | |
− | -- Merge two tags of the same type with the same attributes:
| + | local function merge_nodes (node1, node2) |
− | local function merge_tags (tag1, tag2) | + | if type (node1) == 'string' and type (node2) == 'string' then |
− | for _, node in ipairs (tag2) do | + | return node1 .. ' ' .. node2 |
− | tag1 [#tag1 + 1] = node
| + | end |
| + | if same_tag_and_attributes (node1, node2) then |
| + | local merged = node1 -- clone? |
| + | for _, node in ipairs (node2) do |
| + | merged [#merged], merged [#merged + 1] = merge_nodes (merged [#merged], node) |
| + | end |
| + | return merged |
| end | | end |
− | return tag1 | + | -- Unmergeable: |
| + | return node1, node2 |
| end | | 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, |
Строка 71: |
Строка 79: |
| tag [attr.name] = attr.value | | tag [attr.name] = attr.value |
| end | | end |
− | local accumulator
| + | for _, node in ipairs (tbl) do |
− | for _, fragment in ipairs (tbl) do | + | tag [#tag], tag [#tag + 1] = merge_nodes (tag [#tag], node) |
− | --mw.log ('In glueing. fragment = ' .. mw.dumpObject (fragment) .. ', accumulator = ' .. mw.dumpObject (accumulators)) | |
− | -- If this tag is of the same type and with the same attributes as the previous one, merge them:
| |
− | if same_tag_and_attributes (fragment, accumulator) then
| |
− | --mw.log ('The same. fragment = ' .. mw.dumpObject (fragment) .. ', accumulator = ' .. mw.dumpObject (accumulator))
| |
− | merge_tags (accumulator, fragment)
| |
− | else
| |
− | --mw.log ('Not the same. fragment = ' .. mw.dumpObject (fragment) .. ', accumulator = ' .. mw.dumpObject (accumulator))
| |
− | tag [#tag + 1] = accumulator
| |
− | accumulator = fragment
| |
− | end
| |
| end | | end |
− | tag [#tag + 1] = accumulator
| + | return tag |
− | return tag | |
| end, | | end, |
| open = lt * spaces * Cg (possible, '__name') * V'attributes' * spaces * gt, | | open = lt * spaces * Cg (possible, '__name') * V'attributes' * spaces * gt, |
Строка 101: |
Строка 98: |
| | | |
| local test = [====[ | | local test = [====[ |
− | <
| + | < |
| ]====] | | ]====] |
| --test = 'Before span<span attr="value">First <i>italicised</i> span</span> Free <span>Second span</span>' | | --test = 'Before span<span attr="value">First <i>italicised</i> span</span> Free <span>Second span</span>' |