Class: YDocx::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ydocx/command.rb

Constant Summary collapse

@@format =
/^\-(f|\-format)$/u
@@help =
/^\-(h|\-help)$/u
@@lang =
/^\-(l|\-lang)$/u
@@version =
/^\-(v|\-version)$/u

Class Method Summary collapse

Class Method Details

.commandObject



21
22
23
# File 'lib/ydocx/command.rb', line 21

def command
  File.basename $0
end

.error(message = '') ⇒ Object



13
14
15
16
17
# File 'lib/ydocx/command.rb', line 13

def error(message='')
  puts message
  puts "see `#{self.command} --help`"
  exit
end

.extname(action) ⇒ Object



18
19
20
# File 'lib/ydocx/command.rb', line 18

def extname(action)
  action == :to_html ? '.html': '.xml'
end

.helpObject



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

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.
  -l, --lang      Language option for templates {de|fr}
  -v, --version   Show version.
  BANNER
  puts banner
  exit
end

.parse(action, argv) ⇒ Object



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
# File 'lib/ydocx/command.rb', line 35

def parse(action, argv)
  if argv.length.odd?
    self.error "#{self.command}: exit with: Invalid option"
  else
    args = Hash[*argv]
  end
  options = {}
  args.each_pair do |option, value|
    if option =~ @@format
      case value
      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 =~ @@lang
      options.merge!({:lang => value})
    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
  end
  if !args.include?('-f') and !args.include?('--format')
    # default fachinfo
    require 'ydocx/templates/fachinfo'
    options.merge!({:style => :frame}) if action == :to_html
  end
  options
end

.report(action, path) ⇒ Object



80
81
82
83
# File 'lib/ydocx/command.rb', line 80

def report(action, path)
  puts "#{self.command}: generated #{File.expand_path(path)}"
  exit
end

.run(action = :to_html) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ydocx/command.rb', line 84

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 = self.parse(action, argv)
      doc = YDocx::Document.open(path, options)
      doc.send(action, path, options)
      ext = self.extname(action)
      self.report action, doc.output_file(ext[1..-1])
    end
  end
end

.versionObject



106
107
108
109
# File 'lib/ydocx/command.rb', line 106

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