module Fuse

Constants

DEFAULTS
LOG_COLOURS
SUMMARY_INDENT
SUMMARY_WIDTH
VERSION

Public Class Methods

log(message, type = nil) click to toggle source
# File lib/fuse.rb, line 24
def log(message, type = nil)
  colour = 30 + (LOG_COLOURS[type] || LOG_COLOURS[:info])
  log_file.puts "\x1b[#{colour}m#{message}\x1b[0m"
end
log_file() click to toggle source
# File lib/fuse.rb, line 20
def log_file
  @log_file ||= $stderr
end
log_file=(file) click to toggle source
# File lib/fuse.rb, line 16
def log_file=(file)
  @log_file = file
end
main() click to toggle source
# File lib/fuse/main.rb, line 28
  def main

    options = {}

    options_parser = OptionParser.new do |opts|

      "

Usage: #{$0} command [options]

Commands:
    server  : Run a local testing server
    compile : Compile the document and send to STDOUT

Options:

      ".strip.lines.each { |line| opts.separator line }

      opts.on('-a',
              '--addr',
              wrap("Server binding address. Defaults to #{DEFAULTS[:server][:addr]}. Use 0.0.0.0 for access from other computers. Be careful; server will allow access to any locally accessible file of Fuse's supported types.")
      ) do |addr|
        options[:addr] = addr
      end

      opts.on('-c',
              '--[no-]compress-assets',
              'Compress assets.'
      ) do |compress|
        options[:compress_assets] = compress
      end

      opts.on('-e',
              '--encoding CHARSET',
              wrap("Output encoding. Default is #{DEFAULTS[:common][:encoding]}.")
      ) do |e|
        options[:encoding] = e
      end

      opts.on('-m',
              '--[no-]embed-assets',
              'Embed assets.'
      ) do |embed|
        options[:embed_assets] = embed
      end

      opts.on('-p',
              '--port PORT',
              Integer,
              wrap("Port on which to listen (only with 'server' command). Default is #{DEFAULTS[:server][:port]}.")
      ) do |port|
        options[:port] = port
      end

      opts.on('-s',
              '--source [FILE|DIR]',
              wrap('The source directory, or HTML or XML document. Default is current directory.')
      ) do |doc|
        options[:source] = doc
      end

      opts.on('-t',
              '--title TITLE',
              'HTML document title.'
      ) do |t|
        options[:title] = t
      end

      opts.on('-v',
              '--version',
              'Show Fuse version.'
      ) { abort Fuse::VERSION }

      opts.on('-w',
              '--[no-]preserve-white',
              wrap("Preserve all white space in HTML. Default is #{DEFAULTS[:common][:preserve_white]}.")
      ) { |w| options[:preserve_white] = w }

      opts.on('-x',
              '--xsl FILE',
              wrap('XSL transformation stylesheet. Default is current directory.')
      ) do |xsl|
        abort "#{xsl} isn't a valid XSL stylesheet" unless xsl.match(/\.xsl$/i)
        options[:xsl] = xsl
      end

      opts.on_tail('-h', '--help', 'Show this message.') { summary opts }

    end

    options_parser.parse!

    options = merge_defaults(options)

    case ARGV[0]

      when 'server'
        Thin::Server.start(options[:addr], options[:port]) do
          use Rack::ShowExceptions
          run Server.new(options)
        end

      when 'compile'
        begin
          doc = Document.new(options)
          log "Compiling #{doc.source_path}"
          out = doc.to_s
          print out
          log "Wrote #{out.bytesize} byte(s) to STDOUT.", :success
        rescue Exception::SourceUnknown::TooManySources
          log "Found more than one potential #{$!.option_name} document. Please specify one with --#{$!.option_name}.", :notice
          log $!.options.join "\n"
        rescue Exception
          $!.message ? log($!.message, :error) : raise
        end

      else
        summary options_parser

    end

  end
root() click to toggle source
# File lib/fuse.rb, line 12
def root
  @root ||= File.expand_path File.dirname(__FILE__)
end

Private Class Methods

merge_defaults(options) click to toggle source
# File lib/fuse/main.rb, line 153
def merge_defaults(options)
  if (defaults = DEFAULTS[(ARGV[0] || '').to_sym])
    defaults.merge(DEFAULTS[:common]).merge(options)
  else
    options
  end
end
summary(opts) click to toggle source
# File lib/fuse/main.rb, line 165
def summary(opts)
  abort opts.summarize([], SUMMARY_WIDTH, SUMMARY_WIDTH - 1, ' ' * SUMMARY_INDENT).join
end
wrap(text, width = 80 - SUMMARY_WIDTH) click to toggle source
# File lib/fuse/main.rb, line 161
def wrap(text, width = 80 - SUMMARY_WIDTH)
  text.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n#{' ' * (SUMMARY_WIDTH + SUMMARY_INDENT + 1)}").strip
end