From 758147528057c7ff8d65ef3435065bce2072ecc1 Mon Sep 17 00:00:00 2001
From: dx <aryadevchavali1@gmail.com>
Date: Sat, 23 May 2020 21:10:22 +0100
Subject: +better implementation of compile_line

This implementation uses the regexes available to compile the line. It
will assess the first character and try to compile it without
regexes (if it's a list item or heading) otherwise it'll use them.
---
 Compiler/src/compiler.cpp | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

(limited to 'Compiler')

diff --git a/Compiler/src/compiler.cpp b/Compiler/src/compiler.cpp
index 7571fc2..89d06cd 100644
--- a/Compiler/src/compiler.cpp
+++ b/Compiler/src/compiler.cpp
@@ -11,3 +11,31 @@ static const std::regex REGEXES[] = {
 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;
+}
-- 
cgit v1.2.3-13-gbd6f