2017-08-13 09:02:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:21:59 -04:00
|
|
|
require "set"
|
|
|
|
require "fileutils"
|
2009-02-27 21:24:23 -05:00
|
|
|
|
2016-08-06 13:21:59 -04:00
|
|
|
require "active_support/core_ext/string/output_safety"
|
|
|
|
require "active_support/core_ext/object/blank"
|
|
|
|
require "action_controller"
|
|
|
|
require "action_view"
|
2010-03-15 17:06:27 -04:00
|
|
|
|
2016-08-06 13:21:59 -04:00
|
|
|
require "rails_guides/markdown"
|
|
|
|
require "rails_guides/indexer"
|
|
|
|
require "rails_guides/helpers"
|
|
|
|
require "rails_guides/levenshtein"
|
2010-03-15 17:06:27 -04:00
|
|
|
|
2009-02-03 20:44:58 -05:00
|
|
|
module RailsGuides
|
|
|
|
class Generator
|
2012-09-18 17:04:12 -04:00
|
|
|
GUIDES_RE = /\.(?:erb|md)\z/
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2018-12-04 19:55:22 -05:00
|
|
|
def initialize(edge:, version:, all:, only:, kindle:, language:, direction: "ltr")
|
2018-11-22 14:48:25 -05:00
|
|
|
@edge = edge
|
|
|
|
@version = version
|
|
|
|
@all = all
|
|
|
|
@only = only
|
|
|
|
@kindle = kindle
|
|
|
|
@language = language
|
|
|
|
@direction = direction
|
2011-12-24 20:26:18 -05:00
|
|
|
|
2017-02-12 04:21:20 -05:00
|
|
|
if @kindle
|
2011-12-24 20:26:18 -05:00
|
|
|
check_for_kindlegen
|
|
|
|
register_kindle_mime_types
|
|
|
|
end
|
|
|
|
|
2017-02-12 04:21:20 -05:00
|
|
|
initialize_dirs
|
2010-03-17 18:03:48 -04:00
|
|
|
create_output_dir_if_needed
|
2017-02-12 04:21:20 -05:00
|
|
|
initialize_markdown_renderer
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
2010-03-15 04:57:08 -04:00
|
|
|
generate_guides
|
|
|
|
copy_assets
|
2017-02-12 04:21:20 -05:00
|
|
|
generate_mobi if @kindle
|
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
|
|
|
private
|
2017-02-12 04:21:20 -05:00
|
|
|
def register_kindle_mime_types
|
|
|
|
Mime::Type.register_alias("application/xml", :opf, %w(opf))
|
|
|
|
Mime::Type.register_alias("application/xml", :ncx, %w(ncx))
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2011-12-24 20:26:18 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def check_for_kindlegen
|
|
|
|
if `which kindlegen`.blank?
|
|
|
|
raise "Can't create a kindle version without `kindlegen`."
|
|
|
|
end
|
2011-12-24 20:26:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def generate_mobi
|
|
|
|
require "rails_guides/kindle"
|
2017-02-12 04:21:20 -05:00
|
|
|
out = "#{@output_dir}/kindlegen.out"
|
|
|
|
Kindle.generate(@output_dir, mobi, out)
|
2016-08-06 13:55:02 -04:00
|
|
|
puts "(kindlegen log at #{out})."
|
|
|
|
end
|
2011-12-24 20:26:18 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def mobi
|
2019-01-09 04:09:01 -05:00
|
|
|
mobi = +"ruby_on_rails_guides_#{@version || @edge[0, 7]}"
|
|
|
|
mobi << ".#{@language}" if @language
|
|
|
|
mobi << ".mobi"
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2011-12-25 07:07:49 -05:00
|
|
|
|
2017-02-12 04:21:20 -05:00
|
|
|
def initialize_dirs
|
|
|
|
@guides_dir = File.expand_path("..", __dir__)
|
|
|
|
|
|
|
|
@source_dir = "#{@guides_dir}/source"
|
|
|
|
@source_dir += "/#{@language}" if @language
|
|
|
|
|
|
|
|
@output_dir = "#{@guides_dir}/output"
|
|
|
|
@output_dir += "/kindle" if @kindle
|
2017-03-07 20:54:06 -05:00
|
|
|
@output_dir += "/#{@language}" if @language
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def create_output_dir_if_needed
|
2017-02-12 04:21:20 -05:00
|
|
|
FileUtils.mkdir_p(@output_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_markdown_renderer
|
|
|
|
Markdown::Renderer.edge = @edge
|
|
|
|
Markdown::Renderer.version = @version
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2010-04-02 17:53:39 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def generate_guides
|
|
|
|
guides_to_generate.each do |guide|
|
|
|
|
output_file = output_file_for(guide)
|
|
|
|
generate_guide(guide, output_file) if generate?(guide, output_file)
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def guides_to_generate
|
2017-02-12 04:21:20 -05:00
|
|
|
guides = Dir.entries(@source_dir).grep(GUIDES_RE)
|
2011-12-24 20:26:18 -05:00
|
|
|
|
2017-02-12 04:21:20 -05:00
|
|
|
if @kindle
|
|
|
|
Dir.entries("#{@source_dir}/kindle").grep(GUIDES_RE).map do |entry|
|
2016-08-06 13:55:02 -04:00
|
|
|
next if entry == "KINDLE.md"
|
|
|
|
guides << "kindle/#{entry}"
|
|
|
|
end
|
2011-12-24 20:26:18 -05:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
|
2017-02-12 04:21:20 -05:00
|
|
|
@only ? select_only(guides) : guides
|
2011-12-24 20:26:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def select_only(guides)
|
2017-02-12 04:21:20 -05:00
|
|
|
prefixes = @only.split(",").map(&:strip)
|
2016-08-06 13:55:02 -04:00
|
|
|
guides.select do |guide|
|
2017-02-12 04:21:20 -05:00
|
|
|
guide.start_with?("kindle", *prefixes)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def copy_assets
|
2017-02-12 04:21:20 -05:00
|
|
|
FileUtils.cp_r(Dir.glob("#{@guides_dir}/assets/*"), @output_dir)
|
2018-11-19 13:19:38 -05:00
|
|
|
|
2018-12-04 19:55:22 -05:00
|
|
|
if @direction == "rtl"
|
2018-11-22 14:48:25 -05:00
|
|
|
overwrite_css_with_right_to_left_direction
|
2018-11-19 11:59:52 -05:00
|
|
|
end
|
2010-03-15 04:57:08 -04:00
|
|
|
end
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2018-11-22 14:48:25 -05:00
|
|
|
def overwrite_css_with_right_to_left_direction
|
|
|
|
FileUtils.mv("#{@output_dir}/stylesheets/main.rtl.css", "#{@output_dir}/stylesheets/main.css")
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def output_file_for(guide)
|
|
|
|
if guide.end_with?(".md")
|
|
|
|
guide.sub(/md\z/, "html")
|
|
|
|
else
|
|
|
|
guide.sub(/\.erb\z/, "")
|
|
|
|
end
|
|
|
|
end
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def output_path_for(output_file)
|
2017-02-12 04:21:20 -05:00
|
|
|
File.join(@output_dir, File.basename(output_file))
|
2011-12-24 20:26:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def generate?(source_file, output_file)
|
2017-02-12 04:21:20 -05:00
|
|
|
fin = File.join(@source_dir, source_file)
|
2016-08-06 13:55:02 -04:00
|
|
|
fout = output_path_for(output_file)
|
2017-02-12 04:21:20 -05:00
|
|
|
@all || !File.exist?(fout) || File.mtime(fout) < File.mtime(fin)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def generate_guide(guide, output_file)
|
|
|
|
output_path = output_path_for(output_file)
|
|
|
|
puts "Generating #{guide} as #{output_file}"
|
2017-02-12 04:21:20 -05:00
|
|
|
layout = @kindle ? "kindle/layout" : "layout"
|
2009-02-21 18:29:11 -05:00
|
|
|
|
2019-03-12 06:52:22 -04:00
|
|
|
view = ActionView::Base.with_empty_template_cache.with_view_paths(
|
2019-01-29 18:17:52 -05:00
|
|
|
[@source_dir],
|
2018-04-02 05:30:23 -04:00
|
|
|
edge: @edge,
|
|
|
|
version: @version,
|
|
|
|
mobi: "kindle/#{mobi}",
|
|
|
|
language: @language
|
|
|
|
)
|
|
|
|
view.extend(Helpers)
|
|
|
|
|
|
|
|
if guide =~ /\.(\w+)\.erb$/
|
|
|
|
return if %w[_license _welcome layout].include?($`)
|
|
|
|
|
|
|
|
# Generate the special pages like the home.
|
|
|
|
# Passing a template handler in the template name is deprecated. So pass the file name without the extension.
|
2019-03-22 13:45:22 -04:00
|
|
|
result = view.render(layout: layout, formats: [$1.to_sym], file: $`)
|
2018-04-02 05:30:23 -04:00
|
|
|
else
|
|
|
|
body = File.read("#{@source_dir}/#{guide}")
|
|
|
|
result = RailsGuides::Markdown.new(
|
|
|
|
view: view,
|
|
|
|
layout: layout,
|
|
|
|
edge: @edge,
|
|
|
|
version: @version
|
|
|
|
).render(body)
|
|
|
|
|
|
|
|
warn_about_broken_links(result)
|
|
|
|
end
|
2010-02-17 14:22:37 -05:00
|
|
|
|
2018-04-02 05:30:23 -04:00
|
|
|
File.open(output_path, "w") do |f|
|
2016-08-06 13:55:02 -04:00
|
|
|
f.write(result)
|
2009-02-21 18:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def warn_about_broken_links(html)
|
|
|
|
anchors = extract_anchors(html)
|
|
|
|
check_fragment_identifiers(html, anchors)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def extract_anchors(html)
|
|
|
|
# Markdown 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 ID: #{anchor}, please make sure that there're no headings with the same name at the same level."
|
|
|
|
else
|
|
|
|
anchors << anchor
|
|
|
|
end
|
2009-03-15 17:16:27 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
# Footnotes.
|
|
|
|
anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
|
|
|
|
anchors += Set.new(html.scan(/<sup\s+class="footnote"\s+id="([^"]+)/).flatten)
|
2017-02-12 04:21:20 -05:00
|
|
|
anchors
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
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
|
2018-11-19 11:59:52 -05:00
|
|
|
unless anchors.member?(CGI.unescape(fragment_identifier))
|
2016-08-06 13:55:02 -04:00
|
|
|
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
|
2009-02-27 21:24:23 -05:00
|
|
|
end
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
end
|