]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - build.rb
Getting rid of Using This Documentation and placing subs elsewhere
[ardour-manual-diverged] / build.rb
old mode 100644 (file)
new mode 100755 (executable)
index 4edb695..82693f1
--- a/build.rb
+++ b/build.rb
@@ -1,14 +1,20 @@
+#!/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)
@@ -58,7 +64,9 @@ class Site
     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)
@@ -81,18 +89,16 @@ class Page
         @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)
@@ -220,7 +226,12 @@ end
 
 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
@@ -232,9 +243,10 @@ class Server
     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
@@ -254,12 +266,18 @@ def main
 
     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