aboutsummaryrefslogtreecommitdiff
path: root/inc/maddy/linkparser.h
blob: e382f211bafc93d58fe4e5710531f6ab9a7abfbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * This project is licensed under the MIT license. For more information see the
 * LICENSE file.
 */
#pragma once

// -----------------------------------------------------------------------------

#include <string>
#include <regex>

#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
  {
    static std::regex re("\\[([^\\]]*)\\]\\(([^\\]]*)\\)");
    static std::string replacement = "<a href=\"$2\">$1</a>";

    line = std::regex_replace(line, re, replacement);
  }
}; // class LinkParser

// -----------------------------------------------------------------------------

} // namespace maddy