Skip to content

Commit

Permalink
Merge pull request #115 from aozorahack/fix-class-attr
Browse files Browse the repository at this point in the history
Remove class attributes of use_jisx0213 and use_unicode
  • Loading branch information
takahashim authored Feb 2, 2024
2 parents b2fd9b3 + c0691d6 commit 235e623
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Style/Documentation:
Exclude:
- 'test/**/*'

Style/DoubleNegation:
Enabled: false

Style/EmptyMethod:
EnforcedStyle: expanded
Enabled: true
Expand Down
28 changes: 18 additions & 10 deletions bin/aozora2html
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ gaiji_dir = options['gaiji-dir'] || nil

css_files = options['css-files']&.split(',')

if options['use-jisx0213']
Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = true
Aozora2Html::Tag::Accent.use_jisx0213 = true
end

if options['use-unicode']
Aozora2Html::Tag::EmbedGaiji.use_unicode = true
end
use_jisx0213 = !!options['use-jisx0213']
use_unicode = !!options['use-unicode']

if options['error-utf8']
Aozora2Html::I18n.use_utf8 = true
Expand Down Expand Up @@ -64,9 +58,23 @@ Dir.mktmpdir do |dir|
require 'aozora2html/zip'
tmpfile = File.join(dir, 'aozora.txt')
Aozora2Html::Zip.unzip(src_file, tmpfile)
Aozora2Html.new(tmpfile, dest_file, gaiji_dir: gaiji_dir, css_files: css_files).process
Aozora2Html.new(
tmpfile,
dest_file,
gaiji_dir: gaiji_dir,
css_files: css_files,
use_jisx0213: use_jisx0213,
use_unicode: use_unicode
).process
else
Aozora2Html.new(src_file, dest_file, gaiji_dir: gaiji_dir, css_files: css_files).process
Aozora2Html.new(
src_file,
dest_file,
gaiji_dir: gaiji_dir,
css_files: css_files,
use_jisx0213: use_jisx0213,
use_unicode: use_unicode
).process
end
unless ARGV[1]
output = File.read(dest_file)
Expand Down
19 changes: 11 additions & 8 deletions lib/aozora2html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Aozora2Html
'5' => 'ヲ゛'.to_sjis
}.freeze

def initialize(input, output, gaiji_dir: nil, css_files: nil)
def initialize(input, output, gaiji_dir: nil, css_files: nil, use_jisx0213: nil, use_unicode: nil)
@stream = if input.respond_to?(:read) ## readable IO?
Jstream.new(input)
else
Expand All @@ -164,6 +164,9 @@ def initialize(input, output, gaiji_dir: nil, css_files: nil)
@gaiji_dir = gaiji_dir || '../../../gaiji/'
@css_files = css_files || ['../../aozora.css']

@use_jisx0213 = use_jisx0213
@use_unicode = use_unicode

@buffer = TextBuffer.new
@ruby_buf = RubyBuffer.new
@section = :head ## 現在処理中のセクション(:head,:head_end,:chuuki,:chuuki_in,:body,:tail)
Expand Down Expand Up @@ -221,7 +224,7 @@ def kuten2png(substring)
codes = matched[0].split('-')
folder = sprintf('%1d-%02d', codes[0], codes[1])
code = sprintf('%1d-%02d-%02d', *codes)
Aozora2Html::Tag::EmbedGaiji.new(self, folder, code, desc.gsub!(IGETA_MARK, ''), gaiji_dir: @gaiji_dir)
Aozora2Html::Tag::EmbedGaiji.new(self, folder, code, desc.gsub!(IGETA_MARK, ''), gaiji_dir: @gaiji_dir, use_jisx0213: @use_jisx0213, use_unicode: @use_unicode)
else
substring
end
Expand Down Expand Up @@ -257,11 +260,11 @@ def read_line
end

def read_accent
Aozora2Html::AccentParser.new(@stream, ACCENT_END, @chuuki_table, @images, gaiji_dir: @gaiji_dir).process
Aozora2Html::AccentParser.new(@stream, ACCENT_END, @chuuki_table, @images, gaiji_dir: @gaiji_dir, use_jisx0213: @use_jisx0213).process
end

