1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
#include "markdown_translator.hpp"
#include <regex>
#include <iostream>
#include <algorithm>
MarkdownTranslator::MarkdownTranslator() : title("Carbon") {
}
MarkdownTranslator::~MarkdownTranslator() {
}
void MarkdownTranslator::processMetadata(const std::vector<std::string>& lines){
// format of keys -> key: value
for(std::string currentLine : lines){
size_t colonPos = currentLine.find(':');
if (colonPos != std::string::npos) {
std::string key = currentLine.substr(0, colonPos);
std::string value = currentLine.substr(colonPos + 1);
value.erase(0, value.find_first_not_of(" \t"));
if (key == "title") {
title = value;
}
}
}
}
std::string MarkdownTranslator::translate(const std::string& markdownContent, const std::string& cssPath) {
std::stringstream htmlOutput;
std::stringstream markdownStream(markdownContent);
std::string line;
std::vector<std::string> headers;
std::string currentLine;
bool metadataExists{false};
std::vector<std::string> metadataLines;
while (std::getline(markdownStream, currentLine)) {
if(currentLine == "---"){
if(metadataExists){
break;
} else {
metadataExists = true;
continue;
}
}
if(!metadataExists){
break;
}
metadataLines.push_back(currentLine);
}
processMetadata(metadataLines);
while (std::getline(markdownStream, currentLine)) {
std::regex headerRegex("^(#{1,3})\\s+(.*)$");
std::smatch matches;
if (std::regex_match(currentLine, matches, headerRegex)) {
int level = matches[1].length();
std::string content = matches[2];
if (level <= 3) {
headers.push_back(std::to_string(level) + ":" + content);
}
}
}
markdownStream.clear();
markdownStream.str(markdownContent);
// Start with basic HTML structure
htmlOutput << "<!DOCTYPE html>\n";
htmlOutput << "<html lang=\"en\">\n";
htmlOutput << "<head>\n";
htmlOutput << " <meta charset=\"UTF-8\">\n";
htmlOutput << " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
htmlOutput << " <title>" + title + "</title>\n";
htmlOutput << " <link rel=\"stylesheet\" href=\"" << cssPath << "\">\n";
htmlOutput << "</head>\n";
htmlOutput << "<body>\n";
// Add navigation sidebar
generateSideBar(htmlOutput, headers);
// Main content container
htmlOutput << " <div class=\"main-content\">\n";
htmlOutput << " <div class=\"container\">\n";
// Process each line of markdown
// State of current parse
bool inFigureBlock{false};
std::vector<std::string> figureLines;
while (std::getline(markdownStream, line)) {
if(line.find(":::figure") == 0){
inFigureBlock = true;
figureLines.clear();
continue;
}
if(inFigureBlock && line == ":::"){
inFigureBlock = false;
htmlOutput << " " << processFigureBlock(figureLines) << "\n";
continue;
}
else if(inFigureBlock){
figureLines.push_back(line);
continue;
}
htmlOutput << " " << processLine(line);
}
// Add article meta information section
htmlOutput << " <div class=\"article-meta\">\n";
htmlOutput << " <p>Last updated: " << getCurrentDateTime() << "</p>\n";
htmlOutput << " </div>\n";
htmlOutput << " </div>\n";
htmlOutput << " </div>\n";
htmlOutput << "</body>\n";
htmlOutput << "</html>\n";
return htmlOutput.str();
}
void MarkdownTranslator::generateSideBar(std::stringstream& output, const std::vector<std::string>& headers) {
output << " <div class=\"nav-sidebar\">\n";
output << " <div class=\"nav-logo\">\n";
output << " <h3>" + title + "</h3>\n";
output << " </div>\n";
output << " <ul class=\"nav-menu\">\n";
output << " <h4>Table of Contents</h4>\n";
output << " <li><a href=\"index.html\">Home</a></li>\n";
output << " <li><a href=\"#\">Recent Changes</a></li>\n";
output << " <li><a href=\"#\">Random Page</a></li>\n";
output << " <ul class=\"nav-submenu\">\n";
// Generate navigation from headers
for (const auto& header : headers) {
size_t separatorPos = header.find(':');
if (separatorPos != std::string::npos) {
int level = std::stoi(header.substr(0, separatorPos));
std::string content = header.substr(separatorPos + 1);
// Create anchor ID from header content
std::string anchorId = createAnchorId(content);
// Add indentation based on header level
std::string indentation = "";
for (int i = 1; i < level; ++i) {
indentation += " ";
}
output << " " << indentation << "<li><a href=\"#" << anchorId << "\">" << content << "</a></li>\n";
}
}
output << " </ul>\n";
output << " </ul>\n";
output << " </div>\n";
}
std::string MarkdownTranslator::createAnchorId(const std::string& text) {
std::string id = text;
// Convert to lowercase
std::transform(id.begin(), id.end(), id.begin(), ::tolower);
// Replace spaces with hyphens
std::replace(id.begin(), id.end(), ' ', '-');
// Remove any non-alphanumeric characters except hyphens
id.erase(
std::remove_if(
id.begin(),
id.end(),
[](char c) { return !(std::isalnum(c) || c == '-'); }
),
id.end()
);
return id;
}
std::string MarkdownTranslator::getCurrentDateTime() {
std::time_t now = std::time(nullptr);
std::tm* localTime = std::localtime(&now);
char buffer[80];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
return std::string(buffer);
}
std::string MarkdownTranslator::processLine(const std::string& line) {
std::string processed = line;
processed = processHeaders(processed);
if (processed == line) {
processed = processBold(processed);
processed = processItalic(processed);
processed = processLinks(processed);
if (!processed.empty() && processed[0] != '<') {
processed = processParagraph(processed);
}
}
return processed + "\n";
}
std::string MarkdownTranslator::processHeaders(const std::string& line) {
// Check for H1-H6
std::regex headerRegex("^(#{1,6})\\s+(.*)$");
std::smatch matches;
if (std::regex_match(line, matches, headerRegex)) {
int level = matches[1].length();
std::string content = matches[2];
std::string anchorId = createAnchorId(content);
return "<h" + std::to_string(level) + " id=\"" + anchorId + "\">" + content + "</h" + std::to_string(level) + ">";
}
return line;
}
std::string MarkdownTranslator::processBold(const std::string& text) {
// Replace **text** or __text__ with <strong>text</strong>
std::string result = text;
std::regex boldRegex("\\*\\*([^\\*]+)\\*\\*|__([^_]+)__");
result = std::regex_replace(result, boldRegex, "<strong>$1$2</strong>");
return result;
}
std::string MarkdownTranslator::processItalic(const std::string& text) {
// Replace *text* or _text_ with <em>text</em>
std::string result = text;
std::regex italicRegex("\\*([^\\*]+)\\*|_([^_]+)_");
result = std::regex_replace(result, italicRegex, "<em>$1$2</em>");
return result;
}
std::string MarkdownTranslator::processLinks(const std::string& text) {
// Replace [text](url) with <a href="url">text</a>
std::string result = text;
std::regex linkRegex("\\[([^\\]]+)\\]\\(([^\\)]+)\\)");
result = std::regex_replace(result, linkRegex, "<a href=\"$2\">$1</a>");
return result;
}
std::string MarkdownTranslator::processParagraph(const std::string& text) {
if (text.empty()) return "";
return "<p>" + text + "</p>";
}
std::string MarkdownTranslator::processSingleFigure(const std::string& text) {
// Extract image details using regex
std::regex imageRegex("!\\[(.*?)\\]\\(([^\\s\"]+)(\\s+\"(.*?)\")?\\)");
std::smatch matches;
if (std::regex_search(text, matches, imageRegex)) {
std::string alt = matches[1].str();
std::string src = matches[2].str();
std::string title = matches.size() > 4 && matches[4].matched ? " title=\"" + matches[4].str() + "\"" : "";
return "<img src=\"" + src + "\" alt=\"" + alt + "\"" + title + ">";
}
return text;
}
std::string MarkdownTranslator::processFigureBlock(const std::vector<std::string>& lines){
std::stringstream html;
std::string imageHtml;
std::string caption;
html << "<figure class=\"custom-figure\">\n";
// Process each line in the figure block
for (const auto& line : lines) {
if (line.find("![") == 0) {
// Process the image
imageHtml = processSingleFigure(line);
html << " " << imageHtml << "\n";
} else if (line.find("Caption:") == 0) {
// Extract caption
caption = line.substr(8);
html << " <figcaption>" << caption << "</figcaption>\n";
} else if (!line.empty()) {
// Process any other content
html << " <p>" << line << "</p>\n";
}
}
html << "</figure>";
return html.str();
}
|