Changes
diff --git a/src/parser.cpp b/src/parser.cpp
index 33084db..6151b60 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -95,6 +95,10 @@ public:
{
continue;
}
+ if(handleAutolink())
+ {
+ continue;
+ }
if(handleEmphasis())
{
continue;
@@ -465,8 +469,60 @@ private:
input32_.substr(url_start, url_end - url_start), url_end + 1};
}
+ // Recognizes an absolute URI enclosed in angle brackets.
+ bool handleAutolink()
+ {
+ if(input32_[pos_] != '<')
+ {
+ return false;
+ }
+
+ size_t start = pos_ + 1;
+ size_t end = start;
+ while(end < input32_.length() && input32_[end] != '>')
+ {
+ char32_t cp = input32_[end];
+ if(cp <= 0x20 || cp == '<' ||
+ una::codepoint::is_whitespace(cp))
+ {
+ return false;
+ }
+ end++;
+ }
+ if(end == input32_.length())
+ {
+ return false;
+ }
+
+ size_t colon = input32_.find(':', start);
+ if(colon == std::u32string::npos || colon >= end ||
+ colon - start < 2 || colon - start > 32)
+ {
+ return false;
+ }
+ for(size_t i = start; i < colon; i++)
+ {
+ char32_t cp = input32_[i];
+ bool letter = (cp >= 'a' && cp <= 'z') ||
+ (cp >= 'A' && cp <= 'Z');
+ bool suffix = (cp >= '0' && cp <= '9') || cp == '+' ||
+ cp == '-' || cp == '.';
+ if(!letter && (i == start || !suffix))
+ {
+ return false;
+ }
+ }
+
+ auto url = input32_.substr(start, end - start);
+ flushText();
+ addLinkLikeMacro("link", url, url, false);
+ pos_ = end + 1;
+ return true;
+ }
+
void addLinkLikeMacro(const std::string& name, const std::u32string& url,
- const std::u32string& label)
+ const std::u32string& label,
+ bool parse_label = true)
{
Macro macro;
macro.name = name;
@@ -476,13 +532,21 @@ private:
group1.addChild(std::make_unique<Node>(Text{una::utf32to8(url)}));
macro.arguments.push_back(std::make_unique<Node>(std::move(group1)));
- // Arg 2: Label (parsed)
+ // Arg 2: Label, with autolink text kept verbatim.
Group group2;
- auto sub = Parser::parse(una::utf32to8(label), prefix_markups_,
- delimited_markups_);
- for(auto& n : sub)
+ if(!parse_label)
{
- group2.addChild(std::move(n));
+ group2.addChild(
+ std::make_unique<Node>(Text{una::utf32to8(label)}));
+ }
+ else
+ {
+ auto sub = Parser::parse(una::utf32to8(label), prefix_markups_,
+ delimited_markups_);
+ for(auto& n : sub)
+ {
+ group2.addChild(std::move(n));
+ }
}
macro.arguments.push_back(std::make_unique<Node>(std::move(group2)));
@@ -552,4 +616,4 @@ Parser::parse(const std::string& input,
return parser.parse();
}
-} // namespace macrodown
\ No newline at end of file
+} // namespace macrodown
diff --git a/tests/test_macrodown.cpp b/tests/test_macrodown.cpp
index 3acad7a..d8ad11d 100644
--- a/tests/test_macrodown.cpp
+++ b/tests/test_macrodown.cpp
@@ -178,3 +178,35 @@ TEST(MacroDownTest, URLsAreVerbatim)
"<p><a href=\"http://its_a_test.com/\">aaa</a></p>\n";
EXPECT_EQ(md.render(*md.parse(input)), expected);
}
+
+TEST(MacroDownTest, AngleBracketAutolinks)
+{
+ MacroDown md;
+ for(const std::string url : {"https://example.com/",
+ "protocol://some.url/",
+ "git+ssh://example.com/repo",
+ "mailto:user@example.com",
+ "https://its_a_test.com/*path*/%name"})
+ {
+ SCOPED_TRACE(url);
+ EXPECT_EQ(md.render(*md.parse("See <" + url + ">.")),
+ "<p>See <a href=\"" + url + "\">" + url +
+ "</a>.</p>\n");
+ }
+}
+
+TEST(MacroDownTest, InvalidAutolinksRemainText)
+{
+ MacroDown md;
+ for(const std::string input : {"<example.com>", "<1http://a.com>",
+ "<https://a b>", "<https://a.com",
+ "<div>", "<https://a\nb>"})
+ {
+ SCOPED_TRACE(input);
+ EXPECT_EQ(md.render(*md.parse(input)), "<p>" + input + "</p>\n");
+ }
+ EXPECT_EQ(md.render(*md.parse(R"(\<https://example.com/>)")),
+ "<p><https://example.com/></p>\n");
+ EXPECT_EQ(md.render(*md.parse("`<https://example.com/>`")),
+ "<p><code><https://example.com/></code></p>\n");
+}