# frozen_string_literal: true class BooruMarkdown < Redcarpet::Render::HTML PATTERNS = [ [/\[spoiler\](.*?)\[\/spoiler\]/m, '\1'].freeze, [/\[bq\](.*?)\[\/bq\]/m, '
\1
'].freeze, [/\[bq="(.*?)"\](.*?)\[\/bq\]/m, '
\2
'].freeze, [/<\/p>\W+

/m, '


'] ].freeze IMAGE_ON_SITE_LINK_PATTERN = />>((?:\d+)(?:[pts]?))/x MEME_ARROW_PATTERN = /^>(\S)/ def initialize #noinspection RubyArgCount super(escape_html: true, safe_links_only: true, hard_wrap: true) end def highlight(text) "#{text}" end def image(link, alt_text, title) "Posted image" end def block_code(code, language) if ['ruby', 'html', 'json', 'js'].include? language "

#{CGI.escapeHTML(code)}
" else "
#{CGI.escapeHTML(code)}
" end end def preprocess(markdown) markdown = markdown.gsub(IMAGE_ON_SITE_LINK_PATTERN) do "\\>\\>#{$1}" # Escape on-site links in the input to the markdown parser. This is a hack. end markdown.gsub(MEME_ARROW_PATTERN) do "\\>#{$1}" end end def postprocess(markup) PATTERNS.each do |pattern, replacement| markup = markup.gsub(pattern, replacement) end markup end def self.instance #noinspection RubyArgCount @@markdown ||= Redcarpet::Markdown.new(BooruMarkdown.new, fenced_code_blocks: true, autolink: true, disable_indented_code_blocks: true, strikethrough: true, superscript: true, underline: true, highlight: true, space_after_headers: true) end end