2010-04-04 03:08:47 +08:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
#
|
2012-05-29 05:17:00 +08:00
|
|
|
# This script generates the guides. It can be invoked via the
|
2012-05-29 05:21:25 +08:00
|
|
|
# guides:generate rake task within the guides directory.
|
2010-04-04 03:08:47 +08:00
|
|
|
#
|
|
|
|
# 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 16:36:12 +08: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-04 03:08:47 +08:00
|
|
|
#
|
2010-04-04 16:36:12 +08: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
|
2012-09-07 12:09:56 +08:00
|
|
|
# generated by Markdown from headers and thus edits alter them.
|
2010-04-04 03:08:47 +08:00
|
|
|
#
|
2010-04-04 16:36:12 +08: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-04 03:08:47 +08:00
|
|
|
#
|
|
|
|
# ALL
|
2010-04-04 16:36:12 +08:00
|
|
|
# Set to "1" to force the generation of all guides.
|
|
|
|
#
|
2010-04-04 03:08:47 +08:00
|
|
|
# ONLY
|
2010-04-04 16:36:12 +08:00
|
|
|
# Use ONLY if you want to generate only one or a set of guides. Prefixes are
|
|
|
|
# enough:
|
2010-04-04 03:08:47 +08:00
|
|
|
#
|
|
|
|
# # generates only association_basics.html
|
|
|
|
# ONLY=assoc ruby rails_guides.rb
|
|
|
|
#
|
|
|
|
# Separate many using commas:
|
|
|
|
#
|
2010-12-25 04:22:28 +08:00
|
|
|
# # generates only association_basics.html and migrations.html
|
2010-04-04 03:08:47 +08:00
|
|
|
# ONLY=assoc,migrations ruby rails_guides.rb
|
|
|
|
#
|
2010-04-04 16:36:12 +08: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 15:03:30 +08: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-02 00:54:02 +08:00
|
|
|
#
|
2010-04-04 16:36:12 +08: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-04 03:08:47 +08:00
|
|
|
#
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2009-02-28 10:24:23 +08:00
|
|
|
require 'set'
|
2010-03-04 08:42:57 +08:00
|
|
|
require 'fileutils'
|
2009-02-28 10:24:23 +08:00
|
|
|
|
2010-03-16 05:06:27 +08:00
|
|
|
require 'active_support/core_ext/string/output_safety'
|
2010-04-07 06:20:27 +08:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2010-03-16 05:06:27 +08:00
|
|
|
require 'action_controller'
|
|
|
|
require 'action_view'
|
|
|
|
|
2015-03-07 15:50:27 +08:00
|
|
|
require 'rails_guides/markdown'
|
2010-03-16 05:06:27 +08:00
|
|
|
require 'rails_guides/indexer'
|
|
|
|
require 'rails_guides/helpers'
|
|
|
|
require 'rails_guides/levenshtein'
|
|
|
|
|
2009-02-04 09:44:58 +08:00
|
|
|
module RailsGuides
|
|
|
|
class Generator
|
2010-04-07 06:20:27 +08:00
|
|
|
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :warnings, :all
|
2010-04-01 06:20:43 +08:00
|
|
|
|
2012-09-19 05:04:12 +08:00
|
|
|
GUIDES_RE = /\.(?:erb|md)\z/
|
2009-02-04 09:44:58 +08:00
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
def initialize(output=nil)
|
2011-12-25 09:26:18 +08:00
|
|
|
set_flags_from_environment
|
|
|
|
|
|
|
|
if kindle?
|
|
|
|
check_for_kindlegen
|
|
|
|
register_kindle_mime_types
|
|
|
|
end
|
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
initialize_dirs(output)
|
2010-03-18 06:03:48 +08:00
|
|
|
create_output_dir_if_needed
|
2011-12-25 09:26:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_flags_from_environment
|
|
|
|
@edge = ENV['EDGE'] == '1'
|
|
|
|
@warnings = ENV['WARNINGS'] == '1'
|
|
|
|
@all = ENV['ALL'] == '1'
|
|
|
|
@kindle = ENV['KINDLE'] == '1'
|
2012-12-07 22:20:02 +08:00
|
|
|
@version = ENV['RAILS_VERSION'] || 'local'
|
2011-12-25 09:26:18 +08:00
|
|
|
@lang = ENV['GUIDES_LANGUAGE']
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_kindle_mime_types
|
|
|
|
Mime::Type.register_alias("application/xml", :opf, %w(opf))
|
2011-12-25 19:18:18 +08:00
|
|
|
Mime::Type.register_alias("application/xml", :ncx, %w(ncx))
|
2009-02-04 09:44:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
2010-03-15 16:57:08 +08:00
|
|
|
generate_guides
|
|
|
|
copy_assets
|
2011-12-25 09:26:18 +08:00
|
|
|
generate_mobi if kindle?
|
2010-03-15 16:57:08 +08:00
|
|
|
end
|
2009-02-04 09:44:58 +08:00
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
private
|
2011-12-25 09:26:18 +08:00
|
|
|
|
|
|
|
def kindle?
|
|
|
|
@kindle
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_kindlegen
|
|
|
|
if `which kindlegen`.blank?
|
|
|
|
raise "Can't create a kindle version without `kindlegen`."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_mobi
|
2012-11-29 00:25:32 +08:00
|
|
|
require 'rails_guides/kindle'
|
2011-12-25 20:07:49 +08:00
|
|
|
out = "#{output_dir}/kindlegen.out"
|
2012-11-29 00:25:32 +08:00
|
|
|
Kindle.generate(output_dir, mobi, out)
|
2011-12-25 19:18:18 +08:00
|
|
|
puts "(kindlegen log at #{out})."
|
2011-12-25 09:26:18 +08:00
|
|
|
end
|
|
|
|
|
2011-12-25 20:07:49 +08:00
|
|
|
def mobi
|
|
|
|
"ruby_on_rails_guides_#@version%s.mobi" % (@lang.present? ? ".#@lang" : '')
|
|
|
|
end
|
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
def initialize_dirs(output)
|
|
|
|
@guides_dir = File.join(File.dirname(__FILE__), '..')
|
2011-12-25 09:26:18 +08:00
|
|
|
@source_dir = "#@guides_dir/source/#@lang"
|
2011-12-25 19:18:18 +08:00
|
|
|
@output_dir = if output
|
|
|
|
output
|
|
|
|
elsif kindle?
|
|
|
|
"#@guides_dir/output/kindle/#@lang"
|
|
|
|
else
|
|
|
|
"#@guides_dir/output/#@lang"
|
|
|
|
end.sub(%r</$>, '')
|
2010-03-15 16:57:08 +08:00
|
|
|
end
|
2009-02-04 09:44:58 +08:00
|
|
|
|
2010-03-18 06:03:48 +08:00
|
|
|
def create_output_dir_if_needed
|
2010-03-15 16:57:08 +08:00
|
|
|
FileUtils.mkdir_p(output_dir)
|
2010-04-03 05:53:39 +08:00
|
|
|
end
|
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
def generate_guides
|
|
|
|
guides_to_generate.each do |guide|
|
2010-03-18 06:03:48 +08:00
|
|
|
output_file = output_file_for(guide)
|
|
|
|
generate_guide(guide, output_file) if generate?(guide, output_file)
|
2009-02-04 09:44:58 +08:00
|
|
|
end
|
2010-03-15 16:57:08 +08:00
|
|
|
end
|
2009-02-04 09:44:58 +08:00
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
def guides_to_generate
|
2010-04-01 06:20:43 +08:00
|
|
|
guides = Dir.entries(source_dir).grep(GUIDES_RE)
|
2011-12-25 09:26:18 +08:00
|
|
|
|
|
|
|
if kindle?
|
|
|
|
Dir.entries("#{source_dir}/kindle").grep(GUIDES_RE).map do |entry|
|
2012-09-01 06:16:37 +08:00
|
|
|
next if entry == 'KINDLE.md'
|
2011-12-25 09:26:18 +08:00
|
|
|
guides << "kindle/#{entry}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-04 16:36:12 +08:00
|
|
|
ENV.key?('ONLY') ? select_only(guides) : guides
|
2009-02-04 09:44:58 +08:00
|
|
|
end
|
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
def select_only(guides)
|
2010-04-04 16:36:12 +08:00
|
|
|
prefixes = ENV['ONLY'].split(",").map(&:strip)
|
2010-03-15 16:57:08 +08:00
|
|
|
guides.select do |guide|
|
2012-02-04 20:25:24 +08:00
|
|
|
prefixes.any? { |p| guide.start_with?(p) || guide.start_with?("kindle") }
|
2010-03-15 16:57:08 +08:00
|
|
|
end
|
|
|
|
end
|
2009-02-22 07:29:11 +08:00
|
|
|
|
2010-03-15 16:57:08 +08:00
|
|
|
def copy_assets
|
2010-04-04 17:30:31 +08:00
|
|
|
FileUtils.cp_r(Dir.glob("#{guides_dir}/assets/*"), output_dir)
|
2010-03-15 16:57:08 +08:00
|
|
|
end
|
2009-02-22 07:29:11 +08:00
|
|
|
|
2010-03-18 06:03:48 +08:00
|
|
|
def output_file_for(guide)
|
2012-09-19 05:04:12 +08:00
|
|
|
if guide.end_with?('.md')
|
|
|
|
guide.sub(/md\z/, 'html')
|
2011-12-25 09:26:18 +08:00
|
|
|
else
|
2012-09-19 05:04:12 +08:00
|
|
|
guide.sub(/\.erb\z/, '')
|
2011-12-25 09:26:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def output_path_for(output_file)
|
|
|
|
File.join(output_dir, File.basename(output_file))
|
2010-03-18 06:03:48 +08:00
|
|
|
end
|
2010-08-14 13:13:00 +08:00
|
|
|
|
2010-03-18 06:03:48 +08:00
|
|
|
def generate?(source_file, output_file)
|
|
|
|
fin = File.join(source_dir, source_file)
|
2011-12-25 09:26:18 +08:00
|
|
|
fout = output_path_for(output_file)
|
2013-11-02 04:15:53 +08:00
|
|
|
all || !File.exist?(fout) || File.mtime(fout) < File.mtime(fin)
|
2010-03-18 06:03:48 +08:00
|
|
|
end
|
2009-02-22 07:29:11 +08:00
|
|
|
|
2010-03-18 06:03:48 +08:00
|
|
|
def generate_guide(guide, output_file)
|
2011-12-25 09:26:18 +08:00
|
|
|
output_path = output_path_for(output_file)
|
|
|
|
puts "Generating #{guide} as #{output_file}"
|
2011-12-25 19:18:18 +08:00
|
|
|
layout = kindle? ? 'kindle/layout' : 'layout'
|
2011-12-25 09:26:18 +08:00
|
|
|
|
|
|
|
File.open(output_path, 'w') do |f|
|
2011-12-25 23:12:20 +08:00
|
|
|
view = ActionView::Base.new(source_dir, :edge => @edge, :version => @version, :mobi => "kindle/#{mobi}")
|
2010-03-15 16:57:08 +08:00
|
|
|
view.extend(Helpers)
|
2010-04-03 05:53:39 +08:00
|
|
|
|
2011-12-25 09:26:18 +08:00
|
|
|
if guide =~ /\.(\w+)\.erb$/
|
2010-04-03 05:53:39 +08:00
|
|
|
# Generate the special pages like the home.
|
2011-11-11 01:06:20 +08:00
|
|
|
# Passing a template handler in the template name is deprecated. So pass the file name without the extension.
|
2011-12-25 09:26:18 +08:00
|
|
|
result = view.render(:layout => layout, :formats => [$1], :file => $`)
|
2009-02-22 07:29:11 +08:00
|
|
|
else
|
2010-03-15 16:57:08 +08:00
|
|
|
body = File.read(File.join(source_dir, guide))
|
2012-09-07 12:09:56 +08:00
|
|
|
result = RailsGuides::Markdown.new(view, layout).render(body)
|
2010-02-18 03:22:37 +08:00
|
|
|
|
2010-04-07 06:20:27 +08:00
|
|
|
warn_about_broken_links(result) if @warnings
|
2009-02-22 07:29:11 +08:00
|
|
|
end
|
2010-04-01 06:20:43 +08:00
|
|
|
|
2011-12-25 09:26:18 +08:00
|
|
|
f.write(result)
|
2009-02-22 07:29:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-28 10:24:23 +08:00
|
|
|
def warn_about_broken_links(html)
|
2009-03-16 05:16:27 +08:00
|
|
|
anchors = extract_anchors(html)
|
|
|
|
check_fragment_identifiers(html, anchors)
|
|
|
|
end
|
2010-08-14 13:13:00 +08:00
|
|
|
|
2009-03-16 05:16:27 +08:00
|
|
|
def extract_anchors(html)
|
2012-09-07 12:09:56 +08:00
|
|
|
# Markdown generates headers with IDs computed from titles.
|
2009-03-16 05:16:27 +08:00
|
|
|
anchors = Set.new
|
|
|
|
html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
|
|
|
|
if anchors.member?(anchor)
|
2012-09-07 12:09:56 +08:00
|
|
|
puts "*** DUPLICATE ID: #{anchor}, please make sure that there're no headings with the same name at the same level."
|
2009-03-16 05:16:27 +08:00
|
|
|
else
|
|
|
|
anchors << anchor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-28 05:07:13 +08:00
|
|
|
# Footnotes.
|
2009-02-28 10:24:23 +08:00
|
|
|
anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
|
2010-08-28 05:07:13 +08:00
|
|
|
anchors += Set.new(html.scan(/<sup\s+class="footnote"\s+id="([^"]+)/).flatten)
|
2009-03-16 05:16:27 +08:00
|
|
|
return anchors
|
|
|
|
end
|
2010-08-14 13:13:00 +08:00
|
|
|
|
2009-03-16 05:16:27 +08:00
|
|
|
def check_fragment_identifiers(html, anchors)
|
2009-02-28 10:24:23 +08: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-16 03:03:37 +08:00
|
|
|
guess = anchors.min { |a, b|
|
|
|
|
Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
|
|
|
|
}
|
2009-03-24 20:10:38 +08:00
|
|
|
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
|
2009-02-28 10:24:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-04 09:44:58 +08:00
|
|
|
end
|
|
|
|
end
|