lib/hayde/generator.rb in hayde-0.1.8 vs lib/hayde/generator.rb in hayde-0.1.9
- old
+ new
@@ -8,11 +8,11 @@
# part of the generation process.
#
# Options:
#
# :warnings
-# If you are writing a guide, please work always with :warnings = true.
+# If you are writing a guide, please work always with :warnings = true.
# Users can generate the guides, and thus this flag is off by default.
#
# 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.
@@ -46,42 +46,42 @@
require 'hayde/file_utils'
module Hayde
class Generator
include Hayde::Utils::Files
-
+
attr_accessor :output_dir, :assets_dir, :warnings, :edge, :force, :layout
filelist_attribute :sources
GUIDES_RE = /\.(?:textile|html\.erb)$/
- def initialize(output = nil)
- set_defaults(output)
+ def initialize
+ set_defaults
yield self if block_given?
+ FileUtils.mkdir_p(@output_dir)
end
def generate
puts "Generating guides has been started."
generate_guides
puts "Copying assets ..."
copy_assets
puts "Generating guides has been finished."
end
-
+
def clean
FileUtils.rm_r Dir.glob(File.join(output_dir, '*'))
end
private
-
- def set_defaults(output)
+
+ def set_defaults
root_dir = Rails.root if defined? Rails
root_dir ||= Dir.pwd
@layout = 'layout'
+ @output_dir = File.join(root_dir, 'docs', 'guides')
@assets_dir = File.join(root_dir, 'guides', 'assets')
- @output_dir = output || File.join(root_dir, 'docs', 'guides')
- FileUtils.mkdir_p(@output_dir)
end
def generate_guides
sources.each do |source|
output = output_file_for(source)
@@ -90,18 +90,18 @@
end
def copy_assets
copy(assets_dir, output_dir, /\.$|\.svn/)
end
-
+
def copy(source, destination, exclude)
Dir.foreach(source) do |file|
next if exclude && exclude.match(file)
source_file = File.join(source, file)
destination_file = File.join(destination, file)
-
+
if File.directory?(source_file)
FileUtils.mkdir(destination_file) if !File.directory?(destination_file)
copy(source_file, destination_file, exclude)
else
FileUtils.cp(source_file, destination_file)
@@ -111,11 +111,11 @@
def output_file_for(source)
output = File.basename(source).sub(GUIDES_RE, '.html')
File.join(output_dir, output)
end
-
+
def generate?(source, output)
force || !File.exists?(output) || File.mtime(output) < File.mtime(source)
end
def generate_guide(source, output)
@@ -206,23 +206,23 @@
es = ERB::Util.h($2)
css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
code_blocks << %{<div class="code_container"><code class="#{css_class}">#{es}</code></div>}
"\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n"
end
-
+
body = yield body
-
+
body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|
code_blocks[$1.to_i]
end
end
def warn_about_broken_links(html)
anchors = extract_anchors(html)
check_fragment_identifiers(html, anchors)
end
-
+
def extract_anchors(html)
# Textile generates headers with IDs computed from titles.
anchors = Set.new
html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
if anchors.member?(anchor)
@@ -234,11 +234,11 @@
# Also, footnotes are rendered as paragraphs this way.
anchors += Set.new(html.scan(/<p\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|
@@ -248,5 +248,6 @@
end
end
end
end
end
+