Module: Fuse

Defined in:
lib/fuse.rb,
lib/fuse/main.rb

Defined Under Namespace

Classes: Document, Exception, Server

Constant Summary collapse

VERSION =
'0.1.7'
LOG_COLOURS =
{
    info:     6, # cyan
    success:  2, # green
    error:    1, # red
    notice:   3  # yellow
}
DEFAULTS =
{
  common: {
    source: '.',
    encoding: 'UTF-8',
    preserve_white: true
  },
  server: {
      addr: '127.0.0.1',
      port: 9460,
      embed_assets: false
  },
  compile: {
      embed_assets: true,
      compress_assets: true
  }
}
SUMMARY_WIDTH =
30
SUMMARY_INDENT =
4

Class Method Summary collapse

Class Method Details

.log(message, type = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/fuse.rb', line 27

def self.log(message, type = nil)
  m = "\x1b["
  m << (30 + (LOG_COLOURS[type] || LOG_COLOURS[:info])).to_s
  m << 'm'
  m << message
  m << "\x1b[0m"
  log_file.puts m
end

.log_fileObject



23
24
25
# File 'lib/fuse.rb', line 23

def self.log_file
  @log_file ||= $stderr
end

.log_file=(file) ⇒ Object



19
20
21
# File 'lib/fuse.rb', line 19

def self.log_file=(file)
  @log_file = file
end

.mainObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/fuse/main.rb', line 26

def self.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

.rootObject



15
16
17
# File 'lib/fuse.rb', line 15

def self.root
  @root ||= File.expand_path File.dirname(__FILE__)
end