manveru / nagoro

A XHTML templating engine written in Ruby based on so-called pipes

This URL has Read+Write access

manveru (author)
Sun Jul 05 17:28:12 -0700 2009
commit  bb4fef37b8d15984ddbd8bda4233a876289ebbe9
tree    3d017454b4d70b38726f8e288ad85525a0f57757
parent  8f7ed2abe62dbc2360b491f8c872e244a24bc3ba
nagoro / bin / nagoro
100755 84 lines (68 sloc) 1.864 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
#!/usr/bin/env ruby
 
NAGORO_PATH = File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
$LOAD_PATH.unshift(NAGORO_PATH)
 
require 'nagoro/version'
 
require 'optparse'
options = {}
 
optp = OptionParser.new{|opt|
  ruby_version = "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
  nagoro_version = "Nagoro #{Nagoro::VERSION}, on #{ruby_version}"
 
  opt.banner = "Usage: nagoro [options] file1.xhtml file2.xhtml ..."
  opt.define_head nagoro_version
 
  opt.separator ''
  opt.separator 'General Options:'
 
  opt.on('-v', '--version', 'Show Version') do |v|
    puts nagoro_version
    exit
  end
  opt.on('-h', '--help', 'Show this help') do |h|
    puts opt
    exit
  end
  opt.on('-I', '--include PATH', 'Additional path for $LOAD_PATH') do |r|
    options[:include] = r
  end
}
optp.parse!
puts optp if ARGV.empty?
 
if rexml = options[:include]
  $LOAD_PATH.unshift(rexml)
end
 
begin
  require 'rexml/formatters/pretty'
  options[:pretty_available] = true
 
  # TODO:
  # * Happy monkeypatching a bug in REXML, gotta inform dev.
  module REXML
    module Formatters
      class Default
        unless defined?(old_write)
          alias old_write write
 
          WRITE_ARITY = new.method(:write).arity
 
          def write(*args)
            old_write(*args[0...WRITE_ARITY])
          end
        end
      end
    end
  end
rescue LoadError => ex
end
 
require 'nagoro'
require 'rexml/document'
 
ARGV.each do |arg|
  if File.file?(arg)
    xhtml = Nagoro.render(arg, :binding => binding)
    doc = REXML::Document.new(xhtml)
 
    if options[:pretty_available]
      pretty = REXML::Formatters::Pretty.new(2, ie_hack = true)
      # pretty.width = 73
      pretty.compact = true
      out = ''
      pretty.write(doc, out)
      print out
    else
      doc.write($stdout, indent = 1, transitive = false, ie_hack = true)
    end
  end
end