def read_to_nest(endchar)
Aozora2Html::TagParser.new(@stream, endchar, @chuuki_table, @images, gaiji_dir: @gaiji_dir).process
Aozora2Html::TagParser.new(@stream, endchar, @chuuki_table, @images, gaiji_dir: @gaiji_dir, use_jisx0213: @use_jisx0213, use_unicode: @use_unicode).process
end

def finalize
Expand Down Expand Up @@ -654,9 +657,9 @@ def dispatch_gaiji
end

matched = command.match(/U\+([0-9A-F]{4,5})/)
if matched && Aozora2Html::Tag::EmbedGaiji.use_unicode
if matched && (Aozora2Html::Tag::EmbedGaiji.use_unicode || @use_unicode)
unicode_num = matched[1]
Aozora2Html::Tag::EmbedGaiji.new(self, nil, nil, command, unicode_num, gaiji_dir: @gaiji_dir)
Aozora2Html::Tag::EmbedGaiji.new(self, nil, nil, command, unicode_num, gaiji_dir: @gaiji_dir, use_jisx0213: @use_jisx0213, use_unicode: @use_unicode)
else
# Unemb
escape_gaiji(command)
Expand Down Expand Up @@ -1305,10 +1308,10 @@ def hyoki
elsif @chuuki_table[:dakutenkunoji]
@out.printf("\t<li>「濁点付きくの字点」は「%s」で表しました。</li>\r\n".to_sjis, KU + DAKUTEN + NOJI)
end
if @chuuki_table[:newjis] && !Aozora2Html::Tag::EmbedGaiji.use_jisx0213
if @chuuki_table[:newjis] && !(Aozora2Html::Tag::EmbedGaiji.use_jisx0213 || @use_jisx0213)
@out.print "\t<li>「くの字点」をのぞくJIS X 0213にある文字は、画像化して埋め込みました。</li>\r\n".to_sjis
end
if @chuuki_table[:accent] && !Aozora2Html::Tag::Accent.use_jisx0213
if @chuuki_table[:accent] && !(Aozora2Html::Tag::Accent.use_jisx0213 || @use_jisx0213)
@out.print "\t<li>アクセント符号付きラテン文字は、画像化して埋め込みました。</li>\r\n".to_sjis
end
if @images[0]
Expand Down
7 changes: 4 additions & 3 deletions lib/aozora2html/accent_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Aozora2Html
# accent特殊文字を生かすための再帰呼び出し
class AccentParser < Aozora2Html
def initialize(input, endchar, chuuki, image, gaiji_dir:) # rubocop:disable Lint/MissingSuper
def initialize(input, endchar, chuuki, image, gaiji_dir:, use_jisx0213: nil) # rubocop:disable Lint/MissingSuper
unless input.is_a?(Jstream)
raise ArgumentError, 'tag_parser must supply Jstream as input'
end
Expand All @@ -19,6 +19,7 @@ def initialize(input, endchar, chuuki, image, gaiji_dir:) # rubocop:disable Lint
@endchar = endchar # 改行は越えられない <br />を出力していられない
@closed = nil # 改行での強制撤退チェックフラグ
@encount_accent = nil
@use_jisx0213 = use_jisx0213
end

# 出力は配列で返す
Expand Down Expand Up @@ -46,14 +47,14 @@ def parse
if found2.is_a?(Hash)
found3 = found2[@stream.peek_char(1)]
if found3
first = Aozora2Html::Tag::Accent.new(self, *found3, gaiji_dir: @gaiji_dir)
first = Aozora2Html::Tag::Accent.new(self, *found3, gaiji_dir: @gaiji_dir, use_jisx0213: @use_jisx0213)
@encount_accent = true
@chuuki_table[:accent] = true
read_char
read_char
end
elsif found2
first = Aozora2Html::Tag::Accent.new(self, *found2, gaiji_dir: @gaiji_dir)
first = Aozora2Html::Tag::Accent.new(self, *found2, gaiji_dir: @gaiji_dir, use_jisx0213: @use_jisx0213)
@encount_accent = true
read_char
@chuuki_table[:accent] = true
Expand Down
5 changes: 3 additions & 2 deletions lib/aozora2html/tag/accent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class << self
attr_accessor :use_jisx0213
end

