aozora2html 0.7.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/rubocop.yml +19 -0
  3. data/.github/workflows/ruby.yml +37 -0
  4. data/.gitignore +8 -3
  5. data/.rubocop.yml +111 -0
  6. data/.rubocop_todo.yml +7 -0
  7. data/CHANGELOG.md +59 -0
  8. data/Gemfile +2 -0
  9. data/Guardfile +3 -1
  10. data/HACKING.md +45 -0
  11. data/LICENSE +116 -0
  12. data/README.md +29 -16
  13. data/Rakefile +12 -5
  14. data/aozora2html.gemspec +24 -24
  15. data/bin/aozora2html +25 -71
  16. data/lib/aozora2html/accent_parser.rb +99 -0
  17. data/lib/aozora2html/error.rb +17 -0
  18. data/lib/aozora2html/header.rb +141 -0
  19. data/lib/aozora2html/i18n.rb +48 -0
  20. data/lib/aozora2html/ruby_buffer.rb +116 -0
  21. data/lib/aozora2html/string_refinements.rb +36 -0
  22. data/lib/aozora2html/style_stack.rb +33 -0
  23. data/lib/aozora2html/tag/accent.rb +37 -0
  24. data/lib/aozora2html/tag/block.rb +21 -0
  25. data/lib/aozora2html/tag/chitsuki.rb +19 -0
  26. data/lib/aozora2html/tag/dakuten_katakana.rb +25 -0
  27. data/lib/aozora2html/tag/decorate.rb +19 -0
  28. data/lib/aozora2html/tag/dir.rb +17 -0
  29. data/lib/aozora2html/tag/editor_note.rb +19 -0
  30. data/lib/aozora2html/tag/embed_gaiji.rb +52 -0
  31. data/lib/aozora2html/tag/font_size.rb +20 -0
  32. data/lib/aozora2html/tag/gaiji.rb +12 -0
  33. data/lib/aozora2html/tag/img.rb +21 -0
  34. data/lib/aozora2html/tag/indent.rb +8 -0
  35. data/lib/aozora2html/tag/inline.rb +16 -0
  36. data/lib/aozora2html/tag/inline_caption.rb +17 -0
  37. data/lib/aozora2html/tag/inline_font_size.rb +19 -0
  38. data/lib/aozora2html/tag/inline_keigakomi.rb +17 -0
  39. data/lib/aozora2html/tag/inline_yokogumi.rb +17 -0
  40. data/lib/aozora2html/tag/jisage.rb +17 -0
  41. data/lib/aozora2html/tag/jizume.rb +19 -0
  42. data/lib/aozora2html/tag/kaeriten.rb +17 -0
  43. data/lib/aozora2html/tag/keigakomi.rb +19 -0
  44. data/lib/aozora2html/tag/kunten.rb +12 -0
  45. data/lib/aozora2html/tag/midashi.rb +20 -0
  46. data/lib/aozora2html/tag/multiline.rb +9 -0
  47. data/lib/aozora2html/tag/multiline_caption.rb +13 -0
  48. data/lib/aozora2html/tag/multiline_chitsuki.rb +10 -0
  49. data/lib/aozora2html/tag/multiline_jisage.rb +10 -0
  50. data/lib/aozora2html/tag/multiline_midashi.rb +25 -0
  51. data/lib/aozora2html/tag/multiline_style.rb +19 -0
  52. data/lib/aozora2html/tag/multiline_yokogumi.rb +14 -0
  53. data/lib/aozora2html/tag/okurigana.rb +17 -0
  54. data/lib/aozora2html/tag/oneline_chitsuki.rb +10 -0
  55. data/lib/aozora2html/tag/oneline_indent.rb +9 -0
  56. data/lib/aozora2html/tag/oneline_jisage.rb +10 -0
  57. data/lib/aozora2html/tag/reference_mentioned.rb +47 -0
  58. data/lib/aozora2html/tag/ruby.rb +202 -0
  59. data/lib/aozora2html/tag/un_embed_gaiji.rb +30 -0
  60. data/lib/aozora2html/tag.rb +57 -0
  61. data/lib/aozora2html/tag_parser.rb +60 -0
  62. data/lib/aozora2html/text_buffer.rb +50 -0
  63. data/lib/aozora2html/utils.rb +156 -0
  64. data/lib/aozora2html/version.rb +3 -1
  65. data/lib/aozora2html/yaml_loader.rb +37 -0
  66. data/lib/aozora2html/zip.rb +4 -0
  67. data/lib/aozora2html.rb +1359 -8
  68. data/lib/extensions.rb +12 -0
  69. data/lib/jstream.rb +139 -0
  70. data/sample/chukiichiran_kinyurei.html +15 -2
  71. data/sample/chukiichiran_kinyurei.txt +15 -2
  72. data/test/test_aozora2html.rb +323 -73
  73. data/test/test_aozora_accent_parser.rb +34 -6
  74. data/test/test_command_parse.rb +216 -0
  75. data/test/test_compat.rb +3 -4
  76. data/test/test_dakuten_katakana_tag.rb +12 -13
  77. data/test/test_decorate_tag.rb +11 -7
  78. data/test/test_dir_tag.rb +11 -7
  79. data/test/test_editor_note_tag.rb +9 -6
  80. data/test/test_exception.rb +11 -9
  81. data/test/test_font_size_tag.rb +22 -11
  82. data/test/test_gaiji_tag.rb +22 -14
  83. data/test/test_header.rb +45 -0
  84. data/test/test_helper.rb +3 -1
  85. data/test/test_i18n.rb +39 -0
  86. data/test/test_img_tag.rb +11 -6
  87. data/test/test_inline_caption_tag.rb +11 -7
  88. data/test/test_inline_font_size_tag.rb +15 -11
  89. data/test/test_inline_keigakomi_tag.rb +11 -7
  90. data/test/test_inline_yokogumi_tag.rb +11 -7
  91. data/test/test_jizume_tag.rb +11 -8
  92. data/test/test_jstream.rb +33 -30
  93. data/test/test_kaeriten_tag.rb +11 -7
  94. data/test/test_keigakomi_tag.rb +14 -11
  95. data/test/test_midashi_tag.rb +39 -0
  96. data/test/test_multiline_caption_tag.rb +11 -8
  97. data/test/test_multiline_midashi_tag.rb +26 -26
  98. data/test/test_multiline_style_tag.rb +11 -8
  99. data/test/test_multiline_yokogumi_tag.rb +11 -8
  100. data/test/test_okurigana_tag.rb +11 -7
  101. data/test/test_ruby_parse.rb +130 -0
  102. data/test/test_ruby_tag.rb +11 -7
  103. data/test/test_tag_parser.rb +31 -29
  104. data/vendor/jis2ucs/README.md +3 -6
  105. data/yml/accent_table.yml +240 -0
  106. data/yml/command_table.yml +61 -0
  107. data/yml/jis2ucs.yml +11234 -0
  108. metadata +99 -21
  109. data/.travis.yml +0 -12
  110. data/appveyor.yml +0 -23
  111. data/lib/accent_tag.rb +0 -23
  112. data/lib/aozora2html/jis2ucs.rb +0 -11237
  113. data/lib/embed_gaiji_tag.rb +0 -34
  114. data/lib/t2hs.rb +0 -2535
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
  require 'fileutils'
