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
@@ -8,6 +8,21 @@ module Markbridge
8
8
  class TagReconciler
9
9
  MAX_AUTO_CLOSE_DEPTH = 5
10
10
 
11
+ # The elements whose closing tag ends what is open inside.
12
+ BLOCKS = [
13
+ AST::Quote,
14
+ AST::Spoiler,
15
+ AST::Align,
16
+ AST::List,
17
+ AST::Table,
18
+ AST::TableRow,
19
+ AST::TableCell,
20
+ ].freeze
21
+ # The elements that need no closing tag of their own: the next
22
+ # item, cell or row ends them, or the end of what they are in.
23
+ IMPLIED_END = [AST::ListItem, AST::TableRow, AST::TableCell].freeze
24
+ private_constant :BLOCKS, :IMPLIED_END
25
+
11
26
  def initialize(registry:)
12
27
  @registry = registry
13
28
  end
@@ -50,19 +65,92 @@ module Markbridge
50
65
 
51
66
  match_depth = find_matching_handler_depth(handler, context)
52
67
  return false if match_depth.nil? || match_depth.zero?
53
- return false unless all_auto_closeable?(context, match_depth)
68
+ return false unless all_auto_closeable?(context, match_depth - 1)
54
69
 
55
- intervening = context.elements_from_current(match_depth - 1).map(&:class)
70
+ intervening = context.elements_from_current(match_depth - 1)
56
71
 
57
72
  count = match_depth + 1
58
73
  count.times { context.pop }
59
74
  context.auto_close!(count)
60
75
 
61
- intervening.reverse_each { |klass| context.push(klass.new) }
76
+ # A copy without the children, so a color keeps its color and
77
+ # a link its address.
78
+ intervening.reverse_each { |element| context.reopen(element.dup.replace_children([])) }
62
79
 
63
80
  true
64
81
  end
65
82
 
83
+ # Closes the target when only items, cells and rows are open in
84
+ # it, which end without a closing tag of their own:
85
+ # `[list][*]a[/list]`. {#close_inside_block} and the caller do
86
+ # the same with more work; this is for the closing tag of every
87
+ # well-formed list and table.
88
+ #
89
+ # @param handler [BaseHandler] the handler for the closing tag
90
+ # @param context [ParserState]
91
+ # @return [Boolean] true if the target was closed
92
+ def close_implied_ends(handler:, context:)
93
+ open = context.elements_from_current
94
+ depth =
95
+ open.index do |element|
96
+ @registry.handler_for_element(element) == handler || !implied_end?(element)
97
+ end
98
+ # Not the current element: the caller has looked at it.
99
+ return false unless depth&.positive?
100
+ return false unless @registry.handler_for_element(open.fetch(depth)) == handler
101
+
102
+ # Like in #close_inside_block: their closing tags may still come.
103
+ depth.times do |index|
104
+ context.end_early(@registry.handler_for_element(open.fetch(index)))
105
+ end
106
+ (depth + 1).times { context.pop }
107
+ true
108
+ end
109
+
110
+ # Ends everything that is still open inside a block when the
111
+ # closing tag of the block comes: `[quote][list][*]a[/quote]`
112
+ # ends the item and the list with the quote. A block has its own
113
+ # content, so nothing that was opened in it goes on behind it.
114
+ # The closing tag of the block itself is left to the caller.
115
+ #
116
+ # @param handler [BaseHandler] the handler for the closing tag
117
+ # @param context [ParserState]
118
+ def close_inside_block(handler:, context:)
119
+ return unless BLOCKS.include?(handler.element_class)
120
+
121
+ open = context.elements_from_current
122
+ depth = open.index { |element| @registry.handler_for_element(element) == handler }
123
+ return unless depth
124
+
125
+ inside = open.first(depth)
126
+ context.auto_close!(inside.count { |element| !implied_end?(element) })
127
+ # The closing tags of what ends here may still come. They
128
+ # are no text then.
129
+ inside.each { |element| context.end_early(@registry.handler_for_element(element)) }
130
+ depth.times { context.pop }
131
+ end
132
+
133
+ # Ends an item that has no list around it, with what is still
134
+ # open in it, when the closing tag is for an element around the
135
+ # item: in `[quote][*]a[/quote]` the item ends with the quote.
136
+ # Nothing else ends such an item, so without this the closing
137
+ # tag would be text. An item in a list is left to the list.
138
+ #
139
+ # @param handler [BaseHandler] the handler for the closing tag
140
+ # @param context [ParserState]
141
+ def close_item_without_list(handler:, context:)
142
+ open = context.elements_from_current
143
+ item = open.index { |element| element.instance_of?(AST::ListItem) }
144
+ return unless item
145
+
146
+ around = open.drop(item + 1)
147
+ return if around.first.instance_of?(AST::List)
148
+ return if around.none? { |element| @registry.handler_for_element(element) == handler }
149
+
150
+ context.auto_close!(item)
151
+ (item + 1).times { context.pop }
152
+ end
153
+
66
154
  # Attempt to reorder closing tags
