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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9a481ff76e961a6f9c93e9cea998a6cdbe02ee46000c3acfba4e00023c12fd4
4
- data.tar.gz: ffcc31afdae7383a83a2a51a26bfacfcd1c482dd5e779144cb6263b8547ed63c
3
+ metadata.gz: c9ad987225e9dab32e407329674c9a6c772ab9bc8043aba55f9b06b46dd582c4
4
+ data.tar.gz: 83d7f495b8bef0a5f4d66918c4ef4f14bebf26f5f6c2ae36f971ad4c3c3ac8ed
5
5
  SHA512:
6
- metadata.gz: 46ba479a92c780d448385002567fea76f51478d18af74b6e7675bf916d01e5bbf61d56fd4f6b9f8ce1fd702662a9d41f15f1d43d86ec605e62604f2171b17795
7
- data.tar.gz: 1eb764723017c5ca0afefb538945b37343dddf9aefb891947a7c3357451078bea5b045588c66275e517a95343ebef0a9bb9bfaef17639ded233333a819af0c9f
6
+ metadata.gz: 4f79c05404d549214260eaa7beddc3c12d2b4e7338ac5c82b8ddf9f37380c87be74ddef268d347cda4e5d85703547e7be4f334a6906fa03728b0bc8b67f6e73f
7
+ data.tar.gz: 6dbb715200059a3940992584ceca9a9e1b2d556ab5b066c9a317fe4400771473adc87a37ceb9337a61c264cf881c0017f093116a7dd820484ea815b2c3ea02f6
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Markbridge
4
+ module AST
5
+ # Represents text that was removed from a document, the +<del>+ of
6
+ # HTML. A browser shows it struck through, but it is not
7
+ # strikethrough formatting.
8
+ #
9
+ # @example Deleted text
10
+ # deletion = AST::Deletion.new
11
+ # deletion << AST::Text.new("old wording")
12
+ class Deletion < Element
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Markbridge
4
+ module AST
5
+ # Represents highlighted text, the +<mark>+ of HTML.
6
+ #
7
+ # @example Highlighted text
8
+ # highlight = AST::Highlight.new
9
+ # highlight << AST::Text.new("important")
10
+ class Highlight < Element
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Markbridge
4
+ module AST
5
+ # Represents text that was added to a document, the +<ins>+ of HTML.
6
+ # A browser shows it underlined, but it is not underline formatting.
7
+ #
8
+ # @example Inserted text
9
+ # insertion = AST::Insertion.new
10
+ # insertion << AST::Text.new("new wording")
11
+ class Insertion < Element
12
+ end
13
+ end
14
+ end
@@ -11,16 +11,27 @@ module Markbridge
11
11
  # @example Spoiler with title
12
12
  # spoiler = AST::Spoiler.new(title: "Click to reveal")
13
13
  # spoiler << AST::Text.new("Hidden content")
14
+ #
15
+ # @example Block spoiler, on lines of its own
16
+ # spoiler = AST::Spoiler.new(block: true)
17
+ # spoiler << AST::Text.new("Hidden content")
14
18
  class Spoiler < Element
15
19
  # @return [String, nil] the spoiler title/label
16
20
  attr_reader :title
17
21
 
22
+ # @return [Boolean, nil] +true+ for a spoiler that is a block; anything
23
+ # else is a spoiler inside a line of text
24
+ attr_reader :block
25
+
18
26
  # Create a new Spoiler element.
19
27
  #
20
28
  # @param title [String, nil] optional title for the spoiler
21
- def initialize(title: nil)
29
+ # @param block [Boolean, nil] +true+ when the source shows the spoiler
30
+ # as a block of its own, like the [spoiler] of phpBB
31
+ def initialize(title: nil, block: nil)
22
32
  super()
23
33
  @title = title
34
+ @block = block
24
35
  end
25
36
  end
26
37
  end
@@ -41,21 +41,42 @@ module Markbridge
41
41
  # @example Header cell
42
42
  # cell = AST::TableCell.new(header: true)
43
43
  # cell << AST::Text.new("header")