@@ -6,110 +7,359 @@ require 'tmpdir'
6
7
 
7
8
  class Aozora2HtmlTest < Test::Unit::TestCase
8
9
  def setup
10
+ input = StringIO.new("abc\r\n")
11
+ output = StringIO.new
12
+ @parser = Aozora2Html.new(input, output)
9
13
  end
10
14
 
11
15
  def test_aozora2html_new
12
16
  Dir.mktmpdir do |dir|
13
- input = File.join(dir,'dummy.txt')
14
- output = File.join(dir,'dummy2.txt')
17
+ input = File.join(dir, 'dummy.txt')
18
+ output = File.join(dir, 'dummy2.txt')
15
19
  File.binwrite(input, "テスト\r\n")
16
20
  parser = Aozora2Html.new(input, output)
17
21
  begin
18
22
  assert_equal Aozora2Html, parser.class
19
23
  ensure
20
- parser.close
24
+ parser.__send__(:close)
21
25
  end
22
26
  end
23
27
  end
24
28
 
25
- def test_scount
29
+ def test_line_number
26
30
  Dir.mktmpdir do |dir|
27
- input = File.join(dir,'dummy.txt')
28
- output = File.join(dir,'dummy2.txt')
31
+ input = File.join(dir, 'dummy.txt')
32
+ output = File.join(dir, 'dummy2.txt')
29
33
  File.binwrite(input, "a\r\nb\r\nc\r\n")