67
155
  #
68
156
  # @param handler [BaseHandler] the handler for the closing tag
@@ -91,15 +179,21 @@ module Markbridge
91
179
 
92
180
  private
93
181
 
182
+ def implied_end?(element)
183
+ IMPLIED_END.include?(element.class)
184
+ end
185
+
94
186
  # Number of stack elements to pop in order to close `handler`, or nil
95
187
  # when the handler is not on the stack within MAX_AUTO_CLOSE_DEPTH or
96
- # any intervening element is not auto-closeable.
188
+ # any intervening element is not auto-closeable. The element that
189
+ # is closed need not be: `[url=…][b]text[/url]` ends the bold with
190
+ # the link.
97
191
  def auto_close_count(handler, context)
98
192
  context
99
193
  .elements_from_current(MAX_AUTO_CLOSE_DEPTH - 1)
100
194
  .each_with_index do |element, depth|
101
- return nil unless @registry.auto_closeable?(element.class)
102
195
  return depth + 1 if @registry.handler_for_element(element) == handler
196
+ return nil unless @registry.auto_closeable?(element.class)
103
197
  end
104
198
 
105
199
  nil
@@ -172,7 +172,7 @@ module Markbridge
172
172
 
173
173
  # List handlers
174
174
  registry.register(%w[list ul ol ulist olist], Handlers::ListHandler.new)
175
- registry.register(%w[* li .], Handlers::ListItemHandler.new)
175
+ registry.register(%w[* li], Handlers::ListItemHandler.new)
176
176
 
177
177
  # Table handlers
178
178
  registry.register("table", Handlers::TableHandler.new)
@@ -22,11 +22,16 @@ module Markbridge
22
22
 
23
23
  private
24
24
 
25
+ # A line break right after [code] or [pre] is where the author
26
+ # started the code on a line of its own. It is not a first, empty
27
+ # line of the code.
25
28
  def create_element(token:, content:)
29
+ block = BLOCK_TAGS.include?(token.tag)
30
+ content = content.delete_prefix("\n") if block
26
31
  element =
27
32
  AST::Code.new(
28
33
  language: token.attrs[:lang] || token.attrs[:option],
29
- block: (true if BLOCK_TAGS.include?(token.tag)),
34
+ block: (true if block),
30
35
  )
31
36
  element << AST::Text.new(content) unless content.empty?
