Skip to content

Commit

Permalink
Fix #70; Add CLI option --error-utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Dec 12, 2021
1 parent 33c2817 commit c6991ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bin/aozora2html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ opt.on('--gaiji-dir DIR', 'setting gaiji directory')
opt.on('--css-files FILES', 'setting css directory')
opt.on('--use-jisx0213', 'use JIS X 0213 character')
opt.on('--use-unicode', 'use Unicode character')
opt.on('--error-utf8', 'show error messages in UTF-8, not Shift_JIS')
opt.version = Aozora2Html::VERSION
options = opt.getopts

Expand All @@ -27,6 +28,10 @@ if options['use-unicode']
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
end

if options['error-utf8']
Aozora2Html::I18n.use_utf8 = true
end

if ARGV.size < 1 || ARGV.size > 2
$stderr.print opt.banner
exit 1
Expand Down
12 changes: 11 additions & 1 deletion lib/aozora2html/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class Aozora2Html
#
# コード内に日本語メッセージが氾濫しないようにするためのクラス
class I18n
@use_utf8 = nil

class << self
attr_accessor :use_utf8
end

MSG = {
tag_syntax_error: '注記を重ねる際の原則、「狭い範囲を先に、広い範囲を後に」が守られていません。リンク先の指針を参考に、書き方をあらためてください',
undefined_header: '未定義な見出しです',
Expand All @@ -26,7 +32,11 @@ class I18n
}.freeze

def self.t(msg, *args)
(MSG[msg].encode('shift_jis') % args)
if Aozora2Html::I18n.use_utf8
(MSG[msg].encode('shift_jis') % args).force_encoding('cp932').encode('utf-8')
else
MSG[msg].encode('shift_jis') % args
end
end
end
end
13 changes: 13 additions & 0 deletions test/test_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ def test_t
.force_encoding('cp932').encode('utf-8')
end

def test_error_utf8
orig_value = Aozora2Html::I18n.use_utf8
Aozora2Html::I18n.use_utf8 = true
begin
assert_equal '警告(123行目):JIS外字「①」が使われています',
Aozora2Html::I18n.t(:warn_jis_gaiji,
123,
'①'.encode('cp932').force_encoding('shift_jis'))
ensure
Aozora2Html::I18n.use_utf8 = orig_value
end
end

def test_ruby_puts_behavior
$stdout = StringIO.new
begin
Expand Down

0 comments on commit c6991ac

Please sign in to comment.