2012-04-17 17:02:21 -04:00
|
|
|
class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
|
2012-07-23 19:45:08 -04:00
|
|
|
|
|
|
|
attr_reader :template
|
|
|
|
alias_method :h, :template
|
|
|
|
|
2015-02-27 20:17:57 -05:00
|
|
|
def initialize(template, color_scheme, options = {})
|
2012-07-23 19:45:08 -04:00
|
|
|
@template = template
|
2015-02-27 20:17:57 -05:00
|
|
|
@color_scheme = color_scheme
|
2012-07-23 19:45:08 -04:00
|
|
|
@project = @template.instance_variable_get("@project")
|
2014-02-04 02:48:33 -05:00
|
|
|
@options = options.dup
|
2012-07-23 19:45:08 -04:00
|
|
|
super options
|
|
|
|
end
|
|
|
|
|
2015-03-10 08:32:28 -04:00
|
|
|
def preprocess(full_document)
|
|
|
|
# Redcarpet doesn't allow SMB links when `safe_links_only` is enabled.
|
|
|
|
# FTP links are allowed, so we trick Redcarpet.
|
|
|
|
full_document.gsub("smb://", "ftp://smb:")
|
|
|
|
end
|
|
|
|
|
2014-10-10 02:32:05 -04:00
|
|
|
# If project has issue number 39, apostrophe will be linked in
|
|
|
|
# regular text to the issue as Redcarpet will convert apostrophe to
|
|
|
|
# #39;
|
|
|
|
# We replace apostrophe with right single quote before Redcarpet
|
|
|
|
# does the processing and put the apostrophe back in postprocessing.
|
|
|
|
# This only influences regular text, code blocks are untouched.
|
2014-10-09 08:20:49 -04:00
|
|
|
def normal_text(text)
|
|
|
|
return text unless text.present?
|
|
|
|
text.gsub("'", "’")
|
|
|
|
end
|
|
|
|
|
2014-12-03 09:27:31 -05:00
|
|
|
# Stolen from Rugments::Plugins::Redcarpet as this module is not required
|
|
|
|
# from Rugments's gem root.
|
2012-04-17 17:02:21 -04:00
|
|
|
def block_code(code, language)
|
2014-12-03 09:27:31 -05:00
|
|
|
lexer = Rugments::Lexer.find_fancy(language, code) || Rugments::Lexers::PlainText
|
2012-12-22 07:18:40 -05:00
|
|
|
|
2014-12-03 09:27:31 -05:00
|
|
|
# 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'
|
|
|
|
code.gsub! /^ /, "\t"
|
|
|
|
end
|
2012-12-22 07:18:40 -05:00
|
|
|
|
2014-12-03 09:27:31 -05:00
|
|
|
formatter = Rugments::Formatters::HTML.new(
|
2015-02-27 20:17:57 -05:00
|
|
|
cssclass: "code highlight #{@color_scheme} #{lexer.tag}"
|
2014-12-03 09:27:31 -05:00
|
|
|
)
|
|
|
|
formatter.format(lexer.lex(code))
|
2012-04-17 17:02:21 -04:00
|
|
|
end
|
2012-08-01 20:29:15 -04:00
|
|
|
|
2013-01-16 16:37:39 -05:00
|
|
|
def link(link, title, content)
|
|
|
|
h.link_to_gfm(content, link, title: title)
|
|
|
|
end
|
|
|
|
|
2014-02-04 02:48:33 -05:00
|
|
|
def header(text, level)
|
|
|
|
if @options[:no_header_anchors]
|
|
|
|
"<h#{level}>#{text}</h#{level}>"
|
|
|
|
else
|
|
|
|
id = ActionController::Base.helpers.strip_tags(h.gfm(text)).downcase() \
|
|
|
|
.gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-/, '').gsub(/-$/, '')
|
|
|
|
"<h#{level} id=\"#{id}\">#{text}<a href=\"\##{id}\"></a></h#{level}>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-01 20:29:15 -04:00
|
|
|
def postprocess(full_document)
|
2015-03-10 08:32:28 -04:00
|
|
|
full_document.gsub!("ftp://smb:", "smb://")
|
|
|
|
|
2014-10-10 02:24:58 -04:00
|
|
|
full_document.gsub!("’", "'")
|
2014-05-23 06:58:34 -04:00
|
|
|
unless @template.instance_variable_get("@project_wiki") || @project.nil?
|
|
|
|
full_document = h.create_relative_links(full_document)
|
|
|
|
end
|
Fix invalid Atom feeds when using emoji, horizontal rules, or images.
Fixes issues #880, #723, #1113: Markdown must be rendered to XHTML, not HTML, when generating summary content for Atom feeds. Otherwise, content-less tags like <img> and <hr>, generated when issue descriptions, merge request descriptions, comments, or commit messages use emoji, horizontal rules, or images, are not terminated and make the Atom XML invalid.
2015-02-21 16:12:13 -05:00
|
|
|
h.gfm_with_options(full_document, @options)
|
2012-08-01 20:29:15 -04:00
|
|
|
end
|
2012-06-08 04:23:31 -04:00
|
|
|
end
|