paper-body.blade.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. @php
  2. $choiceQuestions = $questions['choice'] ?? [];
  3. $fillQuestions = $questions['fill'] ?? [];
  4. $answerQuestions = $questions['answer'] ?? [];
  5. $gradingMode = $grading ?? false;
  6. // 是否在题干后显示题目ID (Q000123)
  7. $showQuestionId = config('exam.show_question_id_in_pdf', false);
  8. // 是否在题号后显示题目难度(用于校验排序)
  9. $showQuestionDifficulty = config('exam.show_question_difficulty_in_pdf', false);
  10. // 判卷模式是否显示题目题干与选项,默认显示;可通过 EXAM_PDF_GRADING_SHOW_STEM 关闭
  11. $showGradingStem = config('exam.pdf_grading_show_stem', true);
  12. // 格式化题目ID为6位补0格式
  13. $formatQuestionId = function($id) {
  14. if (empty($id)) return '';
  15. return '(Q' . str_pad($id, 6, '0', STR_PAD_LEFT) . ')';
  16. };
  17. $formatDifficulty = function($difficulty) {
  18. if ($difficulty === null || $difficulty === '') return '';
  19. $value = (float) $difficulty;
  20. return sprintf('[%.2f]', $value);
  21. };
  22. // 【新增】动态计算大题号 - 根据有题目的题型分配序号
  23. $sectionNumbers = [
  24. 'choice' => null,
  25. 'fill' => null,
  26. 'answer' => null
  27. ];
  28. $currentSectionNumber = 1;
  29. // 只给有题目的题型分配序号
  30. if (!empty($choiceQuestions)) {
  31. $sectionNumbers['choice'] = $currentSectionNumber++;
  32. }
  33. if (!empty($fillQuestions)) {
  34. $sectionNumbers['fill'] = $currentSectionNumber++;
  35. }
  36. if (!empty($answerQuestions)) {
  37. $sectionNumbers['answer'] = $currentSectionNumber++;
  38. }
  39. // 获取题型名称的辅助函数
  40. $getSectionTitle = function($type, $sectionNumber) {
  41. $typeNames = [
  42. 'choice' => '选择题',
  43. 'fill' => '填空题',
  44. 'answer' => '解答题'
  45. ];
  46. // 将数字转换为中文数字
  47. $chineseNumbers = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  48. $chineseNumber = $chineseNumbers[$sectionNumber] ?? (string)$sectionNumber;
  49. return $chineseNumber . '、' . $typeNames[$type];
  50. };
  51. // 检查是否有数学公式处理标记,避免重复处理
  52. $mathProcessed = false;
  53. // 检查所有题型中是否有任何题目包含 math_processed 标记
  54. foreach ([$choiceQuestions, $fillQuestions, $answerQuestions] as $questionType) {
  55. foreach ($questionType as $q) {
  56. if (isset($q->math_processed) && $q->math_processed) {
  57. $mathProcessed = true;
  58. break 2; // 找到标记就退出两层循环
  59. }
  60. }
  61. }
  62. $boxCounter = app(\App\Support\GradingMarkBoxCounter::class);
  63. $layoutDeciderService = app(\App\Support\OptionLayoutDecider::class);
  64. // 与判题卡共用同一计数规则,避免方框数量不一致
  65. $countBlanks = fn($text) => $boxCounter->countFillBlanks($text);
  66. $renderBoxes = function($num) {
  67. // 判卷方框放大 1.2 倍,保持单行布局
  68. if ($num == 2) {
  69. // 两个方框时,使用右对齐布局
  70. return '<div style="display:flex;justify-content:flex-end;gap:4px;">' .
  71. str_repeat('<span style="display:inline-block;width:17px;height:17px;line-height:17px;border:1px solid #333;"></span>', $num) .
  72. '</div>';
  73. }
  74. return str_repeat('<span style="display:inline-block;width:17px;height:17px;line-height:17px;border:1px solid #333;margin-right:4px;vertical-align:middle;"></span>', $num);
  75. };
  76. @endphp
  77. {{-- 【新增】步骤方框CSS样式 --}}
  78. <style>
  79. .solution-step {
  80. display: block;
  81. margin: 8px 0;
  82. padding: 4px 0;
  83. }
  84. .step-box {
  85. display: inline-block;
  86. margin-right: 8px;
  87. vertical-align: middle;
  88. }
  89. .step-label {
  90. white-space: normal;
  91. vertical-align: middle;
  92. }
  93. .solution-section {
  94. margin: 10px 0;
  95. padding: 8px;
  96. background-color: #f9f9f9;
  97. }
  98. </style>
  99. <!-- 动态大题号 - 选择题 -->
  100. @if($sectionNumbers['choice'] !== null)
  101. <div class="section-title">{{ $getSectionTitle('choice', $sectionNumbers['choice']) }}
  102. @if(count($choiceQuestions) > 0)
  103. @php
  104. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $choiceQuestions));
  105. @endphp
  106. (本大题共 {{ count($choiceQuestions) }} 小题,共 {{ $choiceTotal }} 分)
  107. @else
  108. (本大题共 0 小题,共 0 分)
  109. @endif
  110. </div>
  111. @if(count($choiceQuestions) > 0)
  112. @foreach($choiceQuestions as $index => $q)
  113. @php
  114. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  115. $questionNumber = $q->question_number ?? ($index + 1);
  116. $cleanContent = preg_replace('/^\d+[\.、]\s*/', '', $q->content);
  117. $cleanContent = trim($cleanContent);
  118. $options = $q->options ?? [];
  119. if (empty($options)) {
  120. // 【修复】选项标记必须在行首或空白后,避免误匹配 SVG 注释中的 BD:DC 等内容
  121. $pattern = '/(?:^|\s)([A-D])[\.、:.:]\s*(.+?)(?=(?:^|\s)[A-D][\.、:.:]|$)/su';
  122. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  123. foreach ($matches as $match) {
  124. $optionText = trim($match[2]);
  125. if (!empty($optionText)) {
  126. $options[] = $optionText;
  127. }
  128. }
  129. }
  130. }
  131. $stemLine = $cleanContent;
  132. if (!empty($options)) {
  133. // 【修复】只匹配行首或空白后的选项标记,避免误匹配 SVG 注释中的 BD:DC 等内容
  134. if (preg_match('/^(.+?)(?=(?:^|\s)[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  135. $stemLine = trim($stemMatch[1]);
  136. }
  137. }
  138. // 选择题只做占位符归一,不再兜底追加下划线,避免出现“( )+下划线”重复。
  139. [$renderedStem] = \App\Support\BlankPlaceholderRenderer::replaceToBlankSpan($stemLine, null, true, false);
  140. // 选择题:句尾不保留句号。
  141. $renderedStem = \App\Support\BlankPlaceholderRenderer::normalizeTerminalPunctuation($renderedStem, 'remove');
  142. $renderedStem = $mathProcessed ? $renderedStem : \App\Services\MathFormulaProcessor::processFormulas($renderedStem);
  143. @endphp
  144. <div class="question">
  145. <div class="question-grid">
  146. <div class="question-lead">
  147. @if($gradingMode)
  148. <span class="grading-boxes">{!! $renderBoxes(1) !!}</span>
  149. @endif
  150. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  151. </div>
  152. @if(!$gradingMode || $showGradingStem)
  153. <div class="question-main">
  154. <span class="question-stem">{!! $renderedStem !!}</span>
  155. @if($showQuestionId && !empty($q->id))
  156. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  157. @if($showQuestionDifficulty)
  158. <span style="font-size:10px;color:#999;margin-left:4px;white-space:nowrap;">{{ $formatDifficulty($q->difficulty ?? null) }}</span>
  159. @endif
  160. @endif
  161. </div>
  162. @endif
  163. @if((!$gradingMode || $showGradingStem) && !empty($options))
  164. @include('pdf.partials.exam-choice-options', [
  165. 'options' => $options,
  166. 'gradingMode' => $gradingMode,
  167. 'mathProcessed' => $mathProcessed,
  168. 'logQuestionNumber' => $questionNumber,
  169. ])
  170. @endif
  171. @if($gradingMode)
  172. @php
  173. $solutionText = trim($q->solution ?? '');
  174. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  175. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  176. $solutionHtml = $solutionText === ''
  177. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  178. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  179. @endphp
  180. @if($showGradingStem)
  181. <div class="question-lead spacer"></div>
  182. @endif
  183. <div class="answer-meta">
  184. @php
  185. $choiceAnswerRaw = $layoutDeciderService->normalizeCompactMathForDisplay((string) ($q->answer ?? ''));
  186. @endphp
  187. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? $choiceAnswerRaw : \App\Services\MathFormulaProcessor::processFormulas($choiceAnswerRaw) !!}</span></div>
  188. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  189. </div>
  190. @endif
  191. </div>
  192. </div>
  193. @endforeach
  194. @else
  195. <div class="question">
  196. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  197. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  198. </div>
  199. </div>
  200. @endif
  201. @endif
  202. <!-- 动态大题号 - 填空题 -->
  203. @if($sectionNumbers['fill'] !== null)
  204. <div class="section-title">{{ $getSectionTitle('fill', $sectionNumbers['fill']) }}
  205. @if(count($fillQuestions) > 0)
  206. @php
  207. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $fillQuestions));
  208. @endphp
  209. (本大题共 {{ count($fillQuestions) }} 小题,共 {{ $fillTotal }} 分)
  210. @else
  211. (本大题共 0 小题,共 0 分)
  212. @endif
  213. </div>
  214. @if(count($fillQuestions) > 0)
  215. @foreach($fillQuestions as $index => $q)
  216. @php
  217. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  218. $questionNumber = $q->question_number ?? (count($choiceQuestions) + $index + 1);
  219. $blankSpan = \App\Support\BlankPlaceholderRenderer::defaultBlankSpan();
  220. [$renderedContent, $hasPlaceholders] = \App\Support\BlankPlaceholderRenderer::replaceToBlankSpan((string) $q->content, $blankSpan, false, false);
  221. // 填空题保留兜底:题干无任何占位时,在末尾补一个标准空位。
  222. if (!$hasPlaceholders) {
  223. $renderedContent .= ' ' . $blankSpan;
  224. }
  225. // 填空题:句尾统一为实心小圆点(英文句点)。
  226. $renderedContent = \App\Support\BlankPlaceholderRenderer::normalizeTerminalPunctuation($renderedContent, 'dot');
  227. // 填空题:若末尾是“句号 + 括注说明”(后可跟图片),将该句号统一为英文小点。
  228. $renderedContent = \App\Support\BlankPlaceholderRenderer::normalizePeriodBeforeTrailingParentheticalNote($renderedContent, '.');
  229. $renderedContent = \App\Support\BlankPlaceholderRenderer::appendTerminalPunctuationIfMissing($renderedContent, '.');
  230. $renderedContent = $mathProcessed ? $renderedContent : \App\Services\MathFormulaProcessor::processFormulas($renderedContent);
  231. @endphp
  232. <div class="question">
  233. <div class="question-grid">
  234. <div class="question-lead">
  235. @if($gradingMode)
  236. <span class="grading-boxes">{!! $renderBoxes($countBlanks($q->content ?? '')) !!}</span>
  237. @endif
  238. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  239. </div>
  240. @if(!$gradingMode || $showGradingStem)
  241. <div class="question-main">
  242. <span class="question-stem">{!! $renderedContent !!}</span>
  243. @if($showQuestionId && !empty($q->id))
  244. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  245. @if($showQuestionDifficulty)
  246. <span style="font-size:10px;color:#999;margin-left:4px;white-space:nowrap;">{{ $formatDifficulty($q->difficulty ?? null) }}</span>
  247. @endif
  248. @endif
  249. </div>
  250. @endif
  251. @if($gradingMode)
  252. @php
  253. $solutionText = trim($q->solution ?? '');
  254. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  255. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  256. $solutionHtml = $solutionText === ''
  257. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  258. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  259. @endphp
  260. @if($showGradingStem)
  261. <div class="question-lead spacer"></div>
  262. @endif
  263. <div class="answer-meta">
  264. @php
  265. $fillAnswerRaw = $layoutDeciderService->normalizeCompactMathForDisplay((string) ($q->answer ?? ''));
  266. @endphp
  267. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? $fillAnswerRaw : \App\Services\MathFormulaProcessor::processFormulas($fillAnswerRaw) !!}</span></div>
  268. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  269. </div>
  270. @endif
  271. </div>
  272. </div>
  273. @endforeach
  274. @else
  275. <div class="question">
  276. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  277. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  278. </div>
  279. </div>
  280. @endif
  281. @endif
  282. <!-- 动态大题号 - 解答题 -->
  283. @if($sectionNumbers['answer'] !== null)
  284. <div class="section-title">{{ $getSectionTitle('answer', $sectionNumbers['answer']) }}
  285. @if(count($answerQuestions) > 0)
  286. (本大题共 {{ count($answerQuestions) }} 小题,共 {{ array_sum(array_column($answerQuestions, 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  287. @else
  288. (本大题共 0 小题,共 0 分)
  289. @endif
  290. </div>
  291. @if(count($answerQuestions) > 0)
  292. @foreach($answerQuestions as $index => $q)
  293. @php
  294. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  295. $questionNumber = $q->question_number ?? (count($choiceQuestions) + count($fillQuestions) + $index + 1);
  296. // 解答题小题排版优化(仅在小题编号语境下换行,避免误伤 f(1) 这类函数表达)
  297. $answerStem = (string) ($q->content ?? '');
  298. preg_match_all('/[((][1-9][0-9]*[))]/u', $answerStem, $subQuestionMatches);
  299. $subQuestionCount = count($subQuestionMatches[0] ?? []);
  300. // 前提:只有出现“至少两个小题编号(形成系列)”才做自动换行
  301. if ($subQuestionCount >= 2) {
  302. // 开头的 (1)/(2) 不额外插入换行,只做标准化空格
  303. $answerStem = preg_replace('/^\s*([((][1-9][0-9]*[))])\s*/u', '$1 ', $answerStem) ?? $answerStem;
  304. // 句读后接小题编号:断行
  305. $answerStem = preg_replace('/([。;;!?!?::.])\s*([((][1-9][0-9]*[))])\s*/u', '$1<br>$2 ', $answerStem) ?? $answerStem;
  306. // 换行簇(实际换行、字面量\\n、已有<br>)后接小题编号:统一压成单个 <br>
  307. $answerStem = preg_replace('/(?:(?:\\\\r\\\\n|\\\\n)|(?:\r?\n)|(?:<br\s*\/?>)|\s)+\s*([((][1-9][0-9]*[))])\s*/u', '<br>$1 ', $answerStem) ?? $answerStem;
  308. // 关键词引导的小题也换行,如 “求(1)…(2)… / 写出(1)…”
  309. $answerStem = preg_replace('/(求出|求解|求|写出|计算|证明|判断|化简)\s*([((][1-9][0-9]*[))])\s*/u', '$1<br>$2 ', $answerStem) ?? $answerStem;
  310. }
  311. $answerStemRendered = $mathProcessed
  312. ? $answerStem
  313. : \App\Services\MathFormulaProcessor::processFormulas($answerStem);
  314. @endphp
  315. <div class="question">
  316. <div class="question-grid">
  317. <div class="question-lead">
  318. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  319. </div>
  320. @if(!$gradingMode || $showGradingStem)
  321. <div class="question-main">
  322. @unless($gradingMode)
  323. <span class="question-score-inline">(本小题满分 {{ $q->score ?? 10 }} 分)
  324. @if($showQuestionId && !empty($q->id))
  325. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  326. @if($showQuestionDifficulty)
  327. <span style="font-size:10px;color:#999;margin-left:4px;white-space:nowrap;">{{ $formatDifficulty($q->difficulty ?? null) }}</span>
  328. @endif
  329. @endif
  330. </span>
  331. @endunless
  332. <span class="question-stem">{!! $answerStemRendered !!}</span>
  333. </div>
  334. @endif
  335. @unless($gradingMode)
  336. <div class="question-lead spacer"></div>
  337. <div class="answer-area boxy">
  338. <span class="answer-label">作答</span>
  339. </div>
  340. @endunless
  341. @if($gradingMode)
  342. @php
  343. $solutionRaw = $q->solution ?? '';
  344. $solutionProcessed = $mathProcessed ? $solutionRaw : \App\Services\MathFormulaProcessor::processFormulas($solutionRaw);
  345. // 去掉分步得分等分值标记
  346. $solutionProcessed = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solutionProcessed);
  347. // 【修复】优化解析分段格式 - 支持两种格式:
  348. // 1. 【解题思路】格式
  349. // 2. 解题过程:格式
  350. // 先处理【】格式
  351. $solutionProcessed = preg_replace('/【(解题思路|详细解答|最终答案)】/u', "\n\n===SECTION_START===\n【$1】\n===SECTION_END===\n\n", $solutionProcessed);
  352. // 【扩展】处理多种"解题过程"格式,包括带括号的内容
  353. $solutionProcessed = preg_replace('/(解题过程\s*[^:\n]*:)/u', "\n\n===SECTION_START===\n【解题过程】\n===SECTION_END===\n\n", $solutionProcessed);
  354. // 按section分割内容
  355. $sections = explode('===SECTION_START===', $solutionProcessed);
  356. $processedSections = [];
  357. $stepPattern = '(步骤\s*[0-9一二三四五六七八九十百零两]+\s*[::]?|第\s*[0-9一二三四五六七八九十百零两]+\s*步\s*[::]?)';
  358. foreach ($sections as $section) {
  359. if (empty(trim($section))) continue;
  360. // 去掉结尾标记
  361. $section = str_replace('===SECTION_END===', '', $section);
  362. // 检查是否是解题相关部分
  363. if (preg_match('/【(解题思路|详细解答|最终答案|解题过程)】/u', $section, $matches)) {
  364. $sectionTitle = $matches[0];
  365. $sectionContent = preg_replace('/【(解题思路|详细解答|最终答案|解题过程)】/u', '', $section);
  366. // 【修复】处理步骤 - 在每个"步骤N"或"第N步"前添加方框
  367. // 【优化】使用split分割步骤,为所有步骤添加方框(包括第一个)
  368. if (preg_match('/' . $stepPattern . '/u', $sectionContent)) {
  369. // 使用前瞻断言分割,保留分隔符
  370. $allSteps = preg_split('/(?=' . $stepPattern . ')/u', $sectionContent, -1, PREG_SPLIT_NO_EMPTY);
  371. if (count($allSteps) > 0) {
  372. $processed = '';
  373. // 与判题卡计数规则一致:仅显式步骤前加方框;前置引导语不加方框
  374. for ($i = 0; $i < count($allSteps); $i++) {
  375. $stepText = trim($allSteps[$i]);
  376. if (!empty($stepText)) {
  377. $prefix = ($i > 0) ? '<br>' : '';
  378. $isStep = preg_match('/^' . $stepPattern . '/u', $stepText);
  379. if ($isStep) {
  380. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  381. } else {
  382. $processed .= $prefix . '<span class="step-label">' . $stepText . '</span>';
  383. }
  384. }
  385. }
  386. $sectionContent = $processed;
  387. }
  388. } else {
  389. // 没有明确步骤:在标题后添加一个方框作为开始
  390. $sectionContent = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ' . trim($sectionContent);
  391. }
  392. // 包装section
  393. $processedSections[] = '<div class="solution-section"><strong>' . $sectionTitle . '</strong><br>' . $sectionContent . '</div>';
  394. } else {
  395. // 非解题部分直接保留
  396. $processedSections[] = $section;
  397. }
  398. }
  399. // 重新组合所有部分
  400. $solutionProcessed = implode('', $processedSections);
  401. // 【新增】如果没有匹配到任何section标记,整个solution都没有方框,则默认加一个方框
  402. if (empty($processedSections) || (count($processedSections) === 1 && !str_contains($processedSections[0] ?? '', 'step-box'))) {
  403. // 检查是否有步骤关键词
  404. if (preg_match('/' . $stepPattern . '/u', $solutionProcessed)) {
  405. // 有步骤关键词:为每个步骤添加方框
  406. $allSteps = preg_split('/(?=' . $stepPattern . ')/u', $solutionProcessed, -1, PREG_SPLIT_NO_EMPTY);
  407. if (count($allSteps) > 0) {
  408. $processed = '';
  409. for ($i = 0; $i < count($allSteps); $i++) {
  410. $stepText = trim($allSteps[$i]);
  411. if (!empty($stepText)) {
  412. // 只有真正以"步骤"或"第X步"开头的部分才加方框
  413. // 第一个部分如果不是步骤开头(如【分析】),则不加方框
  414. $isStep = preg_match('/^' . $stepPattern . '/u', $stepText);
  415. $prefix = ($i > 0) ? '<br>' : '';
  416. if ($isStep) {
  417. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  418. } else {
  419. // 非步骤的前缀文本,直接输出不加方框
  420. $processed .= $prefix . '<span class="step-label">' . $stepText . '</span>';
  421. }
  422. }
  423. }
  424. $solutionProcessed = $processed;
  425. }
  426. } else {
  427. // 没有步骤关键词:默认在开头添加一个方框
  428. $solutionProcessed = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . trim($solutionProcessed) . '</span></span>';
  429. }
  430. }
  431. // 将多余的换行转换为<br>,但保留合理的段落间距
  432. $solutionProcessed = preg_replace('/\n{3,}/u', "\n\n", $solutionProcessed);
  433. $solutionProcessed = nl2br($solutionProcessed);
  434. @endphp
  435. @if($showGradingStem)
  436. <div class="question-lead spacer"></div>
  437. @endif
  438. <div class="answer-meta">
  439. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  440. <div class="answer-line solution-parsed">{!! $solutionProcessed !!}</div>
  441. </div>
  442. @endif
  443. </div>
  444. </div>
  445. @endforeach
  446. @else
  447. <div class="question">
  448. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  449. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  450. </div>
  451. </div>
  452. @endif
  453. @endif