answer-detail-page.blade.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. @php
  2. $choiceQuestions = $questions['choice'] ?? [];
  3. $fillQuestions = $questions['fill'] ?? [];
  4. $answerQuestions = $questions['answer'] ?? [];
  5. $allQuestions = collect()
  6. ->concat($choiceQuestions)
  7. ->concat($fillQuestions)
  8. ->concat($answerQuestions)
  9. ->sortBy(fn ($q) => (int) ($q->question_number ?? 0))
  10. ->values();
  11. $normalizeQuickAnswer = function (?string $answer): string {
  12. $answer = trim((string) $answer);
  13. if ($answer === '') {
  14. return '—';
  15. }
  16. $answer = preg_replace('/\s+/u', ' ', $answer) ?? $answer;
  17. $answer = str_replace([';', ';'], ' / ', $answer);
  18. return trim($answer);
  19. };
  20. $quickAnswers = $allQuestions->map(function ($q) use ($normalizeQuickAnswer) {
  21. $answerText = $normalizeQuickAnswer($q->answer ?? '');
  22. $isLong = mb_strlen(strip_tags($answerText)) > 20
  23. || str_contains($answerText, "\n")
  24. || str_contains($answerText, "\r")
  25. || str_contains($answerText, '{')
  26. || str_contains($answerText, '\\frac')
  27. || str_contains($answerText, '见解析');
  28. $processedAnswer = \App\Services\MathFormulaProcessor::processFormulas($answerText);
  29. // 速查统一为流式:移除显式换行
  30. $processedAnswer = preg_replace('/<br\s*\/?>/iu', ' ', $processedAnswer) ?? $processedAnswer;
  31. return [
  32. 'no' => (int) ($q->question_number ?? 0),
  33. 'answer' => $processedAnswer,
  34. 'long' => $isLong,
  35. 'extended' => false,
  36. ];
  37. })->values();
  38. $normalizeDetailHtml = function (?string $text): string {
  39. $text = trim((string) $text);
  40. if ($text === '') {
  41. return '';
  42. }
  43. $text = preg_replace('/font-size\s*:[^;"]+;?/iu', '', $text) ?? $text;
  44. $text = preg_replace('/line-height\s*:[^;"]+;?/iu', '', $text) ?? $text;
  45. $text = preg_replace('/style\s*=\s*([\'"])\s*\1/iu', '', $text) ?? $text;
  46. return $text;
  47. };
  48. $formatDetailForReadability = function (?string $text): string {
  49. $text = trim((string) $text);
  50. if ($text === '') {
  51. return '';
  52. }
  53. // 仅做轻量分行:遇到“步骤X”时换行,避免整段拥挤
  54. $text = preg_replace('/\s*(步骤\s*[0-9一二三四五六七八九十]+\s*[::])/u', '<br>$1', $text) ?? $text;
  55. $text = preg_replace('/\s*(第\s*[0-9一二三四五六七八九十]+\s*步\s*[::])/u', '<br>$1', $text) ?? $text;
  56. // 软换行点:分号后允许优先换行,缓解长公式串拥挤
  57. $text = str_replace([';', ';'], [';<wbr>', ';<wbr>'], $text);
  58. // 轻量语义断点:在“则/故/所以”前给换行机会,避免整段公式堆在一行
  59. $text = preg_replace('/,\s*(则|故|所以)/u', ',<br>$1', $text) ?? $text;
  60. // 清理重复换行
  61. $text = preg_replace('/(?:<br>\s*){2,}/u', '<br>', $text) ?? $text;
  62. return ltrim($text, '<br>');
  63. };
  64. @endphp
  65. <div class="answer-detail-page">
  66. <div class="answer-quick">
  67. <div class="answer-quick-label">答案速查</div>
  68. <div class="answer-quick-flow answer-quick-flow-ordered">
  69. @foreach($quickAnswers as $item)
  70. <span class="answer-quick-item {{ $item['long'] ? 'answer-quick-item-long' : 'answer-quick-item-compact' }}">
  71. <strong>{{ $item['no'] }}.&nbsp;</strong>{!! $item['answer'] !!}
  72. </span>
  73. @endforeach
  74. </div>
  75. </div>
  76. <div class="answer-detail-two-cols">
  77. @foreach($allQuestions as $q)
  78. @php
  79. $no = (int) ($q->question_number ?? 0);
  80. $rawAnswer = trim((string) ($q->answer ?? ''));
  81. $rawSolution = $normalizeDetailHtml($q->solution ?? '');
  82. $isSeeSolutionAnswer = (bool) preg_match('/^见\s*解析[。\.]?$|^详见解析/u', $rawAnswer);
  83. $renderAnswer = $rawAnswer !== ''
  84. ? \App\Services\MathFormulaProcessor::processFormulas($rawAnswer)
  85. : '—';
  86. $renderSolution = $rawSolution !== ''
  87. ? \App\Services\MathFormulaProcessor::processFormulas($formatDetailForReadability($rawSolution))
  88. : '(无详解)';
  89. @endphp
  90. <div class="answer-detail-item">
  91. <div class="entry-line">
  92. <span class="entry-head">
  93. <span class="qno">{{ $no }}.</span>
  94. @if(!$isSeeSolutionAnswer)
  95. <span class="answer-only">{!! $renderAnswer !!}</span>
  96. @endif
  97. <span class="analysis-tag">【解析】</span>
  98. </span>
  99. <span class="solution-only">{!! $renderSolution !!}</span>
  100. </div>
  101. </div>
  102. @endforeach
  103. </div>
  104. </div>