提交 efa7717b 编写于 作者: P Prem Sichanugrist 提交者: Prem Sichanugrist

4.0 release note now renders correctly with index

上级 544f6bcb
...@@ -7,15 +7,25 @@ class Markdown ...@@ -7,15 +7,25 @@ class Markdown
def initialize(view, layout) def initialize(view, layout)
@view = view @view = view
@layout = layout @layout = layout
@index_counter = Hash.new(0)
end end
def render(body) def render(body)
@header, _, @body = body.partition(/^\-{40,}$/) @raw_header, _, @raw_body = body.partition(/^\-{40,}$/).map(&:strip)
render_header generate_header
render_body generate_title
generate_body
generate_structure
generate_index
render_page
end end
private private
def dom_id(nodes)
nodes.map{ |node| node.text.downcase.gsub(/[^a-z0-9]+/, '-') }.join('-')
end
def engine def engine
@engine ||= Redcarpet::Markdown.new(Renderer, { @engine ||= Redcarpet::Markdown.new(Renderer, {
no_intra_emphasis: true, no_intra_emphasis: true,
...@@ -26,17 +36,82 @@ def engine ...@@ -26,17 +36,82 @@ def engine
}) })
end end
def render_header def generate_body
header_content = engine.render(@header) @body = engine.render(@raw_body)
@view.content_for(:header_section) { header_content.html_safe } end
def generate_header
@header = engine.render(@raw_header).html_safe
end
def generate_structure
@raw_index = ''
@body = Nokogiri::HTML(@body).tap do |doc|
hierarchy = []
doc.at('body').children.each do |node|
if node.name =~ /^h[3-6]$/
case node.name
when 'h3'
hierarchy = [node]
node[:id] = dom_id(hierarchy)
@raw_index += "1. [#{node.text}](##{node[:id]})\n"
when 'h4'
hierarchy = hierarchy[0, 1] + [node]
node[:id] = dom_id(hierarchy)
@raw_index += " * [#{node.text}](##{node[:id]})\n"
when 'h5'
hierarchy = hierarchy[0, 2] + [node]
node[:id] = dom_id(hierarchy)
when 'h6'
hierarchy = hierarchy[0, 3] + [node]
node[:id] = dom_id(hierarchy)
end
node.inner_html = "#{node_index(hierarchy)} #{node.text}"
end
end
end.to_html
end
def generate_index
@index = Nokogiri::HTML(engine.render(@raw_index)).tap do |doc|
doc.at('ol')[:class] = 'chapters'
end.to_html
@index = <<-INDEX.html_safe
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
#{@index}
</div>
INDEX
end
def generate_title
@title = "Ruby on Rails Guides: #{Nokogiri::HTML(@header).at(:h2).text}".html_safe
end
@view.content_for(:page_title) do def node_index(hierarchy)
"Ruby on Rails Guides: #{Nokogiri::HTML(header_content).at(:h2).text}".html_safe case hierarchy.size
when 1
@index_counter[2] = @index_counter[3] = @index_counter[4] = 0
"#{@index_counter[1] += 1}"
when 2
@index_counter[3] = @index_counter[4] = 0
"#{@index_counter[1]}.#{@index_counter[2] += 1}"
when 3
@index_counter[4] = 0
"#{@index_counter[1]}.#{@index_counter[2]}.#{@index_counter[3] += 1}"
when 4
"#{@index_counter[1]}.#{@index_counter[2]}.#{@index_counter[3]}.#{@index_counter[4] += 1}"
end end
end end
def render_body def render_page
@view.render(:layout => @layout, :text => engine.render(@body)) @view.content_for(:header_section) { @header }
@view.content_for(:page_title) { @title }
@view.content_for(:index_section) { @index.html_safe }
@view.render(:layout => @layout, :text => @body)
end end
end end
end end
...@@ -5,11 +5,23 @@ def initialize(options={}) ...@@ -5,11 +5,23 @@ def initialize(options={})
super super
end end
def block_code(code, language)
<<-HTML
<notextile>
<div class="code_container">
<pre class="brush: #{brush_for(language)}; gutter: false; toolbar: false">
#{ERB::Util.h(code).strip}
</pre>
</div>
</notextile>
HTML
end
def header(text, header_level) def header(text, header_level)
# Always increase the heading level by, so we can use h1, h2 heading in the document # Always increase the heading level by, so we can use h1, h2 heading in the document
header_level += 1 header_level += 1
%(<h#{header_level} id="#{dom_id(text)}">#{text}</h#{header_level}>) %(<h#{header_level}>#{text}</h#{header_level}>)
end end
def preprocess(full_document) def preprocess(full_document)
...@@ -18,6 +30,19 @@ def preprocess(full_document) ...@@ -18,6 +30,19 @@ def preprocess(full_document)
private private
def brush_for(code_type)
case code_type
when 'ruby', 'sql', 'plain'
code_type
when 'erb'
'ruby; html-script: true'
when 'html'
'xml' # html is understood, but there are .xml rules in the CSS
else
'plain'
end
end
def convert_notes(body) def convert_notes(body)
# The following regexp detects special labels followed by a # The following regexp detects special labels followed by a
# paragraph, perhaps at the end of the document. # paragraph, perhaps at the end of the document.
...@@ -39,10 +64,6 @@ def convert_notes(body) ...@@ -39,10 +64,6 @@ def convert_notes(body)
%Q(<div class="#{css_class}"><p>#{$2.strip}</p></div>\n) %Q(<div class="#{css_class}"><p>#{$2.strip}</p></div>\n)
end end
end end
def dom_id(text)
text.downcase.gsub(/[^a-z0-9]+/, '-').strip
end
end end
end end
end end
...@@ -12,6 +12,7 @@ Upgrading to Rails 4.0 ...@@ -12,6 +12,7 @@ Upgrading to Rails 4.0
TODO. This is a WIP guide. TODO. This is a WIP guide.
If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.2 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 4.0. Then take heed of the following changes: If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3.2 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 4.0. Then take heed of the following changes:
### Rails 4.0 requires at least Ruby 1.9.3 ### Rails 4.0 requires at least Ruby 1.9.3
...@@ -28,6 +29,7 @@ Rails 4.0 requires Ruby 1.9.3 or higher. Support for all of the previous Ruby ve ...@@ -28,6 +29,7 @@ Rails 4.0 requires Ruby 1.9.3 or higher. Support for all of the previous Ruby ve
TODO: Update the versions above. TODO: Update the versions above.
* Rails 4.0 removes `vendor/plugins` completely. You have to replace these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`. * Rails 4.0 removes `vendor/plugins` completely. You have to replace these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
TODO: Configuration changes in environment files TODO: Configuration changes in environment files
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册