aboutsummaryrefslogtreecommitdiff
path: root/inc/maddy/orderedlistparser.h
blob: e41d3b1faed676ab0cc17d4b96fbc0db36133422 (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
139
140
141
142
/*
 * This project is licensed under the MIT license. For more information see the
 * LICENSE file.
 */
#pragma once

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

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

#include "maddy/blockparser.h"

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

namespace maddy {

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

/**
 * OrderedListParser
 *
 * @class
 */
class OrderedListParser : public BlockParser
{
public:
  /**
   * ctor
   *
   * @method
   * @param {std::function<void(std::string&)>} parseLineCallback
   * @param {std::function<std::shared_ptr<BlockParser>(const std::string& line)>} getBlockParserForLineCallback
   */
   OrderedListParser(
    std::function<void(std::string&)> parseLineCallback,
    std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
  )
    : BlockParser(parseLineCallback, getBlockParserForLineCallback)
    , isStarted(false)
    , isFinished(false)
  {}

  /**
   * IsStartingLine
   *
   * An ordered list starts with `1. `.
   *
   * @method
   * @param {const std::string&} line
   * @return {bool}
   */
  static bool
  IsStartingLine(const std::string& line)
  {
    static std::regex re("^1\\. .*");
    return std::regex_match(line, re);
  }

  /**
   * IsFinished
   *
   * @method
   * @return {bool}
   */
  bool
  IsFinished() const override
  {
    return this->isFinished;
  }

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

  bool
  isLineParserAllowed() const override
  {
    return true;
  }

  void
  parseBlock(std::string& line) override
  {
    bool isStartOfNewListItem = this->isStartOfNewListItem(line);
    uint32_t indentation = getIndentationWidth(line);

    static std::regex orderedlineRegex("^[1-9]+[0-9]*\\. ");
    line = std::regex_replace(line, orderedlineRegex, "");
    static std::regex unorderedlineRegex("^\\* ");
    line = std::regex_replace(line, unorderedlineRegex, "");

    if (!this->isStarted)
    {
      line = "<ol><li>" + line;
      this->isStarted = true;
      return;
    }

    if (indentation >= 2)
    {
      line = line.substr(2);
      return;
    }

    if (
      line.empty() ||
      line.find("</li><li>") != std::string::npos ||
      line.find("</li></ol>") != std::string::npos ||
      line.find("</li></ul>") != std::string::npos
    )
    {
      line = "</li></ol>" + line;
      this->isFinished = true;
      return;
    }

    if (isStartOfNewListItem)
    {
      line = "</li><li>" + line;
    }
  }

private:
  bool isStarted;
  bool isFinished;

  bool
  isStartOfNewListItem(const std::string& line) const
  {
    static std::regex re("^(?:[1-9]+[0-9]*\\. |\\* ).*");
    return std::regex_match(line, re);
  }
}; // class OrderedListParser

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

} // namespace maddy