paper-body.blade.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. // 判卷模式是否显示题目题干与选项,默认显示;可通过 EXAM_PDF_GRADING_SHOW_STEM 关闭
  9. $showGradingStem = config('exam.pdf_grading_show_stem', true);
  10. // 格式化题目ID为6位补0格式
  11. $formatQuestionId = function($id) {
  12. if (empty($id)) return '';
  13. return '(Q' . str_pad($id, 6, '0', STR_PAD_LEFT) . ')';
  14. };
  15. // 【新增】动态计算大题号 - 根据有题目的题型分配序号
  16. $sectionNumbers = [
  17. 'choice' => null,
  18. 'fill' => null,
  19. 'answer' => null
  20. ];
  21. $currentSectionNumber = 1;
  22. // 只给有题目的题型分配序号
  23. if (!empty($choiceQuestions)) {
  24. $sectionNumbers['choice'] = $currentSectionNumber++;
  25. }
  26. if (!empty($fillQuestions)) {
  27. $sectionNumbers['fill'] = $currentSectionNumber++;
  28. }
  29. if (!empty($answerQuestions)) {
  30. $sectionNumbers['answer'] = $currentSectionNumber++;
  31. }
  32. // 获取题型名称的辅助函数
  33. $getSectionTitle = function($type, $sectionNumber) {
  34. $typeNames = [
  35. 'choice' => '选择题',
  36. 'fill' => '填空题',
  37. 'answer' => '解答题'
  38. ];
  39. // 将数字转换为中文数字
  40. $chineseNumbers = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
  41. $chineseNumber = $chineseNumbers[$sectionNumber] ?? (string)$sectionNumber;
  42. return $chineseNumber . '、' . $typeNames[$type];
  43. };
  44. // 检查是否有数学公式处理标记,避免重复处理
  45. $mathProcessed = false;
  46. // 检查所有题型中是否有任何题目包含 math_processed 标记
  47. foreach ([$choiceQuestions, $fillQuestions, $answerQuestions] as $questionType) {
  48. foreach ($questionType as $q) {
  49. if (isset($q->math_processed) && $q->math_processed) {
  50. $mathProcessed = true;
  51. break 2; // 找到标记就退出两层循环
  52. }
  53. }
  54. }
  55. // 计算填空空格数量
  56. $countBlanks = function($text) {
  57. $count = 0;
  58. $count += preg_match_all('/_{2,}/u', $text, $m);
  59. $count += preg_match_all('/(\s*)/u', $text, $m);
  60. $count += preg_match_all('/\(\s*\)/', $text, $m);
  61. return max(1, $count);
  62. };
  63. // 计算步骤数量
  64. $countSteps = function($text) {
  65. $matches = [];
  66. $cnt = preg_match_all('/第\s*\d+\s*步/u', $text ?? '', $matches);
  67. return max(1, $cnt);
  68. };
  69. $renderBoxes = function($num) {
  70. // 判卷方框放大 1.2 倍,保持单行布局
  71. if ($num == 2) {
  72. // 两个方框时,使用右对齐布局
  73. return '<div style="display:flex;justify-content:flex-end;gap:4px;">' .
  74. str_repeat('<span style="display:inline-block;width:17px;height:17px;line-height:17px;border:1px solid #333;"></span>', $num) .
  75. '</div>';
  76. }
  77. 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);
  78. };
  79. @endphp
  80. {{-- 【新增】步骤方框CSS样式 --}}
  81. <style>
  82. .solution-step {
  83. display: block;
  84. margin: 8px 0;
  85. padding: 4px 0;
  86. }
  87. .step-box {
  88. display: inline-block;
  89. margin-right: 8px;
  90. vertical-align: middle;
  91. }
  92. .step-label {
  93. white-space: normal;
  94. vertical-align: middle;
  95. }
  96. .solution-section {
  97. margin: 10px 0;
  98. padding: 8px;
  99. background-color: #f9f9f9;
  100. }
  101. </style>
  102. <!-- 动态大题号 - 选择题 -->
  103. @if($sectionNumbers['choice'] !== null)
  104. <div class="section-title">{{ $getSectionTitle('choice', $sectionNumbers['choice']) }}
  105. @if(count($choiceQuestions) > 0)
  106. @php
  107. $choiceTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $choiceQuestions));
  108. @endphp
  109. (本大题共 {{ count($choiceQuestions) }} 小题,共 {{ $choiceTotal }} 分)
  110. @else
  111. (本大题共 0 小题,共 0 分)
  112. @endif
  113. </div>
  114. @if(count($choiceQuestions) > 0)
  115. @foreach($choiceQuestions as $index => $q)
  116. @php
  117. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  118. $questionNumber = $q->question_number ?? ($index + 1);
  119. $cleanContent = preg_replace('/^\d+[\.、]\s*/', '', $q->content);
  120. $cleanContent = trim($cleanContent);
  121. $options = $q->options ?? [];
  122. if (empty($options)) {
  123. // 【修复】选项标记必须在行首或空白后,避免误匹配 SVG 注释中的 BD:DC 等内容
  124. $pattern = '/(?:^|\s)([A-D])[\.、:.:]\s*(.+?)(?=(?:^|\s)[A-D][\.、:.:]|$)/su';
  125. if (preg_match_all($pattern, $cleanContent, $matches, PREG_SET_ORDER)) {
  126. foreach ($matches as $match) {
  127. $optionText = trim($match[2]);
  128. if (!empty($optionText)) {
  129. $options[] = $optionText;
  130. }
  131. }
  132. }
  133. }
  134. $stemLine = $cleanContent;
  135. if (!empty($options)) {
  136. // 【修复】只匹配行首或空白后的选项标记,避免误匹配 SVG 注释中的 BD:DC 等内容
  137. if (preg_match('/^(.+?)(?=(?:^|\s)[A-D][\.、:.:])/su', $cleanContent, $stemMatch)) {
  138. $stemLine = trim($stemMatch[1]);
  139. }
  140. }
  141. // 将题干中的空括号/下划线替换为短波浪线;如无占位符,则在末尾追加短波浪线
  142. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  143. // 【修复】扩展下划线转换规则,支持LaTeX格式和多种占位符
  144. $renderedStem = $stemLine;
  145. // 先处理LaTeX格式的underline命令
  146. $renderedStem = preg_replace('/\\\underline\{[^}]*\}/', $blankSpan, $renderedStem);
  147. $renderedStem = preg_replace('/\\\qquad+/', $blankSpan, $renderedStem);
  148. // 【修复】在处理填空占位符时,保护LaTeX公式不被破坏
  149. // 先标记LaTeX公式区域
  150. $latexPlaceholders = [];
  151. $counter = 0;
  152. $renderedStem = preg_replace_callback('/\$[^$]+\$/u', function($matches) use (&$latexPlaceholders, &$counter, $blankSpan) {
  153. $placeholder = '<<<LATEX_' . $counter . '>>>';
  154. $latexPlaceholders[$placeholder] = $matches[0];
  155. $counter++;
  156. return $placeholder;
  157. }, $renderedStem);
  158. // 现在处理普通占位符(不会破坏LaTeX公式)
  159. $renderedStem = preg_replace(['/(\s*)/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $renderedStem);
  160. // 恢复LaTeX公式(并进行HTML实体编码防止被浏览器解析)
  161. foreach ($latexPlaceholders as $placeholder => $latexContent) {
  162. $encodedLatex = htmlspecialchars($latexContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  163. $renderedStem = str_replace($placeholder, $encodedLatex, $renderedStem);
  164. }
  165. // 如果没有占位符,在末尾添加
  166. if ($renderedStem === $stemLine) {
  167. $renderedStem .= ' ' . $blankSpan;
  168. }
  169. $renderedStem = $mathProcessed ? $renderedStem : \App\Services\MathFormulaProcessor::processFormulas($renderedStem);
  170. @endphp
  171. <div class="question">
  172. <div class="question-grid">
  173. <div class="question-lead">
  174. @if($gradingMode)
  175. <span class="grading-boxes">{!! $renderBoxes(1) !!}</span>
  176. @endif
  177. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  178. </div>
  179. @if(!$gradingMode || $showGradingStem)
  180. <div class="question-main">
  181. <span class="question-stem">{!! $renderedStem !!}</span>
  182. @if($showQuestionId && !empty($q->id))
  183. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  184. @endif
  185. </div>
  186. @endif
  187. @if((!$gradingMode || $showGradingStem) && !empty($options))
  188. @php
  189. // 计算选项长度并动态选择布局
  190. $optCount = count($options);
  191. $maxOptionLength = 0;
  192. foreach ($options as $opt) {
  193. $optText = strip_tags(\App\Services\MathFormulaProcessor::processFormulas($opt));
  194. $maxOptionLength = max($maxOptionLength, mb_strlen($optText, 'UTF-8'));
  195. }
  196. // 根据最长选项长度和选项数量动态选择布局
  197. // 短选项(≤15字符)且选项数≤4:4列布局
  198. // 中等选项(16-30字符)或选项数>4:2列布局
  199. // 长选项(>30字符):1列布局
  200. if ($maxOptionLength <= 13) {
  201. $optionsClass = 'options-grid-4';
  202. $layoutDesc = '4列布局';
  203. } elseif ($maxOptionLength <= 26) {
  204. $optionsClass = 'options-grid-2';
  205. $layoutDesc = '2列布局';
  206. } else {
  207. $optionsClass = 'options-grid-1';
  208. $layoutDesc = '1列布局';
  209. }
  210. \Illuminate\Support\Facades\Log::debug('选择题布局决策', [
  211. 'question_number' => $questionNumber,
  212. 'opt_count' => $optCount,
  213. 'max_length' => $maxOptionLength,
  214. 'selected_class' => $optionsClass,
  215. 'layout' => $layoutDesc
  216. ]);
  217. @endphp
  218. <div class="question-lead spacer"></div>
  219. <div class="{{ $optionsClass }}">
  220. @foreach($options as $optIndex => $opt)
  221. @php
  222. // 兼容两种格式:数字索引 (0,1,2,3) 或字母键 (A,B,C,D)
  223. if (is_numeric($optIndex)) {
  224. $label = chr(65 + (int)$optIndex);
  225. } else {
  226. $label = strtoupper($optIndex);
  227. }
  228. // 【修复】根据是否已预处理决定处理方式
  229. if ($mathProcessed) {
  230. // 已预处理:数据已包含处理好的 <img> 和公式,直接使用
  231. $renderedOpt = $opt;
  232. } else {
  233. // 未预处理:先转义保护,processFormulas() 内部会解码并处理
  234. $encodedOpt = htmlspecialchars($opt, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  235. $renderedOpt = \App\Services\MathFormulaProcessor::processFormulas($encodedOpt);
  236. }
  237. @endphp
  238. <div class="option option-compact">
  239. <strong>{{ $label }}.</strong>&nbsp;{!! $renderedOpt !!}
  240. </div>
  241. @endforeach
  242. </div>
  243. @endif
  244. @if($gradingMode)
  245. @php
  246. $solutionText = trim($q->solution ?? '');
  247. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  248. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  249. $solutionHtml = $solutionText === ''
  250. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  251. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  252. @endphp
  253. @if($showGradingStem)
  254. <div class="question-lead spacer"></div>
  255. @endif
  256. <div class="answer-meta">
  257. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  258. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  259. </div>
  260. @endif
  261. </div>
  262. </div>
  263. @endforeach
  264. @else
  265. <div class="question">
  266. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  267. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  268. </div>
  269. </div>
  270. @endif
  271. @endif
  272. <!-- 动态大题号 - 填空题 -->
  273. @if($sectionNumbers['fill'] !== null)
  274. <div class="section-title">{{ $getSectionTitle('fill', $sectionNumbers['fill']) }}
  275. @if(count($fillQuestions) > 0)
  276. @php
  277. $fillTotal = array_sum(array_map(fn($q) => $q->score ?? 5, $fillQuestions));
  278. @endphp
  279. (本大题共 {{ count($fillQuestions) }} 小题,共 {{ $fillTotal }} 分)
  280. @else
  281. (本大题共 0 小题,共 0 分)
  282. @endif
  283. </div>
  284. @if(count($fillQuestions) > 0)
  285. @foreach($fillQuestions as $index => $q)
  286. @php
  287. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  288. $questionNumber = $q->question_number ?? (count($choiceQuestions) + $index + 1);
  289. $blankSpan = '<span style="display:inline-block; min-width:80px; border-bottom:1.2px dashed #444; vertical-align:bottom;">&nbsp;</span>';
  290. // 【修复】扩展下划线转换规则,支持LaTeX格式和多种占位符
  291. $renderedContent = $q->content;
  292. // 【修复】在处理填空占位符时,保护LaTeX公式不被破坏
  293. // 先标记LaTeX公式区域(支持包含反斜杠和花括号的LaTeX命令)
  294. $latexPlaceholders = [];
  295. $counter = 0;
  296. $renderedContent = preg_replace_callback('/\$(?:[^\$]|\\.)*\$/u', function($matches) use (&$latexPlaceholders, &$counter, $blankSpan) {
  297. $placeholder = '<<<LATEX_FILL_' . $counter . '>>>';
  298. $latexPlaceholders[$placeholder] = $matches[0];
  299. $counter++;
  300. return $placeholder;
  301. }, $renderedContent);
  302. // 现在处理普通占位符(不会破坏LaTeX公式)
  303. $renderedContent = preg_replace(['/(\s*)/u', '/\(\s*\)/', '/_{2,}/'], $blankSpan, $renderedContent);
  304. // 恢复LaTeX公式(并进行HTML实体编码防止被浏览器解析)
  305. foreach ($latexPlaceholders as $placeholder => $latexContent) {
  306. $encodedLatex = htmlspecialchars($latexContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  307. $renderedContent = str_replace($placeholder, $encodedLatex, $renderedContent);
  308. }
  309. // 如果没有占位符且内容没有变化,在末尾添加
  310. // 但要检查是否已经有填空占位符(如\underline{\qquad})
  311. if ($renderedContent === $q->content && !preg_match('/\\\\underline|\\\\qquad|(\s*)|\(\s*\)/', $renderedContent)) {
  312. $renderedContent .= ' ' . $blankSpan;
  313. }
  314. $renderedContent = $mathProcessed ? $renderedContent : \App\Services\MathFormulaProcessor::processFormulas($renderedContent);
  315. @endphp
  316. <div class="question">
  317. <div class="question-grid">
  318. <div class="question-lead">
  319. @if($gradingMode)
  320. <span class="grading-boxes">{!! $renderBoxes($countBlanks($q->content ?? '')) !!}</span>
  321. @endif
  322. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  323. </div>
  324. @if(!$gradingMode || $showGradingStem)
  325. <div class="question-main">
  326. <span class="question-stem">{!! $renderedContent !!}</span>
  327. @if($showQuestionId && !empty($q->id))
  328. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  329. @endif
  330. </div>
  331. @endif
  332. @if($gradingMode)
  333. @php
  334. $solutionText = trim($q->solution ?? '');
  335. // 去掉前置的"解题思路"标签,避免出现"解题思路:【解题思路】"重复
  336. $solutionText = preg_replace('/^【?\s*解题思路\s*】?\s*[::]?\s*/u', '', $solutionText);
  337. $solutionHtml = $solutionText === ''
  338. ? '<span style="color:#999;font-style:italic;">(暂无解题思路)</span>'
  339. : ($mathProcessed ? $solutionText : \App\Services\MathFormulaProcessor::processFormulas($solutionText));
  340. @endphp
  341. @if($showGradingStem)
  342. <div class="question-lead spacer"></div>
  343. @endif
  344. <div class="answer-meta">
  345. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  346. <div class="answer-line"><strong>解题思路:</strong><span class="solution-content">{!! $solutionHtml !!}</span></div>
  347. </div>
  348. @endif
  349. </div>
  350. </div>
  351. @endforeach
  352. @else
  353. <div class="question">
  354. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  355. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  356. </div>
  357. </div>
  358. @endif
  359. @endif
  360. <!-- 动态大题号 - 解答题 -->
  361. @if($sectionNumbers['answer'] !== null)
  362. <div class="section-title">{{ $getSectionTitle('answer', $sectionNumbers['answer']) }}
  363. @if(count($answerQuestions) > 0)
  364. (本大题共 {{ count($answerQuestions) }} 小题,共 {{ array_sum(array_column($answerQuestions, 'score')) }} 分。解答应写出文字说明、证明过程或演算步骤)
  365. @else
  366. (本大题共 0 小题,共 0 分)
  367. @endif
  368. </div>
  369. @if(count($answerQuestions) > 0)
  370. @foreach($answerQuestions as $index => $q)
  371. @php
  372. // 【修复】使用question_number字段作为显示序号,确保全局序号一致性
  373. $questionNumber = $q->question_number ?? (count($choiceQuestions) + count($fillQuestions) + $index + 1);
  374. @endphp
  375. <div class="question">
  376. <div class="question-grid">
  377. <div class="question-lead">
  378. <span class="question-number">{{ $gradingMode ? '题目 ' : '' }}{{ $questionNumber }}.</span>
  379. </div>
  380. @if(!$gradingMode || $showGradingStem)
  381. <div class="question-main">
  382. @unless($gradingMode)
  383. <span class="question-score-inline">(本小题满分 {{ $q->score ?? 10 }} 分)
  384. @if($showQuestionId && !empty($q->id))
  385. <span class="question-id" style="font-size:10px;color:#999;margin-left:4px;">{!! $formatQuestionId($q->id) !!}</span>
  386. @endif
  387. </span>
  388. @endunless
  389. <span class="question-stem">{!! $mathProcessed ? $q->content : \App\Services\MathFormulaProcessor::processFormulas($q->content) !!}</span>
  390. </div>
  391. @endif
  392. @unless($gradingMode)
  393. <div class="question-lead spacer"></div>
  394. <div class="answer-area boxy">
  395. <span class="answer-label">作答</span>
  396. </div>
  397. @endunless
  398. @if($gradingMode)
  399. @php
  400. $solutionRaw = $q->solution ?? '';
  401. $solutionProcessed = $mathProcessed ? $solutionRaw : \App\Services\MathFormulaProcessor::processFormulas($solutionRaw);
  402. // 去掉分步得分等分值标记
  403. $solutionProcessed = preg_replace('/(\s*\d+\s*分\s*)/u', '', $solutionProcessed);
  404. // 【修复】优化解析分段格式 - 支持两种格式:
  405. // 1. 【解题思路】格式
  406. // 2. 解题过程:格式
  407. // 先处理【】格式
  408. $solutionProcessed = preg_replace('/【(解题思路|详细解答|最终答案)】/u', "\n\n===SECTION_START===\n【$1】\n===SECTION_END===\n\n", $solutionProcessed);
  409. // 【扩展】处理多种"解题过程"格式,包括带括号的内容
  410. $solutionProcessed = preg_replace('/(解题过程\s*[^:\n]*:)/u', "\n\n===SECTION_START===\n【解题过程】\n===SECTION_END===\n\n", $solutionProcessed);
  411. // 按section分割内容
  412. $sections = explode('===SECTION_START===', $solutionProcessed);
  413. $processedSections = [];
  414. foreach ($sections as $section) {
  415. if (empty(trim($section))) continue;
  416. // 去掉结尾标记
  417. $section = str_replace('===SECTION_END===', '', $section);
  418. // 检查是否是解题相关部分
  419. if (preg_match('/【(解题思路|详细解答|最终答案|解题过程)】/u', $section, $matches)) {
  420. $sectionTitle = $matches[0];
  421. $sectionContent = preg_replace('/【(解题思路|详细解答|最终答案|解题过程)】/u', '', $section);
  422. // 【修复】处理步骤 - 在每个"步骤N"或"第N步"前添加方框
  423. // 【优化】使用split分割步骤,为所有步骤添加方框(包括第一个)
  424. if (preg_match('/(步骤\s*\d+|第\s*\d+\s*步)/u', $sectionContent)) {
  425. // 使用前瞻断言分割,保留分隔符
  426. $allSteps = preg_split('/(?=步骤\s*\d+|第\s*\d+\s*步)/u', $sectionContent, -1, PREG_SPLIT_NO_EMPTY);
  427. if (count($allSteps) > 0) {
  428. $processed = '';
  429. // 为每个步骤添加方框(包括第一个)
  430. for ($i = 0; $i < count($allSteps); $i++) {
  431. $stepText = trim($allSteps[$i]);
  432. if (!empty($stepText)) {
  433. // 为每个步骤添加方框和换行(第一个步骤前面不加<br>)
  434. $prefix = ($i > 0) ? '<br>' : '';
  435. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  436. }
  437. }
  438. $sectionContent = $processed;
  439. }
  440. } else {
  441. // 没有明确步骤:在标题后添加一个方框作为开始
  442. $sectionContent = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">&nbsp;</span></span> ' . trim($sectionContent);
  443. }
  444. // 包装section
  445. $processedSections[] = '<div class="solution-section"><strong>' . $sectionTitle . '</strong><br>' . $sectionContent . '</div>';
  446. } else {
  447. // 非解题部分直接保留
  448. $processedSections[] = $section;
  449. }
  450. }
  451. // 重新组合所有部分
  452. $solutionProcessed = implode('', $processedSections);
  453. // 【新增】如果没有匹配到任何section标记,整个solution都没有方框,则默认加一个方框
  454. if (empty($processedSections) || (count($processedSections) === 1 && !str_contains($processedSections[0] ?? '', 'step-box'))) {
  455. // 检查是否有步骤关键词
  456. if (preg_match('/(步骤\s*\d+|第\s*\d+\s*步)/u', $solutionProcessed)) {
  457. // 有步骤关键词:为每个步骤添加方框
  458. $allSteps = preg_split('/(?=步骤\s*\d+|第\s*\d+\s*步)/u', $solutionProcessed, -1, PREG_SPLIT_NO_EMPTY);
  459. if (count($allSteps) > 0) {
  460. $processed = '';
  461. for ($i = 0; $i < count($allSteps); $i++) {
  462. $stepText = trim($allSteps[$i]);
  463. if (!empty($stepText)) {
  464. // 只有真正以"步骤"或"第X步"开头的部分才加方框
  465. // 第一个部分如果不是步骤开头(如【分析】),则不加方框
  466. $isStep = preg_match('/^(步骤\s*\d+|第\s*\d+\s*步)/u', $stepText);
  467. $prefix = ($i > 0) ? '<br>' : '';
  468. if ($isStep) {
  469. $processed .= $prefix . '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . $stepText . '</span></span>';
  470. } else {
  471. // 非步骤的前缀文本,直接输出不加方框
  472. $processed .= $prefix . '<span class="step-label">' . $stepText . '</span>';
  473. }
  474. }
  475. }
  476. $solutionProcessed = $processed;
  477. }
  478. } else {
  479. // 没有步骤关键词:默认在开头添加一个方框
  480. $solutionProcessed = '<span class="solution-step"><span class="step-box">' . $renderBoxes(1) . '</span><span class="step-label">' . trim($solutionProcessed) . '</span></span>';
  481. }
  482. }
  483. // 将多余的换行转换为<br>,但保留合理的段落间距
  484. $solutionProcessed = preg_replace('/\n{3,}/u', "\n\n", $solutionProcessed);
  485. $solutionProcessed = nl2br($solutionProcessed);
  486. @endphp
  487. @if($showGradingStem)
  488. <div class="question-lead spacer"></div>
  489. @endif
  490. <div class="answer-meta">
  491. <div class="answer-line"><strong>正确答案:</strong><span class="solution-content">{!! $mathProcessed ? ($q->answer ?? '') : \App\Services\MathFormulaProcessor::processFormulas($q->answer ?? '') !!}</span></div>
  492. <div class="answer-line solution-parsed">{!! $solutionProcessed !!}</div>
  493. </div>
  494. @endif
  495. </div>
  496. </div>
  497. @endforeach
  498. @else
  499. <div class="question">
  500. <div class="question-content" style="font-style: italic; color: #999; padding: 20px; border: 1px dashed #ccc; background: #f9f9f9;">
  501. 该题型正在生成中或暂无题目,请稍后刷新页面查看
  502. </div>
  503. </div>
  504. @endif
  505. @endif