mirror of
https://github.com/progsource/maddy.git
synced 2026-03-25 16:00:39 +01:00
* Add regex for title text version of links Original parser matched: [name](http:://link) Add match for: [name](http:://link "title text") * Add tests and improve regex's * Several new tests in test_maddy_linkparser.cpp (Some with paths for future improvement, and one to ensure an overzealous future update doesn't disallow actually-used special characters like o-umlaut). * URLs now ignore leading/trailing spaces. * URLs now don't match on internal spaces or quotes. * Small grammar fix in CONTRIBUTING.md * Updated changelog.
60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
/*
|
|
* This project is licensed under the MIT license. For more information see the
|
|
* LICENSE file.
|
|
*/
|
|
#pragma once
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
#include <regex>
|
|
#include <string>
|
|
|
|
#include "maddy/lineparser.h"
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
namespace maddy {
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
/**
|
|
* LinkParser
|
|
*
|
|
* Has to be used after the `ImageParser`.
|
|
*
|
|
* @class
|
|
*/
|
|
class LinkParser : public LineParser
|
|
{
|
|
public:
|
|
/**
|
|
* Parse
|
|
*
|
|
* From Markdown: `[text](http://example.com)`
|
|
*
|
|
* To HTML: `<a href="http://example.com">text</a>`
|
|
*
|
|
* @method
|
|
* @param {std::string&} line The line to interpret
|
|
* @return {void}
|
|
*/
|
|
void Parse(std::string& line) override
|
|
{
|
|
// Match [name](http:://link "title text")
|
|
// NOTE: the 'no quote' bit at the beginning (^") is a hack for now:
|
|
// there should eventually be something that replaces it with '%22'.
|
|
static std::regex re(R"(\[([^\]]*)\]\( *([^)^ ^"]*) *\"([^\"]*)\" *\))");
|
|
static std::string replacement = "<a href=\"$2\" title=\"$3\">$1</a>";
|
|
line = std::regex_replace(line, re, replacement);
|
|
|
|
// Match [name](http:://link)
|
|
static std::regex re2(R"(\[([^\]]*)\]\( *([^)^ ^"]*) *\))");
|
|
static std::string replacement2 = "<a href=\"$2\">$1</a>";
|
|
line = std::regex_replace(line, re2, replacement2);
|
|
}
|
|
}; // class LinkParser
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
} // namespace maddy
|