30
34
  parser = Aozora2Html.new(input, output)
31
35
 
32
36
  begin
33
- assert_equal 0, parser.scount
34
- ch = parser.read_char
35
- assert_equal "a",ch
36
- assert_equal 1, parser.scount
37
- ch = parser.read_char
38
- assert_equal "\r\n",ch
39
- assert_equal 1, parser.scount
40
- ch = parser.read_char
41
- assert_equal "b",ch
42
- assert_equal 2, parser.scount
43
- ch = parser.read_char
44
- assert_equal "\r\n",ch
45
- assert_equal 2, parser.scount
46
- ch = parser.read_char
47
- assert_equal "c",ch
48
- assert_equal 3, parser.scount
37
+ assert_equal 0, parser.line_number
38
+ ch = parser.__send__(:read_char)
39
+ assert_equal 'a', ch
40
+ assert_equal 1, parser.line_number
41
+ ch = parser.__send__(:read_char)
42
+ assert_equal "\r\n", ch
43
+ assert_equal 1, parser.line_number
44
+ ch = parser.__send__(:read_char)
45
+ assert_equal 'b', ch
46
+ assert_equal 2, parser.line_number
47
+ ch = parser.__send__(:read_char)
48
+ assert_equal "\r\n", ch
49
+ assert_equal 2, parser.line_number
50
+ ch = parser.__send__(:read_char)
51
+ assert_equal 'c', ch
52
+ assert_equal 3, parser.line_number
49
53
  ensure
50
- parser.close
54
+ parser.__send__(:close)
51
55
  end
52
56
  end
53
57
  end
54
58
 
59
+ def test_line_number2
60
+ input = StringIO.new("a\r\nb\r\nc\r\n")
61
+ output = StringIO.new
62
+ parser = Aozora2Html.new(input, output)
63
+ assert_equal 0, parser.line_number
64
+ ch = parser.__send__(:read_char)
65
+ assert_equal 'a', ch
66
+ assert_equal 1, parser.line_number
67
+ ch = parser.__send__(:read_char)
68
+ assert_equal "\r\n", ch
69
+ assert_equal 1, parser.line_number
70
+ ch = parser.__send__(:read_char)
71
+ assert_equal 'b', ch
72
+ assert_equal 2, parser.line_number
73
+ ch = parser.__send__(:read_char)
74
+ assert_equal "\r\n", ch
75
+ assert_equal 2, parser.line_number
76
+ ch = parser.__send__(:read_char)
77
+ assert_equal 'c', ch
78
+ assert_equal 3, parser.line_number
79
+ end
80
+
55
81
  def test_read_line
56
- Dir.mktmpdir do |dir|
57
- input = File.join(dir,'dummy.txt')
58
- output = File.join(dir,'dummy2.txt')
59
- File.binwrite(input, "ab\r\nc\r\n")
60
- parser = Aozora2Html.new(input, output)
61
- begin
62
- parsed = parser.read_line
63
- assert_equal "ab", parsed
64
- ensure
65
- parser.close
66
- end
67
- end
82
+ input = StringIO.new("ab\r\nc\r\n")
83
+ output = StringIO.new
84
+ parser = Aozora2Html.new(input, output)
85
+ parsed = parser.__send__(:read_line)
86
+ assert_equal 'ab', parsed
68
87
  end
