@@ -163,15 +163,36 @@ class AtCoderService {
163
163
break ;
164
164
}
165
165
166
+ // divやsectionでサンプル系の要素があればそこで終了
167
+ if (currentElement.localName == 'div' || currentElement.localName == 'section' ) {
168
+ // クラス名やidに「sample」や「example」が含まれていればセクション終了
169
+ final className = currentElement.className.toLowerCase ();
170
+ final id = currentElement.id.toLowerCase ();
171
+ if (className.contains ('sample' ) || className.contains ('example' ) ||
172
+ id.contains ('sample' ) || id.contains ('example' )) {
173
+ break ;
174
+ }
175
+ }
176
+
166
177
// preタグの場合は特別処理(入力形式のフォーマットなど)
167
178
if (currentElement.localName == 'pre' ) {
168
179
// preタグの内容をログ出力する
169
180
developer.log ("preタグの内容: ${currentElement .text }" );
170
181
171
- // preタグの内容をそのまま追加
172
- contentBuffer.write ('\n\n ```\n ' );
173
- contentBuffer.write (currentElement.text.trim ());
174
- contentBuffer.write ('\n ```\n\n ' );
182
+ // 入力セクションの場合はTeX形式で表示、それ以外はコードブロック
183
+ if (headingTexts.contains ('入力' ) || headingTexts.contains ('Input' )) {
184
+ // 入力形式の場合はTeX表示(コードブロック化しない)
185
+ // preタグ内のテキストも数式として処理
186
+ final processedText = _processTextWithMath (currentElement);
187
+ contentBuffer.write ('\n\n ' );
188
+ contentBuffer.write (processedText);
189
+ contentBuffer.write ('\n\n ' );
190
+ } else {
191
+ // その他(出力形式など)の場合はコードブロック
192
+ contentBuffer.write ('\n\n ```\n ' );
193
+ contentBuffer.write (currentElement.text.trim ());
194
+ contentBuffer.write ('\n ```\n\n ' );
195
+ }
175
196
} else {
176
197
// テキストノードの内容を追加し、数式を自動検出
177
198
final text = _processTextWithMath (currentElement);
@@ -184,9 +205,14 @@ class AtCoderService {
184
205
185
206
// 特定のセクションの追加処理(入力形式の場合)
186
207
String content = contentBuffer.toString ().trim ();
208
+
209
+ // 入力セクションの場合の追加処理とクリーンアップ
187
210
if (headingTexts.contains ('入力' ) || headingTexts.contains ('Input' )) {
211
+ // 不要な数値のみの行や入力例データを除去
212
+ content = _cleanInputSectionContent (content);
213
+
188
214
// 「入力は以下の形式で与えられる」のような文言だけで、実際のフォーマットがない場合
189
- if (! content.contains ('```' )) {
215
+ if (! content.contains ('```' ) && ! content. contains ( ' \$ ' ) ) {
190
216
developer.log ("入力形式の例がないため、preタグを直接探す" );
191
217
192
218
// 入力例から入力形式を推測
@@ -207,9 +233,11 @@ class AtCoderService {
207
233
208
234
if (examplePre != null ) {
209
235
developer.log ("入力例から形式を推測: ${examplePre .text }" );
210
- contentBuffer.write ('\n\n ```\n ' );
211
- contentBuffer.write (examplePre.text.trim ());
212
- contentBuffer.write ('\n ```\n\n ' );
236
+ // 入力形式はTeX表示(コードブロック化しない)
237
+ final processedText = _processTextWithMath (examplePre);
238
+ contentBuffer.write ('\n\n ' );
239
+ contentBuffer.write (processedText);
240
+ contentBuffer.write ('\n\n ' );
213
241
214
242
// 入力形式の説明も追加
215
243
contentBuffer.write ('\n\n 1行目は整数aが与えられる。' );
@@ -222,23 +250,74 @@ class AtCoderService {
222
250
final preText = pre.text.trim ();
223
251
if (preText.contains ('a' ) && preText.contains ('b c' ) && preText.contains ('s' )) {
224
252
developer.log ("入力形式のpreタグを直接発見: $preText " );
225
- contentBuffer.write ('\n\n ```\n ' );
226
- contentBuffer.write (preText);
227
- contentBuffer.write ('\n ```\n\n ' );
253
+ // 入力形式はTeX表示(コードブロック化しない)
254
+ final processedText = _processTextWithMath (pre);
255
+ contentBuffer.write ('\n\n ' );
256
+ contentBuffer.write (processedText);
257
+ contentBuffer.write ('\n\n ' );
228
258
break ;
229
259
}
230
260
}
231
261
}
232
262
}
233
263
}
234
264
235
- return contentBuffer.toString ().trim ();
265
+ // すべてのセクションで余分な改行を削除
266
+ content = _removeExcessiveNewlines (content);
267
+
268
+ return content;
236
269
} catch (e) {
237
270
developer.log ("セクション内容抽出中にエラーが発生しました: $e " );
238
271
return '' ;
239
272
}
240
273
}
241
274
275
+ /// 入力セクションの内容をクリーンアップする
276
+ String _cleanInputSectionContent (String content) {
277
+ final lines = content.split ('\n ' );
278
+ final cleanedLines = < String > [];
279
+
280
+ for (var line in lines) {
281
+ final trimmedLine = line.trim ();
282
+
283
+ // 空行をスキップ
284
+ if (trimmedLine.isEmpty) {
285
+ continue ;
286
+ }
287
+
288
+ // 単純な数値のみの行をスキップ(入力例のデータの可能性)
289
+ if (RegExp (r'^\d+$' ).hasMatch (trimmedLine)) {
290
+ continue ;
291
+ }
292
+
293
+ // 入力例のパターンをスキップ(例:#..#.)
294
+ if (RegExp (r'^[.#]+$' ).hasMatch (trimmedLine)) {
295
+ continue ;
296
+ }
297
+
298
+ // 不要な「整数aが与えられる」などの汎用的な説明をスキップ
299
+ if (trimmedLine.contains ('行目は整数' ) && trimmedLine.contains ('が与えられる' )) {
300
+ continue ;
301
+ }
302
+
303
+ cleanedLines.add (line);
304
+ }
305
+
306
+ return cleanedLines.join ('\n ' ).trim ();
307
+ }
308
+
309
+ /// 余分な改行を削除する
310
+ String _removeExcessiveNewlines (String content) {
311
+ // 3つ以上連続する改行を2つに制限
312
+ content = content.replaceAll (RegExp (r'\n{3,}' ), '\n\n ' );
313
+
314
+ // 行末の空白を削除
315
+ final lines = content.split ('\n ' );
316
+ final trimmedLines = lines.map ((line) => line.trimRight ()).toList ();
317
+
318
+ return trimmedLines.join ('\n ' ).trim ();
319
+ }
320
+
242
321
/// HTMLテキストから数式を検出し、適切に$で囲む
243
322
String _processTextWithMath (Element element) {
244
323
String text = '' ;
0 commit comments