🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
MarkdownANSI.hpp
浏览该文件的文档.
1
22
23#pragma once
24
25#include "AstGlobal.h"
26#include "AstUtil/StringView.hpp"
27#include "MarkdownSax.hpp"
28#include <string>
29#include <vector>
30
31AST_NAMESPACE_BEGIN
32
48class AST_UTIL_API MarkdownANSI final : public MarkdownSax
49{
50public:
51 MarkdownANSI() = default;
52 ~MarkdownANSI() override = default;
53
54 // 文档生命周期
55 void startDocument() override;
56 void endDocument() override;
57
58 // 块级元素
59 void startHeading(int level) override;
60 void endHeading(int level) override;
61
62 void startParagraph() override;
63 void endParagraph() override;
64
65 void startCodeBlock(StringView language) override;
66 void codeLine(StringView line) override; // 代码行
67 void endCodeBlock() override;
68
69 void startList(bool ordered) override; // true: 有序, false: 无序
70 void startListItem() override;
71 void endListItem() override;
72 void endList() override;
73
74 void startBlockquote() override;
75 void endBlockquote() override;
76
77 void horizontalRule() override;
78
79 // void newline(bool force = false) override;
80
81 // 表格
82 void startTable() override;
83 void startTableHead() override;
84 void endTableHead() override;
85 void startTableBody() override;
86 void endTableBody() override;
87 void startTableRow() override;
88 void endTableRow() override;
89 void startTableCell(ETableAlign align) override;
90 void endTableCell() override;
91 void endTable() override;
92
93 // 行内元素
94 void text(StringView txt) override;
95 void startEmphasis() override; // 斜体
96 void endEmphasis() override;
97 void startStrong() override; // 粗体
98 void endStrong() override;
99 void startDelete() override; // 删除线
100 void endDelete() override;
101 void codeSpan(StringView code) override;
102 void startLink(StringView url) override;
103 void endLink() override;
104 void image(StringView alt, StringView url) override;
105
106public:
108 std::string& output() { return output_; }
109 const std::string& output() const { return output_; }
110 void clearOutput() { output_.clear(); }
111
113 void setTableTruncate(bool enable) { truncateCells_ = enable; }
114 bool isTableTruncate() const { return truncateCells_; }
115
116private:
117 // ---- ANSI 辅助 ----
118 void emitActiveStyles();
119 void emitStyleTransition(int oldStyles, int newStyles);
120 void emitBlockPrefix();
121 void outputCodeBlockGutter(int lineNum);
122
124 std::string& curBuf() { return inCell_ ? cellContent_ : output_; }
125
126 A_DISABLE_COPY(MarkdownANSI);
127
128 // ---- 行内样式位掩码 ----
129 enum InlineStyle : int
130 {
131 STYLE_NONE = 0,
132 STYLE_BOLD = 1 << 0,
133 STYLE_ITALIC = 1 << 1,
134 STYLE_LINK = 1 << 2,
135 STYLE_DELETE = 1 << 3,
136 };
137
138 // ---- 块级元素帧(仅用于块引用嵌套跟踪) ----
139 struct BlockFrame
140 {
141 enum Type { QUOTE };
142 BlockFrame() = default;
143 BlockFrame(Type type) : type(type) {}
144 Type type = QUOTE;
145 };
146
147 // ---- 输出缓冲 ----
148 std::string output_;
149
150 // ---- 行内状态 ----
151 int activeStyles_ = STYLE_NONE;
152
153 // ---- 块级状态 ----
154 std::vector<BlockFrame> blockStack_;
155
157 struct ListFrame
158 {
159 bool ordered;
160 int itemNumber;
161 };
162 std::vector<ListFrame> listStack_;
163
164 bool inCodeBlock_ = false;
165 int codeLineNumber_ = 0;
166
167 // ---- 表格缓冲 ----
168 struct CellData
169 {
170 std::string rendered;
171 int width = 0;
172 ETableAlign align = ETableAlign::eDefault;
173 };
174 struct RowData
175 {
176 std::vector<CellData> cells;
177 bool isHeader = false;
178 };
179
180 bool inTable_ = false;
181 bool inTableHead_ = false;
182 bool inCell_ = false;
184 std::string cellContent_;
185 bool truncateCells_ = true;
186 RowData currentRow_;
187 std::vector<RowData> tableRows_;
188
189 // ---- 链接状态 ----
190 std::string linkUrl_;
191};
192
195AST_NAMESPACE_END
Markdown SAX → ANSI 终端转义序列渲染器
定义 MarkdownANSI.hpp:49
void setTableTruncate(bool enable)
设置是否截断超出列宽的表格单元格内容(默认开启)
定义 MarkdownANSI.hpp:113
std::string & output()
获取输出 ANSI 字符串
定义 MarkdownANSI.hpp:108
定义 MarkdownSax.hpp:45
virtual void endTableHead()
表头区域结束
定义 MarkdownSax.hpp:86
virtual void startHeading(int level)=0
virtual void endTableBody()
表体区域结束
定义 MarkdownSax.hpp:90
virtual void endTableRow()
表格行结束
定义 MarkdownSax.hpp:94
virtual void startTableRow()
表格行开始
定义 MarkdownSax.hpp:92
virtual void startTableBody()
表体区域开始
定义 MarkdownSax.hpp:88
virtual void endTableCell()
表格单元格结束
定义 MarkdownSax.hpp:99
virtual void endTable()
表格结束
定义 MarkdownSax.hpp:101
virtual void startTableHead()
表头区域开始
定义 MarkdownSax.hpp:84
virtual void startTableCell(ETableAlign align)
表格单元格开始
定义 MarkdownSax.hpp:97
virtual void startTable()
表格开始
定义 MarkdownSax.hpp:82
ETableAlign
表格列对齐方式
定义 MarkdownSax.hpp:36
@ eDefault
默认(无显式对齐)