69
88
 
89
+ using Aozora2Html::StringRefinements
90
+
70
91
  def test_char_type
71
- Dir.mktmpdir do |dir|
72
- input = File.join(dir,'dummy.txt')
73
- output = File.join(dir,'dummy2.txt')
74
- File.binwrite(input, "ab\r\nc\r\n")
75
- parser = Aozora2Html.new(input, output)
92
+ assert_equal :kanji, Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-2-3', 'name', gaiji_dir: nil).char_type
93
+ assert_equal :kanji, Aozora2Html::Tag::UnEmbedGaiji.new(nil, 'foo').char_type
94
+ assert_equal :hankaku, Aozora2Html::Tag::Accent.new(nil, 123, 'abc', gaiji_dir: nil).char_type
95
+ assert_equal :else, Aozora2Html::Tag::Okurigana.new(nil, 'abc').char_type
96
+ assert_equal :else, Aozora2Html::Tag::InlineKeigakomi.new(nil, 'abc').char_type
97
+ assert_equal :katakana, Aozora2Html::Tag::DakutenKatakana.new(nil, 1, 'abc', gaiji_dir: nil).char_type
76
98
 
77
- begin
78
- assert_equal :kanji, parser.char_type(Embed_Gaiji_tag.new(nil,"foo","1-2-3","name"))
79
- assert_equal :kanji, parser.char_type(UnEmbed_Gaiji_tag.new(nil,"foo"))
80
- assert_equal :hankaku, parser.char_type(Accent_tag.new(nil,123,"abc"))
81
- assert_equal :else, parser.char_type(Okurigana_tag.new(nil,"abc"))
82
- assert_equal :else, parser.char_type(Inline_keigakomi_tag.new(nil,"abc"))
83
- assert_equal :katakana, parser.char_type(Dakuten_katakana_tag.new(nil,1,"abc"))
84
-
85
- assert_equal :hiragana, parser.char_type("あ".encode("shift_jis"))
86
- assert_equal :hiragana, parser.char_type("っ".encode("shift_jis"))
87
- assert_equal :katakana, parser.char_type("ヴ".encode("shift_jis"))
88
- assert_equal :katakana, parser.char_type("ー".encode("shift_jis"))
89
- assert_equal :zenkaku, parser.char_type("w".encode("shift_jis"))
90
- assert_equal :hankaku, parser.char_type("z".encode("shift_jis"))
91
- assert_equal :kanji, parser.char_type("漢".encode("shift_jis"))
92
- assert_equal :hankaku_terminate, parser.char_type("!".encode("shift_jis"))
93
- assert_equal :else, parser.char_type("?".encode("shift_jis"))
94
- ensure
95
- parser.close
96
- end
97
- end
99
+ assert_equal :hiragana, 'あ'.to_sjis.char_type
100
+ assert_equal :hiragana, 'っ'.to_sjis.char_type
101
+ assert_equal :katakana, 'ヴ'.to_sjis.char_type
102
+ assert_equal :katakana, 'ー'.to_sjis.char_type
103
+ assert_equal :zenkaku, 'A'.to_sjis.char_type
104
+ assert_equal :zenkaku, 'w'.to_sjis.char_type
105
+ assert_equal :hankaku, 'z'.to_sjis.char_type
106
+ assert_equal :kanji, '漢'.to_sjis.char_type
107
+ assert_equal :hankaku_terminate, '!'.to_sjis.char_type
108
+ assert_equal :else, '?'.to_sjis.char_type
109
+ assert_equal :else, 'Å'.to_sjis.char_type
98
110
  end
99
111
 
100
112
  def test_read_char
