#include "../includes/compiler.hpp"
#include
#include
#include
#include
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[] = {"$1", "$1",
"$1", "$1"};
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 < 7; ++depth);
asprintf(&r, "%s", depth, raw + depth, depth);
return r;
}
else if (raw[0] == '-')
{
asprintf(&r, "%s", 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;
}
std::string compile_file(const char *filename)
{
std::vector v;
std::ifstream fp(filename);
if (!fp.is_open())
{
std::cerr << "File " << filename << " not found" << std::endl;
exit(1);
}
bool paragraphing = false;
std::string line;
for (int i = 0; std::getline(fp, line) ; ++i) {
std::cout << "Compiled " << i + 1 << " of " << filename << "\n";
if (line[0] == '#' || line[0] == '-')
{
if (paragraphing)
v.push_back("
");
paragraphing = false;
}
else if (line[0] == '\0' || line[0] == '\n')
{
if (paragraphing)
v.push_back("");
else
v.push_back("");
paragraphing = !paragraphing;
continue;
}
v.push_back(compile_line(line.c_str()));
}
fp.close();
std::string result = "";
for (auto i : v)
result += i + "\n";
return result;
}