2017-08-13 09:02:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-08-31 12:01:06 -04:00
|
|
|
module RailsGuides
|
|
|
|
class Markdown
|
|
|
|
class Renderer < Redcarpet::Render::HTML
|
2017-02-12 04:21:20 -05:00
|
|
|
cattr_accessor :edge, :version
|
2012-08-31 12:01:06 -04:00
|
|
|
|
2012-08-31 17:49:17 -04:00
|
|
|
def block_code(code, language)
|
|
|
|
<<-HTML
|
|
|
|
<div class="code_container">
|
|
|
|
<pre class="brush: #{brush_for(language)}; gutter: false; toolbar: false">
|
2012-11-05 17:24:22 -05:00
|
|
|
#{ERB::Util.h(code)}
|
2012-08-31 17:49:17 -04:00
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
2016-11-30 13:27:35 -05:00
|
|
|
def link(url, title, content)
|
2017-02-09 21:13:41 -05:00
|
|
|
if url.start_with?("http://api.rubyonrails.org")
|
2016-11-30 13:27:35 -05:00
|
|
|
%(<a href="#{api_link(url)}">#{content}</a>)
|
|
|
|
elsif title
|
|
|
|
%(<a href="#{url}" title="#{title}">#{content}</a>)
|
|
|
|
else
|
|
|
|
%(<a href="#{url}">#{content}</a>)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-13 17:30:33 -04:00
|
|
|
def header(text, header_level)
|
2015-12-21 16:36:35 -05:00
|
|
|
# Always increase the heading level by 1, so we can use h1, h2 heading in the document
|
2012-08-31 12:01:06 -04:00
|
|
|
header_level += 1
|
|
|
|
|
2018-09-23 06:57:36 -04:00
|
|
|
header_with_id = text.scan(/(.*){#(.*)}/)
|
|
|
|
unless header_with_id.empty?
|
2018-10-04 17:57:32 -04:00
|
|
|
%(<h#{header_level} id="#{header_with_id[0][1].strip}">#{header_with_id[0][0].strip}</h#{header_level}>)
|
2018-09-23 06:57:36 -04:00
|
|
|
else
|
|
|
|
%(<h#{header_level}>#{text}</h#{header_level}>)
|
|
|
|
end
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
|
2012-09-02 14:53:01 -04:00
|
|
|
def paragraph(text)
|
2017-01-29 07:16:10 -05:00
|
|
|
if text =~ %r{^NOTE:\s+Defined\s+in\s+<code>(.*?)</code>\.?$}
|
|
|
|
%(<div class="note"><p>Defined in <code><a href="#{github_file_url($1)}">#{$1}</a></code>.</p></div>)
|
2018-07-28 17:37:17 -04:00
|
|
|
elsif /^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:]/.match?(text)
|
2012-09-02 14:53:01 -04:00
|
|
|
convert_notes(text)
|
2016-08-06 13:21:59 -04:00
|
|
|
elsif text.include?("DO NOT READ THIS FILE ON GITHUB")
|
2012-09-17 00:46:26 -04:00
|
|
|
elsif text =~ /^\[<sup>(\d+)\]:<\/sup> (.+)$/
|
|
|
|
linkback = %(<a href="#footnote-#{$1}-ref"><sup>#{$1}</sup></a>)
|
|
|
|
%(<p class="footnote" id="footnote-#{$1}">#{linkback} #{$2}</p>)
|
2012-09-02 14:53:01 -04:00
|
|
|
else
|
2012-09-17 00:46:26 -04:00
|
|
|
text = convert_footnotes(text)
|
2012-09-02 14:53:01 -04:00
|
|
|
"<p>#{text}</p>"
|
|
|
|
end
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-09-17 00:46:26 -04:00
|
|
|
def convert_footnotes(text)
|
|
|
|
text.gsub(/\[<sup>(\d+)\]<\/sup>/i) do
|
|
|
|
%(<sup class="footnote" id="footnote-#{$1}-ref">) +
|
|
|
|
%(<a href="#footnote-#{$1}">#{$1}</a></sup>)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 17:49:17 -04:00
|
|
|
def brush_for(code_type)
|
|
|
|
case code_type
|
2016-08-06 14:20:22 -04:00
|
|
|
when "ruby", "sql", "plain"
|
|
|
|
code_type
|
|
|
|
when "erb", "html+erb"
|
|
|
|
"ruby; html-script: true"
|
|
|
|
when "html"
|
|
|
|
"xml" # HTML is understood, but there are .xml rules in the CSS
|
2016-10-25 01:16:51 -04:00
|
|
|
else
|
2016-08-06 14:20:22 -04:00
|
|
|
"plain"
|
2012-08-31 17:49:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 12:01:06 -04:00
|
|
|
def convert_notes(body)
|
|
|
|
# The following regexp detects special labels followed by a
|
|
|
|
# paragraph, perhaps at the end of the document.
|
|
|
|
#
|
|
|
|
# It is important that we do not eat more than one newline
|
|
|
|
# because formatting may be wrong otherwise. For example,
|
2017-11-28 13:27:43 -05:00
|
|
|
# if a bulleted list follows, the first item is not rendered
|
2012-08-31 12:01:06 -04:00
|
|
|
# as a list item, but as a paragraph starting with a plain
|
|
|
|
# asterisk.
|
2013-04-13 03:49:54 -04:00
|
|
|
body.gsub(/^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:](.*?)(\n(?=\n)|\Z)/m) do
|
2016-09-01 18:43:33 -04:00
|
|
|
css_class = \
|
|
|
|
case $1
|
|
|
|
when "CAUTION", "IMPORTANT"
|
|
|
|
"warning"
|
|
|
|
when "TIP"
|
|
|
|
"info"
|
|
|
|
else
|
|
|
|
$1.downcase
|
|
|
|
end
|
2012-09-02 14:53:01 -04:00
|
|
|
%(<div class="#{css_class}"><p>#{$2.strip}</p></div>)
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-30 13:27:35 -05:00
|
|
|
|
2017-01-29 07:16:10 -05:00
|
|
|
def github_file_url(file_path)
|
2017-02-12 04:21:20 -05:00
|
|
|
tree = version || edge
|
2017-01-29 07:16:10 -05:00
|
|
|
|
2017-05-12 23:54:11 -04:00
|
|
|
root = file_path[%r{(\w+)/}, 1]
|
2017-04-19 20:02:03 -04:00
|
|
|
path = \
|
|
|
|
case root
|
|
|
|
when "abstract_controller", "action_controller", "action_dispatch"
|
|
|
|
"actionpack/lib/#{file_path}"
|
|
|
|
when /\A(action|active)_/
|
|
|
|
"#{root.sub("_", "")}/lib/#{file_path}"
|
|
|
|
else
|
|
|
|
file_path
|
|
|
|
end
|
2017-01-29 07:16:10 -05:00
|
|
|
|
2017-02-12 04:21:20 -05:00
|
|
|
"https://github.com/rails/rails/tree/#{tree}/#{path}"
|
2016-11-30 13:27:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def api_link(url)
|
2018-07-28 17:37:17 -04:00
|
|
|
if %r{http://api\.rubyonrails\.org/v\d+\.}.match?(url)
|
2017-02-12 04:21:20 -05:00
|
|
|
url
|
|
|
|
elsif edge
|
|
|
|
url.sub("api", "edgeapi")
|
|
|
|
else
|
|
|
|
url.sub(/(?<=\.org)/, "/#{version}")
|
2016-11-30 13:27:35 -05:00
|
|
|
end
|
|
|
|
end
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|