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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/lib/markbridge/ast/deletion.rb +15 -0
  3. data/lib/markbridge/ast/highlight.rb +13 -0
  4. data/lib/markbridge/ast/insertion.rb +14 -0
  5. data/lib/markbridge/ast/spoiler.rb +12 -1
  6. data/lib/markbridge/ast/table.rb +25 -4
  7. data/lib/markbridge/ast.rb +3 -0
  8. data/lib/markbridge/normalizer/carrier.rb +86 -0
  9. data/lib/markbridge/normalizer/report.rb +12 -7
  10. data/lib/markbridge/normalizer/rule_set.rb +22 -0
  11. data/lib/markbridge/normalizer/walker.rb +233 -36
  12. data/lib/markbridge/normalizer.rb +106 -23
  13. data/lib/markbridge/parsers/bbcode/closing_strategies/base.rb +4 -2
  14. data/lib/markbridge/parsers/bbcode/closing_strategies/reordering.rb +5 -0
  15. data/lib/markbridge/parsers/bbcode/closing_strategies/tag_reconciler.rb +99 -5
  16. data/lib/markbridge/parsers/bbcode/handler_registry.rb +1 -1
  17. data/lib/markbridge/parsers/bbcode/handlers/code_handler.rb +6 -1
  18. data/lib/markbridge/parsers/bbcode/handlers/continued_formatting.rb +33 -0
  19. data/lib/markbridge/parsers/bbcode/handlers/email_handler.rb +5 -0
  20. data/lib/markbridge/parsers/bbcode/handlers/list_handler.rb +0 -5
  21. data/lib/markbridge/parsers/bbcode/handlers/list_item_handler.rb +38 -4
  22. data/lib/markbridge/parsers/bbcode/handlers/open_table_part.rb +67 -0
  23. data/lib/markbridge/parsers/bbcode/handlers/table_cell_handler.rb +11 -3
  24. data/lib/markbridge/parsers/bbcode/handlers/table_handler.rb +0 -9
  25. data/lib/markbridge/parsers/bbcode/handlers/table_row_handler.rb +3 -11
  26. data/lib/markbridge/parsers/bbcode/handlers/url_handler.rb +6 -0
  27. data/lib/markbridge/parsers/bbcode/parser.rb +1 -0
  28. data/lib/markbridge/parsers/bbcode/parser_state.rb +51 -0
  29. data/lib/markbridge/parsers/bbcode/scanner.rb +42 -1
  30. data/lib/markbridge/parsers/bbcode.rb +2 -0
  31. data/lib/markbridge/parsers/html/handler_registry.rb +32 -3
  32. data/lib/markbridge/parsers/html/handlers/raw_handler.rb +14 -1
  33. data/lib/markbridge/parsers/html/handlers/table_cell_handler.rb +6 -1
  34. data/lib/markbridge/parsers/html/parser.rb +34 -5
  35. data/lib/markbridge/parsers/media_wiki/inline_parser.rb +183 -13
  36. data/lib/markbridge/parsers/media_wiki/inline_tag_registry.rb +14 -2
  37. data/lib/markbridge/parsers/media_wiki/parser.rb +95 -9
  38. data/lib/markbridge/parsers/text_formatter/handler_registry.rb +50 -3
  39. data/lib/markbridge/parsers/text_formatter/handlers/attribute_handler.rb +9 -2
  40. data/lib/markbridge/parsers/text_formatter/handlers/heading_handler.rb +27 -0
  41. data/lib/markbridge/parsers/text_formatter/handlers/self_closing_handler.rb +28 -0
  42. data/lib/markbridge/parsers/text_formatter/handlers/simple_handler.rb +8 -2
  43. data/lib/markbridge/parsers/text_formatter/handlers/table_cell_handler.rb +6 -1
  44. data/lib/markbridge/parsers/text_formatter/parser.rb +23 -7
  45. data/lib/markbridge/parsers/text_formatter.rb +2 -0
  46. data/lib/markbridge/renderers/discourse/base_url.rb +89 -0
  47. data/lib/markbridge/renderers/discourse/inline/joins.rb +22 -0
  48. data/lib/markbridge/renderers/discourse/inline/serializer.rb +57 -13
  49. data/lib/markbridge/renderers/discourse/markdown_escaper.rb +8 -1
  50. data/lib/markbridge/renderers/discourse/paragraphs.rb +1 -1
  51. data/lib/markbridge/renderers/discourse/postprocessor.rb +12 -0
  52. data/lib/markbridge/renderers/discourse/renderer.rb +6 -1
  53. data/lib/markbridge/renderers/discourse/rendering_interface.rb +5 -3
  54. data/lib/markbridge/renderers/discourse/tags/color_tag.rb +1 -1
  55. data/lib/markbridge/renderers/discourse/tags/deletion_tag.rb +25 -0
  56. data/lib/markbridge/renderers/discourse/tags/highlight_tag.rb +25 -0
  57. data/lib/markbridge/renderers/discourse/tags/image_tag.rb +7 -2
  58. data/lib/markbridge/renderers/discourse/tags/insertion_tag.rb +25 -0
  59. data/lib/markbridge/renderers/discourse/tags/paragraph_tag.rb +13 -3
  60. data/lib/markbridge/renderers/discourse/tags/quote_tag.rb +10 -1
  61. data/lib/markbridge/renderers/discourse/tags/size_tag.rb +1 -1
  62. data/lib/markbridge/renderers/discourse/tags/spoiler_tag.rb +46 -2
  63. data/lib/markbridge/renderers/discourse/tags/table_tag.rb +38 -21
  64. data/lib/markbridge/renderers/discourse/tags/url_tag.rb +6 -1
  65. data/lib/markbridge/renderers/discourse.rb +4 -0
  66. data/lib/markbridge/version.rb +1 -1
  67. data/lib/markbridge.rb +17 -3
  68. metadata +13 -1