32
37
  element
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Markbridge
4
+ module Parsers
5
+ module BBCode
6
+ module Handlers
7
+ # For the handlers of a list item and a table cell, which end the
8
+ # one before them with everything that is still open in it.
9
+ module ContinuedFormatting
10
+ private
11
+
12
+ # Bold, a color or a link that a new item or cell has ended goes
13
+ # on in the new one: `[list][b][*]one[*]two[/b][/list]` has two
14
+ # bold items, and `[td][b]one[td]two` two bold cells, like
15
+ # s9e\TextFormatter reads them. A copy that nothing is put into
16
+ # is taken away again at the end of the parse.
17
+ #
18
+ # @param elements [Array<AST::Element>] what was ended, the
19
+ # innermost first
20
+ # @param context [ParserState]
21
+ # @param registry [HandlerRegistry]
22
+ def continue_formatting(elements, context, registry)
23
+ elements.reverse_each do |element|
24
+ next unless registry.auto_closeable?(element.class)
25
+
26
+ context.reopen(element.dup.replace_children([]))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -17,6 +17,11 @@ module Markbridge
17
17
  context.push(element, token:)
18
18
  end
19
19
 
20
+ # Like a link, see UrlHandler.
21
+ def auto_closeable?
22
+ true
23
+ end
24
+
20
25
  attr_reader :element_class
21
26
  end
22
27
  end
@@ -19,11 +19,6 @@ module Markbridge
19
19
  context.push(element, token:)
20
20
  end
21
21
 
22
- def on_close(token:, context:, registry:, tokens: nil)
23
- context.pop if context.current.instance_of?(AST::ListItem)
24
- super
25
- end
26
-
27
22
  attr_reader :element_class
28
23
  end
29
24
  end
@@ -4,20 +4,54 @@ module Markbridge
4
4
  module Parsers
5
5
  module BBCode
6
6
  module Handlers
7
- # Handler for list item tags (*, li, .)
7
+ # Handler for list item tags (*, li)
8
8
  class ListItemHandler < BaseHandler
9
+ include ContinuedFormatting
10
+
9
11
  def initialize
10
12
  @element_class = AST::ListItem
11
13
  end
12
14
 
15
+ # A new item ends the item that is open, with everything that
16
+ # is still open in it: `[*]a [b]bold[*]b` has two items, and
17
+ # the bold ends with the first. With no item open, what is open
18
+ # directly in the list ends: `[list][b]a[*]b` has two items
19
+ # too. An item of a nested list leaves the item around that
20
+ # list open.
21
+ #
22
+ # A `[*]` in a quote, a spoiler, an aligned block or a table
23
+ # cell that is open in an item does not belong to the list
24
+ # around that block. `[list][*]a [quote]b [*] c[/quote][/list]`
25
+ # has one item, and the `[*]` in the quote is text, like
26
+ # s9e\TextFormatter reads it. Without a list around the block
27
+ # nothing changes: there a `[*]` starts an item.
13
28
  def on_open(token:, context:, registry:, tokens: nil)
14
- context.pop if context.current.instance_of?(AST::ListItem)
29
+ open = context.elements_from_current
30
+ depth = open.index { |element| LIMITS.include?(element.class) }
31
+ if depth
32
+ limit = open.fetch(depth).class
33
+ if ENCLOSING.include?(limit)
34
+ if open.any? { |element| element.instance_of?(AST::List) }
35
+ return context.add_child(AST::Text.new(token.source))
36
+ end
37
+ else
38
+ context.auto_close!(depth)
39
+ (limit == AST::ListItem ? depth + 1 : depth).times { context.pop }
40
+ formatting = open.first(depth)
41
+ end
42
+ end
15
43
 
16
- element = AST::ListItem.new
17
- context.push(element, token:)
44
+ context.push(AST::ListItem.new, token:)
45
+ continue_formatting(formatting, context, registry) if formatting
18
46
  end
19
47
 
20
48
  attr_reader :element_class
49
+
50
+ # A block inside an item that has its own content.
51
+ ENCLOSING = [AST::Quote, AST::Spoiler, AST::Align, AST::TableCell].freeze
52
+ # Where the search for what the new item ends stops.
53
+ LIMITS = [AST::ListItem, AST::List, *ENCLOSING].freeze
54
+ private_constant :ENCLOSING, :LIMITS
21
55
  end
22
56
  end
