aozora2html 0.7.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +34 -0
  3. data/.gitignore +8 -3
  4. data/.rubocop.yml +227 -0
  5. data/.travis.yml +5 -0
  6. data/CHANGELOG.md +33 -0
  7. data/LICENSE +116 -0
  8. data/README.md +16 -11
  9. data/aozora2html.gemspec +6 -6
  10. data/bin/aozora2html +6 -54
  11. data/lib/aozora2html/accent_parser.rb +91 -0
  12. data/lib/aozora2html/error.rb +16 -0
  13. data/lib/aozora2html/header.rb +139 -0
  14. data/lib/aozora2html/i18n.rb +28 -0
  15. data/lib/aozora2html/ruby_buffer.rb +81 -0
  16. data/lib/aozora2html/style_stack.rb +27 -0
  17. data/lib/aozora2html/tag/accent.rb +39 -0
  18. data/lib/aozora2html/tag/block.rb +19 -0
  19. data/lib/aozora2html/tag/chitsuki.rb +15 -0
  20. data/lib/aozora2html/tag/dakuten_katakana.rb +23 -0
  21. data/lib/aozora2html/tag/decorate.rb +18 -0
  22. data/lib/aozora2html/tag/dir.rb +15 -0
  23. data/lib/aozora2html/tag/editor_note.rb +16 -0
  24. data/lib/aozora2html/tag/embed_gaiji.rb +48 -0
  25. data/lib/aozora2html/tag/font_size.rb +17 -0
  26. data/lib/aozora2html/tag/gaiji.rb +11 -0
  27. data/lib/aozora2html/tag/img.rb +21 -0
  28. data/lib/aozora2html/tag/indent.rb +8 -0
  29. data/lib/aozora2html/tag/inline.rb +13 -0
  30. data/lib/aozora2html/tag/inline_caption.rb +15 -0
  31. data/lib/aozora2html/tag/inline_font_size.rb +18 -0
  32. data/lib/aozora2html/tag/inline_keigakomi.rb +15 -0
  33. data/lib/aozora2html/tag/inline_yokogumi.rb +16 -0
  34. data/lib/aozora2html/tag/jisage.rb +15 -0
  35. data/lib/aozora2html/tag/jizume.rb +16 -0
  36. data/lib/aozora2html/tag/kaeriten.rb +15 -0
  37. data/lib/aozora2html/tag/keigakomi.rb +13 -0
  38. data/lib/aozora2html/tag/kunten.rb +12 -0
  39. data/lib/aozora2html/tag/midashi.rb +18 -0
  40. data/lib/aozora2html/tag/multiline.rb +6 -0
  41. data/lib/aozora2html/tag/multiline_caption.rb +15 -0
  42. data/lib/aozora2html/tag/multiline_chitsuki.rb +8 -0
  43. data/lib/aozora2html/tag/multiline_jisage.rb +8 -0
  44. data/lib/aozora2html/tag/multiline_midashi.rb +22 -0
  45. data/lib/aozora2html/tag/multiline_style.rb +17 -0
  46. data/lib/aozora2html/tag/multiline_yokogumi.rb +17 -0
  47. data/lib/aozora2html/tag/okurigana.rb +15 -0
  48. data/lib/aozora2html/tag/oneline_chitsuki.rb +9 -0
  49. data/lib/aozora2html/tag/oneline_indent.rb +2 -0
  50. data/lib/aozora2html/tag/oneline_jisage.rb +7 -0
  51. data/lib/aozora2html/tag/reference_mentioned.rb +46 -0
  52. data/lib/aozora2html/tag/ruby.rb +98 -0
  53. data/lib/aozora2html/tag/un_embed_gaiji.rb +24 -0
  54. data/lib/aozora2html/tag.rb +55 -0
  55. data/lib/aozora2html/tag_parser.rb +53 -0
  56. data/lib/aozora2html/utils.rb +93 -0
  57. data/lib/aozora2html/version.rb +1 -1
  58. data/lib/aozora2html/yaml_loader.rb +31 -0
  59. data/lib/aozora2html.rb +0 -4
  60. data/lib/extensions.rb +44 -0
  61. data/lib/jstream.rb +68 -0
  62. data/lib/t2hs.rb +837 -1765
  63. data/test/test_aozora2html.rb +320 -43
  64. data/test/test_aozora_accent_parser.rb +13 -2
  65. data/test/test_command_parse.rb +213 -0
  66. data/test/test_dakuten_katakana_tag.rb +5 -4
  67. data/test/test_decorate_tag.rb +5 -4
  68. data/test/test_dir_tag.rb +5 -4
  69. data/test/test_editor_note_tag.rb +4 -4
  70. data/test/test_exception.rb +4 -4
  71. data/test/test_font_size_tag.rb +15 -7
  72. data/test/test_gaiji_tag.rb +14 -7
  73. data/test/test_header.rb +60 -0
  74. data/test/test_i18n.rb +23 -0
  75. data/test/test_img_tag.rb +5 -4
  76. data/test/test_inline_caption_tag.rb +5 -4
  77. data/test/test_inline_font_size_tag.rb +7 -6
  78. data/test/test_inline_keigakomi_tag.rb +5 -4
  79. data/test/test_inline_yokogumi_tag.rb +5 -4
  80. data/test/test_jizume_tag.rb +7 -6
  81. data/test/test_jstream.rb +5 -5
  82. data/test/test_kaeriten_tag.rb +5 -4
  83. data/test/test_keigakomi_tag.rb +8 -7
  84. data/test/test_midashi_tag.rb +38 -0
  85. data/test/test_multiline_caption_tag.rb +7 -6
  86. data/test/test_multiline_midashi_tag.rb +24 -7
  87. data/test/test_multiline_style_tag.rb +7 -6
  88. data/test/test_multiline_yokogumi_tag.rb +7 -6
  89. data/test/test_okurigana_tag.rb +5 -4
  90. data/test/test_ruby_parse.rb +130 -0
  91. data/test/test_ruby_tag.rb +5 -4
  92. data/test/test_tag_parser.rb +10 -10
  93. data/vendor/jis2ucs/README.md +3 -6
  94. data/yml/accent_table.yml +240 -0
  95. data/yml/command_table.yml +61 -0
  96. data/yml/jis2ucs.yml +11234 -0
  97. metadata +74 -42
  98. data/appveyor.yml +0 -19
  99. data/lib/accent_tag.rb +0 -23
  100. data/lib/aozora2html/jis2ucs.rb +0 -11237
  101. data/lib/embed_gaiji_tag.rb +0 -34
