aozora2html 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rubocop.yml +19 -0
  3. data/.github/workflows/ruby.yml +4 -1
  4. data/.rubocop.yml +36 -152
  5. data/.rubocop_todo.yml +7 -0
  6. data/CHANGELOG.md +26 -0
  7. data/Gemfile +2 -0
  8. data/Guardfile +3 -1
  9. data/HACKING.md +45 -0
  10. data/README.md +14 -6
  11. data/Rakefile +12 -5
  12. data/aozora2html.gemspec +24 -22
  13. data/bin/aozora2html +21 -19
  14. data/lib/aozora2html/accent_parser.rb +62 -54
  15. data/lib/aozora2html/error.rb +5 -4
  16. data/lib/aozora2html/header.rb +20 -18
  17. data/lib/aozora2html/i18n.rb +40 -20
  18. data/lib/aozora2html/ruby_buffer.rb +63 -28
  19. data/lib/aozora2html/string_refinements.rb +36 -0
  20. data/lib/aozora2html/style_stack.rb +6 -0
  21. data/lib/aozora2html/tag/accent.rb +10 -12
  22. data/lib/aozora2html/tag/block.rb +11 -9
  23. data/lib/aozora2html/tag/chitsuki.rb +6 -2
  24. data/lib/aozora2html/tag/dakuten_katakana.rb +10 -8
  25. data/lib/aozora2html/tag/decorate.rb +4 -3
  26. data/lib/aozora2html/tag/dir.rb +4 -2
  27. data/lib/aozora2html/tag/editor_note.rb +7 -4
  28. data/lib/aozora2html/tag/embed_gaiji.rb +15 -11
  29. data/lib/aozora2html/tag/font_size.rb +5 -2
  30. data/lib/aozora2html/tag/gaiji.rb +4 -3
  31. data/lib/aozora2html/tag/img.rb +4 -4
  32. data/lib/aozora2html/tag/indent.rb +3 -3
  33. data/lib/aozora2html/tag/inline.rb +10 -7
  34. data/lib/aozora2html/tag/inline_caption.rb +4 -2
  35. data/lib/aozora2html/tag/inline_font_size.rb +4 -3
  36. data/lib/aozora2html/tag/inline_keigakomi.rb +4 -2
  37. data/lib/aozora2html/tag/inline_yokogumi.rb +4 -3
  38. data/lib/aozora2html/tag/jisage.rb +3 -1
  39. data/lib/aozora2html/tag/jizume.rb +3 -0
  40. data/lib/aozora2html/tag/kaeriten.rb +4 -2
  41. data/lib/aozora2html/tag/keigakomi.rb +15 -9
  42. data/lib/aozora2html/tag/kunten.rb +4 -4
  43. data/lib/aozora2html/tag/midashi.rb +3 -1
  44. data/lib/aozora2html/tag/multiline.rb +3 -0
  45. data/lib/aozora2html/tag/multiline_caption.rb +6 -8
  46. data/lib/aozora2html/tag/multiline_chitsuki.rb +3 -1
  47. data/lib/aozora2html/tag/multiline_jisage.rb +3 -1
  48. data/lib/aozora2html/tag/multiline_midashi.rb +6 -3
  49. data/lib/aozora2html/tag/multiline_style.rb +5 -3
  50. data/lib/aozora2html/tag/multiline_yokogumi.rb +6 -9
  51. data/lib/aozora2html/tag/okurigana.rb +4 -2
  52. data/lib/aozora2html/tag/oneline_chitsuki.rb +3 -2
  53. data/lib/aozora2html/tag/oneline_indent.rb +8 -1
  54. data/lib/aozora2html/tag/oneline_jisage.rb +3 -0
  55. data/lib/aozora2html/tag/reference_mentioned.rb +22 -21
  56. data/lib/aozora2html/tag/ruby.rb +174 -70
  57. data/lib/aozora2html/tag/un_embed_gaiji.rb +8 -2
  58. data/lib/aozora2html/tag.rb +40 -38
  59. data/lib/aozora2html/tag_parser.rb +23 -16
  60. data/lib/aozora2html/text_buffer.rb +50 -0
  61. data/lib/aozora2html/utils.rb +113 -50
  62. data/lib/aozora2html/version.rb +3 -1
  63. data/lib/aozora2html/yaml_loader.rb +8 -2
  64. data/lib/aozora2html/zip.rb +4 -0
  65. data/lib/aozora2html.rb +1358 -3
  66. data/lib/extensions.rb +2 -34
  67. data/lib/jstream.rb +96 -25
  68. data/sample/chukiichiran_kinyurei.html +15 -2
  69. data/sample/chukiichiran_kinyurei.txt +15 -2
  70. data/test/test_aozora2html.rb +137 -148
  71. data/test/test_aozora_accent_parser.rb +26 -9
  72. data/test/test_command_parse.rb +25 -22
  73. data/test/test_compat.rb +3 -4
  74. data/test/test_dakuten_katakana_tag.rb +10 -12
  75. data/test/test_decorate_tag.rb +9 -6
  76. data/test/test_dir_tag.rb +9 -6
  77. data/test/test_editor_note_tag.rb +8 -5
  78. data/test/test_exception.rb +10 -8
  79. data/test/test_font_size_tag.rb +16 -13
  80. data/test/test_gaiji_tag.rb +15 -14
  81. data/test/test_header.rb +25 -40
  82. data/test/test_helper.rb +3 -1
  83. data/test/test_i18n.rb +22 -6
  84. data/test/test_img_tag.rb +9 -5
  85. data/test/test_inline_caption_tag.rb +9 -6
  86. data/test/test_inline_font_size_tag.rb +13 -10
  87. data/test/test_inline_keigakomi_tag.rb +9 -6
  88. data/test/test_inline_yokogumi_tag.rb +9 -6
  89. data/test/test_jizume_tag.rb +9 -7
  90. data/test/test_jstream.rb +33 -30
  91. data/test/test_kaeriten_tag.rb +9 -6
  92. data/test/test_keigakomi_tag.rb +11 -9
  93. data/test/test_midashi_tag.rb +15 -14
  94. data/test/test_multiline_caption_tag.rb +7 -5
  95. data/test/test_multiline_midashi_tag.rb +24 -25
  96. data/test/test_multiline_style_tag.rb +9 -7
  97. data/test/test_multiline_yokogumi_tag.rb +7 -5
  98. data/test/test_okurigana_tag.rb +9 -6
  99. data/test/test_ruby_parse.rb +14 -14
  100. data/test/test_ruby_tag.rb +9 -6
  101. data/test/test_tag_parser.rb +28 -26
  102. metadata +60 -14
  103. data/.travis.yml +0 -12
  104. data/lib/t2hs.rb +0 -1607
