|
| 1 | +diff --git a/cpp/enrmrkd/enrmrkd.h b/cpp/enrmrkd/enrmrkd.h |
| 2 | +--- a/cpp/enrmrkd/enrmrkd.h |
| 3 | ++++ b/cpp/enrmrkd/enrmrkd.h |
| 4 | +@@ -244,7 +244,10 @@ |
| 5 | + |
| 6 | + /* Text is inside an equation. This is processed the same way as inlined code |
| 7 | + * spans (`code`). */ |
| 8 | +- ENRMRKD_TEXT_LATEXMATH |
| 9 | ++ ENRMRKD_TEXT_LATEXMATH, |
| 10 | ++ |
| 11 | ++ /* An escaped punctuation character, after syntax and reference resolution. */ |
| 12 | ++ ENRMRKD_TEXT_ESCAPED |
| 13 | + } ENRMRKD_TEXTTYPE; |
| 14 | + |
| 15 | + |
| 16 | +diff --git a/cpp/enrmrkd/enrmrkd.c b/cpp/enrmrkd/enrmrkd.c |
| 17 | +--- a/cpp/enrmrkd/enrmrkd.c |
| 18 | ++++ b/cpp/enrmrkd/enrmrkd.c |
| 19 | +@@ -4785,7 +4785,8 @@ |
| 20 | + if(ISNEWLINE(mark->beg+1)) |
| 21 | + enforce_hardbreak = 1; |
| 22 | + else |
| 23 | +- ENRMRKD_TEXT(text_type, STR(mark->beg+1), 1); |
| 24 | ++ ENRMRKD_TEXT(text_type == ENRMRKD_TEXT_NORMAL ? ENRMRKD_TEXT_ESCAPED : text_type, |
| 25 | ++ STR(mark->beg+1), 1); |
| 26 | + break; |
| 27 | + |
| 28 | + case ' ': /* Non-trivial space. */ |
| 29 | +diff --git a/cpp/parser/MD4CParser.cpp b/cpp/parser/MD4CParser.cpp |
| 30 | +--- a/cpp/parser/MD4CParser.cpp |
| 31 | ++++ b/cpp/parser/MD4CParser.cpp |
| 32 | +@@ -1,11 +1,13 @@ |
| 33 | + #include "MD4CParser.hpp" |
| 34 | + #include "enrmrkd.h" |
| 35 | ++#include <algorithm> |
| 36 | + #include <cctype> |
| 37 | + #include <cstdlib> |
| 38 | + extern "C" { |
| 39 | + #include "enrmrkd-entity.h" |
| 40 | + } |
| 41 | + #include <cstring> |
| 42 | ++#include <string_view> |
| 43 | + #include <vector> |
| 44 | + |
| 45 | + namespace Markdown { |
| 46 | +@@ -71,6 +73,30 @@ |
| 47 | + appendCodepoint(output, codepoint); |
| 48 | + } |
| 49 | + |
| 50 | ++bool isPathWhitespace(const std::string &text, size_t at) { |
| 51 | ++ const auto c = static_cast<unsigned char>(text[at]); |
| 52 | ++ if (c < 128) |
| 53 | ++ return c == ' ' || (c >= '\t' && c <= '\r'); |
| 54 | ++ if (c != 0xC2 && c != 0xE1 && c != 0xE2 && c != 0xE3 && c != 0xEF) |
| 55 | ++ return false; |
| 56 | ++ // Match JavaScript's \s, including non-ASCII spaces in decoded prose. |
| 57 | ++ constexpr std::string_view spaces[] = { |
| 58 | ++ "\u00a0", "\u1680", "\u2000", "\u2001", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", |
| 59 | ++ "\u2008", "\u2009", "\u200a", "\u2028", "\u2029", "\u202f", "\u205f", "\u3000", "\ufeff"}; |
| 60 | ++ for (const auto space : spaces) |
| 61 | ++ if (text.compare(at, space.size(), space) == 0) |
| 62 | ++ return true; |
| 63 | ++ return false; |
| 64 | ++} |
| 65 | ++ |
| 66 | ++bool isDriveLetter(unsigned char c) { |
| 67 | ++ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); |
| 68 | ++} |
| 69 | ++ |
| 70 | ++bool isWordCharacter(unsigned char c) { |
| 71 | ++ return isDriveLetter(c) || (c >= '0' && c <= '9') || c == '_'; |
| 72 | ++} |
| 73 | ++ |
| 74 | + struct HtmlTag { |
| 75 | + std::string name; |
| 76 | + std::unordered_map<std::string, std::string> attributes; |
| 77 | +@@ -84,6 +110,8 @@ |
| 78 | + std::shared_ptr<MarkdownASTNode> root; |
| 79 | + std::vector<std::shared_ptr<MarkdownASTNode>> nodeStack; |
| 80 | + std::string currentText; |
| 81 | ++ size_t pathScanned = 0; |
| 82 | ++ bool inWindowsPath = false; |
| 83 | + const char *inputText = nullptr; |
| 84 | + bool inHtmlBlock = false; |
| 85 | + std::string htmlBlockContent; |
| 86 | +@@ -108,6 +136,8 @@ |
| 87 | + nodeStack.reserve(std::min(estimatedDepth, static_cast<size_t>(128))); |
| 88 | + nodeStack.push_back(root); |
| 89 | + currentText.clear(); |
| 90 | ++ pathScanned = 0; |
| 91 | ++ inWindowsPath = false; |
| 92 | + currentText.reserve(256); |
| 93 | + inHtmlBlock = false; |
| 94 | + htmlBlockContent.clear(); |
| 95 | +@@ -120,6 +150,26 @@ |
| 96 | + nodeStack.back()->addChild(std::move(textNode)); |
| 97 | + currentText.clear(); |
| 98 | + } |
| 99 | ++ pathScanned = 0; |
| 100 | ++ inWindowsPath = false; |
| 101 | ++ } |
| 102 | ++ |
| 103 | ++ bool endsInWindowsPath() { |
| 104 | ++ // Inspect only appended text, so many escapes cannot rescan a growing paragraph. |
| 105 | ++ for (; pathScanned < currentText.size(); ++pathScanned) { |
| 106 | ++ const size_t at = pathScanned; |
| 107 | ++ if (isPathWhitespace(currentText, at)) { |
| 108 | ++ inWindowsPath = false; |
| 109 | ++ } else if (!inWindowsPath && (currentText[at] == '/' || currentText[at] == '\\') && |
| 110 | ++ at >= 2 && currentText[at - 1] == ':' && isDriveLetter(currentText[at - 2]) && |
| 111 | ++ (at == 2 || !isWordCharacter(currentText[at - 3]))) { |
| 112 | ++ inWindowsPath = true; |
| 113 | ++ } |
| 114 | ++ } |
| 115 | ++ const size_t end = currentText.size(); |
| 116 | ++ return inWindowsPath || |
| 117 | ++ (end >= 2 && currentText[end - 1] == ':' && isDriveLetter(currentText[end - 2]) && |
| 118 | ++ (end == 2 || !isWordCharacter(currentText[end - 3]))); |
| 119 | + } |
| 120 | + |
| 121 | + void pushNode(std::shared_ptr<MarkdownASTNode> node) { |
| 122 | +@@ -504,6 +554,15 @@ |
| 123 | + |
| 124 | + if (type == ENRMRKD_TEXT_ENTITY) { |
| 125 | + appendCharacterReference(impl->currentText, text, size); |
| 126 | ++ return 0; |
| 127 | ++ } |
| 128 | ++ |
| 129 | ++ if (type == ENRMRKD_TEXT_ESCAPED) { |
| 130 | ++ const bool inImage = std::any_of(impl->nodeStack.begin(), impl->nodeStack.end(), |
| 131 | ++ [](const auto &node) { return node->type == NodeType::Image; }); |
| 132 | ++ if (!inImage && impl->endsInWindowsPath()) |
| 133 | ++ impl->currentText += '\\'; |
| 134 | ++ impl->currentText.append(text, size); |
| 135 | + return 0; |
| 136 | + } |
| 137 | + |
0 commit comments