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
@@ -0,0 +1,216 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'aozora2html'
5
+
6
+ class CommandParseTest < Test::Unit::TestCase
7
+ def setup
8
+ end
9
+
10
+ def test_parse_command1
11
+ src = "デボルド―※[#濁点付き片仮名ワ、1-7-82]ルモオル\r\n"
12
+ parsed = parse_text(src)
13
+ expected = "デボルド—<img src=\"../../../gaiji/1-07/1-07-82.png\" alt=\"※(濁点付き片仮名ワ、1-7-82)\" class=\"gaiji\" />ルモオル<br />\r\n"
14
+ assert_equal expected, parsed
15
+ end
16
+
17
+ def test_parse_command2
18
+ src = "繁雑な日本の 〔e'tiquette〕 も、\r\n"
19
+ parsed = parse_text(src)
20
+ expected = %Q|繁雑な日本の <img src="../../../gaiji/1-09/1-09-63.png" alt="※(アキュートアクセント付きE小文字)" class="gaiji" />tiquette も、<br />\r\n|
21
+ assert_equal expected, parsed
22
+ end
23
+
24
+ def test_parse_command3
25
+ src = "〔Sito^t qu'on le touche il re'sonne.〕\r\n"
26
+ parsed = parse_text(src)
27
+ 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|
28
+ assert_equal expected, parsed
29
+ end
30
+
31
+ def test_parse_command4
32
+ src = "presqu'〔i^le〕\r\n"
33
+ parsed = parse_text(src)
34
+ expected = %Q|presqu'<img src="../../../gaiji/1-09/1-09-68.png" alt="※(サーカムフレックスアクセント付きI小文字)" class="gaiji" />le<br />\r\n|
35
+ assert_equal expected, parsed
36
+ end
37
+
38
+ def test_parse_command5
39
+ src = "[二十歳の 〔E'tude〕]\r\n"
40
+ parsed = parse_text(src)
41
+ expected = %Q|[二十歳の <img src="../../../gaiji/1-09/1-09-32.png" alt="※(アキュートアクセント付きE)" class="gaiji" />tude]<br />\r\n|
42
+ assert_equal expected, parsed
43
+ end
44
+
45
+ def test_parse_command6
46
+ src = "責[#「責」に白ゴマ傍点]空文庫\r\n"
47
+ parsed = parse_text(src)
48
+ expected = %Q(<em class="white_sesame_dot">責</em>空文庫<br />\r\n)
49
+ assert_equal expected, parsed
50
+ end
51
+
52
+ def test_parse_command7
53
+ src = "[#丸傍点]青空文庫で読書しよう[#丸傍点終わり]。\r\n"
54
+ parsed = parse_text(src)
55
+ expected = %Q(<em class="black_circle">青空文庫で読書しよう</em>。<br />\r\n)
56
+ assert_equal expected, parsed
57
+ end
58
+
59
+ def test_parse_command8
60
+ src = "この形は傍線[#「傍線」に傍線]と書いてください。\r\n"
61
+ parsed = parse_text(src)
62
+ expected = %Q(この形は<em class="underline_solid">傍線</em>と書いてください。<br />\r\n)
63
+ assert_equal expected, parsed
64
+ end
65
+
66
+ def test_parse_command9
67
+ src = "[#左に鎖線]青空文庫で読書しよう[#左に鎖線終わり]。\r\n"
68
+ parsed = parse_text(src)
69
+ expected = %Q(<em class="overline_dotted">青空文庫で読書しよう</em>。<br />\r\n)
70
+ assert_equal expected, parsed
71
+ end
72
+
73
+ def test_parse_command10
74
+ src = "「クリス、宇宙航行委員会が選考[#「選考」は太字]するんだ。きみは志願できない。待つ[#「待つ」は太字]んだ」\r\n"
75
+ parsed = parse_text(src)
76
+ expected = %Q(「クリス、宇宙航行委員会が<span class="futoji">選考</span>するんだ。きみは志願できない。<span class="futoji">待つ</span>んだ」<br />\r\n)
77
+ assert_equal expected, parsed
78
+ end
79
+
80
+ def test_parse_command11
81
+ src = "Which, teaching us, hath this exordium: Nothing from nothing ever yet was born.[#「Nothing from nothing ever yet was born.」は斜体]\r\n"
82
+ parsed = parse_text(src)
83
+ expected = %Q(Which, teaching us, hath this exordium: <span class="shatai">Nothing from nothing ever yet was born.</span><br />\r\n)
84
+ assert_equal expected, parsed
85
+ end
86
+
87
+ def test_parse_command_warichu
88
+ src = "[#割り注]価は四百円であった。[#割り注終わり]\r\n"
89
+ parsed = parse_text(src)
90
+ expected = %Q(<span class="warichu">(価は四百円であった。)</span><br />\r\n)
91
+ assert_equal expected, parsed
92
+ end
93
+
94
+ def test_parse_command_warichu2
95
+ src = "飽海郡南平田村大字飛鳥[#割り注]東は字大林四三七[#改行]西は字神内一一一ノ一[#割り注終わり]\r\n"
96
+ parsed = parse_text(src)
97
+ expected = %Q(飽海郡南平田村大字飛鳥<span class="warichu">(東は字大林四三七<span class="notes">[#改行]</span>西は字神内一一一ノ一)</span><br />\r\n)
98
+ assert_equal expected, parsed
99
+ end
100
+
101
+ def test_parse_command_unicode
102
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
103
+ src = "※[#「衄のへん+卩」、U+5379、287-2]\r\n"
104
+ parsed = parse_text(src)
105
+ expected = "&#x5379;<br />\r\n"
106
+ assert_equal expected, parsed
107
+ end
108
+
109
+ def test_parse_command_teisei1
110
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
111
+ src = "吹喋[#「喋」に「ママ」の注記]\r\n"
112
+ parsed = parse_text(src)
113
+ expected = "吹<ruby><rb>喋</rb><rp>(</rp><rt>ママ</rt><rp>)</rp></ruby><br />\r\n"
114
+ assert_equal expected, parsed
115
+ end
116
+
117
+ def test_parse_command_teisei2
118
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
119
+ src = "紋附だとか[#「紋附だとか」は底本では「絞附だとか」]\r\n"
120
+ parsed = parse_text(src)
121
+ expected = %Q(紋附だとか<span class="notes">[#「紋附だとか」は底本では「絞附だとか」]</span><br />\r\n)
122
+ assert_equal expected, parsed
123
+ end
124
+
125
+ def test_parse_command_teisei3
126
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
127
+ src = "私は籠《ざる》[#ルビの「ざる」は底本では「さる」]をさげ\r\n"
128
+ parsed = parse_text(src)
129
+ expected = %Q(私は<ruby><rb>籠</rb><rp>(</rp><rt>ざる</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ざる」は底本では「さる」]</span>をさげ<br />\r\n)
130
+ assert_equal expected, parsed
131
+ end
132
+
133
+ def test_parse_command_teisei4
134
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
135
+ src = "広場へに[#「広場へに」はママ]店でもだそう。\r\n"
136
+ parsed = parse_text(src)
137
+ expected = %Q(広場へに<span class="notes">[#「広場へに」はママ]</span>店でもだそう。<br />\r\n)
138
+ assert_equal expected, parsed
139
+ end
140
+
141
+ def test_parse_command_teisei5
142
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
143
+ src = "お湯《ゆう》[#ルビの「ゆう」はママ]\r\n"
144
+ parsed = parse_text(src)
145
+ expected = %Q(お<ruby><rb>湯</rb><rp>(</rp><rt>ゆう</rt><rp>)</rp></ruby><span class="notes">[#ルビの「ゆう」はママ]</span><br />\r\n)
146
+ assert_equal expected, parsed
147
+ end
148
+
149
+ def test_parse_command_tcy
150
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
151
+ src = "米機B29[#「29」は縦中横]の編隊は、\r\n"
152
+ parsed = parse_text(src)
153
+ expected = %Q(米機B<span dir="ltr">29</span>の編隊は、<br />\r\n)
154
+ assert_equal expected, parsed
155
+ end
156
+
157
+ def test_parse_command_tcy2
158
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
159
+ src = "[#縦中横](※[#ローマ数字1、1-13-21])[#縦中横終わり]\r\n"
160
+ parsed = parse_text(src)
161
+ 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|
162
+ assert_equal expected, parsed
163
+ end
164
+
165
+ def test_parse_command_kogaki
166
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
167
+ src = "それ以上である。(5)[#「(5)」は行右小書き]\r\n"
168
+ parsed = parse_text(src)
169
+ expected = %Q(それ以上である。<sup class="superscript">(5)</sup><br />\r\n)
170
+ assert_equal expected, parsed
171
+ end
172
+
173
+ def test_parse_command_uetsuki
174
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
175
+ src = "22[#「2」は上付き小文字]\r\n"
176
+ parsed = parse_text(src)
177
+ expected = %Q(2<sup class="superscript">2</sup><br />\r\n)
178
+ assert_equal expected, parsed
179
+ end
180
+
181
+ def test_parse_command_bouki
182
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
183
+ src = "支部長の顔にさっと血が流れ[#「血が流れ」に「×」の傍記]た\r\n"
184
+ parsed = parse_text(src)
185
+ expected = %Q(支部長の顔にさっと<ruby><rb>血が流れ</rb><rp>(</rp><rt>×&nbsp;×&nbsp;×&nbsp;×</rt><rp>)</rp></ruby>た<br />\r\n)
186
+ assert_equal expected, parsed
187
+ end
188
+
189
+ def test_parse_command_ruby
190
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
191
+ src = "グリーンランドの中央部八千尺の氷河地帯にあるといわれる、[#横組み]“Ser-mik-Suah《セルミク・シュアー》”[#横組み終わり]の冥路《よみじ》の国。\r\n"
192
+ parsed = parse_text(src)
193
+ 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)
194
+ assert_equal expected, parsed
195
+ end
196
+
197
+ using Aozora2Html::StringRefinements
198
+
199
+ def parse_text(input_text)
200
+ input = StringIO.new(input_text.to_sjis)
201
+ output = StringIO.new
202
+ parser = Aozora2Html.new(input, output)
203
+ parser.instance_eval { @section = :tail }
204
+ catch(:terminate) do
205
+ loop do
206
+ parser.__send__(:parse)
207
+ end
208
+ end
209
+ output.string.to_utf8
210
+ end
211
+
212
+ def teardown
213
+ Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = false
214
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = false
215
+ end
216
+ end
data/test/test_compat.rb CHANGED
@@ -1,11 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'aozora2html'
3
5
 
4
6
  class CompatTest < Test::Unit::TestCase
5
7
  def test_array_to_s
6
- assert_equal "abc", ["a", "b", "c"].join
8
+ assert_equal 'abc', ['a', 'b', 'c'].join
7
9
  end
8
10
  end
9
-
10
-
11
-
@@ -1,26 +1,25 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
 
5
6
  class DakutenKatakanaTagTest < Test::Unit::TestCase
6
7
  def setup
7
- @orig_gaiji_dir = $gaiji_dir
8
- $gaiji_dir = "g_dir"
9
- stub(@parser).block_allowed_context?{true}
8
+ @parser = Object.new
9
+ @gaiji_dir = 'g_dir'
10
+ stub(@parser).block_allowed_context? { true }
10
11
  end
11
12
 
13
+ using Aozora2Html::StringRefinements
14
+
12
15
  def test_dakuten_katakana_new
13
- tag = Dakuten_katakana_tag.new(@parser,1,"".encode("shift_jis"))
14
- assert_equal Dakuten_katakana_tag, tag.class
15
- assert_equal true, tag.kind_of?(Inline_tag)
16
+ tag = Aozora2Html::Tag::DakutenKatakana.new(@parser, 1, ''.to_sjis, gaiji_dir: @gaiji_dir)
17
+ assert_equal Aozora2Html::Tag::DakutenKatakana, tag.class
18
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Inline)
16
19
  end
17
20
 
18
21
  def test_to_s
19
- tag = Dakuten_katakana_tag.new(@parser,1,"".encode("shift_jis"))
20
- assert_equal "<img src=\"g_dir/1-07/1-07-81.png\" alt=\"※(濁点付き片仮名「ア」、1-07-81)\" class=\"gaiji\" />", tag.to_s.encode("utf-8")
21
- end
22
-
23
- def teardown
24
- $gaiji_dir = @orig_gaiji_dir
22
+ tag = Aozora2Html::Tag::DakutenKatakana.new(@parser, 1, ''.to_sjis, gaiji_dir: @gaiji_dir)
23
+ assert_equal '<img src="g_dir/1-07/1-07-81.png" alt="※(濁点付き片仮名「ア」、1-07-81)" class="gaiji" />', tag.to_s.to_utf8
25
24
  end
26
25
  end
@@ -1,21 +1,25 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
 
5
6
  class DecorateTagTest < Test::Unit::TestCase
6
7
  def setup
7
- stub(@parser).block_allowed_context?{true}
8
+ @parser = Object.new
9
+ stub(@parser).block_allowed_context? { true }
8
10
  end
9
11
 
10
12
  def test_decorate_new
11
- tag = Decorate_tag.new(@parser,"aa",1,:dai)
12
- assert_equal Decorate_tag, tag.class
13
- assert_equal true, tag.kind_of?(Inline_tag)
13
+ tag = Aozora2Html::Tag::Decorate.new(@parser, 'aa', 1, :dai)
14
+ assert_equal Aozora2Html::Tag::Decorate, tag.class
15
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Inline)
14
16
  end
15
17
 
18
+ using Aozora2Html::StringRefinements
19
+
16
20
  def test_to_s
17
- tag = Decorate_tag.new(@parser,"テスト".encode("shift_jis"),"foo","span")
18
- assert_equal "<span class=\"foo\">テスト</span>", tag.to_s.encode("utf-8")
21
+ tag = Aozora2Html::Tag::Decorate.new(@parser, 'テスト'.to_sjis, 'foo', 'span')
22
+ assert_equal '<span class="foo">テスト</span>', tag.to_s.to_utf8
19
23
  end
20
24
 
21
25
  def teardown
data/test/test_dir_tag.rb CHANGED
@@ -1,21 +1,25 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
 
5
6
  class DirTagTest < Test::Unit::TestCase
6
7
  def setup
7
- stub(@parser).block_allowed_context?{true}
8
+ @parser = Object.new
9
+ stub(@parser).block_allowed_context? { true }
8
10
  end
9
11
 
12
+ using Aozora2Html::StringRefinements
13
+
10
14
  def test_dir_new
11
- tag = Dir_tag.new(@parser,"テスト".encode("shift_jis"))
12
- assert_equal Dir_tag, tag.class
13
- assert_equal true, tag.kind_of?(Inline_tag)
15
+ tag = Aozora2Html::Tag::Dir.new(@parser, 'テスト'.to_sjis)
16
+ assert_equal Aozora2Html::Tag::Dir, tag.class
17
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Inline)
14
18
  end
15
19
 
16
20
  def test_to_s
17
- tag = Dir_tag.new(@parser,"テスト".encode("shift_jis"))
18
- assert_equal "<span dir=\"ltr\">テスト</span>", tag.to_s.encode("utf-8")
21
+ tag = Aozora2Html::Tag::Dir.new(@parser, 'テスト'.to_sjis)
22
+ assert_equal '<span dir="ltr">テスト</span>', tag.to_s.to_utf8
19
23
  end
20
24
 
21
25
  def teardown
@@ -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
 
@@ -6,15 +7,17 @@ class EditorNoteTagTest < Test::Unit::TestCase
6
7
  def setup
7
8
  end
8
9
 
10
+ using Aozora2Html::StringRefinements
11
+
9
12
  def test_editor_note_new
10
- tag = Editor_note_tag.new(nil,"注記のテスト".encode("shift_jis"))
11
- assert_equal Editor_note_tag, tag.class
12
- assert_equal true, tag.kind_of?(Inline_tag)
13
+ tag = Aozora2Html::Tag::EditorNote.new(nil, '注記のテスト'.to_sjis)
14
+ assert_equal Aozora2Html::Tag::EditorNote, tag.class
15
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Inline)
13
16
  end
14
17
 
15
18
  def test_to_s
16
- tag = Editor_note_tag.new(nil,"注記のテスト".encode("shift_jis"))
17
- assert_equal "<span class=\"notes\">[#注記のテスト]</span>", tag.to_s.encode("utf-8")
19
+ tag = Aozora2Html::Tag::EditorNote.new(nil, '注記のテスト'.to_sjis)
20
+ assert_equal '<span class="notes">[#注記のテスト]</span>', tag.to_s.to_utf8
18
21
  end
19
22
 
20
23
  def teardown
@@ -1,23 +1,25 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
 
5
6
  class ExceptionTest < Test::Unit::TestCase
6
7
  def test_raise_aozora_exception
7
- assert_raises(Aozora_Exception) do
8
- raise Aozora_Exception, "error!"
8
+ assert_raises(Aozora2Html::Error) do
9
+ raise Aozora2Html::Error, 'error!'
9
10
  end
10
11
  end
11
12
 
13
+ using Aozora2Html::StringRefinements
14
+
12
15
  def test_raise_aozora_error
13
- error_msg = ""
16
+ error_msg = ''
14
17
  begin
15
- raise Aozora_Error, "sample error"
16
- rescue Aozora_Error => msg
17
- error_msg = msg.message(123)
18
+ raise Aozora2Html::Error, 'sample error'
19
+ rescue Aozora2Html::Error => e
20
+ error_msg = e.message(123)
18
21
  end
19
22
  assert_equal "エラー(123行目):sample error. \r\n処理を停止します",
20
- error_msg.encode("utf-8")
21
-
23
+ error_msg.to_utf8
22
24
  end
23
25
  end
@@ -1,31 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'aozora2html'
3
5
 
4
6
  class FontSizeTagTest < Test::Unit::TestCase
5
7
  def setup
6
- stub(@parser).block_allowed_context?{true}
8
+ @parser = Object.new
9
+ stub(@parser).block_allowed_context? { true }
7
10
  end
8
11
 
9
12
  def test_font_size_new
10
- tag = Font_size_tag.new(@parser,1,:dai)
11
- assert_equal Font_size_tag, tag.class
12
- assert_equal true, tag.kind_of?(Block_tag)
13
- assert_equal true, tag.kind_of?(Multiline_tag)
13
+ tag = Aozora2Html::Tag::FontSize.new(@parser, 1, :dai)
14
+ assert_equal Aozora2Html::Tag::FontSize, tag.class
15
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Block)
16
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Multiline)
14
17
  end
15
18
 
19
+ using Aozora2Html::StringRefinements
20
+
16
21
  def test_to_s
17
- tag = Font_size_tag.new(@parser,1,:dai)
18
- assert_equal "<div class=\"dai1\" style=\"font-size: large;\">", tag.to_s.encode("utf-8")
22
+ tag = Aozora2Html::Tag::FontSize.new(@parser, 1, :dai)
23
+ assert_equal '<div class="dai1" style="font-size: large;">', tag.to_s.to_utf8
19
24
  end
20
25
 
21
26
  def test_to_s2
22
- tag = Font_size_tag.new(@parser,2,:dai)
23
- assert_equal "<div class=\"dai2\" style=\"font-size: x-large;\">", tag.to_s.encode("utf-8")
27
+ tag = Aozora2Html::Tag::FontSize.new(@parser, 2, :dai)
28
+ assert_equal '<div class="dai2" style="font-size: x-large;">', tag.to_s.to_utf8
24
29
  end
25
30
 
26
31
  def test_to_s3
27
- tag = Font_size_tag.new(@parser,3,:sho)
28
- assert_equal "<div class=\"sho3\" style=\"font-size: xx-small;\">", tag.to_s.encode("utf-8")
32
+ tag = Aozora2Html::Tag::FontSize.new(@parser, 3, :sho)
33
+ assert_equal '<div class="sho3" style="font-size: xx-small;">', tag.to_s.to_utf8
34
+ end
35
+
36
+ def test_to_s0
37
+ assert_raise(Aozora2Html::Error.new('文字サイズの指定が不正です'.to_sjis)) do
38
+ _tag = Aozora2Html::Tag::FontSize.new(@parser, 0, :sho)
39
+ end
29
40
  end
30
41
 
31
42
  def teardown
@@ -1,42 +1,50 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
  require 'aozora2html'
4
5
 
5
6
  class EmbedGaijiTagTest < Test::Unit::TestCase
6
7
  def setup
7
- @orig_gaiji_dir = $gaiji_dir
8
- $gaiji_dir = "g_dir/"
8
+ @gaiji_dir = 'g_dir/'
9
9
  end
10
10
 
11
+ using Aozora2Html::StringRefinements
12
+
11
13
  def test_gaiji_new
12
- egt = Embed_Gaiji_tag.new(nil,"foo","1-2-3","name")
13
- assert_equal "<img src=\"g_dir/foo/1-2-3.png\" alt=\"※(name)\" class=\"gaiji\" />", egt.to_s.encode("utf-8")
14
+ egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-2-3', 'name', gaiji_dir: @gaiji_dir)
15
+ assert_equal '<img src="g_dir/foo/1-2-3.png" alt="※(name)" class="gaiji" />', egt.to_s.to_utf8
14
16
  end
15
17
 
16
18
  def test_unembed_gaiji_new
17
- egt = UnEmbed_Gaiji_tag.new(nil,"テストtest".encode("Shift_JIS"))
18
- assert_equal "<span class=\"notes\">[テストtest]</span>", egt.to_s.encode("utf-8")
19
+ egt = Aozora2Html::Tag::UnEmbedGaiji.new(nil, 'テストtest'.to_sjis)
20
+ assert_equal '<span class="notes">[テストtest]</span>', egt.to_s.to_utf8
19
21
  end
20
22
 
21
23
  def test_espcaed?
22
- egt = UnEmbed_Gaiji_tag.new(nil,"テストtest".encode("Shift_JIS"))
24
+ egt = Aozora2Html::Tag::UnEmbedGaiji.new(nil, 'テストtest'.to_sjis)
23
25
  assert_equal false, egt.escaped?
24
26
  end
25
27
 
26
28
  def test_espcae!
27
- egt = UnEmbed_Gaiji_tag.new(nil,"テストtest".encode("Shift_JIS"))
29
+ egt = Aozora2Html::Tag::UnEmbedGaiji.new(nil, 'テストtest'.to_sjis)
28
30
  egt.escape!
29
31
  assert_equal true, egt.escaped?
30
32
  end
31
33
 
32
34
  def test_jisx0213
33
- Embed_Gaiji_tag.use_jisx0213 = true
34
- egt = Embed_Gaiji_tag.new(nil,"foo","1-06-75","snowman")
35
- assert_equal "&#x2603;", egt.to_s.encode("utf-8")
35
+ Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = true
36
+ egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-06-75', 'snowman', gaiji_dir: @gaiji_dir)
37
+ assert_equal '&#x2603;', egt.to_s.to_utf8
38
+ end
39
+
40
+ def test_use_unicode
41
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = true
42
+ egt = Aozora2Html::Tag::EmbedGaiji.new(nil, 'foo', '1-06-75', 'snowman', '2603', gaiji_dir: @gaiji_dir)
43
+ assert_equal '&#x2603;', egt.to_s.to_utf8
36
44
  end
37
45
 
38
46
  def teardown
39
- Embed_Gaiji_tag.use_jisx0213 = false
40
- $gaiji_dir = @orig_gaiji_dir
47
+ Aozora2Html::Tag::EmbedGaiji.use_jisx0213 = false
48
+ Aozora2Html::Tag::EmbedGaiji.use_unicode = false
41
49
  end
42
50
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'aozora2html'
5
+
6
+ class HeaderTest < Test::Unit::TestCase
7
+ def setup
8
+ @header = Aozora2Html::Header.new(css_files: Array['../../aozora.css'])
9
+ end
10
+
11
+ using Aozora2Html::StringRefinements
12
+
13
+ def test_header_to_html
14
+ @header.push('武装せる市街'.to_sjis)
15
+ @header.push('黒島伝治'.to_sjis)
16
+ actual = @header.to_html.to_utf8
17
+
18
+ # rubocop:disable Layout/LineLength
19
+ expected =
20
+ "<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\r\n \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" >\r\n<head>\r\n\t<meta http-equiv=\"Content-Type\" content=\"text/html;charset=Shift_JIS\" />\r\n\t<meta http-equiv=\"content-style-type\" content=\"text/css\" />\r\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../aozora.css\" />\r\n\t<title>黒島伝治 武装せる市街</title>\r\n\t<script type=\"text/javascript\" src=\"../../jquery-1.4.2.min.js\"></script>\r\n <link rel=\"Schema.DC\" href=\"http://purl.org/dc/elements/1.1/\" />\r\n\t<meta name=\"DC.Title\" content=\"武装せる市街\" />\r\n\t<meta name=\"DC.Creator\" content=\"黒島伝治\" />\r\n\t<meta name=\"DC.Publisher\" content=\"青空文庫\" />\r\n</head>\r\n<body>\r\n<div class=\"metadata\">\r\n<h1 class=\"title\">武装せる市街</h1>\r\n<h2 class=\"author\">黒島伝治</h2>\r\n<br />\r\n<br />\r\n</div>\r\n<div id=\"contents\" style=\"display:none\"></div><div class=\"main_text\">"
21
+ # rubocop:enable Layout/LineLength
22
+
23
+ assert_equal(expected, actual)
24
+ end
25
+
26
+ def test_build_title
27
+ @header.push('武装せる市街'.to_sjis)
28
+ @header.push('黒島伝治'.to_sjis)
29
+ header_info = @header.build_header_info
30
+ actual = @header.build_title(header_info).to_utf8
31
+ expected = '<title>黒島伝治 武装せる市街</title>'
32
+ assert_equal(expected, actual)
33
+ end
34
+
35
+ def test_build_title2
36
+ @header.push('スリーピー・ホローの伝説'.to_sjis)
37
+ @header.push('故ディードリッヒ・ニッカボッカーの遺稿より'.to_sjis)
38
+ @header.push('ワシントン・アーヴィング Washington Irving'.to_sjis)
39
+ @header.push('吉田甲子太郎訳'.to_sjis)
40
+ header_info = @header.build_header_info
41
+ actual = @header.build_title(header_info).to_utf8
42
+ expected = '<title>ワシントン・アーヴィング Washington Irving 吉田甲子太郎訳 スリーピー・ホローの伝説 故ディードリッヒ・ニッカボッカーの遺稿より</title>'
43
+ assert_equal(expected, actual)
44
+ end
45
+ end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib/')
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib/")
2
4
  require 'test/unit'
3
5
  require 'test/unit/rr'
4
6
  require 'tmpdir'
data/test/test_i18n.rb ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'aozora2html'
5
+
6
+ class I18nTest < Test::Unit::TestCase
7
+ using Aozora2Html::StringRefinements
8
+
9
+ def test_t
10
+ assert_equal '警告(123行目):JIS外字「①」が使われています',
11
+ Aozora2Html::I18n.t(:warn_jis_gaiji,
12
+ 123,
13
+ '①'.encode('cp932').force_encoding('shift_jis'))
14
+ .force_encoding('cp932').to_utf8
15
+ end
16
+
17
+ def test_error_utf8
18
+ orig_value = Aozora2Html::I18n.use_utf8
19
+ Aozora2Html::I18n.use_utf8 = true
20
+ begin
21
+ assert_equal '警告(123行目):JIS外字「①」が使われています',
22
+ Aozora2Html::I18n.t(:warn_jis_gaiji,
23
+ 123,
24
+ '①'.encode('cp932').force_encoding('shift_jis'))
25
+ ensure
26
+ Aozora2Html::I18n.use_utf8 = orig_value
27
+ end
28
+ end
29
+
30
+ def test_ruby_puts_behavior
31
+ $stdout = StringIO.new
32
+ begin
33
+ puts '①'.encode('cp932').force_encoding('shift_jis')
34
+ assert_equal "①\n", $stdout.string.force_encoding('cp932').to_utf8
35
+ ensure
36
+ $stdout = STDOUT
37
+ end
38
+ end
39
+ end
data/test/test_img_tag.rb CHANGED
@@ -1,20 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'aozora2html'
3
5
 
4
6
  class ImgTagTest < Test::Unit::TestCase
5
7
  def setup
6
- stub(@parser).block_allowed_context?{true}
8
+ @parser = Object.new
9
+ stub(@parser).block_allowed_context? { true }
7
10
  end
8
11
 
9
12
  def test_img_new
10
- tag = Img_tag.new(@parser,"foo.png","img1","alt img1",40,50)
11
- assert_equal Img_tag, tag.class
12
- assert_equal true, tag.kind_of?(Inline_tag)
13
+ tag = Aozora2Html::Tag::Img.new(@parser, 'foo.png', 'img1', 'alt img1', 40, 50)
14
+ assert_equal Aozora2Html::Tag::Img, tag.class
15
+ assert_equal true, tag.is_a?(Aozora2Html::Tag::Inline)
13
16
  end
14
17
 
18
+ using Aozora2Html::StringRefinements
19
+
15
20
  def test_to_s
16
- tag = Img_tag.new(@parser,"foo.png","img1","alt img1",40,50)
17
- assert_equal "<img class=\"img1\" width=\"40\" height=\"50\" src=\"foo.png\" alt=\"alt img1\" />", tag.to_s.encode("utf-8")
21
+ tag = Aozora2Html::Tag::Img.new(@parser, 'foo.png', 'img1', 'alt img1', 40, 50)
22
+ assert_equal '<img class="img1" width="40" height="50" src="foo.png" alt="alt img1" />', tag.to_s.to_utf8
18
23
  end
19
24
 
20
25
  def teardown