@@ -6,6 +6,9 @@ require 'tmpdir'
6
6
 
7
7
  class Aozora2HtmlTest < Test::Unit::TestCase
8
8
  def setup
9
+ input = StringIO.new("abc\r\n")
10
+ output = StringIO.new
11
+ @parser = Aozora2Html.new(input, output)
9
12
  end
10
13
 
11
14
  def test_aozora2html_new
@@ -22,7 +25,7 @@ class Aozora2HtmlTest < Test::Unit::TestCase
22
25
  end
23
26
  end
24
27
 
25
- def test_scount
28
+ def test_line_number
26
29
  Dir.mktmpdir do |dir|
27
30
  input = File.join(dir,'dummy.txt')
28
31
  output = File.join(dir,'dummy2.txt')
@@ -30,73 +33,347 @@ class Aozora2HtmlTest < Test::Unit::TestCase
30
33
  parser = Aozora2Html.new(input, output)
31
34
 
32
35
  begin
33
- assert_equal 0, parser.scount
36
+ assert_equal 0, parser.line_number
34
37
  ch = parser.read_char
35
38
  assert_equal "a",ch
36
- assert_equal 1, parser.scount
39
+ assert_equal 1, parser.line_number
37
40
  ch = parser.read_char
38
41
  assert_equal "\r\n",ch
39
- assert_equal 1, parser.scount
42
+ assert_equal 1, parser.line_number
40
43
  ch = parser.read_char
41
44
  assert_equal "b",ch
42
- assert_equal 2, parser.scount
45
+ assert_equal 2, parser.line_number
43
46
  ch = parser.read_char
44
47
  assert_equal "\r\n",ch
45
- assert_equal 2, parser.scount
48
+ assert_equal 2, parser.line_number
46
49
  ch = parser.read_char
47
50
  assert_equal "c",ch
48
- assert_equal 3, parser.scount
51
+ assert_equal 3, parser.line_number
49
52
  ensure
50
53
  parser.close
51
54
  end
52
55
  end
53
56
  end
54
57
 
58
+ def test_line_number_2
59
+ input = StringIO.new("a\r\nb\r\nc\r\n")
60
+ output = StringIO.new
61
+ parser = Aozora2Html.new(input, output)
62
+ assert_equal 0, parser.line_number
63
+ ch = parser.read_char
64
+ assert_equal "a",ch
65
+ assert_equal 1, parser.line_number
66
+ ch = parser.read_char
67
+ assert_equal "\r\n",ch
68
+ assert_equal 1, parser.line_number
69
+ ch = parser.read_char
70
+ assert_equal "b",ch
71
+ assert_equal 2, parser.line_number
72
+ ch = parser.read_char
73
+ assert_equal "\r\n",ch
74
+ assert_equal 2, parser.line_number
75
+ ch = parser.read_char
76
+ assert_equal "c",ch
77
+ assert_equal 3, parser.line_number
78
+ end
79
+
55
80
  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