@@ -39,12 +39,33 @@ module Markbridge
39
39
  AST::Strikethrough,
40
40
  AST::Superscript,
41
41
  AST::Subscript,
42
+ AST::Insertion,
43
+ AST::Deletion,
44
+ AST::Highlight,
42
45
  AST::Color,
43
46
  AST::Size,
44
47
  AST::Align,
45
48
  AST::Email,
46
49
  ].freeze
47
50
 
51
+ # Stands in the child list where a block was hoisted out, until
52
+ # {#coalesce} decides whether the two neighbors need a space.
53
+ GAP = Object.new.freeze
54
+ EDGE_SPACE_BEFORE = /[[:space:]]\z/
55
+ EDGE_SPACE_AFTER = /\A[[:space:]]/
56
+ # Stands in the child list where a block is split out: the children
57
+ # behind it go into a copy of the element, and +node+ goes on
58
+ # upward until it stands next to +boundary+.
59
+ Split = Struct.new(:node, :boundary)
60
+ SPACE_AT_START = /\A[[:space:]]+/
61
+ SPACE_AT_END = /[[:space:]]+\z/
62
+ private_constant :GAP,
63
+ :EDGE_SPACE_BEFORE,
64
+ :EDGE_SPACE_AFTER,
65
+ :Split,
66
+ :SPACE_AT_START,
67
+ :SPACE_AT_END
68
+
48
69
  # @param rule_set [RuleSet]
49
70
  # @param report [Report]
50
71
  def initialize(rule_set, report)
@@ -55,15 +76,27 @@ module Markbridge
55
76
  # analyzed at most once per run while the RuleSet itself stays
56
77
  # free of per-call state.
57
78
  @walk_cache = {}
79
+ # The copies of inline elements that {Carrier} has put into a
80
+ # block. One that ends up empty is removed, whatever it is.
81
+ @copies = Set.new
58
82
  end
59
83
 
60
84
  # Normalize +document+'s subtree in place.
61
85
  # @param document [AST::Document]
62
86
  def call(document)
63
- _element, bubble = normalize_element(document, [])
87
+ _element, bubble, _before, _after, tail = normalize_element(document, [])
64
88
  # Defensive: anything that never reached its boundary becomes a
65
89
  # trailing sibling rather than being lost. Well-formed rule tables
66
90
  # do not get here.
91
+ tail.each do |part|
92
+ # A rule that splits the document itself has nowhere to put the
93
+ # parts: they stay its children, in their order.
94
+ if part.instance_of?(Split)
95
+ document << part.node
96
+ else
97
+ part.children.each { |c| document << c }
98
+ end
99
+ end
67
100
  bubble.each { |node, _boundary| document << node }
