2013-01-05 17:46:30 -05:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-08-31 12:01:06 -04:00
|
|
|
require 'redcarpet'
|
|
|
|
require 'nokogiri'
|
|
|
|
require 'rails_guides/markdown/renderer'
|
|
|
|
|
|
|
|
module RailsGuides
|
|
|
|
class Markdown
|
|
|
|
def initialize(view, layout)
|
|
|
|
@view = view
|
|
|
|
@layout = layout
|
2012-08-31 17:49:17 -04:00
|
|
|
@index_counter = Hash.new(0)
|
2012-09-01 16:39:47 -04:00
|
|
|
@raw_header = ''
|
2012-09-16 23:12:45 -04:00
|
|
|
@node_ids = {}
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(body)
|
2012-09-01 16:39:47 -04:00
|
|
|
@raw_body = body
|
|
|
|
extract_raw_header_and_body
|
2012-08-31 17:49:17 -04:00
|
|
|
generate_header
|
|
|
|
generate_title
|
|
|
|
generate_body
|
|
|
|
generate_structure
|
|
|
|
generate_index
|
|
|
|
render_page
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-08-31 17:49:17 -04:00
|
|
|
|
|
|
|
def dom_id(nodes)
|
2012-09-16 23:12:45 -04:00
|
|
|
dom_id = dom_id_text(nodes.last.text)
|
|
|
|
|
|
|
|
# Fix duplicate node by prefix with its parent node
|
|
|
|
if @node_ids[dom_id]
|
|
|
|
if @node_ids[dom_id].size > 1
|
|
|
|
duplicate_nodes = @node_ids.delete(dom_id)
|
|
|
|
new_node_id = "#{duplicate_nodes[-2][:id]}-#{duplicate_nodes.last[:id]}"
|
|
|
|
duplicate_nodes.last[:id] = new_node_id
|
|
|
|
@node_ids[new_node_id] = duplicate_nodes
|
|
|
|
end
|
|
|
|
|
|
|
|
dom_id = "#{nodes[-2][:id]}-#{dom_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@node_ids[dom_id] = nodes
|
|
|
|
dom_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def dom_id_text(text)
|
|
|
|
text.downcase.gsub(/\?/, '-questionmark').gsub(/!/, '-bang').gsub(/[^a-z0-9]+/, ' ')
|
|
|
|
.strip.gsub(/\s+/, '-')
|
2012-08-31 17:49:17 -04:00
|
|
|
end
|
|
|
|
|
2012-08-31 12:01:06 -04:00
|
|
|
def engine
|
|
|
|
@engine ||= Redcarpet::Markdown.new(Renderer, {
|
|
|
|
no_intra_emphasis: true,
|
|
|
|
fenced_code_blocks: true,
|
|
|
|
autolink: true,
|
|
|
|
strikethrough: true,
|
2012-09-01 21:37:50 -04:00
|
|
|
superscript: true,
|
|
|
|
tables: true
|
2012-08-31 12:01:06 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2012-09-01 16:39:47 -04:00
|
|
|
def extract_raw_header_and_body
|
|
|
|
if @raw_body =~ /^\-{40,}$/
|
|
|
|
@raw_header, _, @raw_body = @raw_body.partition(/^\-{40,}$/).map(&:strip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 17:49:17 -04:00
|
|
|
def generate_body
|
|
|
|
@body = engine.render(@raw_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_header
|
|
|
|
@header = engine.render(@raw_header).html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_structure
|
2012-09-16 23:12:45 -04:00
|
|
|
@headings_for_index = []
|
2012-09-01 16:39:47 -04:00
|
|
|
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]
|
2012-09-16 23:12:45 -04:00
|
|
|
@headings_for_index << [1, node, node.inner_html]
|
2012-09-01 16:39:47 -04:00
|
|
|
when 'h4'
|
|
|
|
hierarchy = hierarchy[0, 1] + [node]
|
2012-09-16 23:12:45 -04:00
|
|
|
@headings_for_index << [2, node, node.inner_html]
|
2012-09-01 16:39:47 -04:00
|
|
|
when 'h5'
|
|
|
|
hierarchy = hierarchy[0, 2] + [node]
|
|
|
|
when 'h6'
|
|
|
|
hierarchy = hierarchy[0, 3] + [node]
|
|
|
|
end
|
2012-08-31 17:49:17 -04:00
|
|
|
|
2012-09-16 23:12:45 -04:00
|
|
|
node[:id] = dom_id(hierarchy)
|
2012-09-03 21:21:04 -04:00
|
|
|
node.inner_html = "#{node_index(hierarchy)} #{node.inner_html}"
|
2012-09-01 16:39:47 -04:00
|
|
|
end
|
2012-08-31 17:49:17 -04:00
|
|
|
end
|
2012-09-01 16:39:47 -04:00
|
|
|
end.to_html
|
|
|
|
end
|
2012-08-31 17:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_index
|
2012-09-16 23:12:45 -04:00
|
|
|
if @headings_for_index.present?
|
|
|
|
raw_index = ''
|
|
|
|
@headings_for_index.each do |level, node, label|
|
|
|
|
if level == 1
|
|
|
|
raw_index += "1. [#{label}](##{node[:id]})\n"
|
|
|
|
elsif level == 2
|
|
|
|
raw_index += " * [#{label}](##{node[:id]})\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@index = Nokogiri::HTML(engine.render(raw_index)).tap do |doc|
|
2012-09-01 16:39:47 -04:00
|
|
|
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
|
2012-08-31 17:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_title
|
2012-09-01 16:39:47 -04:00
|
|
|
if heading = Nokogiri::HTML(@header).at(:h2)
|
2013-01-05 17:46:30 -05:00
|
|
|
@title = "#{heading.text} — Ruby on Rails Guides".html_safe
|
2012-09-01 16:39:47 -04:00
|
|
|
else
|
|
|
|
@title = "Ruby on Rails Guides"
|
|
|
|
end
|
2012-08-31 17:49:17 -04:00
|
|
|
end
|
2012-08-31 12:01:06 -04:00
|
|
|
|
2012-08-31 17:49:17 -04:00
|
|
|
def node_index(hierarchy)
|
|
|
|
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}"
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 17:49:17 -04:00
|
|
|
def render_page
|
|
|
|
@view.content_for(:header_section) { @header }
|
|
|
|
@view.content_for(:page_title) { @title }
|
2012-09-01 16:39:47 -04:00
|
|
|
@view.content_for(:index_section) { @index }
|
2012-08-31 17:49:17 -04:00
|
|
|
@view.render(:layout => @layout, :text => @body)
|
2012-08-31 12:01:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|