| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- @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>');
- };
- $renderAnswerSolutionWithStepBoxes = function (string $solutionHtml): string {
- $solution = trim($solutionHtml);
- if ($solution === '' || $solution === '(无详解)') {
- return $solutionHtml;
- }
- // 去掉分步得分等分值标记,与判卷页一致
- $solution = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solution) ?? $solution;
- // 与判卷页一致:优先识别“步骤X / 第X步”
- if (preg_match('/(步骤\s*\d+|第\s*\d+\s*步)/u', $solution)) {
- $parts = preg_split('/(?=步骤\s*\d+|第\s*\d+\s*步)/u', $solution, -1, PREG_SPLIT_NO_EMPTY) ?: [];
- $processed = '';
- foreach ($parts as $index => $part) {
- $stepText = trim((string) $part);
- if ($stepText === '') {
- continue;
- }
- $prefix = $index > 0 ? '<br>' : '';
- $isStep = preg_match('/^(步骤\s*\d+|第\s*\d+\s*步)/u', $stepText);
- if ($isStep) {
- $processed .= $prefix
- . '<span class="solution-step"><span class="step-box"><span class="detail-grade-box"></span></span><span class="step-label">'
- . $stepText
- . '</span></span>';
- } else {
- // 与判卷页保持一致:步骤前引导语不加方框
- $processed .= $prefix . '<span class="step-label">' . $stepText . '</span>';
- }
- }
- return $processed !== '' ? $processed : $solution;
- }
- // 无步骤关键词:与判卷页一致,在解析开头加一个判分框
- return '<span class="solution-step"><span class="step-box"><span class="detail-grade-box"></span></span><span class="step-label">'
- . $solution
- . '</span></span>';
- };
- @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);
- $questionType = strtolower((string) ($q->question_type ?? ''));
- $isAnswerType = $questionType === 'answer';
- $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))
- : '(无详解)';
- if ($isAnswerType) {
- $renderSolution = $renderAnswerSolutionWithStepBoxes($renderSolution);
- }
- @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>
|