68
101
  end
69
102
 
@@ -79,11 +112,17 @@ module Markbridge
79
112
  end
80
113
 
81
114
  # Normalize a node's descendants. Elements recurse; leaves are
82
- # returned untouched. Returns +[node_or_nil, bubble]+ — +nil+ when the
83
- # element was pruned, and +bubble+ is the list of +[node, boundary]+
84
- # pairs to hoist above this node.
115
+ # returned untouched. Returns +[node_or_nil, bubble, before, after,
116
+ # tail]+ — +nil+ when the element was pruned, +bubble+ is the list of
117
+ # +[node, boundary]+ pairs to hoist above this node. +before+ says
118
+ # that a block left the start of the node, so what stands in front
119
+ # of the node was on another line, and +after+ says the same for its
120
+ # end and what follows it. +tail+ is what follows the node when it
121
+ # was split: the blocks that were split out of it, each as a {Split}
122
+ # until it has reached its boundary, and between them the copies of
123
+ # the node with the children that stood behind each block.
85
124
  def normalize_node(node, stack)
86
- return node, EMPTY unless node.is_a?(AST::Element)
125
+ return node, EMPTY, false, false, EMPTY unless node.is_a?(AST::Element)
87
126
 
88
127
  normalize_element(node, stack)
89
128
  end
@@ -99,15 +138,19 @@ module Markbridge
99
138
  bubble = nil
100
139
 
101
140
  children.each_with_index do |child, index|
102
- strategy, boundary = @rules.resolve(child, stack, @walk_cache)
103
- strategy = strategy.call(boundary, child) if strategy.respond_to?(:call)
141
+ strategy, boundary = @rules.decide(child, stack, @walk_cache)
104
142
 
105
143
  if strategy.nil? || strategy == :keep
106
- normalized, child_bubble = normalize_node(child, stack)
107
- next if unchanged?(out, normalized, child, child_bubble)
144
+ normalized, child_bubble, before, after, tail = normalize_node(child, stack)
145
+ if !before && !after && tail.empty? && unchanged?(out, normalized, child, child_bubble)
146
+ next
147
+ end
108
148
 
109
149
  out ||= children[0, index]
150
+ out << GAP if before
110
151
  bubble = append_kept(normalized, child_bubble, child, out, bubble)
152
+ append_tail(tail, child, out)
153
+ out << GAP if after
111
154
  else
112
155
  out ||= children[0, index]
113
156
  bubble = emit(child, strategy, boundary, stack, out, bubble)
@@ -115,31 +158,89 @@ module Markbridge
115
158
  end
116
159
 
117
160
  stack.pop
118
- element.replace_children(coalesce(out)) if out
119
-
120
161
  raised = bubble || EMPTY
121
- return nil, raised if prune?(element, out, children)
162
+ unless out
163
+ kept = element unless prune?(element, children)
164
+ return kept, raised, false, false, EMPTY
165
+ end
122
166
 
123
- [element, raised]
167
+ # For a block the two say nothing new: the parent puts no
168
+ # space next to a block anyway (see #touching?).
169
+ before = out.first.equal?(GAP)
170
+ after = out.last.equal?(GAP)
171
+ return in_parts(element, out, raised, before, after) if out.any?(Split)
172
+
173
+ out = coalesce(out)
174
+ element.replace_children(out)
175
+ kept = element unless prune?(element, out)
176
+ [kept, raised, before, after, EMPTY]
177
+ end
178
+
179
+ # The element whose children hold a {Split}: it keeps the children
180
+ # in front of the first one, and a copy of it gets the children
181
+ # behind each. The whitespace at a cut is taken off (see #trim). A
182
+ # part that is empty then is left out, the element itself too,
183
+ # whatever it is: what it held is in the block now.
184
+ def in_parts(element, out, raised, before, after)
185
+ parts = [[]]
186
+ out.each { |node| node.instance_of?(Split) ? parts.push(node, []) : parts.last << node }
187
+ first, *rest = parts
188
+
189
+ children = coalesce(first)
190
+ trim(children, -1, SPACE_AT_END)
191
+ element.replace_children(children)
192
+ tail =
193
+ rest.filter_map do |part|
194
+ next part if part.instance_of?(Split)
195
+
196
+ children_of_copy = coalesce(part)
197
+ trim(children_of_copy, 0, SPACE_AT_START)
198
+ trim(children_of_copy, -1, SPACE_AT_END) unless part.equal?(rest.last)
199
+ element.dup.replace_children(children_of_copy) unless children_of_copy.empty?
200
+ end
201
+ kept = element unless children.empty?
202
+ [kept, raised, before, after, tail]
203
+ end
204
+
205
+ # Takes the whitespace off a text that stands next to a block that
206
+ # was split out: it is at the end or at the start of a line now.
207
+ # A text that was nothing else is removed.
208
+ def trim(nodes, index, pattern)
209
+ node = nodes.at(index)
210
+ return unless node.instance_of?(AST::Text)
211
+
212
+ text = node.text.sub(pattern, "")
213
+ text.empty? ? nodes.delete_at(index) : nodes[index] = AST::Text.new(text)
214
+ end
215
+
216
+ # Put what follows a child that was split behind it. A block that
217
+ # has reached its boundary, which is the child, lands here. One
218
+ # that has not goes on as a {Split} and splits this element too.
219
+ def append_tail(tail, child, out)
220
+ tail.each do |part|
221
+ out << (part.instance_of?(Split) && part.boundary.equal?(child) ? part.node : part)
222
+ end
124
223
  end