def initialize(parser, code, name, gaiji_dir:)
def initialize(parser, code, name, gaiji_dir:, use_jisx0213: nil)
@code = code
@name = name
@gaiji_dir = gaiji_dir
@use_jisx0213 = use_jisx0213
super
end

Expand All @@ -26,7 +27,7 @@ def char_type
end

def to_s
if Aozora2Html::Tag::Accent.use_jisx0213
if Aozora2Html::Tag::Accent.use_jisx0213 || @use_jisx0213
jisx0213_to_unicode(@code.sub(%r{.*/}, '').to_sym)
else
"<img src=\"#{@gaiji_dir}#{@code}.png\" alt=\"" + GAIJI_MARK + "(#{@name})\" class=\"gaiji\" />"
Expand Down
8 changes: 5 additions & 3 deletions lib/aozora2html/tag/embed_gaiji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class << self
attr_reader :use_unicode
end

def initialize(parser, folder, code, name, unicode_num = nil, gaiji_dir:)
def initialize(parser, folder, code, name, unicode_num = nil, gaiji_dir:, use_jisx0213: nil, use_unicode: nil)
@folder = folder
@code = code
@name = name
@unicode = unicode_num
@gaiji_dir = gaiji_dir
@use_jisx0213 = use_jisx0213
@use_unicode = use_unicode
super
end

Expand All @@ -39,9 +41,9 @@ def jisx0213_to_unicode(code)
end

def to_s
if Aozora2Html::Tag::EmbedGaiji.use_jisx0213 && @code
if (Aozora2Html::Tag::EmbedGaiji.use_jisx0213 || @use_jisx0213) && @code
jisx0213_to_unicode(@code.to_sym)
elsif Aozora2Html::Tag::EmbedGaiji.use_unicode && @unicode
elsif (Aozora2Html::Tag::EmbedGaiji.use_unicode || @use_unicode) && @unicode
"&#x#{@unicode};"
else
"<img src=\"#{@gaiji_dir}#{@folder}/#{@code}.png\" alt=\"" + GAIJI_MARK + "(#{@name})\" class=\"gaiji\" />"
Expand Down
4 changes: 3 additions & 1 deletion lib/aozora2html/tag_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ class Aozora2Html
#
# 青空記法の入れ子に対応(?)
class TagParser < Aozora2Html
def initialize(input, endchar, chuuki, image, gaiji_dir:) # rubocop:disable Lint/MissingSuper
def initialize(input, endchar, chuuki, image, gaiji_dir:, use_jisx0213: nil, use_unicode: nil) # rubocop:disable Lint/MissingSuper
unless input.is_a?(Jstream)
raise ArgumentError, 'tag_parser must supply Jstream as input'
end

@stream = input
@gaiji_dir = gaiji_dir
@use_jisx0213 = use_jisx0213
@use_unicode = use_unicode
@buffer = TextBuffer.new
@ruby_buf = RubyBuffer.new
@chuuki_table = chuuki
Expand Down
11 changes: 10 additions & 1 deletion test/test_aozora_accent_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_invalid
end
end

def test_use_jisx0213
def test_use_jisx0213_class
Aozora2Html::Tag::Accent.use_jisx0213 = true
str = "〔e'tiquette〕\r\n".to_sjis
strio = StringIO.new(str)
Expand All @@ -42,6 +42,15 @@ def test_use_jisx0213
assert_equal expected, parsed.to_s.to_utf8
end

def test_use_jisx0213
str = "〔e'tiquette〕\r\n".to_sjis
strio = StringIO.new(str)
stream = Jstream.new(strio)
parsed = Aozora2Html::AccentParser.new(stream, '〕'.to_sjis, {}, [], gaiji_dir: 'g_dir/', use_jisx0213: true).process
expected = '〔&#x00E9;tiquette'
assert_equal expected, parsed.to_s.to_utf8
end

def teardown
Aozora2Html::Tag::Accent.use_jisx0213 = nil
end
Expand Down
Loading

0 comments on commit 235e623

Please sign in to comment.