101
- Dir.mktmpdir do |dir|
102
- input = File.join(dir,'dummy.txt')
103
- output = File.join(dir,'dummy2.txt')
104
- File.binwrite(input, "/\\r\n".encode("shift_jis"))
105
- parser = Aozora2Html.new(input, output)
106
- begin
107
- char = parser.read_char
108
- assert_equal "/".encode("shift_jis"), char
109
- assert_equal Aozora2Html.class_eval("@@ku"), char
110
- ensure
111
- parser.close
113
+ input = StringIO.new("/\\r\n".to_sjis)
114
+ output = StringIO.new
115
+ parser = Aozora2Html.new(input, output)
116
+ char = parser.__send__(:read_char)
117
+ assert_equal '/'.to_sjis, char
118
+ assert_equal Aozora2Html::KU, char
119
+ end
120
+
121
+ def test_illegal_char_check
122
+ out = StringIO.new
123
+ $stdout = out
124
+ begin
125
+ Aozora2Html::Utils.illegal_char_check('#', 123)
126
+ outstr = out.string
127
+ assert_equal "警告(123行目):1バイトの「#」が使われています\n", outstr.to_utf8
128
+ ensure
129
+ $stdout = STDOUT
130
+ end
131
+ end
132
+
133
+ def test_illegal_char_check_sharp
134
+ out = StringIO.new
135
+ $stdout = out
136
+ begin
137
+ Aozora2Html::Utils.illegal_char_check('♯'.to_sjis, 123)
138
+ outstr = out.string
139
+ assert_equal "警告(123行目):注記記号の誤用の可能性がある、「♯」が使われています\n", outstr.to_utf8
140
+ ensure
141
+ $stdout = STDOUT
142
+ end
143
+ end
144
+
145
+ def test_illegal_char_check_notjis
146
+ out = StringIO.new
147
+ $stdout = out
148
+ begin
149
+ Aozora2Html::Utils.illegal_char_check('①'.encode('cp932').force_encoding('shift_jis'), 123)
150
+ outstr = out.string
151
+ assert_equal "警告(123行目):JIS外字「①」が使われています\n", outstr.force_encoding('cp932').to_utf8
152
+ ensure
153
+ $stdout = STDOUT
154
+ end
155
+ end
156
+
157
+ def test_illegal_char_check_ok
158
+ out = StringIO.new
159
+ $stdout = out
160
+ begin
161
+ Aozora2Html::Utils.illegal_char_check('あ'.to_sjis, 123)
162
+ outstr = out.string
163
+ assert_equal '', outstr
164
+ ensure
165
+ $stdout = STDOUT
166
+ end
167
+ end
168
+
169
+ def test_convert_japanese_number
170
+ assert_equal '3字下げ',
171
+ Aozora2Html::Utils.convert_japanese_number('三字下げ'.to_sjis).to_utf8
172
+ assert_equal '10字下げ',
173
+ Aozora2Html::Utils.convert_japanese_number('十字下げ'.to_sjis).to_utf8
174
+ assert_equal '12字下げ',
175
+ Aozora2Html::Utils.convert_japanese_number('十二字下げ'.to_sjis).to_utf8
176
+ assert_equal '20字下げ',
177
+ Aozora2Html::Utils.convert_japanese_number('二十字下げ'.to_sjis).to_utf8
178
+ assert_equal '20字下げ',
179
+ Aozora2Html::Utils.convert_japanese_number('二〇字下げ'.to_sjis).to_utf8
180
+ assert_equal '23字下げ',
181
+ Aozora2Html::Utils.convert_japanese_number('二十三字下げ'.to_sjis).to_utf8
182
+ assert_equal '2字下げ',
183
+ Aozora2Html::Utils.convert_japanese_number('2字下げ'.to_sjis).to_utf8
184
+ end
185
+
186
+ def test_kuten2png
187
+ assert_equal %q|<img src="../../../gaiji/1-84/1-84-77.png" alt="※(「てへん+劣」、第3水準1-84-77)" class="gaiji" />|,
188
+ @parser.kuten2png('#「てへん+劣」、第3水準1-84-77'.to_sjis).to_s.to_utf8
189
+ assert_equal %q|<img src="../../../gaiji/1-02/1-02-22.png" alt="※(二の字点、1-2-22)" class="gaiji" />|,
190
+ @parser.kuten2png('#二の字点、1-2-22'.to_sjis).to_s.to_utf8
191
+ assert_equal %q|<img src="../../../gaiji/1-06/1-06-57.png" alt="※(ファイナルシグマ、1-6-57)" class="gaiji" />|,
192
+ @parser.kuten2png('#ファイナルシグマ、1-6-57'.to_sjis).to_s.to_utf8
193
+ assert_equal %q(#「口+世」、151-23),
194
+ @parser.kuten2png('#「口+世」、151-23'.to_sjis).to_s.to_utf8
195
+ end
196
+
197
+ def test_terpri?
198
+ assert_equal true, Aozora2Html::TextBuffer.new.terpri?
199
+ assert_equal true, Aozora2Html::TextBuffer.new(['']).terpri?
200
+ assert_equal true, Aozora2Html::TextBuffer.new(['a']).terpri?
201
+ tag = Aozora2Html::Tag::MultilineMidashi.new(@parser, '小'.to_sjis, :normal)
202
+ assert_equal false, Aozora2Html::TextBuffer.new([tag]).terpri?
203
+ assert_equal false, Aozora2Html::TextBuffer.new([tag, tag]).terpri?
204
+ assert_equal false, Aozora2Html::TextBuffer.new([tag, '', '']).terpri?
205
+ assert_equal false, Aozora2Html::TextBuffer.new(['', tag, '']).terpri?
206
+ assert_equal true, Aozora2Html::TextBuffer.new([tag, 'a']).terpri?
207
+ assert_equal true, Aozora2Html::TextBuffer.new(['a', tag]).terpri?
208
+ end
209
+
210
+ def test_new_midashi_id
211
+ midashi_id = @parser.new_midashi_id(1)
212
+ assert_equal midashi_id + 1, @parser.new_midashi_id(1)
213
+ assert_equal midashi_id + 2, @parser.new_midashi_id('小'.to_sjis)
214
+ assert_equal midashi_id + 12, @parser.new_midashi_id('中'.to_sjis)
215
+ assert_equal midashi_id + 112, @parser.new_midashi_id('大'.to_sjis)
216
+ assert_raise(Aozora2Html::Error) do
217
+ @parser.new_midashi_id('?'.to_sjis)
218
+ end
219
+ end
220
+
221
+ def test_multiply
222
+ bouki = @parser.__send__(:multiply, 'x', 5)
223
+ assert_equal 'x&nbsp;x&nbsp;x&nbsp;x&nbsp;x', bouki
224
+ end
225
+
226
+ def test_apply_midashi
227
+ midashi = @parser.__send__(:apply_midashi, '中見出し'.to_sjis)
228
+ assert_equal %Q(<h4 class="naka-midashi"><a class="midashi_anchor" id="midashi10">), midashi.to_s
229
+ midashi = @parser.__send__(:apply_midashi, '大見出し'.to_sjis)
230
+ assert_equal %Q(<h3 class="o-midashi"><a class="midashi_anchor" id="midashi110">), midashi.to_s
231
+ end
232
+
233
+ def test_detect_command_mode
234
+ command = '字下げ終わり'.to_sjis
235
+ assert_equal :jisage, @parser.detect_command_mode(command)
236
+ command = '地付き終わり'.to_sjis
237
+ assert_equal :chitsuki, @parser.detect_command_mode(command)
238
+ command = '中見出し終わり'.to_sjis
239
+ assert_equal :midashi, @parser.detect_command_mode(command)
240
+ command = 'ここで太字終わり'.to_sjis
241
+ assert_equal :futoji, @parser.detect_command_mode(command)
242
+ end
243
+
244
+ def test_tcy
245
+ input = StringIO.new("[#縦中横](※[#ローマ数字1、1-13-21])\r\n".to_sjis)
246
+ output = StringIO.new
247
+ parser = Aozora2Html.new(input, output)
248
+ out = StringIO.new
249
+ $stdout = out
250
+ message = nil
251
+ begin
252
+ parser.__send__(:parse_body)
253
+ parser.__send__(:general_output)
254
+ rescue Aozora2Html::Error => e
255
+ message = e.message.to_utf8
256
+ ensure
257
+ $stdout = STDOUT
258
+ assert_equal "エラー(0行目):縦中横中に改行されました。改行をまたぐ要素にはブロック表記を用いてください. \r\n処理を停止します", message
259
+ end
260
+ end
261
+
262
+ def test_ensure_close
263
+ input = StringIO.new("[#ここから5字下げ]\r\n底本: test\r\n".to_sjis)
264
+ output = StringIO.new
265
+ parser = Aozora2Html.new(input, output)
266
+ out = StringIO.new
267
+ $stdout = out
268
+ message = nil
269
+ begin
270
+ parser.__send__(:parse_body)
271
+ parser.__send__(:parse_body)
272
+ parser.__send__(:parse_body)
273
+ parser.__send__(:general_output)
274
+ rescue Aozora2Html::Error => e
275
+ message = e.message.to_utf8
276
+ ensure
277
+ $stdout = STDOUT
278
+ assert_equal "エラー(0行目):字下げ中に本文が終了しました. \r\n処理を停止します", message
279
+ end
280
+ end
281
+
282
+ def test_ending_check
283
+ input = StringIO.new("本文\r\n\r\n底本:test\r\n".to_sjis)
284
+ output = StringIO.new
285
+ parser = Aozora2Html.new(input, output)
286
+ out = StringIO.new
287
+ $stdout = out
288
+ _message = nil
289
+ begin
290
+ parser.__send__(:parse_body)
291
+ parser.__send__(:parse_body)
292
+ parser.__send__(:parse_body)
293
+ parser.__send__(:parse_body)
294
+ parser.__send__(:parse_body)
295
+ rescue Aozora2Html::Error => e
296
+ _message = e.message.to_utf8
297
+ ensure
298
+ $stdout = STDOUT
299
+ output.seek(0)
300
+ out_text = output.read
301
+ assert_equal "本文<br />\r\n<br />\r\n</div>\r\n<div class=\"bibliographical_information\">\r\n<hr />\r\n<br />\r\n", out_text
302
+ end
303
+ end
304
+
305
+ def test_invalid_closing
306
+ input = StringIO.new("[#ここで太字終わり]\r\n".to_sjis)
307
+ output = StringIO.new
308
+ parser = Aozora2Html.new(input, output)
309
+ out = StringIO.new
310
+ $stdout = out
311
+ message = nil
312
+ begin
313
+ parser.__send__(:parse_body)
314
+ rescue Aozora2Html::Error => e
315
+ message = e.message.to_utf8
316
+ ensure
317
+ $stdout = STDOUT
318
+ assert_equal "エラー(0行目):太字を閉じようとしましたが、太字中ではありません. \r\n処理を停止します", message
319
+ end
320
+ end
321
+
322
+ def test_invalid_nest
323
+ input = StringIO.new("[#太字][#傍線]あ[#太字終わり]\r\n".to_sjis)
324
+ output = StringIO.new
325
+ parser = Aozora2Html.new(input, output)
326
+ out = StringIO.new
327
+ $stdout = out
328
+ message = nil
329
+ begin
330
+ parser.__send__(:parse_body)
331
+ parser.__send__(:parse_body)
332
+ parser.__send__(:parse_body)
333
+ parser.__send__(:parse_body)
334
+ parser.__send__(:parse_body)
335
+ parser.__send__(:parse_body)
336
+ parser.__send__(:parse_body)
337
+ rescue Aozora2Html::Error => e
338
+ message = e.message.to_utf8
339
+ ensure
340
+ $stdout = STDOUT
341
+ assert_equal "エラー(0行目):太字を終了しようとしましたが、傍線中です. \r\n処理を停止します", message
342
+ end
343
+ end
344
+
345
+ def test_command_do
346
+ input = StringIO.new("[#ここから太字]\r\nテスト。\r\n[#ここで太字終わり]\r\n".to_sjis)
347
+ output = StringIO.new
348
+ parser = Aozora2Html.new(input, output)
349
+ out = StringIO.new
350
+ $stdout = out
351
+ _message = nil
352
+ begin
353
+ 9.times do
354
+ parser.__send__(:parse_body)
112
355
  end
356
+ rescue Aozora2Html::Error => e
357
+ _message = e.message.to_utf8
358
+ ensure
359
+ $stdout = STDOUT
360
+ output.seek(0)
361
+ out_text = output.read
362
+ assert_equal "<div class=\"futoji\">\r\nテスト。<br />\r\n</div>\r\n", out_text
113
363
  end
114
364
  end
115
365
 
@@ -1,20 +1,48 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
 
5
- class Aozora2HtmlTest < Test::Unit::TestCase
6
+ class Aozora2HtmlAccentParserTest < Test::Unit::TestCase
6
7
  def setup
7
8
  end
8
9
 
10
+ using Aozora2Html::StringRefinements
11
+
9
12
  def test_new
10
- str = "〔e'tiquette〕\r\n".encode("shift_jis")
13
+ str = "〔e'tiquette〕\r\n".to_sjis
14
+ strio = StringIO.new(str)
15
+ stream = Jstream.new(strio)
16
+ parsed = Aozora2Html::AccentParser.new(stream, '〕'.to_sjis, {}, [], gaiji_dir: 'g_dir/').process
17
+ expected = '〔<img src="g_dir/1-09/1-09-63.png" alt="※(アキュートアクセント付きE小文字)" class="gaiji" />tiquette'
18
+ assert_equal expected, parsed.to_s.to_utf8
19
+ end
20
+
21
+ def test_invalid
22
+ str = "〔e'tiquette\r\n".to_sjis
23
+ strio = StringIO.new(str)
24
+ stream = Jstream.new(strio)
25
+ $stdout = StringIO.new
26
+ begin
27
+ _parsed = Aozora2Html::AccentParser.new(stream, '〕'.to_sjis, {}, [], gaiji_dir: 'g_dir/').process
28
+ out_str = $stdout.string
29
+ assert_equal "警告(1行目):アクセント分解の亀甲括弧の始めと終わりが、行中で揃っていません\n", out_str.to_utf8
30
+ ensure
31
+ $stdout = STDOUT
32
+ end
33
+ end
34
+
35
+ def test_use_jisx0213
36
+ Aozora2Html::Tag::Accent.use_jisx0213 = true
37
+ str = "〔e'tiquette〕\r\n".to_sjis
11
38
  strio = StringIO.new(str)
12
39
  stream = Jstream.new(strio)
13
- parsed = Aozora_accent_parser.new(stream,"".encode("shift_jis"),{},[]).process
14
- expected = "〔<img src=\"../../../gaiji/1-09/1-09-63.png\" alt=\"※(アキュートアクセント付きE小文字)\" class=\"gaiji\" />tiquette"
15
- assert_equal expected, parsed.to_s.encode("utf-8")
40
+ parsed = Aozora2Html::AccentParser.new(stream, ''.to_sjis, {}, [], gaiji_dir: 'g_dir/').process
41
+ expected = '〔&#x00E9;tiquette'
42
+ assert_equal expected, parsed.to_s.to_utf8
16
43
  end
17
44
 
18
45
  def teardown
46
+ Aozora2Html::Tag::Accent.use_jisx0213 = nil
19
47
  end
20
48
  end