Class: YDocx::Command

Inherits:
Object
  • Object
show all
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

Class Method Details

.commandObject



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(message='')
  puts message
  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

.helpObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/ydocx/command.rb', line 23

def help
  banner = <<-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 banner
  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.expand_path(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.expand_path(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
      options = {}
      if option = argv.shift
        if option =~ @@format
          case argv[0]
          when 'fi', 'fachinfo'
            require 'ydocx/templates/fachinfo'
            options.merge!({:style => :frame}) if action == :to_html
          when 'pi', 'patinfo'
            require 'ydocx/templates/patinfo'
            options.merge!({:style => :frame}) if action == :to_html
          when 'pl', 'plain'
            options.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'
          options.merge!({:style => :frame})
        else
          self.error "#{self.command}: exit with #{option}: Unknown option"
        end
      else
        # default fachinfo
        require 'ydocx/templates/fachinfo'
        options.merge!({:style => :frame}) if action == :to_html
      end
      doc = YDocx::Document.open(path)
      doc.send(action, path, options)
      ext = self.extname(action)
      self.report action, doc.output_file(ext[1..-1])
    end
  end
end

.versionObject



92
93
94
95
# File 'lib/ydocx/command.rb', line 92

def version
  puts "#{self.command}: version #{VERSION}"
  exit
end