implement the file switcher thingy

This commit is contained in:
Ryan Tomayko 2010-03-11 08:01:46 -08:00
parent 27b2ee03db
commit 832048d946
5 changed files with 51 additions and 10 deletions

View File

@ -27,6 +27,7 @@ end
# Parse command line options, aborting if anything goes wrong. # Parse command line options, aborting if anything goes wrong.
output_dir = '.' output_dir = '.'
sources = []
ARGV.options { |o| ARGV.options { |o|
o.program_name = File.basename($0) o.program_name = File.basename($0)
o.on("-o", "--output=DIR") { |dir| output_dir = dir } o.on("-o", "--output=DIR") { |dir| output_dir = dir }
@ -34,6 +35,14 @@ ARGV.options { |o|
o.parse! o.parse!
} or abort_with_note } 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 <file>s given"
end
# What a fucking mess. Most of this is duplicated in rocco.rb too. # What a fucking mess. Most of this is duplicated in rocco.rb too.
libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '') libdir = File.expand_path('../../lib', __FILE__).sub(/^#{Dir.pwd}\//, '')
begin begin
@ -65,9 +74,10 @@ end
# Create the output directory if it doesn't already exist. # Create the output directory if it doesn't already exist.
Dir.mkdir output_dir if !File.directory?(output_dir) Dir.mkdir output_dir if !File.directory?(output_dir)
ARGV.each do |filename| # Run each file through Rocco and write output.
rocco = Rocco.new(filename) sources.each do |filename|
dest = "#{output_dir}/#{File.basename(filename, '.rb') + '.html'}" rocco = Rocco.new(filename, sources)
warn "rocco: #{filename} -> #{dest}" dest = File.join(output_dir, File.basename(filename, '.rb') + '.html')
puts "rocco: #{filename} -> #{dest}"
File.open(dest, 'wb') { |fd| fd.write(rocco.to_html) } File.open(dest, 'wb') { |fd| fd.write(rocco.to_html) }
end end

View File

@ -56,14 +56,14 @@ end
#### Public Interface #### Public Interface
# `Rocco.new` takes a source `filename` and an optional `block`. # `Rocco.new` takes a source `filename`, an optional list of source filenames
# When `block` is given, it must read the contents of the file using # for other documentation sources, and an optional `block`. When `block` is
# whatever means necessary and return it as a string. With no `block`, the # given, it must read the contents of the file using whatever means necessary
# file is read to retrieve data. # and return it as a string. With no `block`, the file is read to retrieve data.
class Rocco class Rocco
VERSION = '0.2' VERSION = '0.2'
def initialize(filename, &block) def initialize(filename, sources=[], &block)
@file = filename @file = filename
@data = @data =
if block_given? if block_given?
@ -71,6 +71,7 @@ class Rocco
else else
File.read(filename) File.read(filename)
end end
@sources = sources
@sections = highlight(split(parse(@data))) @sections = highlight(split(parse(@data)))
end end
@ -83,6 +84,10 @@ class Rocco
# respectively. # respectively.
attr_reader :sections 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. # Generate HTML output for the entire document.
require 'rocco/layout' require 'rocco/layout'
def to_html def to_html

View File

@ -8,6 +8,18 @@
<body> <body>
<div id='container'> <div id='container'>
<div id="background"></div> <div id="background"></div>
{{#sources?}}
<div id="jump_to">
Jump To &hellip;
<div id="jump_wrapper">
<div id="jump_page">
{{#sources}}
<a class="source" href="{{ url }}">{{ basename }}</a>
{{/sources}}
</div>
</div>
</div>
{{/sources?}}
<table cellspacing=0 cellpadding=0> <table cellspacing=0 cellpadding=0>
<thead> <thead>
<tr> <tr>

View File

@ -21,4 +21,18 @@ class Rocco::Layout < Mustache
} }
end end
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 end

View File

@ -92,7 +92,7 @@ class Rocco
prerequisites = [@dest, source_file] + rocco_source_files prerequisites = [@dest, source_file] + rocco_source_files
file dest_file => prerequisites do |f| file dest_file => prerequisites do |f|
verbose { puts "rocco: #{source_file} -> #{dest_file}" } 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) } File.open(dest_file, 'wb') { |fd| fd.write(rocco.to_html) }
end end
task @name => dest_file task @name => dest_file