debian-mirror-gitlab/lib/redcarpet/render/gitlab_html.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

2015-09-11 14:41:01 +05:30
require 'active_support/core_ext/string/output_safety'
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
2014-09-02 18:07:02 +05:30
attr_reader :template
alias_method :h, :template
2015-04-26 12:48:37 +05:30
def initialize(template, color_scheme, options = {})
2014-09-02 18:07:02 +05:30
@template = template
2015-04-26 12:48:37 +05:30
@color_scheme = color_scheme
2014-09-02 18:07:02 +05:30
@options = options.dup
2015-09-11 14:41:01 +05:30
@options.reverse_merge!(
# Handled further down the line by Gitlab::Markdown::SanitizationFilter
escape_html: false,
project: @template.instance_variable_get("@project")
)
super(options)
2015-04-26 12:48:37 +05:30
end
def normal_text(text)
2015-09-11 14:41:01 +05:30
ERB::Util.html_escape_once(text)
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
# Stolen from Rouge::Plugins::Redcarpet as this module is not required
# from Rouge's gem root.
2014-09-02 18:07:02 +05:30
def block_code(code, language)
2015-09-11 14:41:01 +05:30
lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText
2015-04-26 12:48:37 +05:30
# XXX HACK: Redcarpet strips hard tabs out of code blocks,
# so we assume you're not using leading spaces that aren't tabs,
# and just replace them here.
if lexer.tag == 'make'
2015-09-11 14:41:01 +05:30
code.gsub!(/^ /, "\t")
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
formatter = Rouge::Formatters::HTMLGitlab.new(
2015-04-26 12:48:37 +05:30
cssclass: "code highlight #{@color_scheme} #{lexer.tag}"
)
formatter.format(lexer.lex(code))
2014-09-02 18:07:02 +05:30
end
def postprocess(full_document)
2015-09-11 14:41:01 +05:30
h.gfm(full_document, @options)
2014-09-02 18:07:02 +05:30
end
end