aboutsummaryrefslogtreecommitdiff
path: root/inc/maddy/horizontallineparser.h
blob: add6bf15ba83ce5f764c1598a12fe3fe572ece42 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * This project is licensed under the MIT license. For more information see the
 * LICENSE file.
 */
#pragma once

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

#include <functional>
#include <string>
#include <regex>

#include "maddy/blockparser.h"

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

namespace maddy {

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

/**
 * HorizontalLineParser
 *
 * From Markdown: `---`
 *
 * To HTML: `<hr/>`
 *
 * @class
 */
class HorizontalLineParser : public BlockParser
{
public:
  /**
   * ctor
   *
   * @method
   * @param {std::function<void(std::string&)>} parseLineCallback
   * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
   */
  HorizontalLineParser(
    std::function<void(std::string&)> parseLineCallback,
    std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
  )
    : BlockParser(parseLineCallback, getBlockParserForLineCallback)
    , lineRegex("^---$")
  {}

  /**
   * IsStartingLine
   *
   * If the line has exact three dashes `---`, then it is a horizontal line.
   *
   * @method
   * @param {const std::string&} line
   * @return {bool}
   */
  static bool
  IsStartingLine(const std::string& line)
  {
    static std::regex re("^---$");
    return std::regex_match(line, re);
  }

  /**
   * IsFinished
   *
   * The horizontal line is always only one line long, so this method always
   * returns true.
   *
   * @method
   * @return {bool}
   */
  bool
  IsFinished() const override
  {
    return true;
  }

protected:
  bool
  isInlineBlockAllowed() const override
  {
    return false;
  }

  bool
  isLineParserAllowed() const override
  {
    return false;
  }

  void
  parseBlock(std::string& line) override
  {
    static std::string replacement = "<hr/>";

    line = std::regex_replace(line, lineRegex, replacement);
  }

private:
  std::regex lineRegex;
}; // class HorizontalLineParser

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

} // namespace maddy