81
+ input = StringIO.new("ab\r\nc\r\n")
82
+ output = StringIO.new
83
+ parser = Aozora2Html.new(input, output)
84
+ parsed = parser.read_line
85
+ assert_equal "ab", parsed
68
86
  end
69
87
 
70
88
  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)
89
+ assert_equal :kanji, @parser.char_type(Aozora2Html::Tag::EmbedGaiji.new(nil,"foo","1-2-3","name"))
90
+ assert_equal :kanji, @parser.char_type(Aozora2Html::Tag::UnEmbedGaiji.new(nil,"foo"))
91
+ assert_equal :hankaku, @parser.char_type(Aozora2Html::Tag::Accent.new(nil,123,"abc"))
92
+ assert_equal :else, @parser.char_type(Aozora2Html::Tag::Okurigana.new(nil,"abc"))
93
+ assert_equal :else, @parser.char_type(Aozora2Html::Tag::InlineKeigakomi.new(nil,"abc"))
94
+ assert_equal :katakana, @parser.char_type(Aozora2Html::Tag::DakutenKatakana.new(nil,1,"abc"))
76
95
 
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
+ assert_equal :hiragana, @parser.char_type("あ".encode("shift_jis"))
97
+ assert_equal :hiragana, @parser.char_type("っ".encode("shift_jis"))
98
+ assert_equal :katakana, @parser.char_type("ヴ".encode("shift_jis"))
99
+ assert_equal :katakana, @parser.char_type("ー".encode("shift_jis"))
100
+ assert_equal :zenkaku, @parser.char_type("A".encode("shift_jis"))
101
+ assert_equal :zenkaku, @parser.char_type("w".encode("shift_jis"))
102
+ assert_equal :hankaku, @parser.char_type("z".encode("shift_jis"))
103
+ assert_equal :kanji, @parser.char_type("漢".encode("shift_jis"))
104
+ assert_equal :hankaku_terminate, @parser.char_type("!".encode("shift_jis"))
105
+ assert_equal :else, @parser.char_type("".encode("shift_jis"))
106
+ assert_equal :else, @parser.char_type("".encode("shift_jis"))
107
+ end
108
+
109
+ def test_read_char
110
+ input = StringIO.new("/\\r\n".encode("shift_jis"))
111
+ output = StringIO.new
112
+ parser = Aozora2Html.new(input, output)
113
+ char = parser.read_char
114
+ assert_equal "/".encode("shift_jis"), char
115
+ assert_equal Aozora2Html::KU, char
116
+ end
117
+
118
+ def test_illegal_char_check
119
+ input = StringIO.new("abc\r\n")
120
+ output = StringIO.new
121
+ parser = Aozora2Html.new(input, output)
122
+ out = StringIO.new
123
+ $stdout = out
124
+ begin
125
+ parser.illegal_char_check("#", 123)
126
+ outstr = out.string
127
+ assert_equal "警告(123行目):1バイトの「#」が使われています\n", outstr.encode("utf-8")
128
+ ensure
129
+ $stdout = STDOUT
130
+ end
131
+ end
132
+
133
+ def test_illegal_char_check_sharp
134
+ input = StringIO.new("abc\r\n")
135
+ output = StringIO.new
136
+ parser = Aozora2Html.new(input, output)
137
+ out = StringIO.new
138
+ $stdout = out
139
+ begin
140
+ parser.illegal_char_check("♯".encode("shift_jis"), 123)
141
+ outstr = out.string
142
+ assert_equal "警告(123行目):注記記号の誤用の可能性がある、「♯」が使われています\n", outstr.encode("utf-8")
143
+ ensure
144
+ $stdout = STDOUT
145
+ end
146
+ end
147
+
148
+ def test_illegal_char_check_notjis
149
+ input = StringIO.new("abc\r\n")
150
+ output = StringIO.new
151
+ parser = Aozora2Html.new(input, output)
152
+ out = StringIO.new
153
+ $stdout = out
154
+ begin
155
+ parser.illegal_char_check("①".encode("cp932").force_encoding("shift_jis"), 123)
156
+ outstr = out.string
157
+ assert_equal "警告(123行目):JIS外字「①」が使われています\n", outstr.force_encoding("cp932").encode("utf-8")
158
+ ensure
159
+ $stdout = STDOUT
160
+ end
161
+ end
162
+
163
+ def test_illegal_char_check_ok
164
+ input = StringIO.new("abc\r\n")
165
+ output = StringIO.new
166
+ parser = Aozora2Html.new(input, output)
167
+ out = StringIO.new
168
+ $stdout = out
169
+ begin
170
+ parser.illegal_char_check("あ".encode("shift_jis"), 123)
171
+ outstr = output.string
172
+ assert_equal "", outstr
173
+ ensure
174
+ $stdout = STDOUT
175
+ end
176
+ end
177
+
178
+ def test_convert_japanese_number
179
+ assert_equal "3字下げ",
180
+ Aozora2Html::Utils.convert_japanese_number("三字下げ".encode("shift_jis")).encode("utf-8")
181
+ assert_equal "10字下げ",
182
+ Aozora2Html::Utils.convert_japanese_number("十字下げ".encode("shift_jis")).encode("utf-8")
183
+ assert_equal "12字下げ",
184
+ Aozora2Html::Utils.convert_japanese_number("十二字下げ".encode("shift_jis")).encode("utf-8")
185
+ assert_equal "20字下げ",
186
+ Aozora2Html::Utils.convert_japanese_number("二十字下げ".encode("shift_jis")).encode("utf-8")
187
+ assert_equal "20字下げ",
188
+ Aozora2Html::Utils.convert_japanese_number("二〇字下げ".encode("shift_jis")).encode("utf-8")
189
+ assert_equal "23字下げ",
190
+ Aozora2Html::Utils.convert_japanese_number("二十三字下げ".encode("shift_jis")).encode("utf-8")
191
+ assert_equal "2字下げ",
192
+ Aozora2Html::Utils.convert_japanese_number("2字下げ".encode("shift_jis")).encode("utf-8")
193
+
194
+ end
195
+
196
+ def test_kuten2png
197
+ assert_equal %q|<img src="../../../gaiji/1-84/1-84-77.png" alt="※(「てへん+劣」、第3水準1-84-77)" class="gaiji" />|,
198
+ @parser.kuten2png("#「てへん+劣」、第3水準1-84-77".encode("shift_jis")).to_s.encode("utf-8")
199
+ assert_equal %q|<img src="../../../gaiji/1-02/1-02-22.png" alt="※(二の字点、1-2-22)" class="gaiji" />|,
200
+ @parser.kuten2png("#二の字点、1-2-22".encode("shift_jis")).to_s.encode("utf-8")
201
+ assert_equal %q|<img src="../../../gaiji/1-06/1-06-57.png" alt="※(ファイナルシグマ、1-6-57)" class="gaiji" />|,
202
+ @parser.kuten2png("#ファイナルシグマ、1-6-57".encode("shift_jis")).to_s.encode("utf-8")
203
+ assert_equal %q|#「口+世」、151-23|,
204
+ @parser.kuten2png("#「口+世」、151-23".encode("shift_jis")).to_s.encode("utf-8")
205
+ end
206
+
207
+ def test_terpri?
208
+ assert_equal true, @parser.terpri?([])
209
+ assert_equal true, @parser.terpri?([""])
210
+ assert_equal true, @parser.terpri?(["a"])
211
+ tag = Aozora2Html::Tag::MultilineMidashi.new(@parser,"小".encode("shift_jis"),:normal)
212
+ assert_equal false, @parser.terpri?([tag])
213
+ assert_equal false, @parser.terpri?([tag,tag])
214
+ assert_equal false, @parser.terpri?([tag,"",""])
215
+ assert_equal false, @parser.terpri?(["",tag,""])
216
+ assert_equal true, @parser.terpri?([tag,"a"])
217
+ assert_equal true, @parser.terpri?(["a",tag])
218
+ end
219
+
220
+ def test_new_midashi_id
221
+ midashi_id = @parser.new_midashi_id(1)
222
+ assert_equal midashi_id + 1, @parser.new_midashi_id(1)
223
+ assert_equal midashi_id + 2, @parser.new_midashi_id("小".encode("shift_jis"))
224
+ assert_equal midashi_id + 12, @parser.new_midashi_id("中".encode("shift_jis"))
225
+ assert_equal midashi_id + 112, @parser.new_midashi_id("大".encode("shift_jis"))
226
+ assert_raise(Aozora2Html::Error) do
227
+ @parser.new_midashi_id("?".encode("shift_jis"))
228
+ end
229
+ end
230
+
231
+ def test_multiply
232
+ bouki = @parser.multiply("x", 5)
233
+ assert_equal "x&nbsp;x&nbsp;x&nbsp;x&nbsp;x", bouki
234
+ end
235
+
236
+ def test_apply_midashi
237
+ midashi = @parser.apply_midashi("中見出し".encode("shift_jis"))
238
+ assert_equal %Q|<h4 class="naka-midashi"><a class="midashi_anchor" id="midashi10">|, midashi.to_s
239
+ midashi = @parser.apply_midashi("大見出し".encode("shift_jis"))
240
+ assert_equal %Q|<h3 class="o-midashi"><a class="midashi_anchor" id="midashi110">|, midashi.to_s
241
+ end
242
+
243
+ def test_detect_command_mode
244
+ command = "字下げ終わり".encode("shift_jis")
245
+ assert_equal :jisage, @parser.detect_command_mode(command)
246
+ command = "地付き終わり".encode("shift_jis")
247
+ assert_equal :chitsuki, @parser.detect_command_mode(command)
248
+ command = "中見出し終わり".encode("shift_jis")
249
+ assert_equal :midashi, @parser.detect_command_mode(command)
250
+ command = "ここで太字終わり".encode("shift_jis")
251
+ assert_equal :futoji, @parser.detect_command_mode(command)
252
+ end
253
+
254
+ def test_tcy
255
+ input = StringIO.new("[#縦中横](※[#ローマ数字1、1-13-21])\r\n".encode("shift_jis"))
256
+ output = StringIO.new
257
+ parser = Aozora2Html.new(input, output)
258
+ out = StringIO.new
259
+ $stdout = out
260
+ message = nil
261
+ begin
262
+ parser.parse_body
263
+ parser.general_output
264
+ rescue Aozora2Html::Error => e
265
+ message = e.message.encode("utf-8")
266
+ ensure
267
+ $stdout = STDOUT
268
+ assert_equal "エラー(0行目):縦中横中に改行されました。改行をまたぐ要素にはブロック表記を用いてください. \r\n処理を停止します", message
269
+ end
270
+ end
271
+
272
+ def test_ensure_close
273
+ input = StringIO.new("[#ここから5字下げ]\r\n底本: test\r\n".encode("shift_jis"))
274
+ output = StringIO.new
275
+ parser = Aozora2Html.new(input, output)
276
+ out = StringIO.new
277
+ $stdout = out
278
+ message = nil
279
+ begin
280
+ parser.parse_body
281
+ parser.parse_body
282
+ parser.parse_body
283
+ parser.general_output
284
+ rescue Aozora2Html::Error => e
285
+ message = e.message.encode("utf-8")
286
+ ensure
287
+ $stdout = STDOUT
288
+ assert_equal "エラー(0行目):字下げ中に本文が終了しました. \r\n処理を停止します", message
289
+ end
290
+ end
291
+
292
+ def test_ending_check
293
+ input = StringIO.new("本文\r\n\r\n底本:test\r\n".encode("shift_jis"))
294
+ output = StringIO.new
295
+ parser = Aozora2Html.new(input, output)
296
+ out = StringIO.new
297
+ $stdout = out
298
+ _message = nil
299
+ begin
300
+ parser.parse_body
301
+ parser.parse_body
302
+ parser.parse_body
303
+ parser.parse_body
304
+ parser.parse_body
305
+ rescue Aozora2Html::Error => e
306
+ _message = e.message.encode("utf-8")
307
+ ensure
308
+ $stdout = STDOUT
309
+ output.seek(0)
310
+ out_text = output.read
311
+ 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
312
+ end
313
+ end
314
+
315
+ def test_invalid_closing
316
+ input = StringIO.new("[#ここで太字終わり]\r\n".encode("shift_jis"))
317
+ output = StringIO.new
318
+ parser = Aozora2Html.new(input, output)
319
+ out = StringIO.new
320
+ $stdout = out
321
+ message = nil
322
+ begin
323
+ parser.parse_body
324
+ rescue Aozora2Html::Error => e
325
+ message = e.message.encode("utf-8")
326
+ ensure
327
+ $stdout = STDOUT
328
+ assert_equal "エラー(0行目):太字を閉じようとしましたが、太字中ではありません. \r\n処理を停止します", message
329
+ end
330
+ end
331
+
332
+ def test_invalid_nest
333
+ input = StringIO.new("[#太字][#傍線]あ[#太字終わり]\r\n".encode("shift_jis"))
334
+ output = StringIO.new
335
+ parser = Aozora2Html.new(input, output)
336
+ out = StringIO.new
337
+ $stdout = out
338
+ message = nil
339
+ begin
340
+ parser.parse_body
341
+ parser.parse_body
342
+ parser.parse_body
343
+ parser.parse_body
344
+ parser.parse_body
345
+ parser.parse_body
346
+ parser.parse_body
347
+ rescue Aozora2Html::Error => e
348
+ message = e.message.encode("utf-8")
349
+ ensure
350
+ $stdout = STDOUT
351
+ assert_equal "エラー(0行目):太字を終了しようとしましたが、傍線中です. \r\n処理を停止します", message
352
+ end
353
+ end
354
+
355
+ def test_command_do
356
+ input = StringIO.new("[#ここから太字]\r\nテスト。\r\n[#ここで太字終わり]\r\n".encode("shift_jis"))
357
+ output = StringIO.new
358
+ parser = Aozora2Html.new(input, output)
359
+ out = StringIO.new
360
+ $stdout = out
361
+ _message = nil
362
+ begin
363
+ 9.times do
364
+ parser.parse_body
96
365
  end
