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
|
@@ -19,6 +19,8 @@ module Markbridge
|
|
|
19
19
|
# r.register("B", SimpleHandler.new(AST::Bold)) # Override default
|
|
20
20
|
# end
|
|
21
21
|
class HandlerRegistry
|
|
22
|
+
include Enumerable
|
|
23
|
+
|
|
22
24
|
# Create a new registry with default mappings
|
|
23
25
|
# @return [HandlerRegistry]
|
|
24
26
|
def self.default
|
|
@@ -48,11 +50,32 @@ module Markbridge
|
|
|
48
50
|
# silently mutating state visible to every parser.
|
|
49
51
|
def freeze
|
|
50
52
|
@mappings.freeze
|
|
53
|
+
@transparent_tags.freeze
|
|
51
54
|
super
|
|
52
55
|
end
|
|
53
56
|
|
|
57
|
+
# Elements that are expected without a handler: <ESC> holds a
|
|
58
|
+
# character that a backslash made literal, <E> an emoticon and
|
|
59
|
+
# <EMOJI> an emoji, each with its text as content.
|
|
60
|
+
DEFAULT_TRANSPARENT_TAGS = %w[ESC E EMOJI].freeze
|
|
61
|
+
|
|
62
|
+
# @return [Set<String>] mutable set of element names that are not
|
|
63
|
+
# counted as unknown when they have no handler. Their content
|
|
64
|
+
# is processed like for any element without one.
|
|
65
|
+
attr_reader :transparent_tags
|
|
66
|
+
|
|
54
67
|
def initialize
|
|
55
68
|
@mappings = {}
|
|
69
|
+
@transparent_tags = Set.new(DEFAULT_TRANSPARENT_TAGS)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Iterate over the registered (element_name, handler) pairs, in
|
|
73
|
+
# the order of registration.
|
|
74
|
+
# @yieldparam element_name [String]
|
|
75
|
+
# @yieldparam handler [#process]
|
|
76
|
+
# @return [Enumerator] when no block is given
|
|
77
|
+
def each(&block)
|
|
78
|
+
@mappings.each(&block)
|
|
56
79
|
end
|
|
57
80
|
|
|
58
81
|
# Register a handler for an element
|
|
@@ -126,13 +149,37 @@ module Markbridge
|
|
|
126
149
|
"ALIGN",
|
|
127
150
|
Handlers::AttributeHandler.new(AST::Align, attribute: :align, param: :alignment),
|
|
128
151
|
)
|
|
129
|
-
|
|
152
|
+
# The alignment BBCodes of the s9e repository have no attribute,
|
|
153
|
+
# the name says how to align. Flarum has [center] on.
|
|
154
|
+
%w[CENTER LEFT RIGHT JUSTIFY].each do |name|
|
|
155
|
+
register(name, Handlers::SimpleHandler.new(AST::Align, alignment: name.downcase))
|
|
156
|
+
end
|
|
157
|
+
# phpBB shows a [spoiler] as a block. ISPOILER is the inline
|
|
158
|
+
# spoiler of Litedown.
|
|
159
|
+
register(
|
|
160
|
+
"SPOILER",
|
|
161
|
+
Handlers::AttributeHandler.new(AST::Spoiler, attribute: :title, block: true),
|
|
162
|
+
)
|
|
163
|
+
register("ISPOILER", Handlers::SimpleHandler.new(AST::Spoiler))
|
|
130
164
|
register("ATTACHMENT", Handlers::AttachmentHandler.new)
|
|
131
165
|
register("ATTACH", Handlers::AttachmentHandler.new)
|
|
132
166
|
|
|
133
|
-
# List item (supports both LI and * for compatibility)
|
|
134
167
|
register("LI", Handlers::SimpleHandler.new(AST::ListItem))
|
|
135
|
-
|
|
168
|
+
|
|
169
|
+
# The [hr] BBCode of the s9e repository and a rule in Litedown
|
|
170
|
+
register("HR", Handlers::SelfClosingHandler.new(AST::HorizontalRule))
|
|
171
|
+
|
|
172
|
+
# Litedown, the Markdown plugin of s9e that Flarum uses. It has
|
|
173
|
+
# its own elements for what BBCode has too: <STRONG> next to <B>.
|
|
174
|
+
register("STRONG", Handlers::SimpleHandler.new(AST::Bold))
|
|
175
|
+
register("EM", Handlers::SimpleHandler.new(AST::Italic))
|
|
176
|
+
register("DEL", Handlers::SimpleHandler.new(AST::Strikethrough))
|
|
177
|
+
register("SUP", Handlers::SimpleHandler.new(AST::Superscript))
|
|
178
|
+
register("SUB", Handlers::SimpleHandler.new(AST::Subscript))
|
|
179
|
+
# <C> is a code span, <CODE> a fenced block.
|
|
180
|
+
register("C", Handlers::SimpleHandler.new(AST::Code))
|
|
181
|
+
heading = Handlers::HeadingHandler.new
|
|
182
|
+
%w[H1 H2 H3 H4 H5 H6].each { |name| register(name, heading) }
|
|
136
183
|
|
|
137
184
|
# Paragraphs
|
|
138
185
|
register("P", Handlers::SimpleHandler.new(AST::Paragraph))
|
|
@@ -13,19 +13,26 @@ module Markbridge
|
|
|
13
13
|
# # For <COLOR color="red">text</COLOR>
|
|
14
14
|
# handler = AttributeHandler.new(AST::Color, attribute: :color, param: :color)
|
|
15
15
|
# registry.register("COLOR", handler)
|
|
16
|
+
#
|
|
17
|
+
# @example With a fixed keyword for every node
|
|
18
|
+
# # <SPOILER title="..."> is a block in phpBB
|
|
19
|
+
# AttributeHandler.new(AST::Spoiler, attribute: :title, block: true)
|
|
16
20
|
class AttributeHandler < BaseHandler
|
|
17
21
|
# @param element_class [Class] the AST node class to instantiate
|
|
18
22
|
# @param attribute [Symbol] the XML attribute name to extract
|
|
19
23
|
# @param param [Symbol] the parameter name to pass to the AST node constructor
|
|
20
|
-
|
|
24
|
+
# @param fixed [Hash{Symbol => Object}] more keywords, which every
|
|
25
|
+
# node gets next to the one from the attribute
|
|
26
|
+
def initialize(element_class, attribute:, param: nil, **fixed)
|
|
21
27
|
@element_class = element_class
|
|
22
28
|
@attribute = attribute
|
|
23
29
|
@param = param || attribute
|
|
30
|
+
@fixed = fixed
|
|
24
31
|
end
|
|
25
32
|
|
|
26
33
|
def process(element:, parent:, processor: nil)
|
|
27
34
|
attrs = extract_attributes(element)
|
|
28
|
-
node = @element_class.new(@param => attrs[@attribute])
|
|
35
|
+
node = @element_class.new(**@fixed, @param => attrs[@attribute])
|
|
29
36
|
parent << node
|
|
30
37
|
|
|
31
38
|
# Return node to signal: process children into this node
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markbridge
|
|
4
|
+
module Parsers
|
|
5
|
+
module TextFormatter
|
|
6
|
+
module Handlers
|
|
7
|
+
# Handler for the heading elements of Litedown, <H1> to <H6>. The
|
|
8
|
+
# level is the digit in the element name.
|
|
9
|
+
class HeadingHandler < BaseHandler
|
|
10
|
+
# @return [AST::Heading] the heading, so the children are
|
|
11
|
+
# processed into it
|
|
12
|
+
def process(element:, parent:, processor: nil)
|
|
13
|
+
heading = AST::Heading.new(level: Integer(element.name.delete_prefix("H")))
|
|
14
|
+
parent << heading
|
|
15
|
+
|
|
16
|
+
heading
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# @return [Class] AST::Heading
|
|
20
|
+
def element_class
|
|
21
|
+
AST::Heading
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markbridge
|
|
4
|
+
module Parsers
|
|
5
|
+
module TextFormatter
|
|
6
|
+
module Handlers
|
|
7
|
+
# Handler for an element without content, like <HR>. Appends an
|
|
8
|
+
# instance of +element_class+ to +parent+ and returns nil, so the
|
|
9
|
+
# parser does not process the children: they are only the markup
|
|
10
|
+
# s9e keeps for unparsing.
|
|
11
|
+
class SelfClosingHandler < BaseHandler
|
|
12
|
+
# @param element_class [Class] the AST node class to instantiate
|
|
13
|
+
def initialize(element_class)
|
|
14
|
+
@element_class = element_class
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @return [nil]
|
|
18
|
+
def process(element:, parent:, processor: nil)
|
|
19
|
+
parent << @element_class.new
|
|
20
|
+
nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :element_class
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -12,17 +12,23 @@ module Markbridge
|
|
|
12
12
|
# @example
|
|
13
13
|
# handler = SimpleHandler.new(AST::Bold)
|
|
14
14
|
# registry.register("B", handler)
|
|
15
|
+
#
|
|
16
|
+
# @example With keywords for every node
|
|
17
|
+
# # [center] has no attribute, the name says how to align
|
|
18
|
+
# SimpleHandler.new(AST::Align, alignment: "center")
|
|
15
19
|
class SimpleHandler < BaseHandler
|
|
16
20
|
# @param element_class [Class] the AST node class to instantiate
|
|
17
|
-
|
|
21
|
+
# @param options [Hash{Symbol => Object}] keywords every node gets
|
|
22
|
+
def initialize(element_class, **options)
|
|
18
23
|
@element_class = element_class
|
|
24
|
+
@options = options
|
|
19
25
|
end
|
|
20
26
|
|
|
21
27
|
# Process the element by creating an AST node and processing children
|
|
22
28
|
# @param element [Nokogiri::XML::Element]
|
|
23
29
|
# @param parent [AST::Element]
|
|
24
30
|
def process(element:, parent:, processor: nil)
|
|
25
|
-
node = @element_class.new
|
|
31
|
+
node = @element_class.new(**@options)
|
|
26
32
|
parent << node
|
|
27
33
|
|
|
28
34
|
# Return node to signal: process children into this node
|
|
@@ -11,7 +11,12 @@ module Markbridge
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def process(element:, parent:, processor: nil)
|
|
14
|
-
node =
|
|
14
|
+
node =
|
|
15
|
+
AST::TableCell.new(
|
|
16
|
+
header: element.name.upcase == "TH",
|
|
17
|
+
colspan: element["colspan"],
|
|
18
|
+
rowspan: element["rowspan"],
|
|
19
|
+
)
|
|
15
20
|
parent << node
|
|
16
21
|
node
|
|
17
22
|
end
|
|
@@ -102,7 +102,6 @@ module Markbridge
|
|
|
102
102
|
# text after them is not visible.
|
|
103
103
|
BLOCK_NODES = [
|
|
104
104
|
AST::Align,
|
|
105
|
-
AST::Code,
|
|
106
105
|
AST::Details,
|
|
107
106
|
AST::Heading,
|
|
108
107
|
AST::HorizontalRule,
|
|
@@ -110,7 +109,6 @@ module Markbridge
|
|
|
110
109
|
AST::ListItem,
|
|
111
110
|
AST::Paragraph,
|
|
112
111
|
AST::Quote,
|
|
113
|
-
AST::Spoiler,
|
|
114
112
|
AST::Table,
|
|
115
113
|
AST::TableCell,
|
|
116
114
|
AST::TableRow,
|
|
@@ -162,11 +160,12 @@ module Markbridge
|
|
|
162
160
|
elsif @handlers.has_handler?(tag_name)
|
|
163
161
|
# The handler returned nil intentionally: a leaf like an image
|
|
164
162
|
# or an attachment, whose content is only fallback text. It is
|
|
165
|
-
# visible, so the text after it is no longer at the line start
|
|
166
|
-
|
|
163
|
+
# visible, so the text after it is no longer at the line start,
|
|
164
|
+
# unless the leaf is a block like a horizontal rule.
|
|
165
|
+
@line_start = block?(ast_parent.children.last)
|
|
167
166
|
else
|
|
168
167
|
# No handler found - track as unknown and process children directly
|
|
169
|
-
@unknown_tags[tag_name] += 1
|
|
168
|
+
@unknown_tags[tag_name] += 1 unless @handlers.transparent_tags.include?(tag_name)
|
|
170
169
|
process_children(element, ast_parent)
|
|
171
170
|
end
|
|
172
171
|
end
|
|
@@ -196,10 +195,21 @@ module Markbridge
|
|
|
196
195
|
@pending_break = nil
|
|
197
196
|
end
|
|
198
197
|
|
|
198
|
+
# Whether the forum shows +node+ as a block: one of the BLOCK_NODES,
|
|
199
|
+
# or a spoiler or code that says so. An inline spoiler and a code
|
|
200
|
+
# span are part of their line.
|
|
201
|
+
#
|
|
202
|
+
# @param node [AST::Node, nil]
|
|
203
|
+
def block?(node)
|
|
204
|
+
return node.block if node.is_a?(AST::Spoiler) || node.is_a?(AST::Code)
|
|
205
|
+
|
|
206
|
+
@block_classes[node.class]
|
|
207
|
+
end
|
|
208
|
+
|
|
199
209
|
# @param element [Nokogiri::XML::Element]
|
|
200
210
|
# @param ast_element [AST::Element]
|
|
201
211
|
def process_handled_children(element, ast_element)
|
|
202
|
-
if
|
|
212
|
+
if block?(ast_element)
|
|
203
213
|
@line_start = true
|
|
204
214
|
process_children(element, ast_element)
|
|
205
215
|
@line_start = true
|
|
@@ -223,7 +233,13 @@ module Markbridge
|
|
|
223
233
|
text = text_node.content
|
|
224
234
|
|
|
225
235
|
if ast_parent.is_a?(AST::Code)
|
|
226
|
-
|
|
236
|
+
# Code is visible, so what follows a code span is not at the
|
|
237
|
+
# start of its line anymore. After a code block the line
|
|
238
|
+
# starts again, see process_handled_children.
|
|
239
|
+
if text.match?(/\S/)
|
|
240
|
+
ast_parent << AST::Text.new(text)
|
|
241
|
+
@line_start = false
|
|
242
|
+
end
|
|
227
243
|
elsif @line_start
|
|
228
244
|
text = text.lstrip
|
|
229
245
|
return if text.empty?
|
|
@@ -10,10 +10,12 @@ require_relative "../ast"
|
|
|
10
10
|
# Handler classes
|
|
11
11
|
require_relative "text_formatter/handlers/base_handler"
|
|
12
12
|
require_relative "text_formatter/handlers/simple_handler"
|
|
13
|
+
require_relative "text_formatter/handlers/self_closing_handler"
|
|
13
14
|
require_relative "text_formatter/handlers/attribute_handler"
|
|
14
15
|
require_relative "text_formatter/handlers/attachment_handler"
|
|
15
16
|
require_relative "text_formatter/handlers/code_handler"
|
|
16
17
|
require_relative "text_formatter/handlers/email_handler"
|
|
18
|
+
require_relative "text_formatter/handlers/heading_handler"
|
|
17
19
|
require_relative "text_formatter/handlers/image_handler"
|
|
18
20
|
require_relative "text_formatter/handlers/list_handler"
|
|
19
21
|
require_relative "text_formatter/handlers/quote_handler"
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markbridge
|
|
4
|
+
module Renderers
|
|
5
|
+
module Discourse
|
|
6
|
+
# Makes the relative address of a link or an image absolute, the way
|
|
7
|
+
# a browser does on a page at the base URL.
|
|
8
|
+
#
|
|
9
|
+
# Discourse drops an address that has no scheme and no leading
|
|
10
|
+
# slash, and an address with a leading slash points to the Discourse
|
|
11
|
+
# site, not to the site the post comes from. With a base URL both
|
|
12
|
+
# keep pointing to where they pointed in the source.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# base = BaseUrl.new("https://wiki.example.com/wiki/Main_Page")
|
|
16
|
+
# base.resolve("./Alta_Via") # => "https://wiki.example.com/wiki/Alta_Via"
|
|
17
|
+
# base.resolve("/files/a.png") # => "https://wiki.example.com/files/a.png"
|
|
18
|
+
# base.resolve("https://example.org/") # => "https://example.org/"
|
|
19
|
+
class BaseUrl
|
|
20
|
+
# The origin and the path. A query and a fragment may follow and
|
|
21
|
+
# are not used. Whitespace anywhere is no URL: a base that is cut
|
|
22
|
+
# off at a space would resolve to the wrong directory.
|
|
23
|
+
PARTS = %r{\A(https?://[^/?#\s]+)([^?#\s]*)(?:[?#]\S*)?\z}i
|
|
24
|
+
SCHEME = /\A[a-z][a-z0-9+.-]*:/i
|
|
25
|
+
private_constant :PARTS, :SCHEME
|
|
26
|
+
|
|
27
|
+
# @param url [String] an http or https URL. Its last path segment
|
|
28
|
+
# is a page, like in a browser: relative addresses are next to
|
|
29
|
+
# it. End the URL with a slash to resolve inside a directory.
|
|
30
|
+
# @raise [ArgumentError] when +url+ is no http or https URL
|
|
31
|
+
def initialize(url)
|
|
32
|
+
match = PARTS.match(url)
|
|
33
|
+
unless match
|
|
34
|
+
raise ArgumentError, "base_url must be an http or https URL, got #{url.inspect}"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
@origin = match[1]
|
|
38
|
+
@path = match[2].empty? ? "/" : match[2]
|
|
39
|
+
@directory = @path[..@path.rindex("/")]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @param address [String, nil] the href of a link or the src of an
|
|
43
|
+
# image
|
|
44
|
+
# @return [String, nil] the absolute address. An address with a
|
|
45
|
+
# scheme, one that starts with `//` or `#`, an empty one and nil
|
|
46
|
+
# come back as they are.
|
|
47
|
+
def resolve(address)
|
|
48
|
+
return address if address.nil? || address.empty? || address.match?(SCHEME)
|
|
49
|
+
|
|
50
|
+
case address[0]
|
|
51
|
+
when "#"
|
|
52
|
+
address
|
|
53
|
+
when "/"
|
|
54
|
+
address.start_with?("//") ? address : "#{@origin}#{address}"
|
|
55
|
+
when "?"
|
|
56
|
+
"#{@origin}#{@path}#{address}"
|
|
57
|
+
else
|
|
58
|
+
"#{@origin}#{without_dot_segments(@directory + address)}"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
# `a/./b` is `a/b` and `a/x/../b` is `a/b`, in the path only: a
|
|
65
|
+
# query or a fragment is left as it is.
|
|
66
|
+
def without_dot_segments(location)
|
|
67
|
+
path, rest = location.split(/(?=[?#])/, 2)
|
|
68
|
+
segments =
|
|
69
|
+
path
|
|
70
|
+
.split("/")
|
|
71
|
+
.each_with_object([]) do |segment, kept|
|
|
72
|
+
case segment
|
|
73
|
+
when "."
|
|
74
|
+
nil
|
|
75
|
+
when ".."
|
|
76
|
+
kept.pop if kept.size > 1
|
|
77
|
+
else
|
|
78
|
+
kept << segment
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
# A slash or a dot segment at the end names a directory.
|
|
82
|
+
slash = "/" if path.end_with?("/", "/.", "/..")
|
|
83
|
+
|
|
84
|
+
"#{segments.join("/")}#{slash}#{rest}"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -206,6 +206,28 @@ module Markbridge
|
|
|
206
206
|
:HTML_START_BYTES,
|
|
207
207
|
:JOIN_ACTIONS
|
|
208
208
|
|
|
209
|
+
# A URL with its scheme at the very end of a text. A Markdown
|
|
210
|
+
# link that ends there is no such text.
|
|
211
|
+
URL_AT_END = %r{(?:https?|ftps?)://[^\s<>]*\z}i
|
|
212
|
+
LINK_AT_END = /\]\([^\s]*\)\z/
|
|
213
|
+
private_constant :URL_AT_END, :LINK_AT_END
|
|
214
|
+
|
|
215
|
+
# For content that a closing `~~` or a closing BBCode tag follows.
|
|
216
|
+
# markdown-it's linkify takes such a marker into a URL that
|
|
217
|
+
# stands right in front of it, so the comment goes between the
|
|
218
|
+
# two. A link, an address without scheme and a mail address end
|
|
219
|
+
# where they should.
|
|
220
|
+
#
|
|
221
|
+
# @param content [String]
|
|
222
|
+
# @return [String] the content, with the comment after a URL at
|
|
223
|
+
# its end
|
|
224
|
+
def self.close_url(content)
|
|
225
|
+
return content unless URL_AT_END.match?(content)
|
|
226
|
+
return content if LINK_AT_END.match?(content)
|
|
227
|
+
|
|
228
|
+
"#{content}#{BOUNDARY_COMMENT}"
|
|
229
|
+
end
|
|
230
|
+
|
|
209
231
|
# Appends +piece+ to +buffer+ with what the join between them
|
|
210
232
|
# needs. On an empty buffer getbyte returns nil and there is no
|
|
211
233
|
# byte in front of the join, so the first piece needs no guard
|
|
@@ -34,6 +34,11 @@ module Markbridge
|
|
|
34
34
|
u: ["[u]", "[/u]", %(<span class="bbcode-u">), "</span>", nil],
|
|
35
35
|
}.freeze
|
|
36
36
|
|
|
37
|
+
# The closing markers that a bare URL in front of them takes into
|
|
38
|
+
# its link: markdown-it's linkify goes on over `~~` and over a
|
|
39
|
+
# BBCode tag. It stops at a `*`.
|
|
40
|
+
GLUING_CLOSE = { s: "~~", u: "[/u]" }.freeze
|
|
41
|
+
|
|
37
42
|
# Whitespace and <br> only: nothing a marker could wrap.
|
|
38
43
|
NOTHING_TO_WRAP = /\A(?:[[:space:]]|<br>)*\z/
|
|
39
44
|
# Leading or trailing whitespace, Unicode-aware: CommonMark's
|
|
@@ -43,6 +48,12 @@ module Markbridge
|
|
|
43
48
|
# `<br>[a](u)`, which keeps the label to the text.
|
|
44
49
|
EDGES = /\A((?:[[:space:]]|<br>)*)(.+?)((?:[[:space:]]|<br>)*)\z/m
|
|
45
50
|
BR = "<br>"
|
|
51
|
+
# A line break in front of the content of a BBCode tag. The tag
|
|
52
|
+
# keeps other whitespace inside, but after a line break the
|
|
53
|
+
# content starts a line, and a `[spoiler]` there begins a block
|
|
54
|
+
# that leaves the tag around it as text. So the break moves out,
|
|
55
|
+
# and the line starts with the tag.
|
|
56
|
+
LEADING_BREAK = /\A(?:[[:blank:]]|<br>)*(?:\n|<br>)/
|
|
46
57
|
|
|
47
58
|
# Text after a bare URL glues to it, unless it starts with
|
|
48
59
|
# whitespace or with sentence punctuation that is followed by
|
|
@@ -78,6 +89,7 @@ module Markbridge
|
|
|
78
89
|
# Spaces and tabs at the start of a piece. Not a no-break space:
|
|
79
90
|
# the escaper writes one where it wants indentation kept.
|
|
80
91
|
LEADING_BLANKS = /\A[ \t]+/
|
|
92
|
+
ONLY_BLANKS = /\A[ \t]+\z/
|
|
81
93
|
NEWLINE = 10
|
|
82
94
|
SPACE = 32
|
|
83
95
|
TAB = 9
|
|
@@ -106,6 +118,7 @@ module Markbridge
|
|
|
106
118
|
:BR,
|
|
107
119
|
:GT,
|
|
108
120
|
:LEADING_BLANKS,
|
|
121
|
+
:ONLY_BLANKS,
|
|
109
122
|
:TRAILING_BREAKS,
|
|
110
123
|
:NEWLINE,
|
|
111
124
|
:SPACE,
|
|
@@ -158,10 +171,14 @@ module Markbridge
|
|
|
158
171
|
# @param children [Array] the children
|
|
159
172
|
# @param html_mode [Boolean]
|
|
160
173
|
# @param before [Proc, nil] called with the buffer and the child
|
|
174
|
+
# @param following [String, nil] what the caller writes right
|
|
175
|
+
# after the result, a closing BBCode tag for example. A bare
|
|
176
|
+
# URL at the end is written as a link then, because the parser
|
|
177
|
+
# would take the tag into the URL.
|
|
161
178
|
# @yieldparam child [Object]
|
|
162
179
|
# @yieldreturn [String, Array<Array>] the child's output
|
|
163
180
|
# @return [String]
|
|
164
|
-
def write_groups(children, html_mode: false, before: nil)
|
|
181
|
+
def write_groups(children, html_mode: false, before: nil, following: nil)
|
|
165
182
|
buffer = +""
|
|
166
183
|
# A bare URL at the end of an output waits for the first piece
|
|
167
184
|
# of the next output that has one.
|
|
@@ -191,7 +208,7 @@ module Markbridge
|
|
|
191
208
|
write_tokens(buffer, output, 0, output.size, html_mode, pending, before, child)
|
|
192
209
|
end
|
|
193
210
|
end
|
|
194
|
-
join_like(buffer, bare_url_piece(pending, buffer,
|
|
211
|
+
join_like(buffer, bare_url_piece(pending, buffer, following)) if pending
|
|
195
212
|
drop_trailing_breaks(buffer, html_mode)
|
|
196
213
|
end
|
|
197
214
|
|
|
@@ -252,7 +269,14 @@ module Markbridge
|
|
|
252
269
|
close =
|
|
253
270
|
matching_close(tokens, index - 1, to, Tokens::OPEN, Tokens::CLOSE, value)
|
|
254
271
|
content =
|
|
255
|
-
run(
|
|
272
|
+
run(
|
|
273
|
+
tokens,
|
|
274
|
+
index,
|
|
275
|
+
close,
|
|
276
|
+
html_mode,
|
|
277
|
+
trailing_blank_lines(buffer, blank),
|
|
278
|
+
GLUING_CLOSE[value],
|
|
279
|
+
)
|
|
256
280
|
# A break at the end of the content moves out of the
|
|
257
281
|
# markers with its newline.
|
|
258
282
|
moved_break = after_break?(content)
|
|
@@ -345,13 +369,23 @@ module Markbridge
|
|
|
345
369
|
|
|
346
370
|
"<br>#{piece.byteslice(1, piece.bytesize - 1)}"
|
|
347
371
|
else
|
|
348
|
-
|
|
349
|
-
return
|
|
372
|
+
return piece unless first == SPACE || first == TAB
|
|
373
|
+
return "" if more_blanks?(buffer, piece)
|
|
374
|
+
return piece unless trailing_blank_lines(buffer, blank) == 2
|
|
350
375
|
|
|
351
376
|
piece.sub(LEADING_BLANKS, "")
|
|
352
377
|
end
|
|
353
378
|
end
|
|
354
379
|
|
|
380
|
+
# Spaces and tabs after a space or a tab. They show nothing, and
|
|
381
|
+
# a buffer that grows by them is read back to its last visible
|
|
382
|
+
# character for every piece: a line of empty elements with a
|
|
383
|
+
# space between them took time with the square of its length.
|
|
384
|
+
def more_blanks?(buffer, piece)
|
|
385
|
+
last = buffer.getbyte(-1)
|
|
386
|
+
(last == SPACE || last == TAB) && piece.match?(ONLY_BLANKS)
|
|
387
|
+
end
|
|
388
|
+
|
|
355
389
|
def after_break?(buffer)
|
|
356
390
|
buffer.equal?(@break_buffer) && buffer.bytesize == @break_end
|
|
357
391
|
end
|
|
@@ -480,14 +514,16 @@ module Markbridge
|
|
|
480
514
|
# its own. Its pieces are joined among themselves; what stands
|
|
481
515
|
# outside the pair is not known here, as a tag that returned a
|
|
482
516
|
# String would not know it either.
|
|
483
|
-
|
|
484
|
-
|
|
517
|
+
# +closing+ is the marker that follows the content when it
|
|
518
|
+
# would glue to a bare URL at the end, see GLUING_CLOSE.
|
|
519
|
+
def run(tokens, from, to, html_mode, blank, closing = nil)
|
|
520
|
+
single_string(tokens, from, to) || joined(tokens, from, to, html_mode, blank, closing)
|
|
485
521
|
end
|
|
486
522
|
|
|
487
|
-
def joined(tokens, from, to, html_mode, blank)
|
|
523
|
+
def joined(tokens, from, to, html_mode, blank, closing)
|
|
488
524
|
buffer = +""
|
|
489
525
|
pending = write_tokens(buffer, tokens, from, to, html_mode, nil, blank:)
|
|
490
|
-
join_like(buffer, bare_url_piece(pending, buffer,
|
|
526
|
+
join_like(buffer, bare_url_piece(pending, buffer, closing)) if pending
|
|
491
527
|
buffer
|
|
492
528
|
end
|
|
493
529
|
|
|
@@ -559,7 +595,7 @@ module Markbridge
|
|
|
559
595
|
open, close, html_open, html_close, char = delimiter
|
|
560
596
|
return content if nothing_to_wrap?(content)
|
|
561
597
|
|
|
562
|
-
if char
|
|
598
|
+
if char ? edge_whitespace?(content) : content.match?(LEADING_BREAK)
|
|
563
599
|
match = content.match(EDGES)
|
|
564
600
|
lead = match[1]
|
|
565
601
|
core = match[2]
|
|
@@ -573,6 +609,8 @@ module Markbridge
|
|
|
573
609
|
"#{lead}#{html_open}#{core}#{html_close}#{trail}"
|
|
574
610
|
else
|
|
575
611
|
core = escape_colon(core) if open.start_with?("[")
|
|
612
|
+
# `~~` and `[/u]` glue to a URL in front of them, a `*` does not.
|
|
613
|
+
core = Joins.close_url(core) unless char == ASTERISK
|
|
576
614
|
"#{lead}#{open}#{keep_backslash(core)}#{close}#{trail}"
|
|
577
615
|
end
|
|
578
616
|
end
|
|
@@ -592,6 +630,11 @@ module Markbridge
|
|
|
592
630
|
|
|
593
631
|
at_start = core.start_with?(char)
|
|
594
632
|
at_end = core.end_with?(char) && !Joins.escaped_end?(core)
|
|
633
|
+
# A pair that is nested at an edge explains an asterisk there
|
|
634
|
+
# and no tilde: the pair may be bold with nothing in it, in
|
|
635
|
+
# front of a text that starts with `~`.
|
|
636
|
+
return at_start || at_end unless char == ASTERISK
|
|
637
|
+
|
|
595
638
|
(at_start && !pair_start) || (at_end && !pair_end) || (at_start && at_end && !spanning)
|
|
596
639
|
end
|
|
597
640
|
|
|
@@ -692,14 +735,15 @@ module Markbridge
|
|
|
692
735
|
content.count(" ") < content.length
|
|
693
736
|
end
|
|
694
737
|
|
|
695
|
-
# `
|
|
696
|
-
#
|
|
738
|
+
# `` or ``. Discourse reads a
|
|
739
|
+
# size only as both numbers: a width alone would stay in the alt
|
|
740
|
+
# text as `|100`.
|
|
697
741
|
def image_piece(token, html_mode)
|
|
698
742
|
_, src, alt, width, height = token
|
|
699
743
|
alt = flat_alt(alt)
|
|
700
744
|
return image_html(src, alt, width, height) if html_mode
|
|
701
745
|
|
|
702
|
-
size =
|
|
746
|
+
size = "#{width}x#{height}" if width && height
|
|
703
747
|
label = @escaper.escape(alt, in_link_label: true)
|
|
704
748
|
"})"
|
|
705
749
|
end
|
|
@@ -103,6 +103,12 @@ module Markbridge
|
|
|
103
103
|
AUTOLINK =
|
|
104
104
|
%r{\A<(?:https?://|mailto:)[^>\s]*>|\A<[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*>}i
|
|
105
105
|
|
|
106
|
+
# The start of an autolink that the text does not close: `<`, a
|
|
107
|
+
# scheme and no whitespace up to the end of the text. Whatever
|
|
108
|
+
# comes next can close it, a link for example, and then the two
|
|
109
|
+
# are one autolink to a Markdown parser, backslashes included.
|
|
110
|
+
OPEN_AUTOLINK = /\A<[a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^\s<>]*\z/
|
|
111
|
+
|
|
106
112
|
# Match HTML-like constructs that need escaping:
|
|
107
113
|
# - Processing instructions: <?php, <?xml, etc.
|
|
108
114
|
# - SGML declarations: <!DOCTYPE, <!ELEMENT, <![CDATA[, <!--, etc.
|
|
@@ -568,7 +574,8 @@ module Markbridge
|
|
|
568
574
|
matched = match[0]
|
|
569
575
|
@inline_result << matched
|
|
570
576
|
pos + matched.bytesize
|
|
571
|
-
elsif HTML_TAG.match?(remaining) || HTML_TAG_START.match?(remaining)
|
|
577
|
+
elsif HTML_TAG.match?(remaining) || HTML_TAG_START.match?(remaining) ||
|
|
578
|
+
OPEN_AUTOLINK.match?(remaining)
|
|
572
579
|
# The backslash keeps the tag text. What is inside the tag
|
|
573
580
|
# goes on through the loop like any text: a backslash, a
|
|
574
581
|
# backtick or an entity in an attribute needs its escape too.
|
|
@@ -79,7 +79,7 @@ module Markbridge
|
|
|
79
79
|
# it reads as a link reference definition, `[color=red]:x`,
|
|
80
80
|
# and cooks to nothing; the backslash keeps the colon text.
|
|
81
81
|
core = "\\#{core}" if open.start_with?("[") && core.start_with?(":")
|
|
82
|
-
"#{lead}#{open}#{core}#{close}#{trail}"
|
|
82
|
+
"#{lead}#{open}#{Inline::Joins.close_url(core)}#{close}#{trail}"
|
|
83
83
|
end
|
|
84
84
|
end
|
|
85
85
|
end
|
|
@@ -55,6 +55,18 @@ module Markbridge
|
|
|
55
55
|
@strip_trailing_invisibles = strip_trailing_invisibles
|
|
56
56
|
end
|
|
57
57
|
|
|
58
|
+
# The cleanup of {#call} without the trim at the ends: runs of
|
|
59
|
+
# blank lines become one blank line, outside of code fences. For a
|
|
60
|
+
# tag that writes something in front of every line of its content,
|
|
61
|
+
# like the `>` of a quote. After that a blank line is no blank
|
|
62
|
+
# line anymore, and {#call} cannot collapse the run.
|
|
63
|
+
#
|
|
64
|
+
# @param text [String]
|
|
65
|
+
# @return [String]
|
|
66
|
+
def collapse_blank_lines(text)
|
|
67
|
+
clean_document(text)
|
|
68
|
+
end
|
|
69
|
+
|
|
58
70
|
# @param text [String]
|
|
59
71
|
# @return [String]
|
|
60
72
|
def call(text)
|