125
224
 
126
225
  # An element is removed when it ends up with no children and is one of
127
- # the wrappers in {PRUNE_WHEN_EMPTY}.
128
- def prune?(element, out, children)
129
- empty = out ? out.empty? : children.empty?
130
- empty && PRUNE_WHEN_EMPTY.include?(element.class)
226
+ # the wrappers in {PRUNE_WHEN_EMPTY}, or a copy that a split has made.
227
+ def prune?(element, children)
228
+ children.empty? && (PRUNE_WHEN_EMPTY.include?(element.class) || @copies.include?(element))
131
229
  end
132
230
 
133
231
  # Resolve one child (against +stack+) and place it into an existing
134
232
  # +out+ — the shared entry point used by {#emit}'s +:unwrap+ recursion,
135
233
  # where +out+ already exists.
136
234
  def resolve_into(child, stack, out, bubble)
137
- strategy, boundary = @rules.resolve(child, stack, @walk_cache)
138
- strategy = strategy.call(boundary, child) if strategy.respond_to?(:call)
235
+ strategy, boundary = @rules.decide(child, stack, @walk_cache)
139
236
 
140
237
  if strategy.nil? || strategy == :keep
141
- child2, child_bubble = normalize_node(child, stack)
142
- append_kept(child2, child_bubble, child, out, bubble)
238
+ child2, child_bubble, before, after, tail = normalize_node(child, stack)
239
+ out << GAP if before
240
+ bubble = append_kept(child2, child_bubble, child, out, bubble)
241
+ append_tail(tail, child, out)
242
+ out << GAP if after
243
+ bubble
143
244
  else
144
245
  emit(child, strategy, boundary, stack, out, bubble)
145
246
  end
@@ -157,7 +258,10 @@ module Markbridge
157
258
  # returning the (possibly newly allocated) +bubble+.
158
259
  def emit(child, strategy, boundary, stack, out, bubble)
159
260
  case strategy
261
+ when :split
262
+ split(child, boundary, stack, out, bubble)
160
263
  when :hoist_after
264
+ out << GAP if block?(child)
161
265
  hoist(child, boundary, stack, bubble)
162
266
  when :unwrap
163
267
  unwrap(child, boundary, stack, out, bubble)
@@ -178,16 +282,53 @@ module Markbridge
178
282
  end
179
283
  end
180
284
 