23
57
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Markbridge
4
+ module Parsers
5
+ module BBCode
6
+ module Handlers
7
+ # For the handlers that end a table cell or a row with a new one.
8
+ module OpenTablePart
9
+ # Blocks with their own content. A `[td]` or a `[tr]` in one of
10
+ # them does not belong to the table around that block, and
11
+ # nothing is ended for it.
12
+ OWN_CONTENT = [AST::Quote, AST::Spoiler, AST::Align, AST::List, AST::ListItem].freeze
13
+ private_constant :OWN_CONTENT
14
+
15
+ private
16
+
17
+ # Ends the cell that is open, with everything that is still
18
+ # open in it: `[td][b]a[td]b` has two cells, and the bold ends
19
+ # with the first. With no cell open, what is open directly in
20
+ # the row ends. A cell of a table in the cell is left alone.
21
+ #
22
+ # @param context [ParserState]
23
+ # @return [Array<AST::Element>] what was open in the cell or in
24
+ # the row, the innermost first
25
+ def close_open_cell(context)
26
+ open = context.elements_from_current
27
+ depth =
28
+ open.index do |element|
29
+ element.instance_of?(AST::TableCell) || row_or_beyond?(element)
30
+ end
31
+ return [] unless depth && !beyond_row?(open.fetch(depth))
32
+
33
+ context.auto_close!(depth)
34
+ (open.fetch(depth).instance_of?(AST::TableCell) ? depth + 1 : depth).times do
35
+ context.pop
36
+ end
37
+ open.first(depth)
38
+ end
39
+
40
+ # Ends the row that is open, with its cell and everything that
41
+ # is still open in it. With no row open, what is open directly
42
+ # in the table ends.
43
+ #
44
+ # @param context [ParserState]
45
+ def close_open_row(context)
46
+ open = context.elements_from_current
47
+ depth = open.index { |element| row_or_beyond?(element) }
48
+ return unless depth && !OWN_CONTENT.include?(open.fetch(depth).class)
49
+
50
+ context.auto_close!(open.first(depth).count { |e| !e.instance_of?(AST::TableCell) })
51
+ (open.fetch(depth).instance_of?(AST::TableRow) ? depth + 1 : depth).times do
52
+ context.pop
53
+ end
54
+ end
55
+
56
+ def row_or_beyond?(element)
57
+ element.instance_of?(AST::TableRow) || beyond_row?(element)
58
+ end
59
+
60
+ def beyond_row?(element)
61
+ element.instance_of?(AST::Table) || OWN_CONTENT.include?(element.class)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -6,16 +6,24 @@ module Markbridge
6
6
  module Handlers
7
7
  # Handler for table cell tags (td, th)
8
8
  class TableCellHandler < BaseHandler
9
+ include OpenTablePart
10
+ include ContinuedFormatting
11
+
9
12
  def initialize
10
13
  @element_class = AST::TableCell
11
14
  end
12
15
 
13
16
  def on_open(token:, context:, registry:, tokens: nil)
14
- # Auto-close previous cell if still open
15
- context.pop if context.current.instance_of?(AST::TableCell)
17
+ formatting = close_open_cell(context)
16
18
 
17
- element = AST::TableCell.new(header: token.tag == "th")
19
+ element =
20
+ AST::TableCell.new(
21
+ header: token.tag == "th",
22
+ colspan: token.attrs[:colspan],
23
+ rowspan: token.attrs[:rowspan],
24
+ )
18
25
  context.push(element, token:)
26
+ continue_formatting(formatting, context, registry)
19
27
  end
20
28
 
21
29
  attr_reader :element_class
@@ -15,15 +15,6 @@ module Markbridge
15
15
  context.push(element, token:)
16
16
  end
17
17
 
18
- def on_close(token:, context:, registry:, tokens: nil)
19
- # Auto-close open cell before closing row
20
- context.pop if context.current.instance_of?(AST::TableCell)
21
- # Auto-close open row before closing table
22
- context.pop if context.current.instance_of?(AST::TableRow)
23
-
24
- super
25
- end
26
-
27
18
  attr_reader :element_class
