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