Class: Command::Init

Inherits:
CommandBase show all
Defined in:
lib/command/init.rb

Instance Attribute Summary

Attributes inherited from CommandBase

#stream_io

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeInit

Returns a new instance of Init.



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

def initialize
  super("[options]")
  if Narou.already_init?
    opt_message("  \u30FBAozoraEpub3 \u306E\u518D\u8A2D\u5B9A\u3092\u884C\u3044\u307E\u3059\u3002\n    MSG\n  else\n    opt_message(<<-MSG)\n  \u30FB\u73FE\u5728\u306E\u30D5\u30A9\u30EB\u30C0\u3092\u5C0F\u8AAC\u683C\u7D0D\u7528\u30D5\u30A9\u30EB\u30C0\u3068\u3057\u3066\u521D\u671F\u5316\u3057\u307E\u3059\u3002\n  \u30FB\u521D\u671F\u5316\u3055\u308C\u308B\u307E\u3067\u306F\u4ED6\u306E\u30B3\u30DE\u30F3\u30C9\u306F\u4F7F\u3048\u307E\u305B\u3093\u3002\n    MSG\n  end\n  @opt.on(\"-p\", \"--path FOLDER\", \"\u6307\u5B9A\u3057\u305F\u30D5\u30A9\u30EB\u30C0\u306E AozoraEpub3 \u3092\u5229\u7528\u3059\u308B\") { |dirname|\n    # no check here since global_setting is not loaded yet\n    @options[\"aozora_dirname\"] = dirname\n  }\n  @opt.on(\"-l\", \"--line-height SIZE\", \"\u884C\u306E\u9AD8\u3055\u3092\u5909\u66F4\u3059\u308B(\u5358\u4F4Dem)\u3002\u30AA\u30B9\u30B9\u30E1\u306F1.8\") do |line_height|\n    begin\n      @options[\"line_height\"] = Helper.string_cast_to_type(line_height, :float)\n    rescue Helper::InvalidVariableType => e\n      error e.message\n      exit Narou::EXIT_ERROR_CODE\n    end\n  end\nend\n")

Class Method Details

.oneline_helpObject



12
13
14
15
16
17
18
# File 'lib/command/init.rb', line 12

def self.oneline_help
  if Narou.already_init?
    "AozoraEpub3 の再設定を行います"
  else
    "現在のフォルダを小説用に初期化します"
  end
end

Instance Method Details

#ask_aozoraepub3_pathObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/command/init.rb', line 141

def ask_aozoraepub3_path
  puts
  print "<bold><green>AozoraEpub3のあるフォルダを入力して下さい:</green></bold>\n(未入力でスキップ".termcolor
  if @global_setting["aozoraepub3dir"]
    puts "、:keep で現在と同じ場所を指定)"
    print "(現在の場所:#{@global_setting["aozoraepub3dir"]}"
  end
  print ")\n>"
  while (input = $stdin.gets)
    break if input.strip! == ""
    checked_input = normalize_aozoraepub3_path(input)
    return checked_input if checked_input
    print "\n<bold><green>入力されたフォルダにAozoraEpub3がありません。" \
          "もう一度入力して下さい:</green></bold>\n&gt;".termcolor
  end
  nil
end

#ask_line_heightObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/command/init.rb', line 159

