From 0867fcdb5a0d6b38a6326914984ad9d02c52dd0e Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 1 Sep 2012 16:39:47 -0400 Subject: [PATCH] Update Markdown renderer to be more flexible --- guides/rails_guides/markdown.rb | 90 +++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/guides/rails_guides/markdown.rb b/guides/rails_guides/markdown.rb index 6ad42769bc..ddfc33fa65 100644 --- a/guides/rails_guides/markdown.rb +++ b/guides/rails_guides/markdown.rb @@ -8,10 +8,12 @@ def initialize(view, layout) @view = view @layout = layout @index_counter = Hash.new(0) + @raw_header = '' end def render(body) - @raw_header, _, @raw_body = body.partition(/^\-{40,}$/).map(&:strip) + @raw_body = body + extract_raw_header_and_body generate_header generate_title generate_body @@ -36,6 +38,12 @@ def engine }) end + def extract_raw_header_and_body + if @raw_body =~ /^\-{40,}$/ + @raw_header, _, @raw_body = @raw_body.partition(/^\-{40,}$/).map(&:strip) + end + end + def generate_body @body = engine.render(@raw_body) end @@ -46,49 +54,57 @@ def generate_header 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 + if @body.present? + @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}" + node.inner_html = "#{node_index(hierarchy)} #{node.text}" + end end - end - end.to_html + end.to_html + end 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 -
-

Chapters

- #{@index} -
- INDEX + if @raw_index.present? + @index = Nokogiri::HTML(engine.render(@raw_index)).tap do |doc| + doc.at('ol')[:class] = 'chapters' + end.to_html + + @index = <<-INDEX.html_safe +
+

Chapters

+ #{@index} +
+ INDEX + end end def generate_title - @title = "Ruby on Rails Guides: #{Nokogiri::HTML(@header).at(:h2).text}".html_safe + if heading = Nokogiri::HTML(@header).at(:h2) + @title = "Ruby on Rails Guides: #{heading.text}".html_safe + else + @title = "Ruby on Rails Guides" + end end def node_index(hierarchy) @@ -110,7 +126,7 @@ def node_index(hierarchy) def render_page @view.content_for(:header_section) { @header } @view.content_for(:page_title) { @title } - @view.content_for(:index_section) { @index.html_safe } + @view.content_for(:index_section) { @index } @view.render(:layout => @layout, :text => @body) end end -- GitLab