+#!/usr/bin/env ruby
+
require 'pathname'
-require 'fileutils'
require 'yaml'
-require 'liquid'
require 'optparse'
+begin require 'liquid'
+rescue LoadError
+ puts "Please install the 'liquid' Ruby gem (available in Debian/Ubuntu as 'ruby-liquid')"
+ exit 1
+end
+
CONFIG = {
pages_dir: '_manual',
layouts_dir: '_layouts',
static_dir: 'source',
- output_dir: '_site' # will get wiped!
+ output_dir: '_site'
}
def child_url?(a, b)
end
def copy_static()
- `rsync -a --delete --exclude='*~' #{static_dir}/. #{output_dir}`
+ unless system("rsync -a --delete --exclude='*~' #{static_dir}/. #{output_dir}")
+ puts "Couldn't copy static files, is rsync installed?"
+ end
end
def find_children(url)
@site = site
@path = path
- canon = canonical
- @out_path = @site.output_dir + canon + Pathname("index.html")
- @url = '/' + canon + '/'
- @sort_url = @path.to_s.sub(/\.html$/, '')
- end
+ relative_path = @path.relative_path_from(@site.pages_dir);
+ a = relative_path.each_filename.map do |x|
+ x.sub(/^[0-9]*[-_]/, '')
+ end
+ a[-1].sub!(/\.html$/, '')
+ s = a.join('/')
- def canonical()
- remove_numbers = lambda {|x| x.sub(/^[0-9]*[-_]/, '') }
- path = @path.relative_path_from(@site.pages_dir)
- a = path.each_filename.map(&remove_numbers)
- a[-1] = a[-1].sub(/\.html$/, '')
- a.join('/')
+ @out_path = @site.output_dir + Pathname(s) + Pathname("index.html")
+ @url = "/#{s}/"
+ @sort_url = @path.to_s.sub(/\.html$/, '')
end
def related_to?(p)
class Server
def start_watcher()
- require 'listen'
+ begin require 'listen'
+ rescue LoadError
+ puts "To use the --watch function, please install the 'listen' Ruby gem"
+ puts "(available in Debian/Ubuntu as 'ruby-listen')"
+ return nil
+ end
listener = Listen.to(CONFIG[:pages_dir], wait_for_delay: 1.0, only: /.html$/) do |modified, added, removed|
Site.new.build
def run(options)
require 'webrick'
listener = options[:watch] && start_watcher
+ port = options[:port] || 8000
- puts "Serving at http://localhost:8000/ ..."
- server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => CONFIG[:output_dir]
+ puts "Serving at http://localhost:#{port}/ ..."
+ server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => CONFIG[:output_dir]
trap 'INT' do
server.shutdown
end
options = {}
OptionParser.new do |opts|
- opts.on("--watch", "Watch for changes") { options[:watch] = true }
+ opts.banner = %{Usage: build.rb <command> [options]
+
+Use 'build.rb' to build the manual. Use 'build.rb serve' to also
+start a web server; setting any web server options implies "serve".
+}
+ opts.on("-w", "--watch", "Watch for changes") { options[:watch] = true }
+ opts.on("-p", "--port N", Integer, "Specify port for web server") { |p| options[:port] = p }
end.parse!
Site.new.build
- if ARGV == ['serve']
+ if options[:watch] || options[:port] || (ARGV.length > 0 && "serve".start_with?(ARGV[0]))
Server.new.run(options)
end
end