def ask_line_height
  # 後方互換のために未設定時の line_height デフォルトは 1.6 だが、
  # オススメは 1.8 なので入力時のデフォルトは 1.8 にする
  line_height = Narou.line_height(default: 1.8)
  puts
  puts("<bold><green>\u884C\u9593\u306E\u8ABF\u6574\u3092\u884C\u3044\u307E\u3059\u3002\u5C0F\u8AAC\u306E\u884C\u306E\u9AD8\u3055\u3092\u8A2D\u5B9A\u3057\u3066\u4E0B\u3055\u3044(\u5358\u4F4D em):</green></bold>\n1em = 1\u6587\u5B57\u5206\u306E\u9AD8\u3055\n\u884C\u306E\u9AD8\u3055\uFF1D1\u6587\u5B57\u5206\u306E\u9AD8\u3055\uFF0B\u884C\u9593\u306E\u9AD8\u3055\n\u30AA\u30B9\u30B9\u30E1\u306F 1.8\n1.6 \u3067\u82E5\u5E72\u884C\u9593\u72ED\u3081\u30021.8 \u3060\u3068\u4E00\u822C\u7684\u306A\u5C0F\u8AAC\u7A0B\u5EA6\u30022.0 \u304F\u3089\u3044\u306B\u3059\u308B\u3068\u304B\u306A\u308A\u30B9\u30AB\u30B9\u30AB\n(\u672A\u5165\u529B\u3067 \#{line_height} \u3092\u63A1\u7528)\n  MSG\n  print \">\"\n  while (input = $stdin.gets)\n    break if input.strip! == \"\"\n    begin\n      line_height = Helper.string_cast_to_type(input, :float)\n      break\n    rescue Helper::InvalidVariableType => e\n      error e.message\n      print \"<bold><green>\u3082\u3046\u4E00\u5EA6\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044:</green></bold>\\n&gt;\".termcolor\n    end\n  end\n  line_height\nend\n".termcolor)

#execute(argv) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/command/init.rb', line 71

def execute(argv)
  super
  if Narou.already_init?
    init_aozoraepub3(true)
  else
    Narou.init
    puts "-" * 30
    init_aozoraepub3
    puts "初期化が完了しました!"
    puts "現在のフォルダ下で各種コマンドが使用出来るようになりました。"
    puts "まずは narou help で簡単な説明を御覧ください。"
  end
end

#init_aozoraepub3(force = false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/command/init.rb', line 85

def init_aozoraepub3(force = false)
  @global_setting = Inventory.load("global_setting", :global)
  if !force && @global_setting["aozoraepub3dir"]
    return
  end
  puts "<bold><green>AozoraEpub3の設定を行います</green></bold>".termcolor
  unless @global_setting["aozoraepub3dir"]
    puts "<bold><red>#{"!!!WARNING!!!".center(70)}</red></bold>".termcolor
    puts "AozoraEpub3の構成ファイルを書き換えます。narouコマンド用に別途新規インストールしておくことをオススメします"
  end
  if @options["aozora_dirname"]
    path = normalize_aozoraepub3_path(@options["aozora_dirname"])
    print "\n<bold><green>指定されたフォルダにAozoraEpub3がありません。</green></bold>\n".termcolor unless path
  end
  aozora_path = path || ask_aozoraepub3_path
  unless aozora_path
    puts "設定をスキップしました。あとで " + "<bold><yellow>narou init</yellow></bold>".termcolor + " で再度設定出来ます"
    return
  end
  line_height = @options["line_height"] || ask_line_height
  puts
  rewrite_aozoraepub3_files(aozora_path, line_height)
  @global_setting["aozoraepub3dir"] = aozora_path
  @global_setting["line-height"] = line_height
  @global_setting.save
  puts "<bold><green>AozoraEpub3の設定を終了しました</green></bold>".termcolor
end

#normalize_aozoraepub3_path(input) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/command/init.rb', line 186

def normalize_aozoraepub3_path(input)
  if Helper.os_windows?
    input.force_encoding(Encoding::Windows_31J).encode!(Encoding::UTF_8)
  end
  input.delete!("\"")
  path = File.expand_path(input)
  if input == ":keep"
    aozora_dir = @global_setting["aozoraepub3dir"]
    if aozora_dir && Narou.aozoraepub3_directory?(aozora_dir)
      return aozora_dir
    end
  elsif Narou.aozoraepub3_directory?(path)
    return path
  end
  nil
end

#opt_message(description) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/command/init.rb', line 46

