Start rewriting 4.0 release note into Markdown

This commit is contained in:
Prem Sichanugrist 2012-08-31 12:01:06 -04:00 committed by Prem Sichanugrist
parent fd9867c7de
commit 544f6bcb90
8 changed files with 658 additions and 529 deletions

View File

@ -36,6 +36,7 @@ group :doc do
# this is our own fork with the fix.
gem 'sdoc', github: 'fxn/sdoc'
gem 'RedCloth', '~> 4.2'
gem 'redcarpet', '~> 2.1.1'
gem 'w3c_validators'
end

View File

@ -45,7 +45,7 @@ Some arguments may be passed via environment variables:
ONLY=name
Useful if you want to generate only one or a set of guides.
Generate only association_basics.html:
ONLY=assoc

View File

@ -393,7 +393,14 @@ div.code_container {
margin: 0.25em 0 1.5em 0;
}
.note tt, .info tt {border:none; background: none; padding: 0;}
#mainCol div.todo {
background: #fff9d8 url(../images/tab_yellow.gif) no-repeat left top;
border: none;
padding: 1em 1em 0.25em 48px;
margin: 0.25em 0 1.5em 0;
}
.note tt, .info tt, .todo tt {border:none; background: none; padding: 0;}
#mainCol ul li {
list-style:none;

View File

@ -36,10 +36,11 @@ to the Gemfile, run
and try again.
ERROR
exit 1
end
require 'rails_guides/markdown'
require "rails_guides/textile_extensions"
RedCloth.send(:include, RailsGuides::TextileExtensions)

View File

@ -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)

View File

@ -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

View File

@ -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

File diff suppressed because it is too large Load Diff