aboutsummaryrefslogtreecommitdiffstats
path: root/guides/rails_guides
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikachu.com>2012-08-31 12:01:06 -0400
committerPrem Sichanugrist <s@sikac.hu>2012-09-17 15:54:21 -0400
commit544f6bcb9095cfc826f892e6bfafee6a9bafa494 (patch)
tree39fd19a2097cd42bbc693707797b60e08fc5cbf8 /guides/rails_guides
parentfd9867c7dea8f030bd712a2e407de25c43b6c400 (diff)
downloadrails-544f6bcb9095cfc826f892e6bfafee6a9bafa494.tar.gz
rails-544f6bcb9095cfc826f892e6bfafee6a9bafa494.tar.bz2
rails-544f6bcb9095cfc826f892e6bfafee6a9bafa494.zip
Start rewriting 4.0 release note into Markdown
Diffstat (limited to 'guides/rails_guides')
-rw-r--r--guides/rails_guides/generator.rb11
-rw-r--r--guides/rails_guides/markdown.rb42
-rw-r--r--guides/rails_guides/markdown/renderer.rb48
3 files changed, 98 insertions, 3 deletions
diff --git a/guides/rails_guides/generator.rb b/guides/rails_guides/generator.rb
index 230bebf3bb..ee97c37fac 100644
--- a/guides/rails_guides/generator.rb
+++ b/guides/rails_guides/generator.rb
@@ -65,7 +65,7 @@ module RailsGuides
class Generator
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :warnings, :all
- GUIDES_RE = /\.(?:textile|erb)$/
+ GUIDES_RE = /\.(?:textile|erb|md|markdown)$/
def initialize(output=nil)
set_flags_from_environment
@@ -171,8 +171,8 @@ module RailsGuides
end
def output_file_for(guide)
- if guide =~/\.textile$/
- guide.sub(/\.textile$/, '.html')
+ if guide =~ /\.(textile|markdown|md)$/
+ guide.sub(/\.(textile|markdown|md)$/, '.html')
else
guide.sub(/\.erb$/, '')
end
@@ -201,6 +201,11 @@ module RailsGuides
# Generate the special pages like the home.
# Passing a template handler in the template name is deprecated. So pass the file name without the extension.
result = view.render(:layout => layout, :formats => [$1], :file => $`)
+ elsif guide =~ /\.(md|markdown)$/
+ body = File.read(File.join(source_dir, guide))
+ result = RailsGuides::Markdown.new(view, layout).render(body)
+
+ warn_about_broken_links(result) if @warnings
else
body = File.read(File.join(source_dir, guide))
body = set_header_section(body, view)
diff --git a/guides/rails_guides/markdown.rb b/guides/rails_guides/markdown.rb
new file mode 100644
index 0000000000..a56d0d2d85
--- /dev/null
+++ b/guides/rails_guides/markdown.rb
@@ -0,0 +1,42 @@
+require 'redcarpet'
+require 'nokogiri'
+require 'rails_guides/markdown/renderer'
+
+module RailsGuides
+ class Markdown
+ def initialize(view, layout)
+ @view = view
+ @layout = layout
+ end
+
+ def render(body)
+ @header, _, @body = body.partition(/^\-{40,}$/)
+ render_header
+ render_body
+ end
+
+ private
+ def engine
+ @engine ||= Redcarpet::Markdown.new(Renderer, {
+ no_intra_emphasis: true,
+ fenced_code_blocks: true,
+ autolink: true,
+ strikethrough: true,
+ superscript: true
+ })
+ end
+
+ def render_header
+ header_content = engine.render(@header)
+ @view.content_for(:header_section) { header_content.html_safe }
+
+ @view.content_for(:page_title) do
+ "Ruby on Rails Guides: #{Nokogiri::HTML(header_content).at(:h2).text}".html_safe
+ end
+ end
+
+ def render_body
+ @view.render(:layout => @layout, :text => engine.render(@body))
+ end
+ end
+end
diff --git a/guides/rails_guides/markdown/renderer.rb b/guides/rails_guides/markdown/renderer.rb
new file mode 100644
index 0000000000..ea3b7fc044
--- /dev/null
+++ b/guides/rails_guides/markdown/renderer.rb
@@ -0,0 +1,48 @@
+module RailsGuides
+ class Markdown
+ class Renderer < Redcarpet::Render::HTML
+ def initialize(options={})
+ super
+ end
+
+ def header(text, header_level)
+ # Always increase the heading level by, so we can use h1, h2 heading in the document
+ header_level += 1
+
+ %(<h#{header_level} id="#{dom_id(text)}">#{text}</h#{header_level}>)
+ end
+
+ def preprocess(full_document)
+ convert_notes(full_document)
+ end
+
+ private
+
+ 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,
+ # if a bulleted list follows the first item is not rendered
+ # as a list item, but as a paragraph starting with a plain
+ # asterisk.
+ body.gsub(/^(TIP|IMPORTANT|CAUTION|WARNING|NOTE|INFO|TODO)[.:](.*?)(\n(?=\n)|\Z)/m) do |m|
+ css_class = case $1
+ when 'CAUTION', 'IMPORTANT'
+ 'warning'
+ when 'TIP'
+ 'info'
+ else
+ $1.downcase
+ end
+ %Q(<div class="#{css_class}"><p>#{$2.strip}</p></div>\n)
+ end
+ end
+
+ def dom_id(text)
+ text.downcase.gsub(/[^a-z0-9]+/, '-').strip
+ end
+ end
+ end
+end