Ignore markdown when reading HTML comments

This commit is contained in:
luca0N! 2022-03-30 15:19:03 -03:00
parent 17f7b3ec8b
commit fc48dfc0a5
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 40 additions and 16 deletions

View File

@ -81,17 +81,19 @@ std::string make_html(std::filesystem::path const &path) {
tag_li = false, tag_li = false,
tag_ul = false, tag_ul = false,
tag_s = false, tag_s = false,
tag_comment = false,
newline = true, newline = true,
manualBreak = false, manualBreak = false,
// Used to ignore spaces at the beginning of header titles. // Used to ignore spaces at the beginning of header titles.
ignoreSpace = false; ignoreSpace = false;
unsigned short char_skip = 0;
// For counting sub-headers (h1, h2, h3, and so on) // For counting sub-headers (h1, h2, h3, and so on)
int tag_h = 0; int tag_h = 0;
while (fgets(buf, buflen, mdFile) != NULL) { while (fgets(buf, buflen, mdFile) != NULL) {
manualBreak = false; manualBreak = false;
if (tag_p && buf[0] == '\n') { if (!tag_comment && tag_p && buf[0] == '\n') {
// Empty newline; end paragraph. // Empty newline; end paragraph.
html += "</p>\n"; html += "</p>\n";
tag_p = false; tag_p = false;
@ -107,9 +109,13 @@ std::string make_html(std::filesystem::path const &path) {
// Read character by character // Read character by character
for (int x = 0; x < buflen; x++) { for (int x = 0; x < buflen; x++) {
if (char_skip > 0) {
char_skip--;
continue;
}
char c = buf[x]; char c = buf[x];
if (c == '\0') break; if (c == '\0') break;
else if (c == '\n') { else if (!tag_comment && c == '\n') {
// The next buffer iteration will hold the // The next buffer iteration will hold the
// first (buflen) bytes of the next new line. // first (buflen) bytes of the next new line.
newline = true; newline = true;
@ -131,16 +137,27 @@ std::string make_html(std::filesystem::path const &path) {
// Start paragraph if newline and no // Start paragraph if newline and no
// special characters were matched. // special characters were matched.
if (!tag_p && newline && x == 0 && if (!tag_comment && (!tag_p && newline && x == 0 &&
c != '#' && c != '-') c != '#' && c != '-'))
html += "<p>", html += "<p>",
tag_p = true; tag_p = true;
switch (c) { switch (c) {
case '<':
// Check for HTML comment
if (buf[x+1] == '!' &&
buf[x+2] == '-' &&
buf[x+3] == '-')
char_skip = 3,
tag_comment = true,
html += "<!--";
else
append(c);
break;
case '*': case '*':
// Bold check // Bold check
// Check whether this character has been escaped. // Check whether this character has been escaped.
if (buf[x-1] == '\\') { if (tag_comment || buf[x-1] == '\\') {
append(&c); append(&c);
break; break;
} }
@ -153,7 +170,7 @@ std::string make_html(std::filesystem::path const &path) {
// Check whether this character has // Check whether this character has
// been escaped. // been escaped.
if (buf[x-1] == '\\') { if (tag_comment || buf[x-1] == '\\') {
append(&c); append(&c);
break; break;
} }
@ -167,7 +184,7 @@ std::string make_html(std::filesystem::path const &path) {
// Headers must be declared at the // Headers must be declared at the
// beginning of a new line. Ignore it // beginning of a new line. Ignore it
// if this is not a new line. // if this is not a new line.
if (!newline) { if (tag_comment || !newline) {
append(&c); append(&c);
break; break;
} }
@ -213,11 +230,10 @@ std::string make_html(std::filesystem::path const &path) {
ignoreSpace = true; ignoreSpace = true;
break; break;
} }
break; break;
case '~': case '~':
// Escape character // Escape character
if (x > 0 && buf[x-1] == '\\') { if (tag_comment || (x > 0 && buf[x-1] == '\\')) {
append(c); append(c);
break; break;
} }
@ -230,8 +246,16 @@ std::string make_html(std::filesystem::path const &path) {
append(c); append(c);
break; break;
case '-': case '-':
if (x != 0) { if (tag_comment &&
append('-'); buf[x+1] == '-' &&
buf[x+2] == '>') {
tag_comment = false,
html += "-->",
char_skip = 2;
continue;
}
if (tag_comment || x != 0) {
append(c);
break; break;
} }
@ -243,7 +267,7 @@ std::string make_html(std::filesystem::path const &path) {
break; break;
case '[': case '[':
// Hyperlink text declaration has begun // Hyperlink text declaration has begun
if (tag_a != NONE) { if (tag_comment || tag_a != NONE) {
// Cannot add hyperlinks inside of hyperlinks; // Cannot add hyperlinks inside of hyperlinks;
append(&c); append(&c);
break; break;
@ -254,7 +278,7 @@ std::string make_html(std::filesystem::path const &path) {
break; break;
case ']': case ']':
// Hyperlink text declaration ended // Hyperlink text declaration ended
if (tag_a != READING_CONTENTS) { if (tag_comment || tag_a != READING_CONTENTS) {
// Ignore if not reading hyperlink. // Ignore if not reading hyperlink.
append(&c); append(&c);
break; break;
@ -263,7 +287,7 @@ std::string make_html(std::filesystem::path const &path) {
break; break;
case '(': case '(':
// Hyperlink address declaration has begun // Hyperlink address declaration has begun
if (tag_a != EXPECTING_URL) { if (tag_comment || tag_a != EXPECTING_URL) {
append(&c); append(&c);
break; break;
} }
@ -271,7 +295,7 @@ std::string make_html(std::filesystem::path const &path) {
break; break;
case ')': case ')':
// Hyperlink address declaration ended // Hyperlink address declaration ended
if (tag_a != READING_URL) { if (tag_comment || tag_a != READING_URL) {
append(&c); append(&c);
break; break;
} }
@ -283,7 +307,7 @@ std::string make_html(std::filesystem::path const &path) {
append("</a>"); append("</a>");
break; break;
case ' ': case ' ':
if (ignoreSpace) { if (!tag_comment && ignoreSpace) {
ignoreSpace = false; ignoreSpace = false;
break; break;
} else append(" "); } else append(" ");