285
+ # Split the elements from the boundary down to here around the
286
+ # block. The block takes copies of the inline formatting it was in
287
+ # with it (see {Carrier}), then it is walked against the stack it
288
+ # will have, like a hoisted one. It goes into +out+ as a {Split}:
289
+ # this element and every one up to the boundary is cut there.
290
+ def split(child, boundary, stack, out, bubble)
291
+ @report.record(boundary.class, child.class, :split)
292
+ above = ancestors_above(boundary, stack)
293
+ carried = stack.drop(above.size).select { |ancestor| carried?(ancestor) }
294
+ Carrier.call(child, carried, @copies)
295
+ node, child_bubble, _before, _after, tail = normalize_node(child, above)
296
+ out << Split.new(node, boundary) unless node.nil?
297
+ # The block may have been split itself: a paragraph with a quote
298
+ # in it. Its parts, and what was split out of it, go the same way
299
+ # as the block and stay behind it. Only what has a boundary of
300
+ # its own further up keeps that one.
301
+ tail.each do |part|
302
+ out << (travels_on?(part, child) ? part : Split.new(plain(part), boundary))
303
+ end
304
+ child_bubble.each { |entry| (bubble ||= []) << entry }
305
+ bubble
306
+ end
307
+
308
+ def travels_on?(part, child)
309
+ part.instance_of?(Split) && !part.boundary.equal?(child)
310
+ end
311
+
312
+ def plain(part)
313
+ part.instance_of?(Split) ? part.node : part
314
+ end
315
+
316
+ def carried?(element)
317
+ CARRIED.any? { |klass| element.is_a?(klass) }
318
+ end
319
+
181
320
  def hoist(child, boundary, stack, bubble)
182
321
  @report.record(boundary.class, child.class, :hoist_after)
183
322
  # Walk the relocated subtree against its destination stack (the
184
323
  # ancestors strictly above the boundary) so its interior never sees
185
324
  # the boundary it is leaving.
186
- child2, child_bubble = normalize_node(child, ancestors_above(boundary, stack))
325
+ child2, child_bubble, _before, _after, tail =
326
+ normalize_node(child, ancestors_above(boundary, stack))
187
327
  bubble ||= []
188
328
  bubble << [child2, boundary] unless child2.nil?
189
- # For the built-in tables a hoisted subtree yields no escaping
190
- # bubbles; carry any (from a custom rule) up as a best effort.
329
+ tail.each { |part| bubble << [plain(part), boundary] }
330
+ # What leaves the hoisted subtree itself comes after it, so it
331
+ # lands behind it (see #land).
191
332
  child_bubble.each { |entry| bubble << entry }
192
333
  bubble
193
334
  end
@@ -205,18 +346,31 @@ module Markbridge
205
346
  # Unwrap: run the child's children through the current +out+, resolved
206
347
  # against the current stack. Resolving them again in the same pass is
207
348
  # what fixes deeply nested links in one go.
349
+ # A block that is unwrapped had its content on lines of its own.
350
+ apart = block?(child)
351
+ out << GAP if apart
208
352
  child.children.each { |grandchild| bubble = resolve_into(grandchild, stack, out, bubble) }
353
+ out << GAP if apart
209
354
  bubble
210
355
  end
211
356
 
212
357
  # Land inbound bubbles whose boundary is +child+ (this level is the
213
358
  # boundary's parent), each after the previous to preserve order;
214
- # propagate the rest upward.
359
+ # propagate the rest upward. A block that goes on upward stood at
360
+ # the end of +child+ or behind it, so it leaves a gap here too:
361
+ # what follows +child+ was on a line of its own.
362
+ #
363
+ # A node that lands here can be the boundary of a later one: a table
364
+ # in a heading in bold leaves the heading, and the heading leaves
365
+ # the bold. The table lands behind the heading then.
215
366
  def land(child_bubble, child, out, bubble)
367
+ landed = nil
216
368
  child_bubble.each do |node, boundary|
217
- if boundary.equal?(child)
369
+ if boundary.equal?(child) || landed&.any? { |earlier| earlier.equal?(boundary) }
218
370
  out << node
371
+ (landed ||= []) << node
219
372
  else
373
+ out << GAP if block?(node)
220
374
  bubble ||= []
221
375
  bubble << [node, boundary]
222
376
  end
@@ -231,21 +385,64 @@ module Markbridge
231
385
  stack.first(index)
232
386
  end
233
387
 
234
- # Coalesce adjacent text (textify can create neighbours that +#<<+
235
- # would have merged) just before committing a changed child list.
388
+ # Finish a changed child list just before it is committed: adjacent
389
+ # text is merged (textify can create neighbors that +#<<+ would have
390
+ # merged), and the place a block was hoisted from gets a space when
391
+ # its two neighbors would touch. With the block between them they
392
+ # were on two lines, and an HTML parser has trimmed the whitespace
393
+ # at such a line edge.
394
+ #
395
+ # A list, a table and a row get no space: their items, rows and
396
+ # cells are blocks, and nothing touches a block (see #touching?).
236
397
  def coalesce(nodes)
