Method: Command::Convert#execute

Defined in:
lib/command/convert.rb

#execute(argv) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/command/convert.rb', line 106

def execute(argv)
  super
  if argv.empty?
    puts @opt.help
    return
  end
  @output_filename = @options["output"]
  if @output_filename
    @ext = File.extname(@output_filename)
    @basename = File.basename(@output_filename, @ext)
  else
    @basename = nil
  end
  if @options["encoding"]
    @enc = Encoding.find(@options["encoding"]) rescue nil
    unless @enc
      error "--enc で指定された文字コードは存在しません。sjis, eucjp, utf-8 等を指定して下さい"
      return
    end
  end

  @multi_device = @options["multi-device"]
  device_names = if @multi_device
                   @multi_device.split(",").map(&:strip).map(&:downcase).select { |name|
                     Device.exists?(name).tap { |this|
                       unless this
                         error "[convert.multi-device] #{name} は有効な端末名ではありません"
                       end
                     }
                   }
                 else
                   [nil]   # nil で device 設定が読まれる
                 end
  # kindle用のmobiを作る過程でepubが作成され、上書きされてしまうので、最初に作るようにする
  kindle = device_names.delete("kindle")
  device_names.unshift(kindle) if kindle

  if @multi_device && device_names.empty?
    error "有効な端末名がひとつもありませんでした"
    exit Narou::EXIT_ERROR_CODE
  end

  device_names.each do |name|
    @device = Narou.get_device(name)
    if name
      puts "<bold><magenta>&gt;&gt; #{@device.display_name}用に変換します</magenta></bold>".termcolor
    end
    self.extend(@device.get_hook_module) if @device
    hook_call(:change_settings)
    convert_novels(argv)
  end

  # device の設定に戻す
  if @multi_device
    device = Narou.get_device
    force_change_settings_function(device.get_relative_variables) if device
  end
end