| 1 |
local unicode = require 'obvious.lib.unicode' |
| 2 |
|
| 3 |
local assert = assert |
| 4 |
local error = error |
| 5 |
local huge = math.huge |
| 6 |
local max = math.max |
| 7 |
local min = math.min |
| 8 |
local setmetatable = setmetatable |
| 9 |
local sfind = string.find |
| 10 |
local sformat = string.format |
| 11 |
local smatch = string.match |
| 12 |
local ssub = string.sub |
| 13 |
local tconcat = table.concat |
| 14 |
local type = type |
| 15 |
local ulength = unicode.length |
| 16 |
local usub = unicode.sub |
| 17 |
|
| 18 |
local rope_mt = {} |
| 19 |
rope_mt.__index = rope_mt |
| 20 |
|
| 21 |
local function rope_tostring(rope, pieces) |
| 22 |
if rope.start_tag then |
| 23 |
pieces[#pieces + 1] = rope.start_tag |
| 24 |
end |
| 25 |
|
| 26 |
for i = 1, #rope do |
| 27 |
if type(rope[i]) == 'string' then |
| 28 |
pieces[#pieces + 1] = rope[i] |
| 29 |
else |
| 30 |
rope_tostring(rope[i], pieces) |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
if rope.end_tag then |
| 35 |
pieces[#pieces + 1] = rope.end_tag |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
function rope_mt:__tostring() |
| 40 |
local pieces = {} |
| 41 |
rope_tostring(self, pieces) |
| 42 |
return tconcat(pieces) |
| 43 |
end |
| 44 |
|
| 45 |
function rope_mt:len() |
| 46 |
return self._len |
| 47 |
end |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
local function rope_substring(rope, start, finish, pieces) |
| 52 |
local chunk_start = 1 |
| 53 |
local chunk_end |
| 54 |
|
| 55 |
if rope._entity then |
| 56 |
pieces[#pieces + 1] = rope[1] |
| 57 |
return |
| 58 |
end |
| 59 |
|
| 60 |
if rope.start_tag then |
| 61 |
pieces[#pieces + 1] = rope.start_tag |
| 62 |
end |
| 63 |
|
| 64 |
for i = 1, #rope do |
| 65 |
if finish < chunk_start then |
| 66 |
break |
| 67 |
end |
| 68 |
|
| 69 |
if type(rope[i]) == 'string' then |
| 70 |
chunk_end = chunk_start + ulength(rope[i]) |
| 71 |
else |
| 72 |
chunk_end = chunk_start + rope[i]._len |
| 73 |
end |
| 74 |
chunk_end = chunk_end - 1 |
| 75 |
|
| 76 |
if start <= chunk_end and finish >= chunk_start then |
| 77 |
local child_start = max(start - chunk_start, 0) + 1 |
| 78 |
local child_end = finish - chunk_start + 1 |
| 79 |
|
| 80 |
if type(rope[i]) == 'string' then |
| 81 |
pieces[#pieces + 1] = usub(rope[i], child_start, child_end) |
| 82 |
else |
| 83 |
rope_substring(rope[i], child_start, child_end, pieces) |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
chunk_start = chunk_end + 1 |
| 88 |
end |
| 89 |
|
| 90 |
if rope.end_tag then |
| 91 |
pieces[#pieces + 1] = rope.end_tag |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
function rope_mt:sub(start, finish) |
| 96 |
finish = finish or self._len |
| 97 |
if finish < 1 or start < 1 then |
| 98 |
error('Negative indices are not yet supported', 2) |
| 99 |
end |
| 100 |
if finish < start then |
| 101 |
return '' |
| 102 |
end |
| 103 |
if start > self._len then |
| 104 |
return '' |
| 105 |
end |
| 106 |
local pieces = {} |
| 107 |
rope_substring(self, start, finish, pieces) |
| 108 |
return tconcat(pieces) |
| 109 |
end |
| 110 |
|
| 111 |
local function calculate_length(rope) |
| 112 |
if rope._len then |
| 113 |
return rope._len |
| 114 |
end |
| 115 |
|
| 116 |
local length = 0 |
| 117 |
|
| 118 |
for i = 1, #rope do |
| 119 |
if type(rope[i]) == 'string' then |
| 120 |
length = length + ulength(rope[i]) |
| 121 |
else |
| 122 |
rope[i]._len = calculate_length(rope[i]) |
| 123 |
length = length + rope[i]._len |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
return length |
| 128 |
end |
| 129 |
|
| 130 |
local function parse_markup(s) |
| 131 |
local current_node = { '' } |
| 132 |
local rope = setmetatable({ current_node }, rope_mt) |
| 133 |
local node_stack = { rope } |
| 134 |
|
| 135 |
local function finish_current_strand() |
| 136 |
if #current_node == 1 and current_node[1] == '' then |
| 137 |
return |
| 138 |
end |
| 139 |
|
| 140 |
current_node = { '' } |
| 141 |
local parent = node_stack[#node_stack] |
| 142 |
parent[#parent + 1] = current_node |
| 143 |
end |
| 144 |
|
| 145 |
local function down(tag) |
| 146 |
node_stack[#node_stack + 1] = current_node |
| 147 |
local child = { '', start_tag = tag } |
| 148 |
current_node[#current_node + 1] = child |
| 149 |
current_node = child |
| 150 |
end |
| 151 |
|
| 152 |
local function up(tag) |
| 153 |
if not current_node.start_tag then |
| 154 |
return nil, 'Imbalanced tags found' |
| 155 |
end |
| 156 |
local start_tag = assert(smatch(current_node.start_tag, '<(%w+)'), sformat("%q didn't match our start tag pattern", current_node.start_tag)) |
| 157 |
local end_tag = assert(smatch(tag, '</(%w+)'), sformat("%q didn't match our end tag pattern", tag)) |
| 158 |
if start_tag ~= end_tag then |
| 159 |
return nil, sformat("end tag %q doesn't match start tag %q", tag, current_node.start_tag) |
| 160 |
end |
| 161 |
current_node.end_tag = tag |
| 162 |
current_node = node_stack[#node_stack] |
| 163 |
node_stack[#node_stack] = nil |
| 164 |
current_node[#current_node + 1] = '' |
| 165 |
|
| 166 |
return true |
| 167 |
end |
| 168 |
|
| 169 |
local end_of_previous_chunk = 1 |
| 170 |
local entity_start, entity_end = sfind(s, '&%w+;') |
| 171 |
local open_tag_start, open_tag_end = sfind(s, '<%w+[^>]*>') |
| 172 |
local close_tag_start, close_tag_end = sfind(s, '</%w+%s*>') |
| 173 |
|
| 174 |
while end_of_previous_chunk < #s do |
| 175 |
local next_special_start = min(entity_start or huge, open_tag_start or huge, close_tag_start or huge, #s + 1) |
| 176 |
|
| 177 |
current_node[#current_node] = current_node[#current_node] .. ssub(s, end_of_previous_chunk, next_special_start - 1) |
| 178 |
end_of_previous_chunk = min(entity_end or huge, open_tag_end or huge, close_tag_end or huge, #s) + 1 |
| 179 |
|
| 180 |
if next_special_start == entity_start then |
| 181 |
finish_current_strand() |
| 182 |
current_node[1] = ssub(s, entity_start, entity_end) |
| 183 |
current_node._len = 1 |
| 184 |
current_node._entity = true |
| 185 |
finish_current_strand() |
| 186 |
entity_start, entity_end = sfind(s, '&%w+;', end_of_previous_chunk) |
| 187 |
elseif next_special_start == open_tag_start then |
| 188 |
down(ssub(s, open_tag_start, open_tag_end)) |
| 189 |
open_tag_start, open_tag_end = sfind(s, '<%w+[^>]*>', end_of_previous_chunk) |
| 190 |
elseif next_special_start == close_tag_start then |
| 191 |
local ok, err = up(ssub(s, close_tag_start, close_tag_end)) |
| 192 |
if not ok then |
| 193 |
return ok, err |
| 194 |
end |
| 195 |
close_tag_start, close_tag_end = sfind(s, '</%w+%s*>', end_of_previous_chunk) |
| 196 |
end |
| 197 |
end |
| 198 |
|
| 199 |
if #node_stack > 1 then |
| 200 |
local pretty = require 'pretty' |
| 201 |
pretty.print(node_stack) |
| 202 |
return nil, 'Unclosed tags at end-of-string' |
| 203 |
end |
| 204 |
|
| 205 |
rope._len = calculate_length(rope) |
| 206 |
return rope |
| 207 |
end |
| 208 |
|
| 209 |
return parse_markup |