237
- nodes.each_with_object([]) do |node, acc|
238
- last = acc.last
239
- # mergeable? is false when +last+ is nil (the first node), so no
240
- # separate nil-guard is needed.
241
- if mergeable?(last, node)
242
- last.merge(node)
243
- else
244
- acc << node
398
+ nodes
399
+ .each_with_index
400
+ .with_object([]) do |(node, index), acc|
401
+ next if node.equal?(GAP)
402
+
403
+ # For the first node this looks at the last one, which does no
404
+ # harm: nothing is in front of the first node to touch.
405
+ gap = nodes.fetch(index - 1).equal?(GAP)
406
+ append(acc, AST::Text.new(" ")) if gap && touching?(acc.last, node)
407
+ append(acc, node)
245
408
  end
409
+ end
410
+
411
+ def append(acc, node)
412
+ last = acc.last
413
+ # mergeable? is false when +last+ is nil (the first node), so no
414
+ # separate nil-guard is needed.
415
+ if mergeable?(last, node)
416
+ last.merge(node)
417
+ else
418
+ acc << node
246
419
  end
247
420
  end
248
421
 
422
+ # A block starts a line and ends one. Code or a spoiler that is
423
+ # hoisted is a block, a code span or an inline spoiler stays where
424
+ # it is.
425
+ def block?(node)
426
+ node.is_a?(AST::Code) || node.is_a?(AST::Spoiler) ||
427
+ BLOCK_NODES.any? { |klass| node.is_a?(klass) }
428
+ end
429
+
430
+ def own_lines?(node)
431
+ Normalizer.own_lines?(node)
432
+ end
433
+
434
+ # Whether +left+ and +right+ stand next to each other without
435
+ # whitespace or a line break between them. Only text is looked into,
436
+ # an inline element counts as something visible. A block is on lines
437
+ # of its own, nothing touches it.
438
+ def touching?(left, right)
439
+ return false if left.nil? || left.instance_of?(AST::LineBreak) || own_lines?(left)
440
+ return false if right.instance_of?(AST::LineBreak) || own_lines?(right)
441
+ return false if left.instance_of?(AST::Text) && left.text.match?(EDGE_SPACE_BEFORE)
442
+
443
+ !(right.instance_of?(AST::Text) && right.text.match?(EDGE_SPACE_AFTER))
444
+ end
445
+
249
446
  def mergeable?(left, right)
250
447
  (left.instance_of?(AST::Text) && right.instance_of?(AST::Text)) ||
251
448
  (left.instance_of?(AST::MarkdownText) && right.instance_of?(AST::MarkdownText))
@@ -3,6 +3,7 @@
3
3
  require_relative "normalizer/rule_set"
4
4
  require_relative "normalizer/report"
5
5
  require_relative "normalizer/text_projection"
6
+ require_relative "normalizer/carrier"
6
7
  require_relative "normalizer/walker"
7
8
 
8
9
  module Markbridge
@@ -28,24 +29,35 @@ module Markbridge
28
29
  # Markbridge::Normalizer.default.violations(ast) # => [...]
29
30
  class Normalizer
30
31
  # The strategy symbols a rule may resolve to.
31
- STRATEGIES = %i[keep hoist_after unwrap textify drop].freeze
32
+ STRATEGIES = %i[keep split hoist_after unwrap textify drop].freeze
32
33
 
33
34
  EMPTY_STACK = [].freeze
34
35
  private_constant :EMPTY_STACK
35
36
 
36
37
  # Containers that hold inline content only: a link's text (CommonMark
37
- # §6.3), and emphasis and heading content.
38
+ # §6.3), and emphasis and heading content. A color and a size are
39
+ # written as a BBCode tag or as nothing around a line of text, like
40
+ # emphasis, and a mail address is a link.
38
41
  INLINE_CONTAINERS = [
39
42
  AST::Url,
43
+ AST::Email,
40
44
  AST::Bold,
41
45
  AST::Italic,
42
46
  AST::Strikethrough,
43
47
  AST::Underline,
44
48
  AST::Superscript,
45
49
  AST::Subscript,
50
+ AST::Insertion,
51
+ AST::Deletion,
52
+ AST::Highlight,
53
+ AST::Color,
54
+ AST::Size,
46
55
  AST::Heading,
47
56
  ].freeze
