aboutsummaryrefslogtreecommitdiff
path: root/Compiler/src/compiler.cpp
blob: 89d06cdf123bcc441c971fae6291f2bb76ed1108 (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
#include "../includes/compiler.hpp"
#include <regex>

static const unsigned short N_ITEMS = 4;
/* Encoded regexes using the Type enum (convert to integer) */
static const std::regex REGEXES[] = {
    std::regex("\\*\\*(.*?)\\*\\*"), std::regex("\\*(.*?)\\*"),
    std::regex("_(.*?)_"), std::regex("~(.*?)~")};

/* Encoded replacement strings using the Type enum (convert to integer) */
static const char *REPLACEMENTS[] = {"<strong>$1</strong>", "<i>$1</i>",
                                     "<u>$1</u>", "<s>$1</s>"};

std::string compile_line(const char *raw)
{
  std::string result;
  char *r;

  if (raw[0] == '#')
  {
    // Get depth of header
    int depth;
    for (depth = 0; (*(raw + depth)) == '#'; ++depth);

    asprintf(&r, "<h%d>%s</h%d>", depth, raw + depth, depth);
    return r;
  }
  else if (raw[0] == '-')
  {
    asprintf(&r, "<li>%s</li>", raw + 1);
    return r;
  }

  result = raw;
  for (int i = 0; i < N_ITEMS; ++i)
  {
    result = std::regex_replace(result, REGEXES[i], REPLACEMENTS[i]);
  }

  return result;
}