Class: Banzai::Filter::BlockquoteFenceLegacyFilter

Inherits:
HTML::Pipeline::TextFilter
  • Object
show all
Defined in:
lib/banzai/filter/blockquote_fence_legacy_filter.rb

Constant Summary collapse

MARKDOWN_CODE_BLOCK_REGEX =
%r{
  (?<code>
    # Code blocks:
    # ```
    # Anything, including `>>>` blocks which are ignored by this filter
    # ```

    ^```
    .+?
    \n```\ *$
  )
}mx
MARKDOWN_HTML_BLOCK_REGEX =
%r{
  (?<html>
    # HTML block:
    # <tag>
    # Anything, including `>>>` blocks which are ignored by this filter
    # </tag>

    ^<[^>]+?>\ *\n
    .+?
    \n</[^>]+?>\ *$
  )
}mx
MARKDOWN_CODE_OR_HTML_BLOCKS =
%r{
    #{MARKDOWN_CODE_BLOCK_REGEX}
  |
    #{MARKDOWN_HTML_BLOCK_REGEX}
}mx
REGEX =
%r{
    #{MARKDOWN_CODE_OR_HTML_BLOCKS}
  |
    (?=(?<=^\n|\A)\ *>>>\ *\n.*\n\ *>>>\ *(?=\n$|\z))(?:
      # Blockquote:
      # >>>
      # Anything, including code and HTML blocks
      # >>>

      (?<=^\n|\A)(?<indent>\ *)>>>\ *\n
      (?<blockquote>
        (?:
            # Any character that doesn't introduce a code or HTML block
            (?!
                ^```
              |
                ^<[^>]+?>\ *\n
            )
            .
          |
            # A code block
            \g<code>
          |
            # An HTML block
            \g<html>
        )+?
      )
      \n\ *>>>\ *(?=\n$|\z)
    )
}mx

Instance Method Summary collapse

Constructor Details

#initialize(text, context = nil, result = nil) ⇒ BlockquoteFenceLegacyFilter

Returns a new instance of BlockquoteFenceLegacyFilter.



73
74
75
# File 'lib/banzai/filter/blockquote_fence_legacy_filter.rb', line 73

def initialize(text, context = nil, result = nil)
  super text, context, result
end

Instance Method Details

#callObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/banzai/filter/blockquote_fence_legacy_filter.rb', line 77

def call
  return @text if MarkdownFilter.glfm_markdown?(context)

  @text.gsub(REGEX) do
    if $~[:blockquote]
      # keep the same number of source lines/positions by replacing the
      # fence lines with newlines
      indent = $~[:indent]
      "\n#{$~[:blockquote].gsub(/^#{Regexp.quote(indent)}/, "#{indent}> ").gsub(/^> $/, '>')}\n"
    else
      $~[0]
    end
  end
end