28
19
  end
29
20
  end
@@ -6,27 +6,19 @@ module Markbridge
6
6
  module Handlers
7
7
  # Handler for table row tags (tr)
8
8
  class TableRowHandler < BaseHandler
9
+ include OpenTablePart
10
+
9
11
  def initialize
10
12
  @element_class = AST::TableRow
11
13
  end
12
14
 
13
15
  def on_open(token:, context:, registry:, tokens: nil)
14
- # Auto-close open cell before starting new row
15
- context.pop if context.current.instance_of?(AST::TableCell)
16
- # Auto-close previous row if still open
17
- context.pop if context.current.instance_of?(AST::TableRow)
16
+ close_open_row(context)
18
17
 
19
18
  element = AST::TableRow.new
20
19
  context.push(element, token:)
21
20
  end
22
21
 
23
- def on_close(token:, context:, registry:, tokens: nil)
24
- # Auto-close open cell before closing row
25
- context.pop if context.current.instance_of?(AST::TableCell)
26
-
27
- super
28
- end
29
-
30
22
  attr_reader :element_class
31
23
  end
32
24
  end
@@ -24,6 +24,12 @@ module Markbridge
24
24
  context.add_child(AST::Url.new(href: content) << AST::Text.new(content))
25
25
  end
26
26
 
27
+ # A link ends with the element it was opened in, and goes on
28
+ # behind it when text follows: `[u][url=…]a[/u] b[/url]`.
29
+ def auto_closeable?
30
+ true
31
+ end
32
+
27
33
  attr_reader :element_class
28
34
 
29
35
  private
@@ -44,6 +44,7 @@ module Markbridge
44
44
 
45
45
  scanner = Scanner.new(normalized)
46
46
  parse_tokens(scanner, context)
47
+ context.drop_empty_reopened
47
48
 
48
49
  @auto_closed_tags_count = context.auto_closed_count
49
50
  @depth_exceeded_count = context.depth_exceeded_count
@@ -17,6 +17,57 @@ module Markbridge
17
17
  @auto_closed_count = 0
18
18
  @depth_exceeded_count = 0
19
19
  @unclosed_raw_tags = Hash.new(0)
20
+ @ended_early = []
21
+ @reopened = []
22
+ end
23
+
24
+ # Push a copy of an element that a mismatched closing tag has
25
+ # ended, so what follows is still inside of it (see
26
+ # TagReconciler#try_reopen).
27
+ # At the depth limit nothing is opened again.
28
+ # @param element [AST::Element] the copy, without children
29
+ def reopen(element)
30
+ return if @depth == MAX_DEPTH
31
+
32
+ holder = @current
33
+ push(element)
34
+ # A list puts what is no item into an item of its own.
35
+ holder = holder.children.last unless holder.children.last.equal?(element)
36
+ @reopened << [holder, element]
37
+ end
38
+
39
+ # Take the reopened elements away that nothing was put into:
40
+ # `[list][s][url=…]a[/s][*]b` opens the link again behind the
41
+ # `[/s]`, and the `[*]` ends it at once. An empty link would be
42
+ # written as its address. The last one first, so an element
43
+ # that held only an empty one goes too.
44
+ def drop_empty_reopened
45
+ @reopened.reverse_each do |holder, element|
46
+ next unless element.children.empty?
47
+
48
+ holder.replace_children(holder.children.reject { |child| child.equal?(element) })
49
+ end
50
+ end
51
+
52
+ # Remember an element that was ended by the closing tag of an
53
+ # element around it, by what identifies its own closing tag.
54
+ # @param key [Object]
55
+ def end_early(key)
56
+ @ended_early << key
57
+ end
58
+
59
+ # Whether an element with this key was ended early and its
60
+ # closing tag has not come yet. The answer is yes only once per
61
+ # element: the closing tag that comes for it is taken as that
62
+ # one.
63
+ # @param key [Object]
64
+ # @return [Boolean]
65
+ def closing_tag_due?(key)
66
+ index = @ended_early.index(key)
67
+ return false unless index
68
+
69
+ @ended_early.delete_at(index)
70
+ true
20
71
  end
