diff options
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/rails_guides/generator.rb | 90 |
1 files changed, 51 insertions, 39 deletions
diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index 7c8d26a1ea..9a4a26cd0b 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -3,60 +3,72 @@ require 'fileutils' module RailsGuides class Generator - attr_reader :output, :view_path, :view, :guides_dir + attr_reader :guides_dir, :source_dir, :output_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_p(@output) - end - - @view_path = File.join(@guides_dir, "source") + def initialize(output=nil) + initialize_dirs(output) + reset_output_dir end def generate - guides = Dir.entries(view_path).find_all {|g| g =~ /\.textile(?:\.erb)?$/ } + generate_guides + copy_assets + end - 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 + private + def initialize_dirs(output) + @guides_dir = File.join(File.dirname(__FILE__), '..') + @source_dir = File.join(@guides_dir, "source") + @output_dir = output || File.join(@guides_dir, "output") + end - guides.each do |guide| + def reset_output_dir + FileUtils.rm_rf(output_dir) + FileUtils.mkdir_p(output_dir) + end + + def generate_guides + guides_to_generate.each do |guide| generate_guide(guide) 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') + def guides_to_generate + guides = Dir.entries(source_dir).grep(/\.textile(?:\.erb)?$/) + ENV.key?("ONLY") ? select_only(guides) : guides end - def generate_guide(guide) - guide =~ /(.*?)\.textile(?:\.erb)?$/ - name = $1 + def select_only(guides) + prefixes = ENV["ONLY"].split(",").map(&:strip) + guides.select do |guide| + prefixes.any? {|p| guide.start_with?(p)} + end + end - puts "Generating #{name}" + def copy_assets + FileUtils.cp_r(File.join(guides_dir, 'images'), File.join(output_dir, 'images')) + FileUtils.cp_r(File.join(guides_dir, 'files'), File.join(output_dir, 'files')) + end - file = File.join(output, "#{name}.html") - File.open(file, 'w') do |f| - @view = ActionView::Base.new(view_path) - @view.extend(Helpers) + def generate_guide(guide) + output_file = guide.sub(/\.textile(?:\.erb)?$/, '.html') + puts "Generating #{output_file}" + File.open(File.join(output_dir, output_file), 'w') do |f| + view = ActionView::Base.new(source_dir) + view.output_buffer = ActiveSupport::SafeBuffer.new + view.extend(Helpers) + if guide =~ /\.textile\.erb$/ # Generate the erb pages with textile formatting - e.g. index/authors result = view.render(:layout => 'layout', :file => guide) result = textile(result) else - body = File.read(File.join(view_path, guide)) - body = set_header_section(body, @view) - body = set_index(body, @view) + body = File.read(File.join(source_dir, guide)) + body = set_header_section(body, view) + body = set_index(body, view) - result = view.render(:layout => 'layout', :text => textile(body).html_safe) + result = view.render(:layout => 'layout', :text => textile(body)) warn_about_broken_links(result) if ENV.key?("WARN_BROKEN_LINKS") end @@ -92,16 +104,16 @@ module RailsGuides # Set index for 2 levels i.level_hash.each do |key, value| - link = view.content_tag(:a, :href => key[:id]) { textile(key[:title]) } + link = view.content_tag(:a, :href => key[:id]) { textile(key[:title]).html_safe } children = value.keys.map do |k| - l = view.content_tag(:a, :href => k[:id]) { textile(k[:title]) } - view.content_tag(:li, l) + l = view.content_tag(:a, :href => k[:id]) { textile(k[:title]).html_safe } + view.content_tag(:li, l.html_safe) end - children_ul = view.content_tag(:ul, children.join(" ")) + children_ul = view.content_tag(:ul, children.join(" ").html_safe) - index << view.content_tag(:li, link + children_ul) + index << view.content_tag(:li, link.html_safe + children_ul.html_safe) end index << '</ol>' |