From fc48dfc0a557b6a66178b074907010f72a4b6a53 Mon Sep 17 00:00:00 2001 From: luca0N! Date: Wed, 30 Mar 2022 15:19:03 -0300 Subject: [PATCH] Ignore markdown when reading HTML comments --- src/MarkdownParser.cxx | 56 ++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/MarkdownParser.cxx b/src/MarkdownParser.cxx index ea633a2..8d19a2b 100644 --- a/src/MarkdownParser.cxx +++ b/src/MarkdownParser.cxx @@ -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 += "

\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 += "

", 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 += "", + 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(""); break; case ' ': - if (ignoreSpace) { + if (!tag_comment && ignoreSpace) { ignoreSpace = false; break; } else append(" ");