gitlab-org--gitlab-foss/lib/banzai/filter/blockquote_fence_filter.rb

72 lines
1.6 KiB
Ruby
Raw Normal View History

module Banzai
module Filter
class BlockquoteFenceFilter < HTML::Pipeline::TextFilter
REGEX = %r{
(?<code>
# Code blocks:
# ```
2016-07-10 19:47:53 +00:00
# Anything, including `>>>` blocks which are ignored by this filter
# ```
2016-07-10 19:47:53 +00:00
^```
.+?
\n```\ *$
)
|
(?<html>
2016-07-10 19:47:53 +00:00
# HTML block:
# <tag>
2016-07-10 19:47:53 +00:00
# Anything, including `>>>` blocks which are ignored by this filter
# </tag>
2016-07-10 19:47:53 +00:00
^<[^>]+?>\ *\n
2016-07-10 19:47:53 +00:00
.+?
\n<\/[^>]+?>\ *$
)
|
2016-07-10 19:47:53 +00:00
(?:
# Blockquote:
# >>>
# Anything, including code and HTML blocks
# >>>
^>>>\ *\n
2016-07-10 19:47:53 +00:00
(?<quote>
(?:
2016-07-10 19:47:53 +00:00
# Any character that doesn't introduce a code or HTML block
(?!
^```
|
^<[^>]+?>\ *\n
2016-07-10 19:47:53 +00:00
)
.
|
2016-07-10 19:47:53 +00:00
# A code block
\g<code>
|
2016-07-10 19:47:53 +00:00
# An HTML block
\g<html>
2016-07-10 19:47:53 +00:00
)+?
)
\n>>>\ *$
)
}mx.freeze
def initialize(text, context = nil, result = nil)
super text, context, result
2016-07-10 19:47:53 +00:00
@text = @text.delete("\r")
end
def call
@text.gsub(REGEX) do
if $~[:quote]
$~[:quote].gsub(/^/, "> ").gsub(/^> $/, ">")
else
$~[0]
end
end
end
end
end
end