aboutsummaryrefslogtreecommitdiffstats
path: root/guides/rails_guides/generator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'guides/rails_guides/generator.rb')
-rw-r--r--guides/rails_guides/generator.rb220
1 files changed, 110 insertions, 110 deletions
diff --git a/guides/rails_guides/generator.rb b/guides/rails_guides/generator.rb
index 881bf2174b..39a57191eb 100644
--- a/guides/rails_guides/generator.rb
+++ b/guides/rails_guides/generator.rb
@@ -102,148 +102,148 @@ module RailsGuides
private
- def kindle?
- @kindle
- end
+ def kindle?
+ @kindle
+ end
- def check_for_kindlegen
- if `which kindlegen`.blank?
- raise "Can't create a kindle version without `kindlegen`."
+ def check_for_kindlegen
+ if `which kindlegen`.blank?
+ raise "Can't create a kindle version without `kindlegen`."
+ end
end
- end
- def generate_mobi
- require "rails_guides/kindle"
- out = "#{output_dir}/kindlegen.out"
- Kindle.generate(output_dir, mobi, out)
- puts "(kindlegen log at #{out})."
- end
+ def generate_mobi
+ require "rails_guides/kindle"
+ out = "#{output_dir}/kindlegen.out"
+ Kindle.generate(output_dir, mobi, out)
+ puts "(kindlegen log at #{out})."
+ end
- def mobi
- "ruby_on_rails_guides_#@version%s.mobi" % (@lang.present? ? ".#@lang" : "")
- end
+ def mobi
+ "ruby_on_rails_guides_#@version%s.mobi" % (@lang.present? ? ".#@lang" : "")
+ end
- def initialize_dirs(output)
- @guides_dir = File.join(File.dirname(__FILE__), "..")
- @source_dir = "#@guides_dir/source/#@lang"
- @output_dir = if output
- output
- elsif kindle?
- "#@guides_dir/output/kindle/#@lang"
- else
- "#@guides_dir/output/#@lang"
- end.sub(%r</$>, "")
- end
+ def initialize_dirs(output)
+ @guides_dir = File.join(File.dirname(__FILE__), "..")
+ @source_dir = "#@guides_dir/source/#@lang"
+ @output_dir = if output
+ output
+ elsif kindle?
+ "#@guides_dir/output/kindle/#@lang"
+ else
+ "#@guides_dir/output/#@lang"
+ end.sub(%r</$>, "")
+ end
- def create_output_dir_if_needed
- FileUtils.mkdir_p(output_dir)
- end
+ def create_output_dir_if_needed
+ FileUtils.mkdir_p(output_dir)
+ end
- 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)
+ 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
end
- end
- def guides_to_generate
- guides = Dir.entries(source_dir).grep(GUIDES_RE)
+ def guides_to_generate
+ guides = Dir.entries(source_dir).grep(GUIDES_RE)
- if kindle?
- Dir.entries("#{source_dir}/kindle").grep(GUIDES_RE).map do |entry|
- next if entry == "KINDLE.md"
- guides << "kindle/#{entry}"
+ if kindle?
+ Dir.entries("#{source_dir}/kindle").grep(GUIDES_RE).map do |entry|
+ next if entry == "KINDLE.md"
+ guides << "kindle/#{entry}"
+ end
end
+
+ ENV.key?("ONLY") ? select_only(guides) : guides
end
- ENV.key?("ONLY") ? select_only(guides) : guides
- end
+ def select_only(guides)
+ prefixes = ENV["ONLY"].split(",").map(&:strip)
+ guides.select do |guide|
+ guide.start_with?("kindle".freeze, *prefixes)
+ end
+ end
- def select_only(guides)
- prefixes = ENV["ONLY"].split(",").map(&:strip)
- guides.select do |guide|
- guide.start_with?("kindle".freeze, *prefixes)
+ def copy_assets
+ FileUtils.cp_r(Dir.glob("#{guides_dir}/assets/*"), output_dir)
end
- end
- def copy_assets
- FileUtils.cp_r(Dir.glob("#{guides_dir}/assets/*"), output_dir)
- end
+ def output_file_for(guide)
+ if guide.end_with?(".md")
+ guide.sub(/md\z/, "html")
+ else
+ guide.sub(/\.erb\z/, "")
+ end
+ end
- def output_file_for(guide)
- if guide.end_with?(".md")
- guide.sub(/md\z/, "html")
- else
- guide.sub(/\.erb\z/, "")
+ def output_path_for(output_file)
+ File.join(output_dir, File.basename(output_file))
end
- end
- def output_path_for(output_file)
- File.join(output_dir, File.basename(output_file))
- end
+ def generate?(source_file, output_file)
+ fin = File.join(source_dir, source_file)
+ fout = output_path_for(output_file)
+ all || !File.exist?(fout) || File.mtime(fout) < File.mtime(fin)
+ end
- def generate?(source_file, output_file)
- fin = File.join(source_dir, source_file)
- fout = output_path_for(output_file)
- all || !File.exist?(fout) || File.mtime(fout) < File.mtime(fin)
- end
+ def generate_guide(guide, output_file)
+ output_path = output_path_for(output_file)
+ puts "Generating #{guide} as #{output_file}"
+ layout = kindle? ? "kindle/layout" : "layout"
- def generate_guide(guide, output_file)
- output_path = output_path_for(output_file)
- puts "Generating #{guide} as #{output_file}"
- layout = kindle? ? "kindle/layout" : "layout"
+ File.open(output_path, "w") do |f|
+ view = ActionView::Base.new(source_dir, edge: @edge, version: @version, mobi: "kindle/#{mobi}", lang: @lang)
+ view.extend(Helpers)
- File.open(output_path, "w") do |f|
- view = ActionView::Base.new(source_dir, edge: @edge, version: @version, mobi: "kindle/#{mobi}", lang: @lang)
- view.extend(Helpers)
+ if guide =~ /\.(\w+)\.erb$/
+ # 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.
+ result = view.render(layout: layout, formats: [$1], file: $`)
+ else
+ body = File.read(File.join(source_dir, guide))
+ result = RailsGuides::Markdown.new(view, layout).render(body)
- if guide =~ /\.(\w+)\.erb$/
- # 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.
- result = view.render(layout: layout, formats: [$1], file: $`)
- else
- body = File.read(File.join(source_dir, guide))
- result = RailsGuides::Markdown.new(view, layout).render(body)
+ warn_about_broken_links(result) if @warnings
+ end
- warn_about_broken_links(result) if @warnings
+ f.write(result)
end
-
- f.write(result)
end
- end
- def warn_about_broken_links(html)
- anchors = extract_anchors(html)
- check_fragment_identifiers(html, anchors)
- end
+ def warn_about_broken_links(html)
+ anchors = extract_anchors(html)
+ check_fragment_identifiers(html, anchors)
+ end
- 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
+ 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
end
- end
- # 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)
- return anchors
- end
+ # 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)
+ 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}."
+ 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
end