@@ -1,27 +1,31 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  class Aozora2Html
4
+ # ユーティリティ関数モジュール
3
5
  module Utils
6
+ using StringRefinements
7
+
8
+ KANJI_NUMS = '一二三四五六七八九〇'.to_sjis
9
+ KANJI_TEN = '十'.to_sjis
10
+ ZENKAKU_NUMS = '0-9'.to_sjis
4
11
 
5
12
  def create_font_size(times, daisho)
6
- size = ""
7
- case times
8
- when 1
9
- size = ""
10
- when 2
11
- size = "x-"
12
- else
13
- if times >= 3
14
- size = "xx-"
15
- else
16
- raise Aozora2Html::Error, I18n.t(:invalid_font_size)
17
- end
18
- end
13
+ size = case times
14
+ when 1
15
+ +''
16
+ when 2
17
+ +'x-'
18
+ else
19
+ raise Aozora2Html::Error, I18n.t(:invalid_font_size) unless times >= 3
20
+
21
+ +'xx-'
22
+ end
19
23
 
20
24
  case daisho
21
25
  when :dai
22
- size << "large"
26
+ size << 'large'
23
27
  when :sho
24
- size << "small"
28
+ size << 'small'
25
29
  else
26
30
  raise Aozora2Html::Error, I18n.t(:invalid_font_size)
27
31
  end
@@ -32,11 +36,11 @@ class Aozora2Html
32
36
 
33
37
  def create_midashi_tag(size)
