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
|
@@ -144,6 +144,10 @@ module Markbridge
|
|
|
144
144
|
#
|
|
145
145
|
# @param node [AST::Element]
|
|
146
146
|
# @param context [RenderContext] rendering context
|
|
147
|
+
# @param following [String, nil] what the tag writes right after
|
|
148
|
+
# the result, like a closing BBCode tag. A bare URL at the end
|
|
149
|
+
# of the children is written as a link then: left bare, the
|
|
150
|
+
# Markdown parser would take the tag into the URL.
|
|
147
151
|
# @yieldparam result [String] the buffer built so far
|
|
148
152
|
# @yieldparam child [AST::Node] the child about to be appended
|
|
149
153
|
# @return [String]
|
|
@@ -151,7 +155,7 @@ module Markbridge
|
|
|
151
155
|
# interface.render_children(item, context:) do |buffer, child|
|
|
152
156
|
# buffer.rstrip! if child.is_a?(AST::List)
|
|
153
157
|
# end
|
|
154
|
-
def render_children(node, context:, &block)
|
|
158
|
+
def render_children(node, context:, following: nil, &block)
|
|
155
159
|
root_call = enter
|
|
156
160
|
outer_parent = @child_parent
|
|
157
161
|
outer_index = @child_index
|
|
@@ -164,6 +168,7 @@ module Markbridge
|
|
|
164
168
|
node.children,
|
|
165
169
|
html_mode: context.html_mode?,
|
|
166
170
|
before: block,
|
|
171
|
+
following:,
|
|
167
172
|
) do |child|
|
|
168
173
|
@child_index = (index += 1)
|
|
169
174
|
render_child(child, context)
|
|
@@ -27,9 +27,11 @@ module Markbridge
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
# An optional block runs at every join point, before a non-empty
|
|
30
|
-
# child output is appended
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
# child output is appended. +following+ is what the tag writes
|
|
31
|
+
# right after the result, when that is a closing BBCode tag — see
|
|
32
|
+
# Renderer#render_children.
|
|
33
|
+
def render_children(element, context: @context, following: nil, &block)
|
|
34
|
+
@renderer.render_children(element, context:, following:, &block)
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
# The output of the children as inline tokens, for a tag that
|
|
@@ -48,7 +48,7 @@ module Markbridge
|
|
|
48
48
|
color = bbcode_color(element.color, interface)
|
|
49
49
|
return interface.render_tokens(element, context:) if color.nil?
|
|
50
50
|
|
|
51
|
-
content = interface.render_children(element, context:)
|
|
51
|
+
content = interface.render_children(element, context:, following: "[/color]")
|
|
52
52
|
# The style that the discourse-bbcode plugin lets through.
|
|
53
53
|
return %(<span style="color:#{color}">#{content}</span>) if interface.html_mode?
|
|
54
54
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markbridge
|
|
4
|
+
module Renderers
|
|
5
|
+
module Discourse
|
|
6
|
+
module Tags
|
|
7
|
+
# Tag for rendering deleted text as the HTML tag, which Markdown has
|
|
8
|
+
# no form for and Discourse keeps. The tags are inline tokens so
|
|
9
|
+
# that the serializer sees them as neighbors: a newline right after
|
|
10
|
+
# the opening tag would leave it alone on its line, where it opens
|
|
11
|
+
# an HTML block.
|
|
12
|
+
class DeletionTag < Tag
|
|
13
|
+
OPEN = Inline::Tokens.html("<del>")
|
|
14
|
+
CLOSE = Inline::Tokens.html("</del>")
|
|
15
|
+
private_constant :OPEN, :CLOSE
|
|
16
|
+
|
|
17
|
+
def render(element, interface)
|
|
18
|
+
tokens = interface.render_tokens(element, context: interface.with_parent(element))
|
|
19
|
+
tokens.unshift(OPEN) << CLOSE
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markbridge
|
|
4
|
+
module Renderers
|
|
5
|
+
module Discourse
|
|
6
|
+
module Tags
|
|
7
|
+
# Tag for rendering highlighted text as the HTML tag, which Markdown has no
|
|
8
|
+
# form for and Discourse keeps. The tags are inline tokens so that
|
|
9
|
+
# the serializer sees them as neighbors: a newline right after the
|
|
10
|
+
# opening tag would leave it alone on its line, where it opens an
|
|
11
|
+
# HTML block.
|
|
12
|
+
class HighlightTag < Tag
|
|
13
|
+
OPEN = Inline::Tokens.html("<mark>")
|
|
14
|
+
CLOSE = Inline::Tokens.html("</mark>")
|
|
15
|
+
private_constant :OPEN, :CLOSE
|
|
16
|
+
|
|
17
|
+
def render(element, interface)
|
|
18
|
+
tokens = interface.render_tokens(element, context: interface.with_parent(element))
|
|
19
|
+
tokens.unshift(OPEN) << CLOSE
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -5,14 +5,19 @@ module Markbridge
|
|
|
5
5
|
module Discourse
|
|
6
6
|
module Tags
|
|
7
7
|
# Images: an image token. The serializer writes
|
|
8
|
-
# `
|
|
8
|
+
# `` or ``
|
|
9
9
|
# with the alt text escaped like a link label, and <img> in an
|
|
10
10
|
# HTML block.
|
|
11
11
|
class ImageTag < Tag
|
|
12
|
+
# @param base_url [BaseUrl, nil] makes a relative src absolute
|
|
13
|
+
def initialize(base_url: nil)
|
|
14
|
+
@base_url = base_url
|
|
15
|
+
end
|
|
16
|
+
|
|
12
17
|
def render(element, _interface)
|
|
13
18
|
[
|
|
14
19
|
Inline::Tokens.image(
|
|
15
|
-
src: element.src,
|
|
20
|
+
src: @base_url ? @base_url.resolve(element.src) : element.src,
|
|
16
21
|
alt: element.alt,
|
|
17
22
|
width: element.width,
|
|
18
23
|
height: element.height,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markbridge
|
|
4
|
+
module Renderers
|
|
5
|
+
module Discourse
|
|
6
|
+
module Tags
|
|
7
|
+
# Tag for rendering inserted text as the HTML tag, which Markdown has no
|
|
8
|
+
# form for and Discourse keeps. The tags are inline tokens so that
|
|
9
|
+
# the serializer sees them as neighbors: a newline right after the
|
|
10
|
+
# opening tag would leave it alone on its line, where it opens an
|
|
11
|
+
# HTML block.
|
|
12
|
+
class InsertionTag < Tag
|
|
13
|
+
OPEN = Inline::Tokens.html("<ins>")
|
|
14
|
+
CLOSE = Inline::Tokens.html("</ins>")
|
|
15
|
+
private_constant :OPEN, :CLOSE
|
|
16
|
+
|
|
17
|
+
def render(element, interface)
|
|
18
|
+
tokens = interface.render_tokens(element, context: interface.with_parent(element))
|
|
19
|
+
tokens.unshift(OPEN) << CLOSE
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -10,7 +10,9 @@ module Markbridge
|
|
|
10
10
|
# Spaces and tabs at the start of the paragraph. Not a no-break
|
|
11
11
|
# space: the escaper writes one where it wants indentation kept.
|
|
12
12
|
LEADING_BLANKS = /\A[ \t]+/
|
|
13
|
-
|
|
13
|
+
VISIBLE = /[^[:space:]]/
|
|
14
|
+
ISLAND = "\n\n"
|
|
15
|
+
private_constant :LEADING_BLANKS, :VISIBLE, :ISLAND
|
|
14
16
|
|
|
15
17
|
def render(element, interface)
|
|
16
18
|
child_context = interface.with_parent(element)
|
|
@@ -20,8 +22,16 @@ module Markbridge
|
|
|
20
22
|
# Also inside a table cell: without the <p> the paragraph
|
|
21
23
|
# runs into the text next to it. A block inside the
|
|
22
24
|
# paragraph closes the <p> early in the HTML parser, which
|
|
23
|
-
# is what the Markdown form does as well.
|
|
24
|
-
|
|
25
|
+
# is what the Markdown form does as well. A paragraph with
|
|
26
|
+
# nothing visible is nothing, like in Markdown: an empty <p>
|
|
27
|
+
# would keep a list item or a list around it alive.
|
|
28
|
+
return "" unless content.match?(VISIBLE)
|
|
29
|
+
|
|
30
|
+
# A blank line in the content is a Markdown island, an
|
|
31
|
+
# upload for example, and an island is a <div>. A <p> ends
|
|
32
|
+
# at a <div>, and an inline element around the island would
|
|
33
|
+
# stay behind in the <p>, empty. A <div> can hold both.
|
|
34
|
+
return content.include?(ISLAND) ? "<div>#{content}</div>" : "<p>#{content}</p>"
|
|
25
35
|
end
|
|
26
36
|
|
|
27
37
|
# Bracket with leading and trailing blank lines so adjacent
|
|
@@ -36,7 +36,7 @@ module Markbridge
|
|
|
36
36
|
"[quote]\n#{content}\n[/quote]"
|
|
37
37
|
else
|
|
38
38
|
# Plain quote rendered as Markdown blockquote
|
|
39
|
-
content
|
|
39
|
+
blockquote(content)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# Bracket with leading and trailing blank lines so consecutive
|
|
@@ -47,6 +47,15 @@ module Markbridge
|
|
|
47
47
|
|
|
48
48
|
private
|
|
49
49
|
|
|
50
|
+
# Two blocks in the content have more than one blank line
|
|
51
|
+
# between them, which the postprocessor collapses at the end.
|
|
52
|
+
# With a `>` in front of every line it does not see them as
|
|
53
|
+
# blank lines, so they are collapsed here.
|
|
54
|
+
def blockquote(content)
|
|
55
|
+
content = Postprocessor::DEFAULT.collapse_blank_lines(content)
|
|
56
|
+
content.split("\n").map { |line| "> #{line}" }.join("\n")
|
|
57
|
+
end
|
|
58
|
+
|
|
50
59
|
# @param element [AST::Element]
|
|
51
60
|
# @return [Boolean] whether a quote that renders as [quote="…"]
|
|
52
61
|
# is somewhere inside +element+
|
|
@@ -49,7 +49,7 @@ module Markbridge
|
|
|
49
49
|
percent = bbcode_percent(element.size, interface)
|
|
50
50
|
return interface.render_tokens(element, context:) if percent.nil?
|
|
51
51
|
|
|
52
|
-
content = interface.render_children(element, context:)
|
|
52
|
+
content = interface.render_children(element, context:, following: "[/size]")
|
|
53
53
|
# The style that the discourse-bbcode plugin lets through.
|
|
54
54
|
return %(<span style="font-size:#{percent}%">#{content}</span>) if interface.html_mode?
|
|
55
55
|
|
|
@@ -9,6 +9,9 @@ module Markbridge
|
|
|
9
9
|
# tag, so a title is written as text in front of the spoiler:
|
|
10
10
|
# `title: [spoiler]…[/spoiler]`.
|
|
11
11
|
#
|
|
12
|
+
# A spoiler with +block+ set is written with the tags on lines of
|
|
13
|
+
# their own, which Discourse reads as a block spoiler.
|
|
14
|
+
#
|
|
12
15
|
# With +bbcode: false+, for a site without the plugin, the content
|
|
13
16
|
# is written as it is, so the hidden text shows.
|
|
14
17
|
class SpoilerTag < Tag
|
|
@@ -19,13 +22,20 @@ module Markbridge
|
|
|
19
22
|
|
|
20
23
|
def render(element, interface)
|
|
21
24
|
child_context = interface.with_parent(element)
|
|
22
|
-
content =
|
|
25
|
+
content =
|
|
26
|
+
interface.render_children(
|
|
27
|
+
element,
|
|
28
|
+
context: child_context,
|
|
29
|
+
following: inline_close(element, interface),
|
|
30
|
+
)
|
|
23
31
|
|
|
24
32
|
# A spoiler with nothing to hide gets no tags, like the
|
|
25
33
|
# serializer's markers: an empty [spoiler] shell would only
|
|
26
34
|
# add noise to the output.
|
|
27
35
|
return content unless content.match?(/[^[:space:]]/)
|
|
28
36
|
|
|
37
|
+
return render_block(element, interface, content) if block?(element, interface)
|
|
38
|
+
|
|
29
39
|
title = title_text(element, interface)
|
|
30
40
|
# Discourse doesn't cook BBCode in the text of a link.
|
|
31
41
|
return "#{title}#{content}" if !@bbcode || inside_link?(interface)
|
|
@@ -34,7 +44,7 @@ module Markbridge
|
|
|
34
44
|
# `[spoiler]:x` at the start of a line reads as a link
|
|
35
45
|
# reference definition, the backslash keeps the colon text.
|
|
36
46
|
content = "\\#{content}" if content.start_with?(":")
|
|
37
|
-
output = "#{title}[spoiler]#{content}
|
|
47
|
+
output = "#{title}[spoiler]#{Inline::Joins.close_url(content)}#{INLINE_CLOSE}"
|
|
38
48
|
# A line that starts with `[spoiler]` and ends with `[/spoiler]`
|
|
39
49
|
# is one block spoiler to Discourse, whatever stands between, so
|
|
40
50
|
# two spoilers on it show their inner tags as text. A comment
|
|
@@ -45,6 +55,40 @@ module Markbridge
|
|
|
45
55
|
|
|
46
56
|
private
|
|
47
57
|
|
|
58
|
+
INLINE_CLOSE = "[/spoiler]"
|
|
59
|
+
private_constant :INLINE_CLOSE
|
|
60
|
+
|
|
61
|
+
# The closing tag when it follows the content on the same line.
|
|
62
|
+
# A bare URL in front of it would take it into the link. (In
|
|
63
|
+
# html_mode a URL is an <a> in any case, so nobody asks there.)
|
|
64
|
+
def inline_close(element, interface)
|
|
65
|
+
return if !@bbcode || element.block || inside_link?(interface)
|
|
66
|
+
|
|
67
|
+
INLINE_CLOSE
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A block spoiler in the text of a link is written like an
|
|
71
|
+
# inline one, as its content. The normalizer moves a block out of
|
|
72
|
+
# a link, so this is for a tree that was not normalized.
|
|
73
|
+
def block?(element, interface)
|
|
74
|
+
element.block && !inside_link?(interface)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# `[spoiler]` and `[/spoiler]` on lines of their own are a block
|
|
78
|
+
# spoiler to Discourse, with blocks inside. The title is a
|
|
79
|
+
# paragraph in front of it. Without the plugin the content is a
|
|
80
|
+
# block of its own too, in html_mode a <div> without the class.
|
|
81
|
+
def render_block(element, interface, content)
|
|
82
|
+
title = title_text(element, interface)&.rstrip
|
|
83
|
+
if interface.html_mode?
|
|
84
|
+
return "#{title}<div#{' class="spoiler"' if @bbcode}>#{content}</div>"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
content = content.gsub(/\A\n+|\n+\z/, "")
|
|
88
|
+
content = "[spoiler]\n#{content}\n[/spoiler]" if @bbcode
|
|
89
|
+
title ? "\n\n#{title}\n\n#{content}\n\n" : "\n\n#{content}\n\n"
|
|
90
|
+
end
|
|
91
|
+
|
|
48
92
|
# Whether a spoiler stands in front of +element+ on the same line,
|
|
49
93
|
# with anything inline between. A text that has something visible
|
|
50
94
|
# after its last newline starts the line, so the search ends
|
|
@@ -22,22 +22,27 @@ module Markbridge
|
|
|
22
22
|
].freeze
|
|
23
23
|
private_constant :BLOCK_NODES
|
|
24
24
|
|
|
25
|
+
# The cells are rendered once when the place or the content of
|
|
26
|
+
# the table already says that it has to be HTML. A table in a
|
|
27
|
+
# table is such a case, and rendering each of them twice would
|
|
28
|
+
# double the work with every level.
|
|
25
29
|
def render(element, interface)
|
|
26
30
|
child_context = interface.with_parent(element)
|
|
27
|
-
rows_data = extract_rows(element, interface, child_context)
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
render_markdown(rows_data)
|
|
33
|
-
else
|
|
34
|
-
# Re-render cells in html_mode so inline Markdown like **bold** becomes
|
|
35
|
-
# <strong>bold</strong>; CommonMark would not parse Markdown inside an HTML block.
|
|
36
|
-
html_rows = extract_rows(element, interface, child_context.with_html_mode(true))
|
|
37
|
-
html = render_html(html_rows)
|
|
38
|
-
# Inside an HTML block a blank line would end the block.
|
|
39
|
-
interface.html_mode? ? html : "\n\n#{html}\n\n"
|
|
32
|
+
if markdown_possible?(element, interface)
|
|
33
|
+
rows_data = extract_rows(element, interface, child_context)
|
|
34
|
+
return "" if rows_data.empty?
|
|
35
|
+
return render_markdown(rows_data) if markdown_rows?(rows_data)
|
|
40
36
|
end
|
|
37
|
+
|
|
38
|
+
# Cells in html_mode, so inline Markdown like **bold** becomes
|
|
39
|
+
# <strong>bold</strong>; CommonMark would not parse Markdown inside an HTML block.
|
|
40
|
+
html_rows = extract_rows(element, interface, child_context.with_html_mode(true))
|
|
41
|
+
return "" if html_rows.empty?
|
|
42
|
+
|
|
43
|
+
html = render_html(html_rows)
|
|
44
|
+
# Inside an HTML block a blank line would end the block.
|
|
45
|
+
interface.html_mode? ? html : "\n\n#{html}\n\n"
|
|
41
46
|
end
|
|
42
47
|
|
|
43
48
|
private
|
|
@@ -56,27 +61,30 @@ module Markbridge
|
|
|
56
61
|
# can detect they're inside a cell via has_parent?.
|
|
57
62
|
cell_context = child_context.with_parent(cell)
|
|
58
63
|
content = interface.render_children(cell, context: cell_context).strip
|
|
59
|
-
{ content:, header: cell.header
|
|
64
|
+
{ content:, header: cell.header?, colspan: cell.colspan, rowspan: cell.rowspan }
|
|
60
65
|
end
|
|
61
66
|
|
|
62
67
|
{ cells: } unless cells.empty?
|
|
63
68
|
end
|
|
64
69
|
end
|
|
65
70
|
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return false if block_in_cell?(element)
|
|
71
|
+
# What can be said without rendering a cell: a Markdown table
|
|
72
|
+
# is not nested in a table and has no block in a cell.
|
|
73
|
+
def markdown_possible?(element, interface)
|
|
74
|
+
!interface.has_parent?(AST::Table) && !block_in_cell?(element)
|
|
75
|
+
end
|
|
72
76
|
|
|
77
|
+
# What the rendered cells say: the header row (when there is
|
|
78
|
+
# one) first, every row as wide as the first, no cell on two
|
|
79
|
+
# lines and no cell over several columns or rows.
|
|
80
|
+
def markdown_rows?(rows_data)
|
|
73
81
|
header_idx = rows_data.index { |r| r[:cells].all? { |c| c[:header] } }
|
|
74
82
|
return false if header_idx&.positive?
|
|
75
83
|
|
|
76
84
|
cell_count = rows_data.first[:cells].length
|
|
77
85
|
rows_data.all? do |row|
|
|
78
86
|
row[:cells].length == cell_count &&
|
|
79
|
-
row[:cells].none? { |c| c[:content].include?("\n") }
|
|
87
|
+
row[:cells].none? { |c| c[:content].include?("\n") || c[:colspan] || c[:rowspan] }
|
|
80
88
|
end
|
|
81
89
|
end
|
|
82
90
|
|
|
@@ -131,12 +139,21 @@ module Markbridge
|
|
|
131
139
|
lines.join("\n")
|
|
132
140
|
end
|
|
133
141
|
|
|
142
|
+
# Discourse keeps colspan and rowspan on a cell. The values are
|
|
143
|
+
# whole numbers, see AST::TableCell.
|
|
144
|
+
def span_attributes(cell)
|
|
145
|
+
attributes = +""
|
|
146
|
+
attributes << %( colspan="#{cell[:colspan]}") if cell[:colspan]
|
|
147
|
+
attributes << %( rowspan="#{cell[:rowspan]}") if cell[:rowspan]
|
|
148
|
+
attributes
|
|
149
|
+
end
|
|
150
|
+
|
|
134
151
|
# Render a single HTML table row
|
|
135
152
|
def html_row(row)
|
|
136
153
|
cells_html =
|
|
137
154
|
row[:cells].map do |cell|
|
|
138
155
|
tag = cell[:header] ? "th" : "td"
|
|
139
|
-
"<#{tag}>#{cell[:content]}</#{tag}>"
|
|
156
|
+
"<#{tag}#{span_attributes(cell)}>#{cell[:content]}</#{tag}>"
|
|
140
157
|
end
|
|
141
158
|
|
|
142
159
|
"<tr>#{cells_html.join}</tr>"
|
|
@@ -21,8 +21,13 @@ module Markbridge
|
|
|
21
21
|
EDGES = /\A([[:space:]]*).*?([[:space:]]*)\z/m
|
|
22
22
|
private_constant :ALLOWED_SCHEMES, :SCHEME_LIKE, :EDGES
|
|
23
23
|
|
|
24
|
+
# @param base_url [BaseUrl, nil] makes a relative href absolute
|
|
25
|
+
def initialize(base_url: nil)
|
|
26
|
+
@base_url = base_url
|
|
27
|
+
end
|
|
28
|
+
|
|
24
29
|
def render(element, interface)
|
|
25
|
-
href = element.href
|
|
30
|
+
href = @base_url ? @base_url.resolve(element.href) : element.href
|
|
26
31
|
linkable = linkable?(href)
|
|
27
32
|
# Url#bare? judges the AST, so label escaping cannot confuse it.
|
|
28
33
|
return bare_tokens(element, href) if linkable && element.bare?
|
|
@@ -11,6 +11,7 @@ require_relative "discourse/html_escaper"
|
|
|
11
11
|
require_relative "discourse/html_block"
|
|
12
12
|
require_relative "discourse/paragraphs"
|
|
13
13
|
require_relative "discourse/link_destination"
|
|
14
|
+
require_relative "discourse/base_url"
|
|
14
15
|
require_relative "discourse/inline/tokens"
|
|
15
16
|
require_relative "discourse/inline/joins"
|
|
16
17
|
require_relative "discourse/inline/serializer"
|
|
@@ -35,6 +36,9 @@ require_relative "discourse/tags/italic_tag"
|
|
|
35
36
|
require_relative "discourse/tags/list_tag"
|
|
36
37
|
require_relative "discourse/tags/list_item_tag"
|
|
37
38
|
require_relative "discourse/tags/paragraph_tag"
|
|
39
|
+
require_relative "discourse/tags/deletion_tag"
|
|
40
|
+
require_relative "discourse/tags/highlight_tag"
|
|
41
|
+
require_relative "discourse/tags/insertion_tag"
|
|
38
42
|
require_relative "discourse/tags/quote_tag"
|
|
39
43
|
require_relative "discourse/tags/size_tag"
|
|
40
44
|
require_relative "discourse/tags/spoiler_tag"
|
data/lib/markbridge/version.rb
CHANGED
data/lib/markbridge.rb
CHANGED
|
@@ -339,10 +339,16 @@ module Markbridge
|
|
|
339
339
|
# name, or nil to drop a color. Without it the values are written
|
|
340
340
|
# as they are, and a value the plugin does not accept raises
|
|
341
341
|
# ArgumentError.
|
|
342
|
+
# @param base_url [String, nil] the http or https URL of the page the
|
|
343
|
+
# content comes from. The relative addresses of links and images
|
|
344
|
+
# are made absolute with it, like a browser does on that page:
|
|
345
|
+
# +page.html+, +./Alta_Via+, +/files/a.png+. Without it they are
|
|
346
|
+
# written as they are, and Discourse drops an address that has no
|
|
347
|
+
# scheme and no leading slash. Mappings in +tags:+ still win.
|
|
342
348
|
# @return [Renderers::Discourse::Renderer]
|
|
343
349
|
# @raise [ArgumentError] for a feature that does not exist, for
|
|
344
|
-
# +bbcode: true+ without +sizes:+,
|
|
345
|
-
# +bbcode: true+
|
|
350
|
+
# +bbcode: true+ without +sizes:+, for a mapping without
|
|
351
|
+
# +bbcode: true+, and for a +base_url:+ that is no http or https URL
|
|
346
352
|
def discourse_renderer(
|
|
347
353
|
tags: nil,
|
|
348
354
|
tag_library: nil,
|
|
@@ -355,7 +361,8 @@ module Markbridge
|
|
|
355
361
|
strip_trailing_invisibles: false,
|
|
356
362
|
features: nil,
|
|
357
363
|
sizes: nil,
|
|
358
|
-
colors: nil
|
|
364
|
+
colors: nil,
|
|
365
|
+
base_url: nil
|
|
359
366
|
)
|
|
360
367
|
# Dup the caller's library before mutating so successive
|
|
361
368
|
# +discourse_renderer+ calls against the same +tag_library:+ don't
|
|
@@ -364,6 +371,7 @@ module Markbridge
|
|
|
364
371
|
# +tag_library:+ branch.
|
|
365
372
|
library = tag_library ? tag_library.dup : Renderers::Discourse::TagLibrary.default
|
|
366
373
|
register_features(library, features || {}, sizes:, colors:)
|
|
374
|
+
register_base_url(library, base_url) if base_url
|
|
367
375
|
library.merge!(tags) if tags
|
|
368
376
|
Array(unregister).each { |klass| library.unregister(klass) }
|
|
369
377
|
|
|
@@ -396,6 +404,12 @@ module Markbridge
|
|
|
396
404
|
register_bbcode_feature(library, features[:bbcode], sizes:, colors:)
|
|
397
405
|
end
|
|
398
406
|
|
|
407
|
+
def register_base_url(library, url)
|
|
408
|
+
base_url = Renderers::Discourse::BaseUrl.new(url)
|
|
409
|
+
library.register(AST::Url, Renderers::Discourse::Tags::UrlTag.new(base_url:))
|
|
410
|
+
library.register(AST::Image, Renderers::Discourse::Tags::ImageTag.new(base_url:))
|
|
411
|
+
end
|
|
412
|
+
|
|
399
413
|
def register_bbcode_feature(library, bbcode, sizes:, colors:)
|
|
400
414
|
if bbcode && sizes.nil?
|
|
401
415
|
raise ArgumentError,
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: markbridge
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Discourse Team
|
|
@@ -25,14 +25,17 @@ files:
|
|
|
25
25
|
- lib/markbridge/ast/bold.rb
|
|
26
26
|
- lib/markbridge/ast/code.rb
|
|
27
27
|
- lib/markbridge/ast/color.rb
|
|
28
|
+
- lib/markbridge/ast/deletion.rb
|
|
28
29
|
- lib/markbridge/ast/details.rb
|
|
29
30
|
- lib/markbridge/ast/document.rb
|
|
30
31
|
- lib/markbridge/ast/element.rb
|
|
31
32
|
- lib/markbridge/ast/email.rb
|
|
32
33
|
- lib/markbridge/ast/event.rb
|
|
33
34
|
- lib/markbridge/ast/heading.rb
|
|
35
|
+
- lib/markbridge/ast/highlight.rb
|
|
34
36
|
- lib/markbridge/ast/horizontal_rule.rb
|
|
35
37
|
- lib/markbridge/ast/image.rb
|
|
38
|
+
- lib/markbridge/ast/insertion.rb
|
|
36
39
|
- lib/markbridge/ast/italic.rb
|
|
37
40
|
- lib/markbridge/ast/line_break.rb
|
|
38
41
|
- lib/markbridge/ast/list.rb
|
|
@@ -60,6 +63,7 @@ files:
|
|
|
60
63
|
- lib/markbridge/html.rb
|
|
61
64
|
- lib/markbridge/mediawiki.rb
|
|
62
65
|
- lib/markbridge/normalizer.rb
|
|
66
|
+
- lib/markbridge/normalizer/carrier.rb
|
|
63
67
|
- lib/markbridge/normalizer/report.rb
|
|
64
68
|
- lib/markbridge/normalizer/rule_set.rb
|
|
65
69
|
- lib/markbridge/normalizer/text_projection.rb
|
|
@@ -77,10 +81,12 @@ files:
|
|
|
77
81
|
- lib/markbridge/parsers/bbcode/handlers/base_handler.rb
|
|
78
82
|
- lib/markbridge/parsers/bbcode/handlers/code_handler.rb
|
|
79
83
|
- lib/markbridge/parsers/bbcode/handlers/color_handler.rb
|
|
84
|
+
- lib/markbridge/parsers/bbcode/handlers/continued_formatting.rb
|
|
80
85
|
- lib/markbridge/parsers/bbcode/handlers/email_handler.rb
|
|
81
86
|
- lib/markbridge/parsers/bbcode/handlers/image_handler.rb
|
|
82
87
|
- lib/markbridge/parsers/bbcode/handlers/list_handler.rb
|
|
83
88
|
- lib/markbridge/parsers/bbcode/handlers/list_item_handler.rb
|
|
89
|
+
- lib/markbridge/parsers/bbcode/handlers/open_table_part.rb
|
|
84
90
|
- lib/markbridge/parsers/bbcode/handlers/quote_handler.rb
|
|
85
91
|
- lib/markbridge/parsers/bbcode/handlers/raw_handler.rb
|
|
86
92
|
- lib/markbridge/parsers/bbcode/handlers/self_closing_handler.rb
|
|
@@ -130,14 +136,17 @@ files:
|
|
|
130
136
|
- lib/markbridge/parsers/text_formatter/handlers/base_handler.rb
|
|
131
137
|
- lib/markbridge/parsers/text_formatter/handlers/code_handler.rb
|
|
132
138
|
- lib/markbridge/parsers/text_formatter/handlers/email_handler.rb
|
|
139
|
+
- lib/markbridge/parsers/text_formatter/handlers/heading_handler.rb
|
|
133
140
|
- lib/markbridge/parsers/text_formatter/handlers/image_handler.rb
|
|
134
141
|
- lib/markbridge/parsers/text_formatter/handlers/list_handler.rb
|
|
135
142
|
- lib/markbridge/parsers/text_formatter/handlers/quote_handler.rb
|
|
143
|
+
- lib/markbridge/parsers/text_formatter/handlers/self_closing_handler.rb
|
|
136
144
|
- lib/markbridge/parsers/text_formatter/handlers/simple_handler.rb
|
|
137
145
|
- lib/markbridge/parsers/text_formatter/handlers/table_cell_handler.rb
|
|
138
146
|
- lib/markbridge/parsers/text_formatter/handlers/url_handler.rb
|
|
139
147
|
- lib/markbridge/parsers/text_formatter/parser.rb
|
|
140
148
|
- lib/markbridge/renderers/discourse.rb
|
|
149
|
+
- lib/markbridge/renderers/discourse/base_url.rb
|
|
141
150
|
- lib/markbridge/renderers/discourse/builders/list_item_builder.rb
|
|
142
151
|
- lib/markbridge/renderers/discourse/html_block.rb
|
|
143
152
|
- lib/markbridge/renderers/discourse/html_escaper.rb
|
|
@@ -159,12 +168,15 @@ files:
|
|
|
159
168
|
- lib/markbridge/renderers/discourse/tags/bold_tag.rb
|
|
160
169
|
- lib/markbridge/renderers/discourse/tags/code_tag.rb
|
|
161
170
|
- lib/markbridge/renderers/discourse/tags/color_tag.rb
|
|
171
|
+
- lib/markbridge/renderers/discourse/tags/deletion_tag.rb
|
|
162
172
|
- lib/markbridge/renderers/discourse/tags/details_tag.rb
|
|
163
173
|
- lib/markbridge/renderers/discourse/tags/email_tag.rb
|
|
164
174
|
- lib/markbridge/renderers/discourse/tags/event_tag.rb
|
|
165
175
|
- lib/markbridge/renderers/discourse/tags/heading_tag.rb
|
|
176
|
+
- lib/markbridge/renderers/discourse/tags/highlight_tag.rb
|
|
166
177
|
- lib/markbridge/renderers/discourse/tags/horizontal_rule_tag.rb
|
|
167
178
|
- lib/markbridge/renderers/discourse/tags/image_tag.rb
|
|
179
|
+
- lib/markbridge/renderers/discourse/tags/insertion_tag.rb
|
|
168
180
|
- lib/markbridge/renderers/discourse/tags/italic_tag.rb
|
|
169
181
|
- lib/markbridge/renderers/discourse/tags/line_break_tag.rb
|
|
170
182
|
- lib/markbridge/renderers/discourse/tags/list_item_tag.rb
|