debian-mirror-gitlab/lib/banzai/filter/blockquote_fence_filter.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
module Banzai
module Filter
class BlockquoteFenceFilter < HTML::Pipeline::TextFilter
REGEX = %r{
2018-11-18 11:00:15 +05:30
#{::Gitlab::Regex.markdown_code_or_html_blocks}
2016-08-24 12:49:21 +05:30
|
(?:
# Blockquote:
# >>>
# Anything, including code and HTML blocks
# >>>
2018-11-08 19:23:39 +05:30
^>>>\ *\n
2016-08-24 12:49:21 +05:30
(?<quote>
(?:
# Any character that doesn't introduce a code or HTML block
(?!
^```
|
2018-11-08 19:23:39 +05:30
^<[^>]+?>\ *\n
2016-08-24 12:49:21 +05:30
)
.
|
# A code block
\g<code>
|
# An HTML block
\g<html>
)+?
)
2018-11-08 19:23:39 +05:30
\n>>>\ *$
2016-08-24 12:49:21 +05:30
)
}mx.freeze
def initialize(text, context = nil, result = nil)
super text, context, result
@text = @text.delete("\r")
end
def call
@text.gsub(REGEX) do
if $~[:quote]
2019-07-07 11:18:12 +05:30
# keep the same number of source lines/positions by replacing the
# fence lines with newlines
"\n" + $~[:quote].gsub(/^/, "> ").gsub(/^> $/, ">") + "\n"
2016-08-24 12:49:21 +05:30
else
$~[0]
end
end
end
end
end
end