aboutsummaryrefslogtreecommitdiff
path: root/inc/maddy/headlineparser.h
blob: 9eafec508be31100e8d98515b2488edcbb3932af (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * 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 {

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

/**
 * HeadlineParser
 *
 * From Markdown:
 *
 * ```
 * # Headline 1
 * ## Headline 2
 * ### Headline 3
 * #### Headline 4
 * ##### Headline 5
 * ###### Headline 6
 * ```
 *
 * To HTML:
 *
 * ```
 * <h1>Headline 1</h1>
 * <h2>Headline 2</h2>
 * <h3>Headline 3</h3>
 * <h4>Headline 4</h4>
 * <h5>Headline 5</h5>
 * <h6>Headline 6</h6>
 * ```
 *
 * @class
 */
class HeadlineParser : public BlockParser
{
public:
  /**
   * ctor
   *
   * @method
   * @param {std::function<void(std::string&)>} parseLineCallback
   * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
   */
  HeadlineParser(
    std::function<void(std::string&)> parseLineCallback,
    std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
  )
    : BlockParser(parseLineCallback, getBlockParserForLineCallback)
  {}

  /**
   * IsStartingLine
   *
   * If the line starts with 1 - 6 `#`, then it is a headline.
   *
   * @method
   * @param {const std::string&} line
   * @return {bool}
   */
  static bool
  IsStartingLine(const std::string& line)
  {
    static std::regex re("^(?:#){1,6} (.*)");
    return std::regex_match(line, re);
  }

  /**
   * IsFinished
   *
   * The headline 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::vector<std::regex> hlRegex = {
      std::regex("^# (.*)")
      , std::regex("^(?:#){2} (.*)")
      , std::regex("^(?:#){3} (.*)")
      , std::regex("^(?:#){4} (.*)")
      , std::regex("^(?:#){5} (.*)")
      , std::regex("^(?:#){6} (.*)")
    };
    static std::vector<std::string> hlReplacement = {
      "<h1>$1</h1>"
      , "<h2>$1</h2>"
      , "<h3>$1</h3>"
      , "<h4>$1</h4>"
      , "<h5>$1</h5>"
      , "<h6>$1</h6>"
    };

    for (uint8_t i = 0; i < 6; ++i)
    {
      line = std::regex_replace(line, hlRegex[i], hlReplacement[i]);
    }
  }
}; // class HeadlineParser

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

} // namespace maddy