def opt_message(description)
  @opt.separator "\n\#{description}\n  Examples:\nnarou init\nnarou init -p /opt/narou/aozora    # AozoraEpub3 \u306E\u30D5\u30A9\u30EB\u30C0\u3092\u76F4\u63A5\u6307\u5B9A\nnarou init -p :keep                # \u8A2D\u5B9A\u6E08\u307F\u3068\u540C\u3058\u5834\u6240\u3092\u6307\u5B9A(\u65E2\u306B\u521D\u671F\u5316\u6E08\u306E\u5834\u5408)\n\n# \u884C\u306E\u9AD8\u3055\u306E\u8ABF\u6574\nnarou init --line-height 1.8       # \u884C\u306E\u9AD8\u3055\u30921.8em\u306B\u8A2D\u5B9A(1.8\u6587\u5B57\u5206\u76F8\u5F53)\n# \u884C\u306E\u9AD8\u3055\u306A\u306E\u3067\u3001\u884C\u9593\u30921\u6587\u5B57\u5206\u3042\u3051\u305F\u3044\u3068\u3044\u3046\u5834\u5408\u306F 1+1 \u3067 2 \u3092\u6307\u5B9A\u3059\u308B\n# (\u672A\u8A2D\u5B9A\u306E\u307E\u307E\u5C0F\u8AAC\u5909\u63DB\u3059\u308B\u3068 1.6 \u3067\u8A08\u7B97\u3055\u308C\u308B)\n# \u53C2\u8003\u60C5\u5831\uFF1AKindle Voyage \u3067\u6587\u5B57\u30B5\u30A4\u30BA\uFF14\u756A\u76EE\u306E\u5927\u304D\u3055\u306E\u5834\u5408\u3001\n#   1.6em : 1\u30DA\u30FC\u30B8\u306B15\u884C\n#   1.8em : 1\u30DA\u30FC\u30B8\u306B13\u884C\n# \u306E\u8868\u793A\u884C\u6570\u306B\u306A\u308B\n\n# \u5165\u529B\u3092\u7701\u7565\u3057\u305F\u3044\u5834\u5408\u3001-p \u3068 -l \u3092\u4E21\u65B9\u6307\u5B9A\u3057\u3066\u3084\u308B\u5FC5\u8981\u3042\u308A\nnarou init -p /path/to/aozora -l 1.8\n\n  Options:\n  MSG\nend\n"

#rewrite_aozoraepub3_files(aozora_path, line_height) ⇒ Object



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

def rewrite_aozoraepub3_files(aozora_path, line_height)
  # chuki_tag.txt の書き換え
  custom_chuki_tag_path = File.join(Narou.preset_dir, "custom_chuki_tag.txt")
  chuki_tag_path = File.join(aozora_path, "chuki_tag.txt")
  custom_chuki_tag = File.read(custom_chuki_tag_path, mode: "r:BOM|UTF-8")
  chuki_tag = File.read(chuki_tag_path, mode: "r:BOM|UTF-8")
  embedded_mark = "### Narou.rb embedded custom chuki ###"
  if chuki_tag =~ /#{embedded_mark}/
    chuki_tag.gsub!(/#{embedded_mark}.+#{embedded_mark}/m, custom_chuki_tag)
  else
    chuki_tag << "\n" + custom_chuki_tag
  end
  File.write(chuki_tag_path, chuki_tag)
  puts "(次のファイルを書き換えました)"
  puts chuki_tag_path
  puts
  # ファイルコピー
  src = ["AozoraEpub3.ini", "vertical_font.css"]
  dst = ["AozoraEpub3.ini", "template/OPS/css_custom/vertical_font.css"]
  puts "(次のファイルをコピーor上書きしました)"
  src.size.times do |i|
    src_full_path = File.join(Narou.preset_dir, src[i])
    dst_full_path = File.join(aozora_path, dst[i])
    Helper.erb_copy(src_full_path, dst_full_path, binding)
    puts dst_full_path
  end
end