aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/rails_guides/generator.rb
diff options
context:
space:
mode:
authorCarl Lerche & Yehuda Katz <wycats@gmail.com>2009-04-13 15:18:45 -0700
committerCarl Lerche & Yehuda Katz <wycats@gmail.com>2009-04-13 15:18:45 -0700
commit906aebceedb95d8caa6db6314bc90f605bdfaf2b (patch)
tree5abc86bb6709b20df7cb5f4d1750b27c641dca4b /railties/guides/rails_guides/generator.rb
parent2036d3ba75da1a0f3061bf5a33c89e2b2eaff420 (diff)
parentc877857d59554d78dbf45f5f9fcaafb8badec4e2 (diff)
downloadrails-906aebceedb95d8caa6db6314bc90f605bdfaf2b.tar.gz
rails-906aebceedb95d8caa6db6314bc90f605bdfaf2b.tar.bz2
rails-906aebceedb95d8caa6db6314bc90f605bdfaf2b.zip
Bring abstract_controller up to date with rails/master
Resolved all the conflicts since 2.3.0 -> HEAD. Following is a list of commits that could not be applied cleanly or are obviated with the abstract_controller refactor. They all need to be revisited to ensure that fixes made in 2.3 do not reappear in 3.0: 2259ecf368e6a6715966f69216e3ee86bf1a82a7 AR not available * This will be reimplemented with ActionORM or equivalent 06182ea02e92afad579998aa80144588e8865ac3 implicitly rendering a js response should not use the default layout [#1844 state:resolved] * This will be handled generically 893e9eb99504705419ad6edac14d00e71cef5f12 Improve view rendering performance in development mode and reinstate template recompiling in production [#1909 state:resolved] * We will need to reimplement rails-dev-boost on top of the refactor; the changes here are very implementation specific and cannot be cleanly applied. The following commits are implicated: 199e750d46c04970b5e7684998d09405648ecbd4 3942cb406e1d5db0ac00e03153809cc8dc4cc4db f8ea9f85d4f1e3e6f3b5d895bef6b013aa4b0690 e3b166aab37ddc2fbab030b146eb61713b91bf55 ae9f258e03c9fd5088da12c1c6cd216cc89a01f7 44423126c6f6133a1d9cf1d0832b527e8711d40f 0cb020b4d6d838025859bd60fb8151c8e21b8e84 workaround for picking layouts based on wrong view_paths [#1974 state:resolved] * The specifics of this commit no longer apply. Since it is a two-line commit, we will reimplement this change. 8c5cc66a831aadb159f3daaffa4208064c30af0e make action_controller/layouts pick templates from the current instance's view_paths instead of the class view_paths [#1974 state:resolved] * This does not apply at all. It should be trivial to apply the feature to the reimplemented ActionController::Base. 87e8b162463f13bd50d27398f020769460a770e3 fix HTML fallback for explicit templates [#2052 state:resolved] * There were a number of patches related to this that simply compounded each other. Basically none of them apply cleanly, and the underlying issue needs to be revisited. After discussing the underlying problem with Koz, we will defer these fixes for further discussion.
Diffstat (limited to 'railties/guides/rails_guides/generator.rb')
-rw-r--r--railties/guides/rails_guides/generator.rb172
1 files changed, 172 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..f93282db2e
--- /dev/null
+++ b/railties/guides/rails_guides/generator.rb
@@ -0,0 +1,172 @@
+require 'set'
+
+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|
+ generate_guide(guide)
+ 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 generate_guide(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 => name)
+ 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
+ warn_about_broken_links(result) if ENV.key?("WARN_BROKEN_LINKS")
+ end
+ end
+ 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|
+ link = view.content_tag(:a, :href => key[:id]) { textile(key[:title]) }
+
+ children = value.keys.map do |k|
+ l = view.content_tag(:a, :href => k[:id]) { textile(k[:title]) }
+ 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)
+ # If the issue with notextile is fixed just remove the wrapper.
+ with_workaround_for_notextile(body) do |body|
+ t = RedCloth.new(body)
+ t.hard_breaks = false
+ t.to_html(:notestuff, :plusplus, :code, :tip)
+ end
+ end
+
+ # For some reason the notextile tag does not always turn off textile. See
+ # LH ticket of the security guide (#7). As a temporary workaround we deal
+ # with code blocks by hand.
+ def with_workaround_for_notextile(body)
+ code_blocks = []
+ body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
+ es = ERB::Util.h($2)
+ css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
+ code_blocks << %{<div class="code_container"><code class="#{css_class}">#{es}</code></div>}
+ "\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n"
+ end
+
+ body = yield body
+
+ body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|
+ code_blocks[$1.to_i]
+ end
+ end
+
+ def warn_about_broken_links(html)
+ anchors = extract_anchors(html)
+ check_fragment_identifiers(html, anchors)
+ end
+
+ def extract_anchors(html)
+ # Textile generates headers with IDs computed from titles.
+ anchors = Set.new
+ html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
+ if anchors.member?(anchor)
+ puts "*** DUPLICATE HEADER ID: #{anchor}, please consider rewording" if ENV.key?("WARN_DUPLICATE_HEADERS")
+ else
+ anchors << anchor
+ end
+ end
+
+ # Also, footnotes are rendered as paragraphs this way.
+ anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
+ return anchors
+ end
+
+ def check_fragment_identifiers(html, anchors)
+ html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
+ next if fragment_identifier == 'mainCol' # in layout, jumps to some DIV
+ unless anchors.member?(fragment_identifier)
+ guess = anchors.min { |a, b|
+ Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
+ }
+ puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
+ end
+ end
+ end
+ end
+end