48
57
 
58
+ # The nodes that are written as a link.
59
+ LINKS = [AST::Url, AST::Email].freeze
60
+
49
61
  # AST nodes the Discourse renderer prints as block-level Markdown (their
50
62
  # output has blank lines around it). One inside an inline container breaks
51
63
  # that container, so it is moved out. Spoiler and single-line Code without
@@ -67,20 +79,83 @@ module Markbridge
67
79
  AST::Event,
68
80
  ].freeze
69
81
 
82
+ # The inline elements whose formatting goes with a block that is
83
+ # split out of them (see {Carrier}): the block was shown bold, in a
84
+ # color or as a link, and its text still is. A heading and a
85
+ # paragraph are split too, but there is nothing to take along.
86
+ CARRIED = [
87
+ AST::Url,
88
+ AST::Email,
89
+ AST::Bold,
90
+ AST::Italic,
91
+ AST::Strikethrough,
92
+ AST::Underline,
93
+ AST::Superscript,
94
+ AST::Subscript,
95
+ AST::Insertion,
96
+ AST::Deletion,
97
+ AST::Highlight,
98
+ AST::Color,
99
+ AST::Size,
100
+ # An inline one: a block spoiler is a block and never around one
101
+ # that is split out.
102
+ AST::Spoiler,
103
+ ].freeze
104
+
105
+ # How a block leaves the container +boundary+. A heading stays one
106
+ # heading and the block goes behind it. A block spoiler may hold
107
+ # blocks, so it passes the question on to what is inside of it.
108
+ # Everything else is split around the block, which keeps the order
109
+ # of the text.
110
+ MOVE =
111
+ lambda do |boundary|
112
+ if boundary.is_a?(AST::Heading)
113
+ :hoist_after
114
+ elsif boundary.is_a?(AST::Spoiler) && boundary.block
115
+ :pass
116
+ else
117
+ :split
118
+ end
119
+ end
120
+
70
121
  # A code span may stay inside an inline container while it is on one line.
71
122
  # A fenced or multi-line block is moved out. This matches
72
123
  # +RenderingInterface#block_context?+: Code prints as a fenced block when
73
124
  # its +block+ flag is set or a Text child has a newline (the language alone
74
125
  # does not make it a block).
75
- KEEP_INLINE_CODE =
76
- lambda do |_boundary, node|
77
- block =
78
- node.block ||
79
- node.children.any? { |c| c.instance_of?(AST::Text) && c.text.include?("\n") }
80
- block ? :hoist_after : :keep
81
- end
126
+ KEEP_INLINE_CODE = ->(boundary, node) { block_code?(node) ? MOVE.call(boundary) : :keep }
127
+
128
+ # A spoiler inside a line of text may stay in an inline container. One
129
+ # with its +block+ flag set is written on lines of its own and is
130
+ # moved out.
131
+ KEEP_INLINE_SPOILER = ->(boundary, node) { node.block ? MOVE.call(boundary) : :keep }
132
+
133
+ # A block in a block or a block in a container, by the class of the
134
+ # container.
135
+ MOVE_BLOCK = ->(boundary, _node) { MOVE.call(boundary) }
82
136
 
83
137
  class << self
138
+ # @param node [AST::Code]
139
+ # @return [Boolean] whether the code is written as a fenced block
140
+ def block_code?(node)
141
+ node.block || node.children.any? { |c| c.instance_of?(AST::Text) && c.text.include?("\n") }
142
+ end
143
+
144
+ # @param node [AST::Node]
145
+ # @return [Boolean] whether the node is written on lines of its own.
146
+ # Not every code and not every spoiler is: a code span and an
147
+ # inline spoiler are a part of their line.
148
+ def own_lines?(node)
149
+ case node
150
+ when AST::Code
151
+ block_code?(node)
152
+ when AST::Spoiler
153
+ node.block ? true : false
154
+ else
155
+ BLOCK_NODES.any? { |klass| node.is_a?(klass) }
156
+ end
157
+ end
158
+
84
159
  # A fresh, customizable normalizer with the default rules. Add more with
85
160
  # {#rule}.
86
161
  # @return [Normalizer]
@@ -102,20 +177,29 @@ module Markbridge
102
177
  rules = RuleSet.new
