aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/rails_guides
diff options
context:
space:
mode:
Diffstat (limited to 'railties/guides/rails_guides')
-rw-r--r--railties/guides/rails_guides/generator.rb112
-rw-r--r--railties/guides/rails_guides/helpers.rb34
-rw-r--r--railties/guides/rails_guides/indexer.rb51
-rw-r--r--railties/guides/rails_guides/textile_extensions.rb38
4 files changed, 235 insertions, 0 deletions
diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb
new file mode 100644
index 0000000000..3cbc2eb1f4
--- /dev/null
+++ b/railties/guides/rails_guides/generator.rb
@@ -0,0 +1,112 @@
+module RailsGuides
+ class Generator
+ attr_reader :output, :view_path, :view, :guides_dir
+
+ def initialize(output = nil)
+ @guides_dir = File.join(File.dirname(__FILE__), '..')
+
+ @output = output || File.join(@guides_dir, "output")
+
+ unless ENV["ONLY"]
+ FileUtils.rm_r(@output) if File.directory?(@output)
+ FileUtils.mkdir(@output)
+ end
+
+ @view_path = File.join(@guides_dir, "source")
+ end
+
+ def generate
+ guides = Dir.entries(view_path).find_all {|g| g =~ /textile$/ }
+
+ if ENV["ONLY"]
+ only = ENV["ONLY"].split(",").map{|x| x.strip }.map {|o| "#{o}.textile" }
+ guides = guides.find_all {|g| only.include?(g) }
+ puts "GENERATING ONLY #{guides.inspect}"
+ end
+
+ guides.each do |guide|
+ guide =~ /(.*?)(\.erb)?\.textile/
+ name = $1
+
+ puts "Generating #{name}"
+
+ file = File.join(output, "#{name}.html")
+ File.open(file, 'w') do |f|
+ @view = ActionView::Base.new(view_path)
+ @view.extend(Helpers)
+
+ if guide =~ /\.erb\.textile/
+ # Generate the erb pages with textile formatting - e.g. index/authors
+ result = view.render(:layout => 'layout', :file => guide)
+ f.write textile(result)
+ else
+ body = File.read(File.join(view_path, guide))
+ body = set_header_section(body, @view)
+ body = set_index(body, @view)
+
+ result = view.render(:layout => 'layout', :text => textile(body))
+ f.write result
+ end
+ end
+ end
+
+ # Copy images and css files to html directory
+ FileUtils.cp_r File.join(guides_dir, 'images'), File.join(output, 'images')
+ FileUtils.cp_r File.join(guides_dir, 'files'), File.join(output, 'files')
+ end
+
+ def set_header_section(body, view)
+ new_body = body.gsub(/(.*?)endprologue\./m, '').strip
+ header = $1
+
+ header =~ /h2\.(.*)/
+ page_title = $1.strip
+
+ header = textile(header)
+
+ view.content_for(:page_title) { page_title }
+ view.content_for(:header_section) { header }
+ new_body
+ end
+
+ def set_index(body, view)
+ index = <<-INDEX
+ <div id="subCol">
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" /> Chapters</h3>
+ <ol class="chapters">
+ INDEX
+
+ i = Indexer.new(body)
+ i.index
+
+ # Set index for 2 levels
+ i.level_hash.each do |key, value|
+ bookmark = '#' + key.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize
+ link = view.content_tag(:a, :href => bookmark) { key }
+
+ children = value.keys.map do |k|
+ bm = '#' + k.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize
+ l = view.content_tag(:a, :href => bm) { k }
+ view.content_tag(:li, l)
+ end
+
+ children_ul = view.content_tag(:ul, children)
+
+ index << view.content_tag(:li, link + children_ul)
+ end
+
+ index << '</ol>'
+ index << '</div>'
+
+ view.content_for(:index_section) { index }
+
+ i.result
+ end
+
+ def textile(body)
+ t = RedCloth.new(body)
+ t.hard_breaks = false
+ t.to_html(:notestuff, :plusplus, :code, :tip)
+ end
+ end
+end
diff --git a/railties/guides/rails_guides/helpers.rb b/railties/guides/rails_guides/helpers.rb
new file mode 100644
index 0000000000..e05793d40e
--- /dev/null
+++ b/railties/guides/rails_guides/helpers.rb
@@ -0,0 +1,34 @@
+module RailsGuides
+ module Helpers
+ def guide(name, url, options = {}, &block)
+ link = content_tag(:a, :href => url) { name }
+ result = content_tag(:dt, link)
+
+ if ticket = options[:ticket]
+ result << content_tag(:dd, lh(ticket), :class => 'ticket')
+ end
+
+ result << content_tag(:dd, capture(&block))
+ concat(result)
+ end
+
+ def lh(id, label = "Lighthouse Ticket")
+ url = "http://rails.lighthouseapp.com/projects/16213/tickets/#{id}"
+ content_tag(:a, label, :href => url)
+ end
+
+ def author(name, nick, image = 'credits_pic_blank.gif', &block)
+ image = "images/#{image}"
+
+ result = content_tag(:img, nil, :src => image, :class => 'left pic', :alt => name)
+ result << content_tag(:h3, name)
+ result << content_tag(:p, capture(&block))
+ concat content_tag(:div, result, :class => 'clearfix', :id => nick)
+ end
+
+ def code(&block)
+ c = capture(&block)
+ content_tag(:code, c)
+ end
+ end
+end
diff --git a/railties/guides/rails_guides/indexer.rb b/railties/guides/rails_guides/indexer.rb
new file mode 100644
index 0000000000..52809829e2
--- /dev/null
+++ b/railties/guides/rails_guides/indexer.rb
@@ -0,0 +1,51 @@
+module RailsGuides
+ class Indexer
+ attr_reader :body, :result, :level_hash
+
+ def initialize(body)
+ @body = body
+ @result = @body.dup
+ end
+
+ def index
+ @level_hash = process(body)
+ end
+
+ private
+
+ def process(string, current_level= 3, counters = [1])
+ s = StringScanner.new(string)
+
+ level_hash = ActiveSupport::OrderedHash.new
+
+ while !s.eos?
+ s.match?(/\h[0-9]\..*$/)
+ if matched = s.matched
+ matched =~ /\h([0-9])\.(.*)$/
+ level, title = $1.to_i, $2
+
+ if level < current_level
+ # This is needed. Go figure.
+ return level_hash
+ elsif level == current_level
+ index = counters.join(".")
+ bookmark = '#' + title.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize
+
+ raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{bookmark}). #{index}#{title}")
+
+ # Recurse
+ counters << 1
+ level_hash[title.strip] = process(s.post_match, current_level + 1, counters)
+ counters.pop
+
+ # Increment the current level
+ last = counters.pop
+ counters << last + 1
+ end
+ end
+ s.getch
+ end
+ level_hash
+ end
+ end
+end
diff --git a/railties/guides/rails_guides/textile_extensions.rb b/railties/guides/rails_guides/textile_extensions.rb
new file mode 100644
index 0000000000..a7fc5ba49c
--- /dev/null
+++ b/railties/guides/rails_guides/textile_extensions.rb
@@ -0,0 +1,38 @@
+module RailsGuides
+ module TextileExtensions
+ def notestuff(body)
+ body.gsub!(/^(IMPORTANT|CAUTION|WARNING|NOTE|INFO)(?:\.|\:)(.*)$/) do |m|
+ css_class = $1.downcase
+ css_class = 'warning' if ['caution', 'important'].include?(css_class)
+
+ result = "<div class='#{css_class}'><p>"
+ result << $2.strip
+ result << '</p></div>'
+ result
+ end
+ end
+
+ def tip(body)
+ body.gsub!(/^(TIP)\:(.*)$/) do |m|
+ result = "<div class='info'><p>"
+ result << $2.strip
+ result << '</p></div>'
+ result
+ end
+ end
+
+ def plusplus(body)
+ body.gsub!(/\+(.*?)\+/) do |m|
+ "<notextile><tt>#{$1}</tt></notextile>"
+ end
+ end
+
+ def code(body)
+ body.gsub!(/\<(yaml|shell|ruby|erb|html|sql)\>(.*?)\<\/\1\>/m) do |m|
+ es = ERB::Util.h($2)
+ css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
+ "<notextile><code class='#{css_class}'>#{es}\n</code></notextile>"
+ end
+ end
+ end
+end