366
+ rescue Aozora2Html::Error => e
367
+ _message = e.message.encode("utf-8")
368
+ ensure
369
+ $stdout = STDOUT
370
+ output.seek(0)
371
+ out_text = output.read
372
+ assert_equal "<div class=\"futoji\">\r\nテスト。<br />\r\n</div>\r\n", out_text
97
373
  end
98
374
  end
99
375
 
376
+
100
377
  def teardown
101
378
  end
102
379
  end
@@ -2,7 +2,7 @@
2
2
  require 'test_helper'
3
3
  require 'aozora2html'
4
4
 
5
- class Aozora2HtmlTest < Test::Unit::TestCase
5
+ class Aozora2HtmlAccentParserTest < Test::Unit::TestCase
6
6
  def setup
7
7
  end
8
8
 
@@ -10,11 +10,22 @@ class Aozora2HtmlTest < Test::Unit::TestCase
10
10
  str = "〔e'tiquette〕\r\n".encode("shift_jis")
11
11
  strio = StringIO.new(str)
12
12
  stream = Jstream.new(strio)
13
- parsed = Aozora_accent_parser.new(stream,"〕".encode("shift_jis"),{},[]).process
13
+ parsed = Aozora2Html::AccentParser.new(stream,"〕".encode("shift_jis"),{},[]).process
14
14
  expected = "〔<img src=\"../../../gaiji/1-09/1-09-63.png\" alt=\"※(アキュートアクセント付きE小文字)\" class=\"gaiji\" />tiquette"
