| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- @php
- $choiceQuestions = $questions['choice'] ?? [];
- $fillQuestions = $questions['fill'] ?? [];
- $answerQuestions = $questions['answer'] ?? [];
- $allQuestions = collect()
- ->concat($choiceQuestions)
- ->concat($fillQuestions)
- ->concat($answerQuestions)
- ->sortBy(fn ($q) => (int) ($q->question_number ?? 0))
- ->values();
- $normalizeQuickAnswer = function (?string $answer): string {
- $answer = trim((string) $answer);
- if ($answer === '') {
- return '—';
- }
- $answer = preg_replace('/\s+/u', ' ', $answer) ?? $answer;
- $answer = str_replace([';', ';'], ' / ', $answer);
- return trim($answer);
- };
- $quickAnswers = $allQuestions->map(function ($q) use ($normalizeQuickAnswer) {
- $answerText = $normalizeQuickAnswer($q->answer ?? '');
- $isLong = mb_strlen(strip_tags($answerText)) > 20
- || str_contains($answerText, "\n")
- || str_contains($answerText, "\r")
- || str_contains($answerText, '{')
- || str_contains($answerText, '\\frac')
- || str_contains($answerText, '见解析');
- $processedAnswer = \App\Services\MathFormulaProcessor::processFormulas($answerText);
- // 速查统一为流式:移除显式换行
- $processedAnswer = preg_replace('/<br\s*\/?>/iu', ' ', $processedAnswer) ?? $processedAnswer;
- return [
- 'no' => (int) ($q->question_number ?? 0),
- 'answer' => $processedAnswer,
- 'long' => $isLong,
- 'extended' => false,
- ];
- })->values();
- $normalizeDetailHtml = function (?string $text): string {
- $text = trim((string) $text);
- if ($text === '') {
- return '';
- }
- $text = preg_replace('/font-size\s*:[^;"]+;?/iu', '', $text) ?? $text;
- $text = preg_replace('/line-height\s*:[^;"]+;?/iu', '', $text) ?? $text;
- $text = preg_replace('/style\s*=\s*([\'"])\s*\1/iu', '', $text) ?? $text;
- return $text;
- };
- $formatDetailForReadability = function (?string $text): string {
- $text = trim((string) $text);
- if ($text === '') {
- return '';
- }
- // 仅做轻量分行:遇到“步骤X”时换行,避免整段拥挤
- $text = preg_replace('/\s*(步骤\s*[0-9一二三四五六七八九十]+\s*[::])/u', '<br>$1', $text) ?? $text;
- $text = preg_replace('/\s*(第\s*[0-9一二三四五六七八九十]+\s*步\s*[::])/u', '<br>$1', $text) ?? $text;
- // 软换行点:分号后允许优先换行,缓解长公式串拥挤
- $text = str_replace([';', ';'], [';<wbr>', ';<wbr>'], $text);
- // 轻量语义断点:在“则/故/所以”前给换行机会,避免整段公式堆在一行
- $text = preg_replace('/,\s*(则|故|所以)/u', ',<br>$1', $text) ?? $text;
- // 清理重复换行
- $text = preg_replace('/(?:<br>\s*){2,}/u', '<br>', $text) ?? $text;
- return ltrim($text, '<br>');
- };
- @endphp
- <div class="answer-detail-page">
- <div class="answer-quick">
- <div class="answer-quick-label">答案速查</div>
- <div class="answer-quick-flow answer-quick-flow-ordered">
- @foreach($quickAnswers as $item)
- <span class="answer-quick-item {{ $item['long'] ? 'answer-quick-item-long' : 'answer-quick-item-compact' }}">
- <strong>{{ $item['no'] }}. </strong>{!! $item['answer'] !!}
- </span>
- @endforeach
- </div>
- </div>
- <div class="answer-detail-two-cols">
- @foreach($allQuestions as $q)
- @php
- $no = (int) ($q->question_number ?? 0);
- $rawAnswer = trim((string) ($q->answer ?? ''));
- $rawSolution = $normalizeDetailHtml($q->solution ?? '');
- $isSeeSolutionAnswer = (bool) preg_match('/^见\s*解析[。\.]?$|^详见解析/u', $rawAnswer);
- $renderAnswer = $rawAnswer !== ''
- ? \App\Services\MathFormulaProcessor::processFormulas($rawAnswer)
- : '—';
- $renderSolution = $rawSolution !== ''
- ? \App\Services\MathFormulaProcessor::processFormulas($formatDetailForReadability($rawSolution))
- : '(无详解)';
- @endphp
- <div class="answer-detail-item">
- <div class="entry-line">
- <span class="entry-head">
- <span class="qno">{{ $no }}.</span>
- @if(!$isSeeSolutionAnswer)
- <span class="answer-only">{!! $renderAnswer !!}</span>
- @endif
- <span class="analysis-tag">【解析】</span>
- </span>
- <span class="solution-only">{!! $renderSolution !!}</span>
- </div>
- </div>
- @endforeach
- </div>
- </div>
|