44
+ #
45
+ # @example Cell over two columns
46
+ # cell = AST::TableCell.new(colspan: 2)
44
47
  class TableCell < Element
48
+ # @return [Integer, nil] the number of columns the cell covers when
49
+ # that is more than one
50
+ attr_reader :colspan
51
+
52
+ # @return [Integer, nil] the number of rows the cell covers when
53
+ # that is more than one
54
+ attr_reader :rowspan
55
+
45
56
  # Create a new table cell.
46
57
  #
47
58
  # @param header [Boolean] whether this is a header cell (th)
48
- def initialize(header: false)
59
+ # @param colspan [Integer, String, nil] the columns the cell covers.
60
+ # A number as text is fine, the parsers pass the attribute as it
61
+ # is. Anything that is no whole number above one is no span.
62
+ # @param rowspan [Integer, String, nil] the rows the cell covers
63
+ def initialize(header: false, colspan: nil, rowspan: nil)
49
64
  super()
50
65
  @header = header
66
+ @colspan = span(colspan)
67
+ @rowspan = span(rowspan)
51
68
  end
52
69
 
53
- # Check if this is a header cell.
54
- #
55
- # @return [Boolean] true if this is a header cell
56
70
  def header?
57
71
  @header
58
72
  end
73
+
74
+ private
75
+
76
+ def span(value)
77
+ number = Integer(value.to_s, 10, exception: false)
78
+ number if number && number > 1
79
+ end
59
80
  end
60
81
  end
61
82
  end
@@ -21,6 +21,9 @@ require_relative "ast/list"
21
21
  require_relative "ast/list_item"
22
22
  require_relative "ast/table"
23
23
  require_relative "ast/paragraph"
24
+ require_relative "ast/deletion"
25
+ require_relative "ast/highlight"
26
+ require_relative "ast/insertion"
24
27
  require_relative "ast/quote"
25
28
  require_relative "ast/size"
26
29
  require_relative "ast/spoiler"
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Markbridge
4
+ class Normalizer
5
+ # Puts copies of inline elements around the inline content of a
6
+ # block, for the +:split+ strategy. A quote that leaves bold keeps
7
+ # the bold this way: every line of text in the quote gets a bold of
8
+ # its own, in a paragraph, a list item or a table cell, at any
9
+ # depth. Code is left alone, it shows no formatting.
10
+ #
11
+ # Carrier.call(quote, [bold])
12
+ # # Quote[Paragraph["a"], List[ListItem["b"]]]
13
+ # # => Quote[Paragraph[Bold["a"]], List[ListItem[Bold["b"]]]]
14
+ module Carrier
15
+ # Blocks whose children are one line of text.
16
+ LINES = [AST::Paragraph, AST::Heading].freeze
17
+ # Blocks whose children are blocks, or text between blocks.
18
+ CONTAINERS = [
19
+ AST::Quote,
20
+ AST::List,
21
+ AST::ListItem,
22
+ AST::Table,
23
+ AST::TableRow,
24
+ AST::TableCell,
25
+ AST::Details,
26
+ AST::Align,
27
+ AST::Spoiler,
28
+ ].freeze
29
+ NOT_BLANK = /[^[:space:]]/
30
+ private_constant :LINES, :CONTAINERS, :NOT_BLANK
31
+
32
+ class << self
33
+ # @param block [AST::Node] the block that is moved, changed in place
34
+ # @param wrappers [Array<AST::Element>] the inline elements it
35
+ # was in, the outermost first. Copies of them are made, without
36
+ # their children.
37
+ # @param copies [#<<] gets every copy that is made
38
+ # @return [void] without wrappers the block stays as it is
39
+ def call(block, wrappers, copies)
40
+ if LINES.any? { |klass| block.is_a?(klass) }
41
+ block.replace_children(wrapped(block.children, wrappers, copies))
42
+ elsif CONTAINERS.any? { |klass| block.is_a?(klass) }
43
+ block.replace_children(with_wrapped_runs(block.children, wrappers, copies))
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ # The children with every run of inline nodes between two blocks
50
+ # wrapped, and the blocks carried into in turn.
51
+ def with_wrapped_runs(children, wrappers, copies)
52
+ out = []
53
+ run = []
54
+ children.each do |child|
55
+ if Normalizer.own_lines?(child)
56
+ out.concat(wrapped(run, wrappers, copies))
57
+ run = []
58
+ call(child, wrappers, copies)
59
+ out << child
60
+ else
61
+ run << child
62
+ end
63
+ end
64
+ out.concat(wrapped(run, wrappers, copies))
65
+ end
66
+
67
+ # @return [Array<AST::Node>] the run in the wrappers, or the run
68
+ # itself when it shows nothing
69
+ def wrapped(run, wrappers, copies)
70
+ return run unless run.any? { |node| visible?(node) }
71
+
72
+ wrappers.reverse_each do |wrapper|
73
+ copy = wrapper.dup.replace_children(run)
74
+ copies << copy
75
+ run = [copy]
76
+ end
77
+ run
78
+ end
79
+
80
+ def visible?(node)
81
+ !node.instance_of?(AST::Text) || node.text.match?(NOT_BLANK)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -6,6 +6,17 @@ module Markbridge
6
6
  # applied. Held as a local per call (never on the Normalizer), so a
