157 lines
4.5 KiB
C++
157 lines
4.5 KiB
C++
#include "ctai_parsestring.h"
|
||
|
||
namespace ctai_parse {
|
||
|
||
QString parseMathInline(const QString& text) {
|
||
QString result = text;
|
||
QRegularExpression regex("\\$([^\\$]+)\\$");
|
||
result.replace(regex, "<span class='math-inline'>\\1</span>");
|
||
return result;
|
||
}
|
||
|
||
QString parseMathBlock(const QString& text) {
|
||
QString result = text;
|
||
QRegularExpression regex("\\$\\$([^\\$]+)\\$\\$");
|
||
result.replace(regex, "<div class='math-block'>\\1</div>");
|
||
return result;
|
||
}
|
||
|
||
QString parseMathSymbols(const QString& text) {
|
||
QString result = text;
|
||
// 希腊字母
|
||
result.replace("\\alpha", "α");
|
||
result.replace("\\beta", "β");
|
||
result.replace("\\gamma", "γ");
|
||
result.replace("\\delta", "δ");
|
||
result.replace("\\theta", "θ");
|
||
result.replace("\\sigma", "σ");
|
||
|
||
// 数学运算符
|
||
result.replace("\\sum", "∑");
|
||
result.replace("\\prod", "∏");
|
||
result.replace("\\int", "∫");
|
||
result.replace("\\times", "×");
|
||
result.replace("\\div", "÷");
|
||
result.replace("\\pm", "±");
|
||
|
||
// 比较符号
|
||
result.replace("\\leq", "≤");
|
||
result.replace("\\geq", "≥");
|
||
result.replace("\\neq", "≠");
|
||
result.replace("\\approx", "≈");
|
||
|
||
// 常用常数
|
||
result.replace("\\infty", "∞");
|
||
result.replace("\\pi", "π");
|
||
|
||
return result;
|
||
}
|
||
|
||
QString parseSubSuper(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("\\^\\{([^\\}]+)\\}"), "<sup>\\1</sup>");
|
||
result.replace(QRegularExpression("\\_\\{([^\\}]+)\\}"), "<sub>\\1</sub>");
|
||
return result;
|
||
}
|
||
|
||
QString parseFraction(const QString& text) {
|
||
QString result = text;
|
||
QRegularExpression regex("\\\\frac\\{([^\\}]+)\\}\\{([^\\}]+)\\}");
|
||
result.replace(regex,
|
||
"<span class='fraction'>"
|
||
"<span class='numerator'>\\1</span>"
|
||
"<span class='denominator'>\\2</span>"
|
||
"</span>");
|
||
return result;
|
||
}
|
||
|
||
QString parseCodeBlockPython(const QString& text) {
|
||
QString result = text;
|
||
QRegularExpression regex("```python\n([^`]+)```");
|
||
result.replace(regex,
|
||
"<pre class='python-code'>"
|
||
"<code class='language-python'>\\1</code>"
|
||
"</pre>");
|
||
return result;
|
||
}
|
||
|
||
QString parseCodeBlock(const QString& text) {
|
||
QString result = text;
|
||
QRegularExpression regex("```([^\\n]*)\n([^`]+)```");
|
||
result.replace(regex,
|
||
"<pre class='code-block'>"
|
||
"<code class='language-\\1'>\\2</code>"
|
||
"</pre>");
|
||
return result;
|
||
}
|
||
|
||
QString parseInlineCode(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("`([^`]+)`"), "<code>\\1</code>");
|
||
return result;
|
||
}
|
||
|
||
QString parseHeaders(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("^# (.+)$", QRegularExpression::MultilineOption),
|
||
"<h1>\\1</h1>");
|
||
result.replace(QRegularExpression("^## (.+)$", QRegularExpression::MultilineOption),
|
||
"<h2>\\1</h2>");
|
||
result.replace(QRegularExpression("^### (.+)$", QRegularExpression::MultilineOption),
|
||
"<h3>\\1</h3>");
|
||
return result;
|
||
}
|
||
|
||
QString parseEmphasis(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("\\*\\*([^\\*]+)\\*\\*"), "<strong>\\1</strong>");
|
||
result.replace(QRegularExpression("\\*([^\\*]+)\\*"), "<em>\\1</em>");
|
||
return result;
|
||
}
|
||
|
||
QString parseLinks(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("\\[([^\\]]+)\\]\\(([^\\)]+)\\)"),
|
||
"<a href=\"\\2\">\\1</a>");
|
||
return result;
|
||
}
|
||
|
||
QString parseLists(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("^- (.+)$", QRegularExpression::MultilineOption),
|
||
"<li>\\1</li>");
|
||
return result;
|
||
}
|
||
|
||
QString parseLineBreaks(const QString& text) {
|
||
QString result = text;
|
||
result.replace(QRegularExpression("\n"), "<br>");
|
||
return result;
|
||
}
|
||
|
||
QString parseMarkdown(const QString& text) {
|
||
QString result = text;
|
||
|
||
// 处理数学相关
|
||
result = parseMathBlock(result);
|
||
result = parseMathInline(result);
|
||
result = parseMathSymbols(result);
|
||
result = parseSubSuper(result);
|
||
result = parseFraction(result);
|
||
|
||
// 处理代码块
|
||
result = parseCodeBlockPython(result);
|
||
result = parseCodeBlock(result);
|
||
result = parseInlineCode(result);
|
||
|
||
// 处理Markdown基本语法
|
||
result = parseHeaders(result);
|
||
result = parseEmphasis(result);
|
||
result = parseLinks(result);
|
||
result = parseLists(result);
|
||
result = parseLineBreaks(result);
|
||
|
||
return result;
|
||
}
|
||
|
||
} // namespace ctai_parse
|