21
72
 
22
73
  # Add element as child to current node and push the element onto the stack
@@ -156,7 +156,7 @@ module Markbridge
156
156
  if current_byte == EQUALS
157
157
  @current_pos += 1
158
158
  skip_whitespace
159
- if (val = scan_attribute_value)
159
+ if (val = scan_option_value)
160
160
  attrs[:option] = val
161
161
  end
162
162
  skip_whitespace
@@ -182,6 +182,47 @@ module Markbridge
182
182
  true
183
183
  end
184
184
 
185
+ # The value after `[tag=`. Without quotes it may have spaces, as in
186
+ # `[quote=John Doe]`: it goes on over the words that follow, up to
187
+ # the `]` or to the first `name=` of a named attribute, or to a
188
+ # line break. The cursor may stand behind the blanks after the
189
+ # value, the caller skips whitespace there anyway.
190
+ def scan_option_value
191
+ start = @current_pos
192
+ value = scan_attribute_value
193
+ return value if value.nil? || quote_byte?(@input.getbyte(start))
194
+
195
+ stop = @current_pos
196
+ loop do
197
+ skip_blanks
198
+ break if attribute_ahead? || !scan_unquoted_value
199
+
200
+ stop = @current_pos
201
+ end
202
+
203
+ @input.byteslice(start, stop - start)
204
+ end
205
+
206
+ def quote_byte?(byte)
207
+ byte == DOUBLE_QUOTE || byte == SINGLE_QUOTE
208
+ end
209
+
210
+ # Skips spaces and tabs.
211
+ def skip_blanks
212
+ @current_pos += 1 while (byte = current_byte) == SPACE || byte == TAB
213
+ end
214
+
215
+ # Whether a `name=` stands at the cursor, the start of a named
216
+ # attribute. The cursor stays where it is.
217
+ def attribute_ahead?
218
+ index = @current_pos
219
+ index += 1 while index < @length && attr_name_byte?(@input.getbyte(index))
220
+ return false if index == @current_pos
221
+
222
+ index += 1 while index < @length && whitespace_byte?(@input.getbyte(index))
223
+ @input.getbyte(index) == EQUALS
224
+ end
225
+
185
226
  def scan_attribute_value
186
227
  byte = current_byte
187
228
  if byte == DOUBLE_QUOTE || byte == SINGLE_QUOTE
@@ -20,6 +20,8 @@ require_relative "bbcode/closing_strategies/reordering"
20
20
 
21
21
  # Base Handlers
22
22
  require_relative "bbcode/handlers/base_handler"
23
+ require_relative "bbcode/handlers/open_table_part"
24
+ require_relative "bbcode/handlers/continued_formatting"
23
25
  require_relative "bbcode/handlers/raw_handler"
24
26
 
25
27
  # Handlers
@@ -9,13 +9,19 @@ module Markbridge
9
9
  # `whitespace_preserving_tags` configure parser whitespace behavior by
10
10
  # tag name, independent of whether a handler is registered — so unknown
11
11
  # tags like <div> or <section> still trigger boundary collapsing and
12
- # <pre>/<code> still pass through verbatim. Both sets are mutable, so
13
- # downstream consumers can add or remove tags freely:
12
+ # <pre>/<code> still pass through verbatim. `transparent_tags` names the
13
+ # tags without a handler that are expected: the parser passes their
14
+ # children through like for any unknown tag, but does not count them
15
+ # in `unknown_tags`. The sets are mutable, so downstream consumers can
16
+ # add or remove tags freely:
14
17
  #
15
18
  # registry = HandlerRegistry.default
16
19
  # registry.block_level_tags << "my-block"
