aboutsummaryrefslogtreecommitdiff
path: root/Compiler/src/compiler.cpp
diff options
context:
space:
mode:
authordx <aryadevchavali1@gmail.com>2020-05-23 21:10:22 +0100
committerdx <aryadevchavali1@gmail.com>2020-05-23 21:13:37 +0100
commit758147528057c7ff8d65ef3435065bce2072ecc1 (patch)
tree8bf09aef14426201e0ce009838d43786812cdcd8 /Compiler/src/compiler.cpp
parentda348e56996ffd163c64f0308db1ddb04d898035 (diff)
downloadmdhtml-758147528057c7ff8d65ef3435065bce2072ecc1.tar.gz
mdhtml-758147528057c7ff8d65ef3435065bce2072ecc1.tar.bz2
mdhtml-758147528057c7ff8d65ef3435065bce2072ecc1.zip
+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.
Diffstat (limited to 'Compiler/src/compiler.cpp')
-rw-r--r--Compiler/src/compiler.cpp28
1 files changed, 28 insertions, 0 deletions
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;
+}