figer update

This commit is contained in:
wangjinlei
2026-01-06 17:34:05 +08:00
parent 041141a68e
commit 87f6a01c94

View File

@@ -18,7 +18,7 @@ use think\log;
class Production extends Base class Production extends Base
{ {
private $supportedTags = ['sup', 'sub', 'blue', 'b', 'i', 't', 'r']; private $supportedTags = ['sup', 'sub', 'blue', 'b', 'i', 't', 'r', 'wmath'];
// 颜色映射自定义标签颜色对应LaTeX的HTML十六进制颜色 // 颜色映射自定义标签颜色对应LaTeX的HTML十六进制颜色
private $colorMap = [ private $colorMap = [
'blue' => '0082AA', 'blue' => '0082AA',
@@ -2225,6 +2225,11 @@ class Production extends Base
if (empty(trim($content))) { if (empty(trim($content))) {
return ''; return '';
} }
//单行处理公式内容
$refArray = []; $refArray = [];
foreach ($references as $ref) { foreach ($references as $ref) {
$refArray[$ref['index'] + 1] = $ref; $refArray[$ref['index'] + 1] = $ref;
@@ -2263,55 +2268,13 @@ class Production extends Base
*/ */
private function textRenderCreate($content,$references) { private function textRenderCreate($content,$references) {
$latex = ''; $latex = '';
if (preg_match('/(?:<blue>)?\[(\d+(?:[-,]\d+)*)\](?:<\/blue>)?/', $content, $matches)) {
// 去除匹配中的 <blue> 和 </blue>
$cleanedMatch = str_replace(['<blue>', '</blue>'], '', $matches[0]);
// 提取引用编号部分(去掉方括号)
$referencePart = trim($cleanedMatch, '[]');
// 分割逗号分隔的不同引用项
$parts = explode(',', $referencePart);
$latexRefs = [];
foreach ($parts as $part) {
// 判断是否是范围形式如 3-5
if (strpos($part, '-') !== false) {
list($start, $end) = explode('-', $part);
$start = intval($start);
$end = intval($end);
// 展开范围并逐个查找 ref_id
for ($i = $start; $i <= $end; $i++) {
$arrayIndex = $i;
if (isset($references[$arrayIndex]) && !empty($references[$arrayIndex]['p_refer_id'])) {
$latexRefs[] = 'ref_' . $references[$arrayIndex]['p_refer_id'];
} else {
$latexRefs[] = $i;
}
}
} else {
// 单个引用编号处理
$index = intval($part);
$arrayIndex = $index;
if (isset($references[$arrayIndex]) && !empty($references[$arrayIndex]['p_refer_id'])) {
$latexRefs[] = 'ref_' . $references[$arrayIndex]['p_refer_id'];
} else {
$latexRefs[] = $index;
}
}
}
return '\parencite{' . implode(',', $latexRefs) . '}';
}
while (!empty($content)) { while (!empty($content)) {
$nextTag = $this->determineNextTag($content); $nextTag = $this->determineNextTag($content);
if ($nextTag === 'no') { if ($nextTag === 'no') {
// 无标签,转义特殊字符后直接拼接 // 无标签,转义特殊字符后直接拼接
$latex .= $this->escapeLatexSpecialChars($content); $latex .= $this->escapeLatexSpecialChars($content);
$content = ''; $content = '';
} else { }else {
$tagOpen = "<{$nextTag}>"; $tagOpen = "<{$nextTag}>";
$tagClose = "</{$nextTag}>"; $tagClose = "</{$nextTag}>";
$beginTag = strpos($content, $tagOpen); $beginTag = strpos($content, $tagOpen);
@@ -2337,12 +2300,54 @@ class Production extends Base
$endTag - $beginTag + strlen($tagClose) $endTag - $beginTag + strlen($tagClose)
); );
// 解析标签内容并应用样式
$style = []; //这里处理引用
$tagContentList = []; if (preg_match('/(?:<blue>)?\[(\d+(?:[-,]\d+)*)\](?:<\/blue>)?/', $tagWrappedContent, $matches)) {
$this->getTextRenderData($tagWrappedContent, $nextTag, $style, $tagContentList); // 去除匹配中的 <blue> 和 </blue>
// 拼接解析后的标签内容 $cleanedMatch = str_replace(['<blue>', '</blue>'], '', $matches[0]);
$latex .= implode('', $tagContentList); // 提取引用编号部分(去掉方括号)
$referencePart = trim($cleanedMatch, '[]');
// 分割逗号分隔的不同引用项
$parts = explode(',', $referencePart);
$latexRefs = [];
foreach ($parts as $part) {
// 判断是否是范围形式如 3-5
if (strpos($part, '-') !== false) {
list($start, $end) = explode('-', $part);
$start = intval($start);
$end = intval($end);
// 展开范围并逐个查找 ref_id
for ($i = $start; $i <= $end; $i++) {
$arrayIndex = $i;
if (isset($references[$arrayIndex]) && !empty($references[$arrayIndex]['p_refer_id'])) {
$latexRefs[] = 'ref_' . $references[$arrayIndex]['p_refer_id'];
} else {
$latexRefs[] = $i;
}
}
} else {
// 单个引用编号处理
$index = intval($part);
$arrayIndex = $index;
if (isset($references[$arrayIndex]) && !empty($references[$arrayIndex]['p_refer_id'])) {
$latexRefs[] = 'ref_' . $references[$arrayIndex]['p_refer_id'];
} else {
$latexRefs[] = $index;
}
}
}
$latex .= '\parencite{' . implode(',', $latexRefs) . '}';
}else{
// 解析标签内容并应用样式
$style = [];
$tagContentList = [];
$this->getTextRenderData($tagWrappedContent, $nextTag, $style, $tagContentList);
// 拼接解析后的标签内容
$latex .= implode('', $tagContentList);
}
// 剩余内容 // 剩余内容
$content = substr($content, $endTag + strlen($tagClose)); $content = substr($content, $endTag + strlen($tagClose));
@@ -2429,54 +2434,65 @@ class Production extends Base
* @param array $textRenderData 输出的LaTeX内容列表 * @param array $textRenderData 输出的LaTeX内容列表
*/ */
private function getTextRenderData($now, $tag, &$style, &$textRenderData) { private function getTextRenderData($now, $tag, &$style, &$textRenderData) {
// 应用当前标签的样式 if($tag === 'wmath'){
$this->addStyle($style, $tag); $pattern = '/data-latex="(.*?)"/';
// 提取标签内的内容(去掉前后标签) if (preg_match($pattern,$now, $matches1)) {
$tagOpen = "<{$tag}>"; $latexContent = $matches1[1];
$tagClose = "</{$tag}>"; // 将 $$ 转换为 $
$content = substr($now, strlen($tagOpen), strlen($now) - strlen($tagOpen) - strlen($tagClose)); $latexContent = str_replace('$$', '$', $latexContent);
$styledContent = $this->applyStyleToContent($latexContent, $style);
while (!empty($content)) {
$nextTag = $this->determineNextTag($content);
if ($nextTag === 'no') {
// 无子标签,应用样式并添加内容
$styledContent = $this->applyStyleToContent($this->escapeLatexSpecialChars($content), $style);
$textRenderData[] = $styledContent; $textRenderData[] = $styledContent;
$content = ''; }
} else { }else{
$subTagOpen = "<{$nextTag}>"; // 应用当前标签的样式
$subBeginTag = strpos($content, $subTagOpen); $this->addStyle($style, $tag);
// 提取标签内的内容(去掉前后标签)
$tagOpen = "<{$tag}>";
$tagClose = "</{$tag}>";
$content = substr($now, strlen($tagOpen), strlen($now) - strlen($tagOpen) - strlen($tagClose));
// 子标签前的内容 while (!empty($content)) {
if ($subBeginTag > 0) { $nextTag = $this->determineNextTag($content);
$prefixContent = $this->escapeLatexSpecialChars(substr($content, 0, $subBeginTag)); if ($nextTag === 'no') {
$styledPrefix = $this->applyStyleToContent($prefixContent, $style); // 无子标签,应用样式并添加内容
$textRenderData[] = $styledPrefix; $styledContent = $this->applyStyleToContent($this->escapeLatexSpecialChars($content), $style);
} $textRenderData[] = $styledContent;
// 找到子标签的最后闭合位置
$subEndTag = $this->getLastTabIndex($content, $nextTag);
if ($subEndTag === false) {
// 无闭合标签,保留原内容
$textRenderData[] = $this->escapeLatexSpecialChars(substr($content, $subBeginTag));
$content = ''; $content = '';
continue; } else {
$subTagOpen = "<{$nextTag}>";
$subBeginTag = strpos($content, $subTagOpen);
// 子标签前的内容
if ($subBeginTag > 0) {
$prefixContent = $this->escapeLatexSpecialChars(substr($content, 0, $subBeginTag));
$styledPrefix = $this->applyStyleToContent($prefixContent, $style);
$textRenderData[] = $styledPrefix;
}
// 找到子标签的最后闭合位置
$subEndTag = $this->getLastTabIndex($content, $nextTag);
if ($subEndTag === false) {
// 无闭合标签,保留原内容
$textRenderData[] = $this->escapeLatexSpecialChars(substr($content, $subBeginTag));
$content = '';
continue;
}
// 子标签包裹的内容
$subNow = substr(
$content,
$subBeginTag,
$subEndTag - $subBeginTag + strlen("</{$nextTag}>")
);
// 深拷贝样式(避免父级样式被修改)
$newStyle = $this->deepCopy($style);
// 递归解析子标签
$this->getTextRenderData($subNow, $nextTag, $newStyle, $textRenderData);
// 剩余内容
$content = substr($content, $subEndTag + strlen("</{$nextTag}>"));
} }
// 子标签包裹的内容
$subNow = substr(
$content,
$subBeginTag,
$subEndTag - $subBeginTag + strlen("</{$nextTag}>")
);
// 深拷贝样式(避免父级样式被修改)
$newStyle = $this->deepCopy($style);
// 递归解析子标签
$this->getTextRenderData($subNow, $nextTag, $newStyle, $textRenderData);
// 剩余内容
$content = substr($content, $subEndTag + strlen("</{$nextTag}>"));
} }
} }
} }