2016-04-27 21:38:33 +00:00
|
|
|
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-04-27 21:38:33 +00:00
|
|
|
# ```
|
2016-07-10 19:47:53 +00:00
|
|
|
|
|
|
|
^```
|
|
|
|
.+?
|
|
|
|
\n```$
|
2016-04-27 21:38:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
(?<html>
|
2016-07-10 19:47:53 +00:00
|
|
|
# HTML block:
|
2016-04-27 21:38:33 +00:00
|
|
|
# <tag>
|
2016-07-10 19:47:53 +00:00
|
|
|
# Anything, including `>>>` blocks which are ignored by this filter
|
2016-04-27 21:38:33 +00:00
|
|
|
# </tag>
|
2016-07-10 19:47:53 +00:00
|
|
|
|
|
|
|
^<[^>]+?>\n
|
|
|
|
.+?
|
|
|
|
\n<\/[^>]+?>$
|
2016-04-27 21:38:33 +00:00
|
|
|
)
|
|
|
|
|
|
2016-07-10 19:47:53 +00:00
|
|
|
(?:
|
|
|
|
# Blockquote:
|
|
|
|
# >>>
|
|
|
|
# Anything, including code and HTML blocks
|
|
|
|
# >>>
|
|
|
|
|
|
|
|
^>>>\n
|
|
|
|
(?<quote>
|
2016-04-27 21:38:33 +00:00
|
|
|
(?:
|
2016-07-10 19:47:53 +00:00
|
|
|
# Any character that doesn't introduce a code or HTML block
|
|
|
|
(?!
|
|
|
|
^```
|
|
|
|
|
|
|
|
|
^<[^>]+?>\n
|
|
|
|
)
|
|
|
|
.
|
2016-04-27 21:38:33 +00:00
|
|
|
|
|
2016-07-10 19:47:53 +00:00
|
|
|
# A code block
|
2016-04-27 21:38:33 +00:00
|
|
|
\g<code>
|
|
|
|
|
|
2016-07-10 19:47:53 +00:00
|
|
|
# An HTML block
|
2016-04-27 21:38:33 +00:00
|
|
|
\g<html>
|
2016-07-10 19:47:53 +00:00
|
|
|
)+?
|
|
|
|
)
|
|
|
|
\n>>>$
|
2016-04-27 21:38:33 +00:00
|
|
|
)
|
|
|
|
}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")
|
2016-04-27 21:38:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
@text.gsub(REGEX) do
|
|
|
|
if $~[:quote]
|
|
|
|
$~[:quote].gsub(/^/, "> ").gsub(/^> $/, ">")
|
|
|
|
else
|
|
|
|
$~[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|