7
7
  # frozen shared instance stays reusable and thread-safe.
8
8
  class Report
9
+ # The name of an AST class without its modules, as the report and
10
+ # {Normalizer#violations} show it. A class without a name, from
11
+ # Class.new, is shown as the nearest ancestor that has one.
12
+ #
13
+ # @param klass [Class]
14
+ # @return [String]
15
+ def self.short_name(klass)
16
+ klass = klass.superclass until klass.name
17
+ klass.name.split("::").last
18
+ end
19
+
9
20
  def initialize
10
21
  @counts = Hash.new(0)
11
22
  end
@@ -14,7 +25,7 @@ module Markbridge
14
25
  # @param child_class [Class] the moved/removed node's class
15
26
  # @param strategy [Symbol] the strategy actually applied
16
27
  def record(parent_class, child_class, strategy)
17
- @counts[[demodulize(parent_class), demodulize(child_class), strategy]] += 1
28
+ @counts[[Report.short_name(parent_class), Report.short_name(child_class), strategy]] += 1
18
29
  end
19
30
 
20
31
  # @return [Boolean]
@@ -30,12 +41,6 @@ module Markbridge
30
41
  def to_a
31
42
  @counts.map { |(parent, child, strategy), count| { parent:, child:, strategy:, count: } }
32
43
  end
33
-
34
- private
35
-
36
- def demodulize(klass)
37
- klass.name.split("::").last
38
- end
39
44
  end
40
45
  end
41
46
  end
@@ -81,6 +81,28 @@ module Markbridge
81
81
  scan_ancestors(candidates, ancestors)
82
82
  end
83
83
 
84
+ # {#resolve} with a callable strategy asked for its answer. A
85
+ # callable that answers +:pass+ has no say about this child: the
86
+ # search goes on with the ancestors inside of its boundary. A rule
87
+ # for an inline spoiler does that for a block spoiler, which may
88
+ # hold blocks.
89
+ #
90
+ # @param child [AST::Node]
91
+ # @param ancestors [Array<AST::Element>] root-first ancestor stack
92
+ # @param walk_cache [Hash{Class => Array<Class>}] see {#resolve}
93
+ # @return [Array(Object, AST::Element), Array(nil, nil)] a strategy
94
+ # that is no callable, and its boundary
95
+ def decide(child, ancestors, walk_cache)
96
+ strategy, boundary = resolve(child, ancestors, walk_cache)
97
+ return strategy, boundary unless strategy.respond_to?(:call)
98
+
99
+ strategy = strategy.call(boundary, child)
100
+ return strategy, boundary unless strategy == :pass
101
+
102
+ inside = ancestors.drop(ancestors.index { |ancestor| ancestor.equal?(boundary) } + 1)
103
+ decide(child, inside, walk_cache)
104
+ end
105
+
84
106
  # Freeze so a shared instance raises if something tries to change it.
85
107
  # Freezing +@by_parent+ and its inner hashes is enough: {#add} writes
86
108
  # there before it touches +@child_classes+, so a frozen instance raises