34
38
  if size.match(SIZE_SMALL)
35
- "h5"
39
+ 'h5'
36
40
  elsif size.match(SIZE_MIDDLE)
37
- "h4"
41
+ 'h4'
38
42
  elsif size.match(SIZE_LARGE)
39
- "h3"
43
+ 'h3'
40
44
  else
41
45
  raise Aozora2Html::Error, I18n.t(:undefined_header)
42
46
  end
@@ -44,34 +48,31 @@ class Aozora2Html
44
48
  module_function :create_midashi_tag
45
49
 
46
50
  def create_midashi_class(type, tag)
51
+ normal_midashi_tag = {
52
+ 'h5' => 'ko-midashi',
53
+ 'h4' => 'naka-midashi',
54
+ 'h3' => 'o-midashi'
55
+ }
56
+
57
+ dogyo_midashi_tag = {
58
+ 'h5' => 'dogyo-ko-midashi',
59
+ 'h4' => 'dogyo-naka-midashi',
60
+ 'h3' => 'dogyo-o-midashi'
61
+ }
62
+
63
+ mado_midashi_tag = {
64
+ 'h5' => 'mado-ko-midashi',
65
+ 'h4' => 'mado-naka-midashi',
66
+ 'h3' => 'mado-o-midashi'
67
+ }
68
+
47
69
  case type
48
70
  when :normal
49
- case tag
50
- when "h5"
51
- "ko-midashi"
52
- when "h4"
53
- "naka-midashi"
54
- when "h3"
55
- "o-midashi"
56
- end
71
+ normal_midashi_tag[tag]
57
72
  when :dogyo
58
- case tag
59
- when "h5"
60
- "dogyo-ko-midashi"
61
- when "h4"
62
- "dogyo-naka-midashi"
63
- when "h3"
64
- "dogyo-o-midashi"
65
- end
73
+ dogyo_midashi_tag[tag]
66
74
  when :mado
67
- case tag
68
- when "h5"
69
- "mado-ko-midashi"
70
- when "h4"
71
- "mado-naka-midashi"
72
- when "h3"
73
- "mado-o-midashi"
74
- end
75
+ mado_midashi_tag[tag]
75
76
  else
76
77
  raise Aozora2Html::Error, I18n.t(:undefined_header)
77
78
  end
@@ -79,15 +80,77 @@ class Aozora2Html
79
80
  module_function :create_midashi_class
80
81
 
81
82
  def convert_japanese_number(command)