17
20
  # registry.whitespace_preserving_tags.delete("tt")
21
+ # registry.transparent_tags.merge(%w[div section font])
18
22
  class HandlerRegistry
23
+ include Enumerable
24
+
19
25
  # HTML5 block-level elements (per MDN). The trim-before-block rule
20
26
  # applies to these regardless of whether a handler is registered.
21
27
  DEFAULT_BLOCK_LEVEL_TAGS = %w[
@@ -70,6 +76,10 @@ module Markbridge
70
76
  # verbatim; outside, `\s+` runs collapse to a single space.
71
77
  DEFAULT_WHITESPACE_PRESERVING_TAGS = %w[pre code textarea tt].freeze
72
78
 
79
+ # Tags that are expected without a handler. The sections of a
80
+ # table only group its rows.
81
+ DEFAULT_TRANSPARENT_TAGS = %w[thead tbody tfoot].freeze
82
+
73
83
  # @return [Set<String>] mutable set of tag names treated as block-level.
74
84
  attr_reader :block_level_tags
75
85
 
@@ -77,12 +87,27 @@ module Markbridge
77
87
  # preserve source whitespace.
78
88
  attr_reader :whitespace_preserving_tags
79
89
 
90
+ # @return [Set<String>] mutable set of tag names that are not
91
+ # counted as unknown when they have no handler. A handler
92
+ # registered for one of them is used as usual.
93
+ attr_reader :transparent_tags
94
+
80
95
  def initialize
81
96
  @handlers = {}
97
+ @transparent_tags = Set.new(DEFAULT_TRANSPARENT_TAGS)
82
98
  @block_level_tags = Set.new(DEFAULT_BLOCK_LEVEL_TAGS)
83
99
  @whitespace_preserving_tags = Set.new(DEFAULT_WHITESPACE_PRESERVING_TAGS)
84
100
  end
85
101
 
102
+ # Iterate over the registered (tag_name, handler) pairs, in the
103
+ # order of registration.
104
+ # @yieldparam tag_name [String]
105
+ # @yieldparam handler [BaseHandler]
106
+ # @return [Enumerator] when no block is given
107
+ def each(&block)
108
+ @handlers.each(&block)
109
+ end
110
+
86
111
  # Register a handler for one or more tag names
87
112
  # @param tag_names [String, Array<String>] tag name(s) to register
88
113
  # @param handler [BaseHandler] the handler instance — must
@@ -124,10 +149,13 @@ module Markbridge
124
149
  new.tap do |registry|
125
150
  registry.register(%w[b strong], Handlers::SimpleHandler.new(AST::Bold))
126
151
  registry.register(%w[i em], Handlers::SimpleHandler.new(AST::Italic))
127
- registry.register(%w[s strike del], Handlers::SimpleHandler.new(AST::Strikethrough))
152
+ registry.register(%w[s strike], Handlers::SimpleHandler.new(AST::Strikethrough))
128
153
  registry.register("u", Handlers::SimpleHandler.new(AST::Underline))
129
154
  registry.register("sup", Handlers::SimpleHandler.new(AST::Superscript))
130
155
  registry.register("sub", Handlers::SimpleHandler.new(AST::Subscript))
156
+ registry.register("ins", Handlers::SimpleHandler.new(AST::Insertion))
157
+ registry.register("del", Handlers::SimpleHandler.new(AST::Deletion))
158
+ registry.register("mark", Handlers::SimpleHandler.new(AST::Highlight))
131
159
  registry.register(%w[code pre tt], Handlers::RawHandler.new(AST::Code))
132
160
  registry.register("a", Handlers::UrlHandler.new)
133
161
  registry.register("img", Handlers::ImageHandler.new)
@@ -174,6 +202,7 @@ module Markbridge
174
202
  @handlers.freeze
175
203
  @block_level_tags.freeze
176
204
  @whitespace_preserving_tags.freeze
205
+ @transparent_tags.freeze
177
206
  super
178
207
  end
179
208
  end