diff options
author | dx <aryadevchavali1@gmail.com> | 2020-05-23 21:37:32 +0100 |
---|---|---|
committer | dx <aryadevchavali1@gmail.com> | 2020-05-23 21:41:33 +0100 |
commit | a01f15e1cee3bf1d8c8008ec887de02197592c5c (patch) | |
tree | 872945e4bb2f32f0925014f2be1ae5234057cf01 | |
parent | 6afdaed5f4f371017447afc3ca1922393ce12c79 (diff) | |
download | mdhtml-a01f15e1cee3bf1d8c8008ec887de02197592c5c.tar.gz mdhtml-a01f15e1cee3bf1d8c8008ec887de02197592c5c.tar.bz2 mdhtml-a01f15e1cee3bf1d8c8008ec887de02197592c5c.zip |
+compile_file function
This will read the file (filename) then compile it line by line.
-rw-r--r-- | Compiler/includes/compiler.hpp | 1 | ||||
-rw-r--r-- | Compiler/src/compiler.cpp | 45 |
2 files changed, 46 insertions, 0 deletions
diff --git a/Compiler/includes/compiler.hpp b/Compiler/includes/compiler.hpp index d761dcf..bd97a8e 100644 --- a/Compiler/includes/compiler.hpp +++ b/Compiler/includes/compiler.hpp @@ -14,5 +14,6 @@ enum Type }; std::string compile_line(const char *raw); +std::string compile_file(const char *filename); #endif // __COMPILER_H_ diff --git a/Compiler/src/compiler.cpp b/Compiler/src/compiler.cpp index 1220457..4be30e0 100644 --- a/Compiler/src/compiler.cpp +++ b/Compiler/src/compiler.cpp @@ -1,4 +1,7 @@ #include "../includes/compiler.hpp" +#include <vector> +#include <iostream> +#include <fstream> #include <regex> static const unsigned short N_ITEMS = 4; @@ -39,3 +42,45 @@ std::string compile_line(const char *raw) return result; } + +std::string compile_file(const char *filename) +{ + std::vector<std::string> 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("</p>"); + paragraphing = false; + } + else if (line[0] == '\0' || line[0] == '\n') + { + if (paragraphing) + v.push_back("</p>"); + else + v.push_back("<p>"); + 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; +} |