Class: YDocx::Command
- Inherits:
-
Object
- Object
- YDocx::Command
- Defined in:
- lib/ydocx/command.rb
Constant Summary collapse
- @@help =
/^\-(h|\-help)$/u
- @@format =
/^\-(f|\-format)$/u
- @@version =
/^\-(v|\-version)$/u
Class Method Summary collapse
- .command ⇒ Object
- .error(message = '') ⇒ Object
- .extname(action) ⇒ Object
- .help ⇒ Object
- .report(action, path) ⇒ Object
- .run(action = :to_html) ⇒ Object
- .version ⇒ Object
Class Method Details
.command ⇒ Object
20 21 22 |
# File 'lib/ydocx/command.rb', line 20 def command File.basename $0 end |
.error(message = '') ⇒ Object
12 13 14 15 16 |
# File 'lib/ydocx/command.rb', line 12 def error(='') puts puts "see `#{self.command} --help`" exit end |
.extname(action) ⇒ Object
17 18 19 |
# File 'lib/ydocx/command.rb', line 17 def extname(action) action == :to_html ? '.html': '.xml' end |
.help ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ydocx/command.rb', line 23 def help = <<-BANNER Usage: #{self.command} file [options] -f, --format Format of style and chapter {(fi|fachinfo)|(pi|patinfo)|(pl|plain)|none}, default fachinfo. -h, --help Display this help message. -v, --version Show version. BANNER puts exit end |
.report(action, path) ⇒ Object
33 34 35 36 |
# File 'lib/ydocx/command.rb', line 33 def report(action, path) puts "#{self.command}: generated #{File.(path)}" exit end |
.run(action = :to_html) ⇒ Object
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 |
# File 'lib/ydocx/command.rb', line 37 def run(action=:to_html) argv = ARGV.dup if argv.empty? or argv[0] =~ @@help self.help elsif argv[0] =~ @@version self.version else file = argv.shift path = File.(file) if !File.exist?(path) self.error "#{self.command}: cannot open #{file}: No such file" elsif !File.extname(path).match(/^\.docx$/) self.error "#{self.command}: cannot open #{file}: Not a docx file" else = {} if option = argv.shift if option =~ @@format case argv[0] when 'fi', 'fachinfo' require 'ydocx/templates/fachinfo' .merge!({:style => :frame}) if action == :to_html when 'pi', 'patinfo' require 'ydocx/templates/patinfo' .merge!({:style => :frame}) if action == :to_html when 'pl', 'plain' .merge!({:style => true}) if action == :to_html when 'none' # pass else self.error "#{self.command}: exit with #{option}: Invalid argument" end elsif option =~ @@help self.help elsif option.downcase =~ /\.(jpeg|jpg|png|gif)$/u and action == :to_html # allow as default # TODO # refactor as normal option # currently, support fachinfo/patinfo format only require 'ydocx/templates/fachinfo' .merge!({:style => :frame}) else self.error "#{self.command}: exit with #{option}: Unknown option" end else # default fachinfo require 'ydocx/templates/fachinfo' .merge!({:style => :frame}) if action == :to_html end doc = YDocx::Document.open(path) doc.send(action, path, ) ext = self.extname(action) self.report action, doc.output_file(ext[1..-1]) end end end |
.version ⇒ Object
92 93 94 95 |
# File 'lib/ydocx/command.rb', line 92 def version puts "#{self.command}: version #{VERSION}" exit end |