15
15
  assert_equal expected, parsed.to_s.encode("utf-8")
16
16
  end
17
17
 
18
+ def test_use_jisx0213
19
+ Aozora2Html::Tag::Accent.use_jisx0213 = true
20
+ str = "〔e'tiquette〕\r\n".encode("shift_jis")
21
+ strio = StringIO.new(str)
22
+ stream = Jstream.new(strio)
23
+ parsed = Aozora2Html::AccentParser.new(stream,"〕".encode("shift_jis"),{},[]).process
24
+ expected = "〔&#x00E9;tiquette"
25
+ assert_equal expected, parsed.to_s.encode("utf-8")
26
+ end
27
+
18
28
  def teardown
29
+ Aozora2Html::Tag::Accent.use_jisx0213 = nil
19
30
  end
20
31
  end
@@ -0,0 +1,213 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+ require 'aozora2html'
4
+
5
+ class CommandParseTest < Test::Unit::TestCase
6
+ def setup
7
+ end
8
+
9
+ def test_parse_command1
10
+ src = "デボルド―※[#濁点付き片仮名ワ、1-7-82]ルモオル\r\n"
11
+ parsed = parse_text(src)
12
+ expected = "デボルド—<img src=\"../../../gaiji/1-07/1-07-82.png\" alt=\"※(濁点付き片仮名ワ、1-7-82)\" class=\"gaiji\" />ルモオル<br />\r\n"
13
+ assert_equal expected, parsed
14
+ end
15
+
16
+ def test_parse_command2
17
+ src = "繁雑な日本の 〔e'tiquette〕 も、\r\n"
18
+ parsed = parse_text(src)
19
+ expected = %Q|繁雑な日本の <img src="../../../gaiji/1-09/1-09-63.png" alt="※(アキュートアクセント付きE小文字)" class="gaiji" />tiquette も、<br />\r\n|
20
+ assert_equal expected, parsed
21
+ end
22
+
23
+ def test_parse_command3
24
+ src = "〔Sito^t qu'on le touche il re'sonne.〕\r\n"
25
+ parsed = parse_text(src)
26
+ expected = %Q|Sit<img src="../../../gaiji/1-09/1-09-74.png" alt="※(サーカムフレックスアクセント付きO小文字)" class="gaiji" />t q<img src="../../../gaiji/1-09/1-09-79.png" alt="※(アキュートアクセント付きU小文字)" class="gaiji" />on le touche il r<img src="../../../gaiji/1-09/1-09-63.png" alt="※(アキュートアクセント付きE小文字)" class="gaiji" />sonne.<br />\r\n|
27
+ assert_equal expected, parsed
28
+ end
29
+
30
+ def test_parse_command4
31
+ src = "presqu'〔i^le〕\r\n"
32
+ parsed = parse_text(src)
33
+ expected = %Q|presqu'<img src="../../../gaiji/1-09/1-09-68.png" alt="※(サーカムフレックスアクセント付きI小文字)" class="gaiji" />le<br />\r\n|
34
+ assert_equal expected, parsed
35
+ end
36
+
37
+ def test_parse_command5
38
+ src = "[二十歳の 〔E'tude〕]\r\n"
39
+ parsed = parse_text(src)
40
+ expected = %Q|[二十歳の <img src="../../../gaiji/1-09/1-09-32.png" alt="※(アキュートアクセント付きE)" class="gaiji" />tude]<br />\r\n|
41
+ assert_equal expected, parsed
42
+ end
43
+
44
+ def test_parse_command6
45
+ src = "責[#「責」に白ゴマ傍点]空文庫\r\n"
46
+ parsed = parse_text(src)
47
+ expected = %Q|<em class="white_sesame_dot">責</em>空文庫<br />\r\n|
48
+ assert_equal expected, parsed
49
+ end
50
+
51
+ def test_parse_command7
52
+ src = "[#丸傍点]青空文庫で読書しよう[#丸傍点終わり]。\r\n"
53
+ parsed = parse_text(src)
54
+ expected = %Q|<em class="black_circle">青空文庫で読書しよう</em>。<br />\r\n|
55
+ assert_equal expected, parsed
56
+ end
57
+
58
+ def test_parse_command8
59
+ src = "この形は傍線[#「傍線」に傍線]と書いてください。\r\n"
60
+ parsed = parse_text(src)
61
+ expected = %Q|この形は<em class="underline_solid">傍線</em>と書いてください。<br />\r\n|
62
+ assert_equal expected, parsed
63
+ end
64
+
65
+ def test_parse_command9
66
+ src = "[#左に鎖線]青空文庫で読書しよう[#左に鎖線終わり]。\r\n"
67
+ parsed = parse_text(src)
68
+ expected = %Q|<em class="overline_dotted">青空文庫で読書しよう</em>。<br />\r\n|
69
+ assert_equal expected, parsed
70
+ end
71
+
72
+ def test_parse_command10
73
+ src = "「クリス、宇宙航行委員会が選考[#「選考」は太字]するんだ。きみは志願できない。待つ[#「待つ」は太字]んだ」\r\n"
74
+ parsed = parse_text(src)
75
+ expected = %Q|「クリス、宇宙航行委員会が<span class="futoji">選考</span>するんだ。きみは志願できない。<span class="futoji">待つ</span>んだ」<br />\r\n|
76
+ assert_equal expected, parsed
77
+ end
78
+
79
+ def test_parse_command11
80
+ src = "Which, teaching us, hath this exordium: Nothing from nothing ever yet was born.[#「Nothing from nothing ever yet was born.」は斜体]\r\n"
81
+ parsed = parse_text(src)
82
+ expected = %Q|Which, teaching us, hath this exordium: <span class="shatai">Nothing from nothing ever yet was born.</span><br />\r\n|
83
+ assert_equal expected, parsed
84
+ end
85
+
86
+ def test_parse_command_warichu
87
+ src = "[#割り注]価は四百円であった。[#割り注終わり]\r\n"
88
+ parsed = parse_text(src)
89
+ expected = %Q|<span class="warichu">(価は四百円であった。)</span><br />\r\n|
90
+ assert_equal expected, parsed
91
+ end
92
+
93
+ def test_parse_command_warichu2
94
+ src = "飽海郡南平田村大字飛鳥[#割り注]東は字大林四三七[#改行]西は字神内一一一ノ一[#割り注終わり]\r\n"
95
+ parsed = parse_text(src)
96
+ expected = %Q|飽海郡南平田村大字飛鳥<span class="warichu">(東は字大林四三七<span class="notes">[#改行]</span>西は字神内一一一ノ一)</span><br />\r\n|
97
+ assert_equal expected, parsed
98
+ end
99
+
100
+ def test_parse_command_unicode
101
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
102
+ src = "※[#「衄のへん+卩」、U+5379、287-2]\r\n"
103
+ parsed = parse_text(src)
104
+ expected = "&#x5379;<br />\r\n"
105
+ assert_equal expected, parsed
106
+ end
107
+
108
+ def test_parse_command_teisei1
109
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
110
+ src = "吹喋[#「喋」に「ママ」の注記]\r\n"
111
+ parsed = parse_text(src)
112
+ expected = "吹<ruby><rb>喋</rb><rp>(</rp><rt>ママ</rt><rp>)</rp></ruby><br />\r\n"
113
+ assert_equal expected, parsed
114
+ end
115
+
116
+ def test_parse_command_teisei2
117
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
118
+ src = "紋附だとか[#「紋附だとか」は底本では「絞附だとか」]\r\n"
119
+ parsed = parse_text(src)
120
+ expected = %Q|紋附だとか<span class="notes">[#「紋附だとか」は底本では「絞附だとか」]</span><br />\r\n|
121
+ assert_equal expected, parsed
122
+ end
123
+
124
+ def test_parse_command_teisei3
125
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
126
+ src = "私は籠《ざる》[#ルビの「ざる」は底本では「さる」]をさげ\r\n"
127
+ parsed = parse_text(src)
128
+ expected = %Q|私は<ruby><rb>籠</rb><rp>(</rp><rt>ざる</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ざる」は底本では「さる」]</span>をさげ<br />\r\n|
129
+ assert_equal expected, parsed
130
+ end
131
+
132
+ def test_parse_command_teisei4
133
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
134
+ src = "広場へに[#「広場へに」はママ]店でもだそう。\r\n"
135
+ parsed = parse_text(src)
136
+ expected = %Q|広場へに<span class="notes">[#「広場へに」はママ]</span>店でもだそう。<br />\r\n|
137
+ assert_equal expected, parsed
138
+ end
139
+
140
+ def test_parse_command_teisei5
141
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
142
+ src = "お湯《ゆう》[#ルビの「ゆう」はママ]\r\n"
143
+ parsed = parse_text(src)
144
+ expected = %Q|お<ruby><rb>湯</rb><rp>(</rp><rt>ゆう</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ゆう」はママ]</span><br />\r\n|
145
+ assert_equal expected, parsed
146
+ end
147
+
148
+ def test_parse_command_tcy
149
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
150
+ src = "米機B29[#「29」は縦中横]の編隊は、\r\n"
151
+ parsed = parse_text(src)
152
+ expected = %Q|米機B<span dir="ltr">29</span>の編隊は、<br />\r\n|
153
+ assert_equal expected, parsed
154
+ end
155
+
156
+ def test_parse_command_tcy2
157
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
158
+ src = "[#縦中横](※[#ローマ数字1、1-13-21])[#縦中横終わり]\r\n"
159
+ parsed = parse_text(src)
160
+ expected = %Q|<span dir="ltr">(<img src="../../../gaiji/1-13/1-13-21.png" alt="※(ローマ数字1、1-13-21)" class="gaiji" />)</span><br />\r\n|
161
+ assert_equal expected, parsed
162
+ end
163
+
164
+ def test_parse_command_kogaki
165
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
166
+ src = "それ以上である。(5)[#「(5)」は行右小書き]\r\n"
167
+ parsed = parse_text(src)
168
+ expected = %Q|それ以上である。<sup class="superscript">(5)</sup><br />\r\n|
169
+ assert_equal expected, parsed
170
+ end
171
+
172
+ def test_parse_command_uetsuki
173
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
174
+ src = "22[#「2」は上付き小文字]\r\n"
175
+ parsed = parse_text(src)
176
+ expected = %Q|2<sup class="superscript">2</sup><br />\r\n|
177
+ assert_equal expected, parsed
178
+ end
179
+
180
+ def test_parse_command_bouki
181
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
182
+ src = "支部長の顔にさっと血が流れ[#「血が流れ」に「×」の傍記]た\r\n"
183
+ parsed = parse_text(src)
184
+ expected = %Q|支部長の顔にさっと<ruby><rb>血が流れ</rb><rp>(</rp><rt>×&nbsp;×&nbsp;×&nbsp;×</rt><rp>)</rp></ruby>た<br />\r\n|
185
+ assert_equal expected, parsed
186
+ end
187
+
188
+ def test_parse_command_ruby
189
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
190
+ src = "グリーンランドの中央部八千尺の氷河地帯にあるといわれる、[#横組み]“Ser-mik-Suah《セルミク・シュアー》”[#横組み終わり]の冥路《よみじ》の国。\r\n"
191
+ parsed = parse_text(src)
192
+ expected = %Q|グリーンランドの中央部八千尺の氷河地帯にあるといわれる、<span class=\"yokogumi\">“<ruby><rb>Ser-mik-Suah</rb><rp>(</rp><rt>セルミク・シュアー</rt><rp>)</rp></ruby>”</span>の<ruby><rb>冥路</rb><rp>(</rp><rt>よみじ</rt><rp>)</rp></ruby>の国。<br />\r\n|
193
+ assert_equal expected, parsed
194
+ end
195
+
196
+ def parse_text(input_text)
197
+ input = StringIO.new(input_text.encode("shift_jis"))
198
+ output = StringIO.new
199
+ parser = Aozora2Html.new(input, output)
200
+ parser.instance_eval{@section=:tail}
201
+ catch(:terminate) do
202
+ loop do
203
+ parser.parse
204
+ end
205
+ end
206
+ output.string.encode("utf-8")
207
+ end
208
+
209
+ def teardown
210
+ Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = false
211
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = false
212
+ end
213
+ end