103
178
 
104
179
  # §6.3 A link may not contain another link, at any depth. Unwrap the
105
- # inner link and keep its text.
106
- rules.add(parent: AST::Url, child: AST::Url, strategy: :unwrap)
180
+ # inner link and keep its text. A mail address is a link too.
181
+ LINKS.each do |outer|
182
+ LINKS.each { |inner| rules.add(parent: outer, child: inner, strategy: :unwrap) }
183
+ end
107
184
 
108
- INLINE_CONTAINERS.each do |container|
185
+ # A paragraph is a line of text too. It is no inline container,
186
+ # it is a block itself, but a block in it is split out the same
187
+ # way. So is a block in an inline spoiler (see MOVE for a block
188
+ # spoiler).
189
+ [*INLINE_CONTAINERS, AST::Paragraph, AST::Spoiler].each do |container|
109
190
  rules.add(parent: container, child: AST::Code, strategy: KEEP_INLINE_CODE)
191
+ rules.add(parent: container, child: AST::Spoiler, strategy: KEEP_INLINE_SPOILER)
110
192
 
111
193
  BLOCK_NODES.each do |block|
112
- next if container == block
113
-
114
- rules.add(parent: container, child: block, strategy: :hoist_after)
194
+ rules.add(parent: container, child: block, strategy: MOVE_BLOCK)
115
195
  end
116
196
  end
117
197
 
118
- rules
198
+ # Heading is a container and a block. A heading in a heading does
199
+ # not move out, it is one heading, like a link in a link is one
200
+ # link: the text stays where it is. This replaces the rule the
201
+ # loop above has added for the pair. (+add+ returns the rule set.)
202
+ rules.add(parent: AST::Heading, child: AST::Heading, strategy: :unwrap)
119
203
  end
120
204
  end
121
205
 
@@ -171,17 +255,16 @@ module Markbridge
171
255
  def collect_violations(element, ancestors, found, walk_cache)
172
256
  stack = ancestors + [element]
173
257
  element.children.each do |child|
174
- strategy, boundary = @rules.resolve(child, stack, walk_cache)
175
- strategy = strategy.call(boundary, child) if strategy.respond_to?(:call)
258
+ strategy, boundary = @rules.decide(child, stack, walk_cache)
176
259
  unless strategy.nil? || strategy == :keep
177
- found << { parent: demodulize(boundary.class), child: demodulize(child.class), strategy: }
260
+ found << {
261
+ parent: Report.short_name(boundary.class),
262
+ child: Report.short_name(child.class),
263
+ strategy:,
264
+ }
178
265
  end
179
266
  collect_violations(child, stack, found, walk_cache) if child.is_a?(AST::Element)
180
267
  end
181
268
  end
182
-
183
- def demodulize(klass)
184
- klass.name.split("::").last
185
- end
186
269
  end
187
270
  end
@@ -9,14 +9,16 @@ module Markbridge
9
9
  end
10
10
 
11
11
  def handle_close(token:, context:, registry:, tokens: nil)
12
- current_handler = registry.handler_for_element(context.current)
13
12
  closing_handler = registry[token.tag]
13
+ @reconciler.close_inside_block(handler: closing_handler, context:)
14
+ @reconciler.close_item_without_list(handler: closing_handler, context:)
15
+ current_handler = registry.handler_for_element(context.current)
14
16
 
15
17
  if current_handler == closing_handler
16
18
  context.pop
17
19
  elsif @reconciler.try_auto_close(handler: closing_handler, context:)
18
20
  # Auto-close succeeded
19
- else
21
+ elsif !context.closing_tag_due?(closing_handler)
20
22
  context.add_child(AST::Text.new(token.source))
21
23
  end
22
24
  end
@@ -18,6 +18,11 @@ module Markbridge
18
18
  return
19
19
  end
20
20
 
21
+ # The second common case: the last item of a list, or the last
22
+ # cell and row of a table, is still open when the closing tag
23
+ # of the list or the table comes.
24
+ return if @reconciler.close_implied_ends(handler: closing_handler, context:)
25
+
21
26
  return if tokens && @reconciler.try_reorder(handler: closing_handler, tokens:, context:)
22
27
  return if @reconciler.try_reopen(handler: closing_handler, context:, tokens:)
23
28