From 832048d946fa59930d86dfefbe653916c81c122c Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Thu, 11 Mar 2010 08:01:46 -0800 Subject: [PATCH] implement the file switcher thingy --- bin/rocco | 18 ++++++++++++++---- lib/rocco.rb | 15 ++++++++++----- lib/rocco/layout.mustache | 12 ++++++++++++ lib/rocco/layout.rb | 14 ++++++++++++++ lib/rocco/tasks.rb | 2 +- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/bin/rocco b/bin/rocco index c88c116..5ba062c 100755 --- a/bin/rocco +++ b/bin/rocco @@ -27,6 +27,7 @@ end # Parse command line options, aborting if anything goes wrong. output_dir = '.' +sources = [] ARGV.options { |o| o.program_name = File.basename($0) o.on("-o", "--output=DIR") { |dir| output_dir = dir } @@ -34,6 +35,14 @@ ARGV.options { |o| o.parse! } or abort_with_note +# Eat sources from ARGV. +sources << ARGV.shift while ARGV.any? + +# Make sure we have some files to work with. +if sources.empty? + abort_with_note "#{File.basename($0)}: no input s given" +end + # What a fucking mess. Most of this is duplicated in rocco.rb too. libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '') begin @@ -65,9 +74,10 @@ end # Create the output directory if it doesn't already exist. Dir.mkdir output_dir if !File.directory?(output_dir) -ARGV.each do |filename| - rocco = Rocco.new(filename) - dest = "#{output_dir}/#{File.basename(filename, '.rb') + '.html'}" - warn "rocco: #{filename} -> #{dest}" +# Run each file through Rocco and write output. +sources.each do |filename| + rocco = Rocco.new(filename, sources) + dest = File.join(output_dir, File.basename(filename, '.rb') + '.html') + puts "rocco: #{filename} -> #{dest}" File.open(dest, 'wb') { |fd| fd.write(rocco.to_html) } end diff --git a/lib/rocco.rb b/lib/rocco.rb index dcff2b9..6dd6d2c 100644 --- a/lib/rocco.rb +++ b/lib/rocco.rb @@ -56,14 +56,14 @@ end #### Public Interface -# `Rocco.new` takes a source `filename` and an optional `block`. -# When `block` is given, it must read the contents of the file using -# whatever means necessary and return it as a string. With no `block`, the -# file is read to retrieve data. +# `Rocco.new` takes a source `filename`, an optional list of source filenames +# for other documentation sources, and an optional `block`. When `block` is +# given, it must read the contents of the file using whatever means necessary +# and return it as a string. With no `block`, the file is read to retrieve data. class Rocco VERSION = '0.2' - def initialize(filename, &block) + def initialize(filename, sources=[], &block) @file = filename @data = if block_given? @@ -71,6 +71,7 @@ class Rocco else File.read(filename) end + @sources = sources @sections = highlight(split(parse(@data))) end @@ -83,6 +84,10 @@ class Rocco # respectively. attr_reader :sections + # A list of all source filenames included in the documentation set. Useful + # for building an index of other files. + attr_reader :sources + # Generate HTML output for the entire document. require 'rocco/layout' def to_html diff --git a/lib/rocco/layout.mustache b/lib/rocco/layout.mustache index 25f8469..48c4af0 100644 --- a/lib/rocco/layout.mustache +++ b/lib/rocco/layout.mustache @@ -8,6 +8,18 @@
+ {{#sources?}} +
+ Jump To … +
+
+ {{#sources}} + {{ basename }} + {{/sources}} +
+
+
+ {{/sources?}} diff --git a/lib/rocco/layout.rb b/lib/rocco/layout.rb index bea5511..a2d7a80 100644 --- a/lib/rocco/layout.rb +++ b/lib/rocco/layout.rb @@ -21,4 +21,18 @@ class Rocco::Layout < Mustache } end end + + def sources? + @doc.sources.length > 1 + end + + def sources + @doc.sources.sort.map do |source| + { + :path => source, + :basename => File.basename(source), + :url => File.basename(source, '.rb') + '.html' + } + end + end end diff --git a/lib/rocco/tasks.rb b/lib/rocco/tasks.rb index 700768f..286eb54 100644 --- a/lib/rocco/tasks.rb +++ b/lib/rocco/tasks.rb @@ -92,7 +92,7 @@ class Rocco prerequisites = [@dest, source_file] + rocco_source_files file dest_file => prerequisites do |f| verbose { puts "rocco: #{source_file} -> #{dest_file}" } - rocco = Rocco.new(source_file) + rocco = Rocco.new(source_file, @sources.to_a) File.open(dest_file, 'wb') { |fd| fd.write(rocco.to_html) } end task @name => dest_file