markbridge 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/markbridge/ast/deletion.rb +15 -0
- data/lib/markbridge/ast/highlight.rb +13 -0
- data/lib/markbridge/ast/insertion.rb +14 -0
- data/lib/markbridge/ast/spoiler.rb +12 -1
- data/lib/markbridge/ast/table.rb +25 -4
- data/lib/markbridge/ast.rb +3 -0
- data/lib/markbridge/normalizer/carrier.rb +86 -0
- data/lib/markbridge/normalizer/report.rb +12 -7
- data/lib/markbridge/normalizer/rule_set.rb +22 -0
- data/lib/markbridge/normalizer/walker.rb +233 -36
- data/lib/markbridge/normalizer.rb +106 -23
- data/lib/markbridge/parsers/bbcode/closing_strategies/base.rb +4 -2
- data/lib/markbridge/parsers/bbcode/closing_strategies/reordering.rb +5 -0
- data/lib/markbridge/parsers/bbcode/closing_strategies/tag_reconciler.rb +99 -5
- data/lib/markbridge/parsers/bbcode/handler_registry.rb +1 -1
- data/lib/markbridge/parsers/bbcode/handlers/code_handler.rb +6 -1
- data/lib/markbridge/parsers/bbcode/handlers/continued_formatting.rb +33 -0
- data/lib/markbridge/parsers/bbcode/handlers/email_handler.rb +5 -0
- data/lib/markbridge/parsers/bbcode/handlers/list_handler.rb +0 -5
- data/lib/markbridge/parsers/bbcode/handlers/list_item_handler.rb +38 -4
- data/lib/markbridge/parsers/bbcode/handlers/open_table_part.rb +67 -0
- data/lib/markbridge/parsers/bbcode/handlers/table_cell_handler.rb +11 -3
- data/lib/markbridge/parsers/bbcode/handlers/table_handler.rb +0 -9
- data/lib/markbridge/parsers/bbcode/handlers/table_row_handler.rb +3 -11
- data/lib/markbridge/parsers/bbcode/handlers/url_handler.rb +6 -0
- data/lib/markbridge/parsers/bbcode/parser.rb +1 -0
- data/lib/markbridge/parsers/bbcode/parser_state.rb +51 -0
- data/lib/markbridge/parsers/bbcode/scanner.rb +42 -1
- data/lib/markbridge/parsers/bbcode.rb +2 -0
- data/lib/markbridge/parsers/html/handler_registry.rb +32 -3
- data/lib/markbridge/parsers/html/handlers/raw_handler.rb +14 -1
- data/lib/markbridge/parsers/html/handlers/table_cell_handler.rb +6 -1
- data/lib/markbridge/parsers/html/parser.rb +34 -5
- data/lib/markbridge/parsers/media_wiki/inline_parser.rb +183 -13
- data/lib/markbridge/parsers/media_wiki/inline_tag_registry.rb +14 -2
- data/lib/markbridge/parsers/media_wiki/parser.rb +95 -9
- data/lib/markbridge/parsers/text_formatter/handler_registry.rb +50 -3
- data/lib/markbridge/parsers/text_formatter/handlers/attribute_handler.rb +9 -2
- data/lib/markbridge/parsers/text_formatter/handlers/heading_handler.rb +27 -0
- data/lib/markbridge/parsers/text_formatter/handlers/self_closing_handler.rb +28 -0
- data/lib/markbridge/parsers/text_formatter/handlers/simple_handler.rb +8 -2
- data/lib/markbridge/parsers/text_formatter/handlers/table_cell_handler.rb +6 -1
- data/lib/markbridge/parsers/text_formatter/parser.rb +23 -7
- data/lib/markbridge/parsers/text_formatter.rb +2 -0
- data/lib/markbridge/renderers/discourse/base_url.rb +89 -0
- data/lib/markbridge/renderers/discourse/inline/joins.rb +22 -0
- data/lib/markbridge/renderers/discourse/inline/serializer.rb +57 -13
- data/lib/markbridge/renderers/discourse/markdown_escaper.rb +8 -1
- data/lib/markbridge/renderers/discourse/paragraphs.rb +1 -1
- data/lib/markbridge/renderers/discourse/postprocessor.rb +12 -0
- data/lib/markbridge/renderers/discourse/renderer.rb +6 -1
- data/lib/markbridge/renderers/discourse/rendering_interface.rb +5 -3
- data/lib/markbridge/renderers/discourse/tags/color_tag.rb +1 -1
- data/lib/markbridge/renderers/discourse/tags/deletion_tag.rb +25 -0
- data/lib/markbridge/renderers/discourse/tags/highlight_tag.rb +25 -0
- data/lib/markbridge/renderers/discourse/tags/image_tag.rb +7 -2
- data/lib/markbridge/renderers/discourse/tags/insertion_tag.rb +25 -0
- data/lib/markbridge/renderers/discourse/tags/paragraph_tag.rb +13 -3
- data/lib/markbridge/renderers/discourse/tags/quote_tag.rb +10 -1
- data/lib/markbridge/renderers/discourse/tags/size_tag.rb +1 -1
- data/lib/markbridge/renderers/discourse/tags/spoiler_tag.rb +46 -2
- data/lib/markbridge/renderers/discourse/tags/table_tag.rb +38 -21
- data/lib/markbridge/renderers/discourse/tags/url_tag.rb +6 -1
- data/lib/markbridge/renderers/discourse.rb +4 -0
- data/lib/markbridge/version.rb +1 -1
- data/lib/markbridge.rb +17 -3
- metadata +13 -1
|
@@ -17,8 +17,8 @@ module Markbridge
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def process(element:, parent:)
|
|
20
|
-
# Get the inner text content
|
|
21
20
|
content = element.inner_text
|
|
21
|
+
content = content.delete_prefix("\n") if leading_newline_ignored?(element)
|
|
22
22
|
|
|
23
23
|
ast_element =
|
|
24
24
|
@element_class.new(language: language_for(element), block: block_for(element))
|
|
@@ -33,6 +33,19 @@ module Markbridge
|
|
|
33
33
|
|
|
34
34
|
private
|
|
35
35
|
|
|
36
|
+
HTML5_DOCUMENT = "Nokogiri::HTML5::Document"
|
|
37
|
+
private_constant :HTML5_DOCUMENT
|
|
38
|
+
|
|
39
|
+
# HTML drops a line break right after <pre>. Nokogiri's HTML5
|
|
40
|
+
# parser has done that already, its HTML4 parser keeps the line
|
|
41
|
+
# break. A line break inside a <code> in the <pre> is content.
|
|
42
|
+
# The class is compared by name, JRuby's Nokogiri has no HTML5.
|
|
43
|
+
def leading_newline_ignored?(element)
|
|
44
|
+
return false unless element.name == "pre" && element.children.first&.text?
|
|
45
|
+
|
|
46
|
+
!element.document.class.name.eql?(HTML5_DOCUMENT)
|
|
47
|
+
end
|
|
48
|
+
|
|
36
49
|
# <pre> is a block by definition, so its content keeps the fenced
|
|
37
50
|
# form even on one line; <code> and <tt> leave the decision to the
|
|
38
51
|
# renderer's newline check.
|
|
@@ -11,7 +11,12 @@ module Markbridge
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def process(element:, parent:)
|
|
14
|
-
ast_element =
|
|
14
|
+
ast_element =
|
|
15
|
+
AST::TableCell.new(
|
|
16
|
+
header: element.name.downcase == "th",
|
|
17
|
+
colspan: element["colspan"],
|
|
18
|
+
rowspan: element["rowspan"],
|
|
19
|
+
)
|
|
15
20
|
parent << ast_element
|
|
16
21
|
ast_element
|
|
17
22
|
end
|
|
@@ -81,6 +81,7 @@ module Markbridge
|
|
|
81
81
|
# end-of-line trim), or nil.
|
|
82
82
|
@line_start = true
|
|
83
83
|
@space_tail = nil
|
|
84
|
+
@pending_break = false
|
|
84
85
|
|
|
85
86
|
# Process all nodes
|
|
86
87
|
children.each { |node| process_node(node, document) }
|
|
@@ -128,6 +129,7 @@ module Markbridge
|
|
|
128
129
|
text = node.text
|
|
129
130
|
|
|
130
131
|
if @preserve_depth.positive?
|
|
132
|
+
add_pending_break(parent)
|
|
131
133
|
parent << AST::Text.new(text)
|
|
132
134
|
# Preserved whitespace is content: it neither starts a line
|
|
133
135
|
# nor collapses against a space that follows it.
|
|
@@ -140,6 +142,7 @@ module Markbridge
|
|
|
140
142
|
text = text.lstrip if @line_start || @space_tail
|
|
141
143
|
return if text.empty?
|
|
142
144
|
|
|
145
|
+
add_pending_break(parent)
|
|
143
146
|
parent << AST::Text.new(text)
|
|
144
147
|
@line_start = false
|
|
145
148
|
@space_tail = (parent if text.end_with?(" "))
|
|
@@ -165,7 +168,7 @@ module Markbridge
|
|
|
165
168
|
# is registered, so unknown tags like <div> or <section> still
|
|
166
169
|
# end lines.
|
|
167
170
|
block = @handlers.block_level_tags.include?(tag_name)
|
|
168
|
-
|
|
171
|
+
block_boundary(tag_name) if block
|
|
169
172
|
|
|
170
173
|
preserving = @handlers.whitespace_preserving_tags.include?(tag_name)
|
|
171
174
|
@preserve_depth += 1 if preserving
|
|
@@ -173,7 +176,30 @@ module Markbridge
|
|
|
173
176
|
dispatch_element(node, tag_name, parent)
|
|
174
177
|
|
|
175
178
|
@preserve_depth -= 1 if preserving
|
|
176
|
-
|
|
179
|
+
block_boundary(tag_name) if block
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# The start or the end of a block-level tag. A tag with a handler
|
|
183
|
+
# builds a block node, which the renderer keeps apart from its
|
|
184
|
+
# neighbors. A tag without one, a <div> for example, builds
|
|
185
|
+
# nothing, so content on the line in front of the boundary gets a
|
|
186
|
+
# line break before the content that follows:
|
|
187
|
+
# <div>a</div><div>b</div> is two lines in a browser.
|
|
188
|
+
# @param tag_name [String]
|
|
189
|
+
def block_boundary(tag_name)
|
|
190
|
+
@pending_break = !@handlers[tag_name] && (@pending_break || !@line_start)
|
|
191
|
+
end_line
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Writes the line break of a block without a handler in front of
|
|
195
|
+
# the content that follows it. A <br> that already ends the line
|
|
196
|
+
# is enough.
|
|
197
|
+
# @param parent [AST::Element]
|
|
198
|
+
def add_pending_break(parent)
|
|
199
|
+
return unless @pending_break
|
|
200
|
+
|
|
201
|
+
@pending_break = false
|
|
202
|
+
parent << AST::LineBreak.new unless parent.children.last.instance_of?(AST::LineBreak)
|
|
177
203
|
end
|
|
178
204
|
|
|
179
205
|
# Dispatch an element to its handler (or the unknown-tag path) and
|
|
@@ -185,6 +211,8 @@ module Markbridge
|
|
|
185
211
|
handler = @handlers[tag_name]
|
|
186
212
|
return handle_unknown_tag(node, parent) unless handler
|
|
187
213
|
|
|
214
|
+
add_pending_break(parent)
|
|
215
|
+
|
|
188
216
|
# Handler returns element if children should be processed, nil otherwise
|
|
189
217
|
size = parent.children.size
|
|
190
218
|
ast_element = handler.process(element: node, parent:)
|
|
@@ -199,12 +227,13 @@ module Markbridge
|
|
|
199
227
|
end
|
|
200
228
|
end
|
|
201
229
|
|
|
202
|
-
# Handle
|
|
203
|
-
#
|
|
230
|
+
# Handle a tag without a handler: the wrapper is ignored, its
|
|
231
|
+
# children are processed. It is counted as unknown unless the
|
|
232
|
+
# registry lists it as transparent.
|
|
204
233
|
# @param node [Nokogiri::XML::Element]
|
|
205
234
|
# @param parent [AST::Element]
|
|
206
235
|
def handle_unknown_tag(node, parent)
|
|
207
|
-
@unknown_tags[node.name] += 1
|
|
236
|
+
@unknown_tags[node.name] += 1 unless @handlers.transparent_tags.include?(node.name)
|
|
208
237
|
process_children(node, parent)
|
|
209
238
|
end
|
|
210
239
|
|
|
@@ -58,6 +58,7 @@ module Markbridge
|
|
|
58
58
|
@length = text.bytesize
|
|
59
59
|
@parent = parent
|
|
60
60
|
@text_buffer = +""
|
|
61
|
+
@unclosed = []
|
|
61
62
|
|
|
62
63
|
while (span_end = @input.byteindex(INTERESTING, @pos))
|
|
63
64
|
# Unconditional on purpose: when the interesting byte sits at
|
|
@@ -160,6 +161,11 @@ module Markbridge
|
|
|
160
161
|
# position. Returns the collected content string, or nil (the
|
|
161
162
|
# exhausted while loop) if no closing run exists.
|
|
162
163
|
#
|
|
164
|
+
# Three apostrophes inside italic open bold, they do not end the
|
|
165
|
+
# italic: the search goes on behind the end of that bold. Five
|
|
166
|
+
# apostrophes there end the bold with three and the italic with
|
|
167
|
+
# the two that are left.
|
|
168
|
+
#
|
|
163
169
|
# @param count [Integer] number of consecutive apostrophes to match
|
|
164
170
|
# @return [String, nil]
|
|
165
171
|
def collect_until_apostrophes(count)
|
|
@@ -168,6 +174,12 @@ module Markbridge
|
|
|
168
174
|
while (index = @input.byteindex("'", probe))
|
|
169
175
|
run = consecutive_apostrophes_at(index)
|
|
170
176
|
if run >= count
|
|
177
|
+
bold_end = (bold_end_after(index + BOLD) if count == ITALIC && run == BOLD)
|
|
178
|
+
if bold_end
|
|
179
|
+
probe = bold_end
|
|
180
|
+
next
|
|
181
|
+
end
|
|
182
|
+
|
|
171
183
|
content = @input.byteslice(start, index - start)
|
|
172
184
|
@pos = index + count
|
|
173
185
|
return content
|
|
@@ -176,14 +188,38 @@ module Markbridge
|
|
|
176
188
|
end
|
|
177
189
|
end
|
|
178
190
|
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
ITALIC = 2
|
|
192
|
+
BOLD = 3
|
|
193
|
+
private_constant :ITALIC, :BOLD
|
|
194
|
+
|
|
195
|
+
# @param from [Integer] the position behind three apostrophes that
|
|
196
|
+
# open bold
|
|
197
|
+
# @return [Integer, nil] the position behind the three apostrophes
|
|
198
|
+
# that close it, nil when nothing closes it
|
|
199
|
+
def bold_end_after(from)
|
|
200
|
+
while (index = @input.byteindex("'", from))
|
|
201
|
+
run = consecutive_apostrophes_at(index)
|
|
202
|
+
return index + BOLD if run >= BOLD
|
|
203
|
+
|
|
204
|
+
from = index + run
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Nobody asks for more than five apostrophes, or whether a run
|
|
209
|
+
# has exactly three. Counting stops here, so a line of thousands
|
|
210
|
+
# of apostrophes is not counted to its end from every position.
|
|
211
|
+
MAX_APOSTROPHES = 6
|
|
212
|
+
private_constant :MAX_APOSTROPHES
|
|
213
|
+
|
|
214
|
+
# Count consecutive apostrophes starting at position, up to
|
|
215
|
+
# MAX_APOSTROPHES. getbyte past end-of-input returns nil, which
|
|
216
|
+
# ends the run.
|
|
181
217
|
#
|
|
182
218
|
# @param pos [Integer]
|
|
183
219
|
# @return [Integer]
|
|
184
220
|
def consecutive_apostrophes_at(pos)
|
|
185
221
|
count = 0
|
|
186
|
-
count += 1 while @input.getbyte(pos + count) == APOSTROPHE
|
|
222
|
+
count += 1 while count < MAX_APOSTROPHES && @input.getbyte(pos + count) == APOSTROPHE
|
|
187
223
|
count
|
|
188
224
|
end
|
|
189
225
|
|
|
@@ -192,7 +228,7 @@ module Markbridge
|
|
|
192
228
|
@pos += 2 # skip [[
|
|
193
229
|
start = @pos
|
|
194
230
|
|
|
195
|
-
close_pos =
|
|
231
|
+
close_pos = link_close(@pos)
|
|
196
232
|
unless close_pos
|
|
197
233
|
@text_buffer << "[["
|
|
198
234
|
return
|
|
@@ -201,15 +237,129 @@ module Markbridge
|
|
|
201
237
|
content = @input.byteslice(start, close_pos - start)
|
|
202
238
|
@pos = close_pos + 2
|
|
203
239
|
|
|
240
|
+
# Nothing between the brackets is not a link, MediaWiki shows
|
|
241
|
+
# the brackets.
|
|
242
|
+
if content.empty?
|
|
243
|
+
@text_buffer << "[[]]"
|
|
244
|
+
return
|
|
245
|
+
end
|
|
246
|
+
|
|
204
247
|
target, display = content.split("|", 2)
|
|
205
248
|
target = target.strip
|
|
249
|
+
# A category puts the page into it and shows nothing in the text.
|
|
250
|
+
return if target.match?(CATEGORY)
|
|
251
|
+
return file_embed(target, display) if target.match?(FILE)
|
|
252
|
+
|
|
206
253
|
display = (display || target).strip
|
|
207
254
|
|
|
208
|
-
url = AST::Url.new(href: target)
|
|
255
|
+
url = AST::Url.new(href: page_href(target))
|
|
209
256
|
url << AST::Text.new(display)
|
|
210
257
|
@parent << url
|
|
211
258
|
end
|
|
212
259
|
|
|
260
|
+
CATEGORY = /\Acategory:/i
|
|
261
|
+
FILE = /\A(?:file|image):/i
|
|
262
|
+
IMAGE_NAME = /\.(?:png|jpe?g|gif|svg|webp|bmp|tiff?)\z/i
|
|
263
|
+
# `200px`, `200x100px` or `x100px`.
|
|
264
|
+
FILE_SIZE = /\A(\d+)?(?:x(\d+))?px\z/i
|
|
265
|
+
FILE_ALT = /\Aalt=/i
|
|
266
|
+
# What says how MediaWiki shows the file, which Markdown has no
|
|
267
|
+
# form for: the frame, the alignment and the named options.
|
|
268
|
+
FILE_OPTION =
|
|
269
|
+
/\A(?:thumb|thumbnail|frame|framed|frameless|border|left|right|center|none|baseline|sub|super|top|text-top|middle|bottom|text-bottom|upright|(?:upright|link|class|lang|page|thumb|thumbnail)=.*)\z/i
|
|
270
|
+
# The parameters are apart by pipes, but not by the pipe of a link
|
|
271
|
+
# in the caption.
|
|
272
|
+
FILE_PARAMETER = /(?:\[\[[^\]]*\]\]|[^|])+/
|
|
273
|
+
WIKI_MARKUP = /\[\[(?:[^\]|]*\|)?([^\]]*)\]\]|'{2,}/
|
|
274
|
+
private_constant :CATEGORY,
|
|
275
|
+
:FILE,
|
|
276
|
+
:IMAGE_NAME,
|
|
277
|
+
:FILE_SIZE,
|
|
278
|
+
:FILE_ALT,
|
|
279
|
+
:FILE_OPTION,
|
|
280
|
+
:FILE_PARAMETER,
|
|
281
|
+
:WIKI_MARKUP
|
|
282
|
+
|
|
283
|
+
# A link in a link in the caption of a file is as deep as wikitext
|
|
284
|
+
# goes. The limit also keeps the search short for a row of `[[`
|
|
285
|
+
# that nothing closes: without it every one of them would look
|
|
286
|
+
# through all the others.
|
|
287
|
+
MAX_LINK_NESTING = 3
|
|
288
|
+
LINK_EDGE = /\[\[|\]\]/
|
|
289
|
+
private_constant :MAX_LINK_NESTING, :LINK_EDGE
|
|
290
|
+
|
|
291
|
+
# The `]]` that closes the link whose content starts at +from+. A
|
|
292
|
+
# link inside, in the caption of a file for example, has its own.
|
|
293
|
+
#
|
|
294
|
+
# @return [Integer, nil] nil when nothing closes the link, or when
|
|
295
|
+
# more than MAX_LINK_NESTING links are open inside of it
|
|
296
|
+
def link_close(from)
|
|
297
|
+
depth = 0
|
|
298
|
+
while (index = @input.byteindex(LINK_EDGE, from))
|
|
299
|
+
if @input.getbyte(index) == BRACKET_OPEN
|
|
300
|
+
return if depth == MAX_LINK_NESTING
|
|
301
|
+
|
|
302
|
+
depth += 1
|
|
303
|
+
elsif depth.zero?
|
|
304
|
+
return index
|
|
305
|
+
else
|
|
306
|
+
depth -= 1
|
|
307
|
+
end
|
|
308
|
+
from = index + 2
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
# `[[File:Name.jpg|thumb|200x100px|alt=...|Caption]]`. An image is
|
|
313
|
+
# an image with the caption as its alternative text. Its address
|
|
314
|
+
# is the page that MediaWiki answers with the file itself,
|
|
315
|
+
# Special:FilePath. Any other file is a link to its page.
|
|
316
|
+
#
|
|
317
|
+
# @param target [String] the name with its namespace
|
|
318
|
+
# @param parameters [String, nil] what follows the first pipe
|
|
319
|
+
def file_embed(target, parameters)
|
|
320
|
+
name = target.sub(FILE, "").lstrip
|
|
321
|
+
alt = caption = width = height = nil
|
|
322
|
+
parameters
|
|
323
|
+
.to_s
|
|
324
|
+
.scan(FILE_PARAMETER) do |parameter|
|
|
325
|
+
parameter = parameter.strip
|
|
326
|
+
if (size = FILE_SIZE.match(parameter))
|
|
327
|
+
width = size[1]&.to_i
|
|
328
|
+
height = size[2]&.to_i
|
|
329
|
+
elsif parameter.match?(FILE_ALT)
|
|
330
|
+
alt = parameter.sub(FILE_ALT, "")
|
|
331
|
+
elsif !parameter.match?(FILE_OPTION)
|
|
332
|
+
caption = parameter
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
text = (alt || caption)&.gsub(WIKI_MARKUP, "\\1")
|
|
336
|
+
|
|
337
|
+
if name.match?(IMAGE_NAME)
|
|
338
|
+
src = page_href("Special:FilePath/#{name}")
|
|
339
|
+
@parent << AST::Image.new(src:, alt: text, width:, height:)
|
|
340
|
+
else
|
|
341
|
+
@parent << (
|
|
342
|
+
AST::Url.new(href: page_href("File:#{name}")) << AST::Text.new(text || name)
|
|
343
|
+
)
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
# The href of a wiki page, in the form MediaWiki's Parsoid writes
|
|
348
|
+
# it: relative to the wiki, with underscores for the spaces of the
|
|
349
|
+
# title, `./Main_Page`. The renderer reads it as a path, so the
|
|
350
|
+
# link stays a link, and a consumer finds every page link by the
|
|
351
|
+
# `./` and can put the address of the wiki in front. A link to a
|
|
352
|
+
# section of the same page, `#History`, and an empty target stay
|
|
353
|
+
# as they are. A `?` in the title is written as `%3F`, like
|
|
354
|
+
# Parsoid does: left as it is, it would start the query of the
|
|
355
|
+
# URL and the link would go to the page without it.
|
|
356
|
+
def page_href(target)
|
|
357
|
+
return target if target.empty? || target.start_with?("#")
|
|
358
|
+
|
|
359
|
+
title, hash, section = target.delete_prefix(":").tr(" ", "_").partition("#")
|
|
360
|
+
"./#{title.gsub("?", "%3F")}#{hash}#{section}"
|
|
361
|
+
end
|
|
362
|
+
|
|
213
363
|
# Parse [url display text] external link.
|
|
214
364
|
def parse_external_link
|
|
215
365
|
@pos += 1 # skip [
|
|
@@ -288,11 +438,11 @@ module Markbridge
|
|
|
288
438
|
# Handle <nowiki>...</nowiki> - preserves content as literal text.
|
|
289
439
|
def handle_nowiki_tag(full_match)
|
|
290
440
|
@pos += full_match.bytesize
|
|
291
|
-
close_pos =
|
|
441
|
+
close_pos, close_size = find_close("nowiki")
|
|
292
442
|
|
|
293
443
|
if close_pos
|
|
294
444
|
@text_buffer << @input.byteslice(@pos, close_pos - @pos)
|
|
295
|
-
@pos = close_pos +
|
|
445
|
+
@pos = close_pos + close_size
|
|
296
446
|
else
|
|
297
447
|
@text_buffer << full_match
|
|
298
448
|
end
|
|
@@ -302,14 +452,13 @@ module Markbridge
|
|
|
302
452
|
# Content inside is not parsed for wiki markup.
|
|
303
453
|
def handle_paired_raw_tag(tag_name, full_match, element_class)
|
|
304
454
|
@pos += full_match.bytesize
|
|
305
|
-
|
|
306
|
-
close_pos = @input.byteindex(close_tag, @pos)
|
|
455
|
+
close_pos, close_size = find_close(tag_name)
|
|
307
456
|
|
|
308
457
|
if close_pos
|
|
309
458
|
element = element_class.new
|
|
310
459
|
element << AST::Text.new(@input.byteslice(@pos, close_pos - @pos))
|
|
311
460
|
@parent << element
|
|
312
|
-
@pos = close_pos +
|
|
461
|
+
@pos = close_pos + close_size
|
|
313
462
|
else
|
|
314
463
|
@text_buffer << full_match
|
|
315
464
|
end
|
|
@@ -319,19 +468,40 @@ module Markbridge
|
|
|
319
468
|
# Content inside IS parsed for wiki markup.
|
|
320
469
|
def handle_paired_tag(tag_name, full_match, element_class)
|
|
321
470
|
@pos += full_match.bytesize
|
|
322
|
-
|
|
323
|
-
close_pos = @input.byteindex(close_tag, @pos)
|
|
471
|
+
close_pos, close_size = find_close(tag_name)
|
|
324
472
|
|
|
325
473
|
if close_pos
|
|
326
474
|
element = element_class.new
|
|
327
475
|
parse_inner_content(@input.byteslice(@pos, close_pos - @pos), parent: element)
|
|
328
476
|
@parent << element
|
|
329
|
-
@pos = close_pos +
|
|
477
|
+
@pos = close_pos + close_size
|
|
330
478
|
else
|
|
331
479
|
@text_buffer << full_match
|
|
332
480
|
end
|
|
333
481
|
end
|
|
334
482
|
|
|
483
|
+
# The end tag of +tag_name+ behind the cursor, in any case:
|
|
484
|
+
# MediaWiki reads `</S>` like `</s>`.
|
|
485
|
+
#
|
|
486
|
+
# A tag that had no end tag behind the cursor has none later in
|
|
487
|
+
# the text either, so the search is done once per tag: a line of
|
|
488
|
+
# `<nowiki>` with no end would be read to its end from each one.
|
|
489
|
+
#
|
|
490
|
+
# @param tag_name [String] letters only, see HTML_TAG_AT_CURSOR
|
|
491
|
+
# @return [Array(Integer, Integer), nil] its position and size
|
|
492
|
+
def find_close(tag_name)
|
|
493
|
+
return if @unclosed.include?(tag_name)
|
|
494
|
+
|
|
495
|
+
found = search_close(tag_name)
|
|
496
|
+
@unclosed << tag_name unless found
|
|
497
|
+
found
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
def search_close(tag_name)
|
|
501
|
+
position = @input.byteindex(%r{</#{tag_name}>}i, @pos)
|
|
502
|
+
[position, Regexp.last_match(0).bytesize] if position
|
|
503
|
+
end
|
|
504
|
+
|
|
335
505
|
# Flush accumulated text buffer to the parent as a Text node.
|
|
336
506
|
# AST::Text copies the (mutable) buffer, so clearing it afterwards
|
|
337
507
|
# reuses the allocated capacity for the next span.
|
|
@@ -21,12 +21,23 @@ module Markbridge
|
|
|
21
21
|
# r.register("mark", :formatting, AST::Bold)
|
|
22
22
|
# end
|
|
23
23
|
class InlineTagRegistry
|
|
24
|
+
include Enumerable
|
|
25
|
+
|
|
24
26
|
Entry = Data.define(:type, :element_class)
|
|
25
27
|
|
|
26
28
|
def initialize
|
|
27
29
|
@entries = {}
|
|
28
30
|
end
|
|
29
31
|
|
|
32
|
+
# Iterate over the registered (tag_name, entry) pairs, in the
|
|
33
|
+
# order of registration.
|
|
34
|
+
# @yieldparam tag_name [String]
|
|
35
|
+
# @yieldparam entry [Entry]
|
|
36
|
+
# @return [Enumerator] when no block is given
|
|
37
|
+
def each(&block)
|
|
38
|
+
@entries.each(&block)
|
|
39
|
+
end
|
|
40
|
+
|
|
30
41
|
# Register a handler for an inline HTML-like tag.
|
|
31
42
|
#
|
|
32
43
|
# @param tag_name [String] the tag name (case-insensitive)
|
|
@@ -72,9 +83,10 @@ module Markbridge
|
|
|
72
83
|
|
|
73
84
|
# Formatting tags -content parsed for inline wiki markup
|
|
74
85
|
registry.register("s", :formatting, AST::Strikethrough)
|
|
75
|
-
registry.register("del", :formatting, AST::
|
|
86
|
+
registry.register("del", :formatting, AST::Deletion)
|
|
76
87
|
registry.register("u", :formatting, AST::Underline)
|
|
77
|
-
registry.register("ins", :formatting, AST::
|
|
88
|
+
registry.register("ins", :formatting, AST::Insertion)
|
|
89
|
+
registry.register("mark", :formatting, AST::Highlight)
|
|
78
90
|
registry.register("sup", :formatting, AST::Superscript)
|
|
79
91
|
registry.register("sub", :formatting, AST::Subscript)
|
|
80
92
|
|
|
@@ -50,12 +50,75 @@ module Markbridge
|
|
|
50
50
|
@inline_parser = InlineParser.new(handlers: @handlers, unknown_tags: @unknown_tags)
|
|
51
51
|
@list_stack = []
|
|
52
52
|
|
|
53
|
-
process_lines(lines)
|
|
53
|
+
process_lines(join_nowiki_lines(lines))
|
|
54
54
|
@document
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
private
|
|
58
58
|
|
|
59
|
+
PRE_TAG_OPEN = /\A\s*<pre\b[^>]*>/i
|
|
60
|
+
PRE_TAG_CLOSE = %r{</pre\s*>}i
|
|
61
|
+
PRE_TAG_CLOSE_TRAILING = %r{</pre\s*>\s*\z}i
|
|
62
|
+
private_constant :PRE_TAG_OPEN, :PRE_TAG_CLOSE, :PRE_TAG_CLOSE_TRAILING
|
|
63
|
+
|
|
64
|
+
NOWIKI_OPEN = /<nowiki>/i
|
|
65
|
+
NOWIKI_CLOSE = %r{</nowiki>}i
|
|
66
|
+
private_constant :NOWIKI_OPEN, :NOWIKI_CLOSE
|
|
67
|
+
|
|
68
|
+
# The parser works line by line, but <nowiki> can go over several
|
|
69
|
+
# lines. Such lines become one line with the line breaks in it,
|
|
70
|
+
# which the inline parser reads as the text of the tag. A
|
|
71
|
+
# <nowiki> that is never closed is left as it is.
|
|
72
|
+
#
|
|
73
|
+
# One pass over the lines: the lines behind an open <nowiki> wait
|
|
74
|
+
# until one of them closes it. A search for the closing line from
|
|
75
|
+
# every open tag would read the rest of the text again each time.
|
|
76
|
+
#
|
|
77
|
+
# The lines of a <pre> block are code and open nothing. Without
|
|
78
|
+
# that a <nowiki> in the code would take the lines up to the next
|
|
79
|
+
# </nowiki> in the text, and the </pre> with them.
|
|
80
|
+
#
|
|
81
|
+
# @param lines [Array<String>]
|
|
82
|
+
# @return [Array<String>]
|
|
83
|
+
def join_nowiki_lines(lines)
|
|
84
|
+
joined = []
|
|
85
|
+
waiting = []
|
|
86
|
+
open = false
|
|
87
|
+
pre = false
|
|
88
|
+
lines.each do |line|
|
|
89
|
+
if !open
|
|
90
|
+
joined << line
|
|
91
|
+
code = pre || pre_block_start?(line)
|
|
92
|
+
pre = code && !line.match?(PRE_TAG_CLOSE)
|
|
93
|
+
open = !code && open_nowiki?(line)
|
|
94
|
+
elsif line.match?(NOWIKI_CLOSE)
|
|
95
|
+
joined << [joined.pop, *waiting, line].join("\n")
|
|
96
|
+
waiting.clear
|
|
97
|
+
# The line that closed a tag is the last one of the joined
|
|
98
|
+
# line, so it alone says whether a tag is open again.
|
|
99
|
+
open = open_nowiki?(line)
|
|
100
|
+
else
|
|
101
|
+
waiting << line
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
joined.concat(waiting)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Whether {#process_lines} starts a <pre> block with this line. A
|
|
108
|
+
# line with a space in front is preformatted text there.
|
|
109
|
+
def pre_block_start?(line)
|
|
110
|
+
pre_tag_line?(line) && !preformatted_line?(line)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Whether the last <nowiki> of the line has no end on the line.
|
|
114
|
+
def open_nowiki?(line)
|
|
115
|
+
open = line.rindex(NOWIKI_OPEN)
|
|
116
|
+
return false unless open
|
|
117
|
+
|
|
118
|
+
close = line.rindex(NOWIKI_CLOSE)
|
|
119
|
+
close.nil? || close < open
|
|
120
|
+
end
|
|
121
|
+
|
|
59
122
|
LINE_ENDING_RE = /\r\n?|[\u2028\u2029]+/
|
|
60
123
|
private_constant :LINE_ENDING_RE
|
|
61
124
|
|
|
@@ -182,6 +245,8 @@ module Markbridge
|
|
|
182
245
|
|
|
183
246
|
if stripped.start_with?("|}")
|
|
184
247
|
break
|
|
248
|
+
elsif stripped.start_with?("|+")
|
|
249
|
+
table_caption(stripped[2..])
|
|
185
250
|
elsif stripped.start_with?("|-")
|
|
186
251
|
# Row separator - next cells will go in a new row
|
|
187
252
|
current_row = nil
|
|
@@ -207,6 +272,20 @@ module Markbridge
|
|
|
207
272
|
# @param table [AST::Table]
|
|
208
273
|
# @param current_row [AST::TableRow, nil]
|
|
209
274
|
# @return [AST::TableRow]
|
|
275
|
+
# The caption of a table, `|+ text`, is a paragraph in front of
|
|
276
|
+
# it: Markdown has no caption for a table. Attributes in front of
|
|
277
|
+
# the text go, like the ones of a cell.
|
|
278
|
+
#
|
|
279
|
+
# @param content [String] the line after `|+`
|
|
280
|
+
def table_caption(content)
|
|
281
|
+
text = split_outside_brackets(content, "|", limit: 2).last.lstrip
|
|
282
|
+
return if text.empty?
|
|
283
|
+
|
|
284
|
+
paragraph = AST::Paragraph.new
|
|
285
|
+
@inline_parser.parse(text, parent: paragraph)
|
|
286
|
+
@document << paragraph
|
|
287
|
+
end
|
|
288
|
+
|
|
210
289
|
def ensure_table_row(table, current_row)
|
|
211
290
|
return current_row if current_row
|
|
212
291
|
|
|
@@ -223,6 +302,10 @@ module Markbridge
|
|
|
223
302
|
# @param content [String] the line content after the leading ! or |
|
|
224
303
|
# @param header [Boolean] whether these are header cells
|
|
225
304
|
# @param row [AST::TableRow]
|
|
305
|
+
COLSPAN = /\bcolspan\s*=\s*["']?(\d+)/i
|
|
306
|
+
ROWSPAN = /\browspan\s*=\s*["']?(\d+)/i
|
|
307
|
+
private_constant :COLSPAN, :ROWSPAN
|
|
308
|
+
|
|
226
309
|
def parse_table_cells(content, header:, row:)
|
|
227
310
|
separator = header ? "!!" : "||"
|
|
228
311
|
cells = split_outside_brackets(content, separator)
|
|
@@ -231,8 +314,14 @@ module Markbridge
|
|
|
231
314
|
# A single | in a cell separates attributes from content
|
|
232
315
|
parts = split_outside_brackets(raw_cell, "|", limit: 2)
|
|
233
316
|
cell_text = parts.last
|
|
234
|
-
|
|
235
|
-
|
|
317
|
+
attributes = parts.first if parts.size == 2
|
|
318
|
+
|
|
319
|
+
cell =
|
|
320
|
+
AST::TableCell.new(
|
|
321
|
+
header:,
|
|
322
|
+
colspan: attributes&.[](COLSPAN, 1),
|
|
323
|
+
rowspan: attributes&.[](ROWSPAN, 1),
|
|
324
|
+
)
|
|
236
325
|
@inline_parser.parse(cell_text.strip, parent: cell)
|
|
237
326
|
row << cell
|
|
238
327
|
end
|
|
@@ -354,11 +443,6 @@ module Markbridge
|
|
|
354
443
|
start_index + consumed.length - 1
|
|
355
444
|
end
|
|
356
445
|
|
|
357
|
-
PRE_TAG_OPEN = /\A\s*<pre\b[^>]*>/i
|
|
358
|
-
PRE_TAG_CLOSE = %r{</pre\s*>}i
|
|
359
|
-
PRE_TAG_CLOSE_TRAILING = %r{</pre\s*>\s*\z}i
|
|
360
|
-
private_constant :PRE_TAG_OPEN, :PRE_TAG_CLOSE, :PRE_TAG_CLOSE_TRAILING
|
|
361
|
-
|
|
362
446
|
# Process a <pre>...</pre> block that may span multiple lines.
|
|
363
447
|
#
|
|
364
448
|
# @param lines [Array<String>]
|
|
@@ -370,7 +454,9 @@ module Markbridge
|
|
|
370
454
|
consumed << lines.fetch(start_index + consumed.length) if terminated
|
|
371
455
|
|
|
372
456
|
combined = consumed.join("\n")
|
|
373
|
-
|
|
457
|
+
# A line break right after <pre> is not content, like in HTML.
|
|
458
|
+
content =
|
|
459
|
+
combined.sub(PRE_TAG_OPEN, "").sub(PRE_TAG_CLOSE_TRAILING, "").delete_prefix("\n")
|
|
374
460
|
|
|
375
461
|
code = AST::Code.new(block: true)
|
|
376
462
|
code << AST::Text.new(content)
|