82
- tmp = command.tr("0-9".encode("shift_jis"), "0-9")
83
- tmp.tr!("一二三四五六七八九〇".encode("shift_jis"),"1234567890")
84
- tmp.gsub!(/(\d)#{"十".encode("shift_jis")}(\d)/){"#{$1}#{$2}"}
85
- tmp.gsub!(/(\d)#{"十".encode("shift_jis")}/){"#{$1}0"}
86
- tmp.gsub!(/#{"十".encode("shift_jis")}(\d)/){"1#{$1}"}
87
- tmp.gsub!(/#{"十".encode("shift_jis")}/,"10")
83
+ tmp = command.tr(ZENKAKU_NUMS, '0-9')
84
+ tmp.tr!(KANJI_NUMS, '1234567890')
85
+ tmp.gsub!(/(\d)#{KANJI_TEN}(\d)/o) { "#{$1}#{$2}" }
86
+ tmp.gsub!(/(\d)#{KANJI_TEN}/o) { "#{$1}0" }
87
+ tmp.gsub!(/#{KANJI_TEN}(\d)/o) { "1#{$1}" }
88
+ tmp.gsub!(/#{KANJI_TEN}/o, '10')
88
89
  tmp
89
90
  end
90
91
  module_function :convert_japanese_number
92
+
93
+ # 使うべきではない文字があるかチェックする
94
+ #
95
+ # 警告を出力するだけで結果には影響を与えない。警告する文字は以下:
96
+ #
97
+ # * 1バイト文字
98
+ # * `#`ではなく`♯`
99
+ # * JIS(JIS X 0208)外字
100
+ #
101
+ # @return [void]
102
+ #
103
+ def illegal_char_check(char, line)
104
+ return unless char.is_a?(String)
105
+
106
+ code = char.unpack1('H*')
107
+ if (code == '21') ||
108
+ (code == '23') ||
109
+ ((code >= 'a1') && (code <= 'a5')) ||
110
+ ((code >= '28') && (code <= '29')) ||
111
+ (code == '5b') ||
112
+ (code == '5d') ||
113
+ (code == '3d') ||
114
+ (code == '3f') ||
115
+ (code == '2b') ||
116
+ ((code >= '7b') && (code <= '7d'))
117
+ puts I18n.t(:warn_onebyte, line, char)
118
+ end
119
+
120
+ if code == '81f2'
121
+ puts I18n.t(:warn_chuki, line, char)
122
+ end
123
+
124
+ if ((code >= '81ad') && (code <= '81b7')) ||
125
+ ((code >= '81c0') && (code <= '81c7')) ||
126
+ ((code >= '81cf') && (code <= '81d9')) ||
127
+ ((code >= '81e9') && (code <= '81ef')) ||
128
+ ((code >= '81f8') && (code <= '81fb')) ||
129
+ ((code >= '8240') && (code <= '824e')) ||
130
+ ((code >= '8259') && (code <= '825f')) ||
131
+ ((code >= '827a') && (code <= '8280')) ||
132
+ ((code >= '829b') && (code <= '829e')) ||
133
+ ((code >= '82f2') && (code <= '82fc')) ||
134
+ ((code >= '8397') && (code <= '839e')) ||
135
+ ((code >= '83b7') && (code <= '83be')) ||
136
+ ((code >= '83d7') && (code <= '83fc')) ||
137
+ ((code >= '8461') && (code <= '846f')) ||
138
+ ((code >= '8492') && (code <= '849e')) ||
139
+ ((code >= '84bf') && (code <= '84fc')) ||
140
+ ((code >= '8540') && (code <= '85fc')) ||
141
+ ((code >= '8640') && (code <= '86fc')) ||
142
+ ((code >= '8740') && (code <= '87fc')) ||
143
+ ((code >= '8840') && (code <= '889e')) ||
144
+ ((code >= '9873') && (code <= '989e')) ||
145
+ ((code >= 'eaa5') && (code <= 'eafc')) ||
146
+ ((code >= 'eb40') && (code <= 'ebfc')) ||
147
+ ((code >= 'ec40') && (code <= 'ecfc')) ||
148
+ ((code >= 'ed40') && (code <= 'edfc')) ||
149
+ ((code >= 'ee40') && (code <= 'eefc')) ||
150
+ ((code >= 'ef40') && (code <= 'effc'))
151
+ puts I18n.t(:warn_jis_gaiji, line, char)
152
+ end
153
+ end
154
+ module_function :illegal_char_check
91
155
  end
92
156
  end
93
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Aozora2Html
2
- VERSION = "2.0.0"
4
+ VERSION = '3.0.0'
3
5
  end
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
4
+
2
5
  class Aozora2Html
6
+ # YAML Loader class for Shift_JIS
3
7
  class YamlLoader
4
8
  def initialize(base_dir)
5
9
  @base_dir = base_dir
@@ -10,10 +14,12 @@ class Aozora2Html
10
14
  normalize_data(tmp_data)
11
15
  end
12
16
 
17
+ using StringRefinements
18
+
13
19
  def normalize_data(data)
14
20
  case data
15
21
  when String
16
- data.encode("shift_jis")
22
+ data.to_sjis
17
23
  when Hash
18
24
  new_data = {}
19
25
  data.each do |k, v|
@@ -21,7 +27,7 @@ class Aozora2Html
21
27
  end
22
28
  new_data
23
29
  when Array
24
- data.map{|item| normalize_data(item)}
30
+ data.map { |item| normalize_data(item) }
25
31
  else
26
32
  # noop
27
33
  data
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zip'
4
+
2
5
  class Aozora2Html
6
+ # zipファイルからテキストを抽出する
3
7
  class Zip
4
8
  def self.unzip(zipfilename, textfilename)
5
9
  ::Zip::File.open(zipfilename) do |zip_file|