2010-04-03 15:08:47 -04:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# This script generates the guides. It can be invoked either directly or via the
|
|
|
|
# generate_guides rake task within the railties directory.
|
|
|
|
#
|
|
|
|
# Guides are taken from the source directory, and the resulting HTML goes into the
|
|
|
|
# output directory. Assets are stored under files, and copied to output/files as
|
|
|
|
# part of the generation process.
|
|
|
|
#
|
|
|
|
# Some arguments may be passed via environment variables:
|
|
|
|
#
|
2010-04-04 04:36:12 -04:00
|
|
|
# WARNINGS
|
|
|
|
# If you are writing a guide, please work always with WARNINGS=1. Users can
|
|
|
|
# generate the guides, and thus this flag is off by default.
|
2010-04-03 15:08:47 -04:00
|
|
|
#
|
2010-04-04 04:36:12 -04:00
|
|
|
# Internal links (anchors) are checked. If a reference is broken levenshtein
|
|
|
|
# distance is used to suggest an existing one. This is useful since IDs are
|
|
|
|
# generated by Textile from headers and thus edits alter them.
|
2010-04-03 15:08:47 -04:00
|
|
|
#
|
2010-04-04 04:36:12 -04:00
|
|
|
# Also detects duplicated IDs. They happen if there are headers with the same
|
|
|
|
# text. Please do resolve them, if any, so guides are valid XHTML.
|
2010-04-03 15:08:47 -04:00
|
|
|
#
|
|
|
|
# ALL
|
2010-04-04 04:36:12 -04:00
|
|
|
# Set to "1" to force the generation of all guides.
|
|
|
|
#
|
2010-04-03 15:08:47 -04:00
|
|
|
# ONLY
|
2010-04-04 04:36:12 -04:00
|
|
|
# Use ONLY if you want to generate only one or a set of guides. Prefixes are
|
|
|
|
# enough:
|
2010-04-03 15:08:47 -04:00
|
|
|
#
|
|
|
|
# # generates only association_basics.html
|
|
|
|
# ONLY=assoc ruby rails_guides.rb
|
|
|
|
#
|
|
|
|
# Separate many using commas:
|
|
|
|
#
|
2010-12-24 15:22:28 -05:00
|
|
|
# # generates only association_basics.html and migrations.html
|
2010-04-03 15:08:47 -04:00
|
|
|
# ONLY=assoc,migrations ruby rails_guides.rb
|
|
|
|
#
|
2010-04-04 04:36:12 -04:00
|
|
|
# Note that if you are working on a guide generation will by default process
|
|
|
|
# only that one, so ONLY is rarely used nowadays.
|
|
|
|
#
|
2011-05-14 03:03:30 -04:00
|
|
|
# GUIDES_LANGUAGE
|
|
|
|
# Use GUIDES_LANGUAGE when you want to generate translated guides in
|
|
|
|
# <tt>source/<GUIDES_LANGUAGE></tt> folder (such as <tt>source/es</tt>).
|
|
|
|
# Ignore it when generating English guides.
|
2011-01-01 11:54:02 -05:00
|
|
|
#
|
2010-04-04 04:36:12 -04:00
|
|
|
# EDGE
|
|
|
|
# Set to "1" to indicate generated guides should be marked as edge. This
|
|
|
|
# inserts a badge and changes the preamble of the home page.
|
2010-04-03 15:08:47 -04:00
|
|
|
#
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2009-02-27 21:24:23 -05:00
|
|
|
require 'set'
|
2010-03-03 19:42:57 -05:00
|
|
|
require 'fileutils'
|
2009-02-27 21:24:23 -05:00
|
|
|
|
2010-03-15 17:06:27 -04:00
|
|
|
require 'active_support/core_ext/string/output_safety'
|
2010-04-06 18:20:27 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2010-03-15 17:06:27 -04:00
|
|
|
require 'action_controller'
|
|
|
|
require 'action_view'
|
|
|
|
|
|
|
|
require 'rails_guides/indexer'
|
|
|
|
require 'rails_guides/helpers'
|
|
|
|
require 'rails_guides/levenshtein'
|
|
|
|
|
2009-02-03 20:44:58 -05:00
|
|
|
module RailsGuides
|
|
|
|
class Generator
|
2010-04-06 18:20:27 -04:00
|
|
|
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :warnings, :all
|
2010-03-31 18:20:43 -04:00
|
|
|
|
|
|
|
GUIDES_RE = /\.(?:textile|html\.erb)$/
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
def initialize(output=nil)
|
2011-05-14 03:03:30 -04:00
|
|
|
@lang = ENV['GUIDES_LANGUAGE']
|
2010-03-15 04:57:08 -04:00
|
|
|
initialize_dirs(output)
|
2010-03-17 18:03:48 -04:00
|
|
|
create_output_dir_if_needed
|
2010-04-06 18:20:27 -04:00
|
|
|
set_flags_from_environment
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
2010-03-15 04:57:08 -04:00
|
|
|
generate_guides
|
|
|
|
copy_assets
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
private
|
|
|
|
def initialize_dirs(output)
|
|
|
|
@guides_dir = File.join(File.dirname(__FILE__), '..')
|
2011-01-01 11:54:02 -05:00
|
|
|
@source_dir = File.join(@guides_dir, "source", @lang.to_s)
|
|
|
|
@output_dir = output || File.join(@guides_dir, "output", @lang.to_s)
|
2010-03-15 04:57:08 -04:00
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2010-03-17 18:03:48 -04:00
|
|
|
def create_output_dir_if_needed
|
2010-03-15 04:57:08 -04:00
|
|
|
FileUtils.mkdir_p(output_dir)
|
2010-04-02 17:53:39 -04:00
|
|
|
end
|
|
|
|
|
2010-04-06 18:20:27 -04:00
|
|
|
def set_flags_from_environment
|
|
|
|
@edge = ENV['EDGE'] == '1'
|
|
|
|
@warnings = ENV['WARNINGS'] == '1'
|
|
|
|
@all = ENV['ALL'] == '1'
|
2010-04-02 17:53:39 -04:00
|
|
|
end
|
2010-03-15 04:57:08 -04:00
|
|
|
|
|
|
|
def generate_guides
|
|
|
|
guides_to_generate.each do |guide|
|
2010-03-17 18:03:48 -04:00
|
|
|
output_file = output_file_for(guide)
|
|
|
|
generate_guide(guide, output_file) if generate?(guide, output_file)
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
2010-03-15 04:57:08 -04:00
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
def guides_to_generate
|
2010-03-31 18:20:43 -04:00
|
|
|
guides = Dir.entries(source_dir).grep(GUIDES_RE)
|
2010-04-04 04:36:12 -04:00
|
|
|
ENV.key?('ONLY') ? select_only(guides) : guides
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
def select_only(guides)
|
2010-04-04 04:36:12 -04:00
|
|
|
prefixes = ENV['ONLY'].split(",").map(&:strip)
|
2010-03-15 04:57:08 -04:00
|
|
|
guides.select do |guide|
|
|
|
|
prefixes.any? {|p| guide.start_with?(p)}
|
|
|
|
end
|
|
|
|
end
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
def copy_assets
|
2010-04-04 05:30:31 -04:00
|
|
|
FileUtils.cp_r(Dir.glob("#{guides_dir}/assets/*"), output_dir)
|
2010-03-15 04:57:08 -04:00
|
|
|
end
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2010-03-17 18:03:48 -04:00
|
|
|
def output_file_for(guide)
|
2010-03-31 18:20:43 -04:00
|
|
|
guide.sub(GUIDES_RE, '.html')
|
2010-03-17 18:03:48 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-03-17 18:03:48 -04:00
|
|
|
def generate?(source_file, output_file)
|
|
|
|
fin = File.join(source_dir, source_file)
|
|
|
|
fout = File.join(output_dir, output_file)
|
2010-04-06 18:20:27 -04:00
|
|
|
all || !File.exists?(fout) || File.mtime(fout) < File.mtime(fin)
|
2010-03-17 18:03:48 -04:00
|
|
|
end
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2010-03-17 18:03:48 -04:00
|
|
|
def generate_guide(guide, output_file)
|
|
|
|
puts "Generating #{output_file}"
|
2010-03-15 04:57:08 -04:00
|
|
|
File.open(File.join(output_dir, output_file), 'w') do |f|
|
2010-04-02 17:53:39 -04:00
|
|
|
view = ActionView::Base.new(source_dir, :edge => edge)
|
2010-03-15 04:57:08 -04:00
|
|
|
view.extend(Helpers)
|
2010-04-02 17:53:39 -04:00
|
|
|
|
2010-03-31 18:20:43 -04:00
|
|
|
if guide =~ /\.html\.erb$/
|
2010-04-02 17:53:39 -04:00
|
|
|
# Generate the special pages like the home.
|
2011-11-10 12:06:20 -05:00
|
|
|
# Passing a template handler in the template name is deprecated. So pass the file name without the extension.
|
|
|
|
result = view.render(:layout => 'layout', :file => $`)
|
2009-02-21 18:29:11 -05:00
|
|
|
else
|
2010-03-15 04:57:08 -04:00
|
|
|
body = File.read(File.join(source_dir, guide))
|
|
|
|
body = set_header_section(body, view)
|
|
|
|
body = set_index(body, view)
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
result = view.render(:layout => 'layout', :text => textile(body))
|
2010-02-17 14:22:37 -05:00
|
|
|
|
2010-04-06 18:20:27 -04:00
|
|
|
warn_about_broken_links(result) if @warnings
|
2009-02-21 18:29:11 -05:00
|
|
|
end
|
2010-03-31 18:20:43 -04:00
|
|
|
|
2010-02-17 14:22:37 -05:00
|
|
|
f.write result
|
2009-02-21 18:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-03 20:44:58 -05:00
|
|
|
def set_header_section(body, view)
|
|
|
|
new_body = body.gsub(/(.*?)endprologue\./m, '').strip
|
|
|
|
header = $1
|
|
|
|
|
|
|
|
header =~ /h2\.(.*)/
|
2010-04-06 14:30:00 -04:00
|
|
|
page_title = "Ruby on Rails Guides: #{$1.strip}"
|
2009-02-03 20:44:58 -05:00
|
|
|
|
|
|
|
header = textile(header)
|
|
|
|
|
2010-02-01 19:04:35 -05:00
|
|
|
view.content_for(:page_title) { page_title.html_safe }
|
|
|
|
view.content_for(:header_section) { header.html_safe }
|
2009-02-03 20:44:58 -05:00
|
|
|
new_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_index(body, view)
|
|
|
|
index = <<-INDEX
|
|
|
|
<div id="subCol">
|
2009-02-21 18:29:11 -05:00
|
|
|
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
2009-02-03 20:44:58 -05:00
|
|
|
<ol class="chapters">
|
2009-02-21 18:29:11 -05:00
|
|
|
INDEX
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2010-04-06 18:20:27 -04:00
|
|
|
i = Indexer.new(body, warnings)
|
2009-02-03 20:44:58 -05:00
|
|
|
i.index
|
|
|
|
|
|
|
|
# Set index for 2 levels
|
|
|
|
i.level_hash.each do |key, value|
|
2010-04-06 18:20:27 -04:00
|
|
|
link = view.content_tag(:a, :href => key[:id]) { textile(key[:title], true).html_safe }
|
2009-02-03 20:44:58 -05:00
|
|
|
|
|
|
|
children = value.keys.map do |k|
|
2010-04-06 18:20:27 -04:00
|
|
|
view.content_tag(:li,
|
|
|
|
view.content_tag(:a, :href => k[:id]) { textile(k[:title], true).html_safe })
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
2010-03-31 19:14:18 -04:00
|
|
|
children_ul = children.empty? ? "" : view.content_tag(:ul, children.join(" ").html_safe)
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2010-03-15 04:57:08 -04:00
|
|
|
index << view.content_tag(:li, link.html_safe + children_ul.html_safe)
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
index << '</ol>'
|
|
|
|
index << '</div>'
|
|
|
|
|
2010-02-01 19:04:35 -05:00
|
|
|
view.content_for(:index_section) { index.html_safe }
|
2009-02-03 20:44:58 -05:00
|
|
|
|
|
|
|
i.result
|
|
|
|
end
|
|
|
|
|
2010-04-06 18:20:27 -04:00
|
|
|
def textile(body, lite_mode=false)
|
2011-08-07 19:20:31 -04:00
|
|
|
t = RedCloth.new(body)
|
|
|
|
t.hard_breaks = false
|
|
|
|
t.lite_mode = lite_mode
|
|
|
|
t.to_html(:notestuff, :plusplus, :code)
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
2009-02-28 11:07:46 -05:00
|
|
|
|
2009-02-27 21:24:23 -05:00
|
|
|
def warn_about_broken_links(html)
|
2009-03-15 17:16:27 -04:00
|
|
|
anchors = extract_anchors(html)
|
|
|
|
check_fragment_identifiers(html, anchors)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-03-15 17:16:27 -04:00
|
|
|
def extract_anchors(html)
|
2009-02-27 21:24:23 -05:00
|
|
|
# Textile generates headers with IDs computed from titles.
|
2009-03-15 17:16:27 -04:00
|
|
|
anchors = Set.new
|
|
|
|
html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
|
|
|
|
if anchors.member?(anchor)
|
2010-04-04 11:42:54 -04:00
|
|
|
puts "*** DUPLICATE ID: #{anchor}, please put and explicit ID, e.g. h4(#explicit-id), or consider rewording"
|
2009-03-15 17:16:27 -04:00
|
|
|
else
|
|
|
|
anchors << anchor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-27 17:07:13 -04:00
|
|
|
# Footnotes.
|
2009-02-27 21:24:23 -05:00
|
|
|
anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
|
2010-08-27 17:07:13 -04:00
|
|
|
anchors += Set.new(html.scan(/<sup\s+class="footnote"\s+id="([^"]+)/).flatten)
|
2009-03-15 17:16:27 -04:00
|
|
|
return anchors
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-03-15 17:16:27 -04:00
|
|
|
def check_fragment_identifiers(html, anchors)
|
2009-02-27 21:24:23 -05:00
|
|
|
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)
|
2009-03-15 15:03:37 -04:00
|
|
|
guess = anchors.min { |a, b|
|
|
|
|
Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
|
|
|
|
}
|
2009-03-24 08:10:38 -04:00
|
|
|
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
|
2009-02-27 21:24:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
end
|