I hate my life so much
This commit is contained in:
parent
be37e4f5ab
commit
91aaf51410
10
assets/HypertextPages/test.nytl.html
Normal file
10
assets/HypertextPages/test.nytl.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>{% WRITE arg1 %}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>{% WRITE arg1 %}</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -5,11 +5,35 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
namespace nytl {
|
namespace nytl {
|
||||||
|
/* ============== For parsing =============================*/
|
||||||
|
struct ParsingContext {
|
||||||
|
std::string text;
|
||||||
|
size_t pos = 0;
|
||||||
|
size_t column = 0;
|
||||||
|
size_t line = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef EOFVAL
|
||||||
|
#error Son in my shift
|
||||||
|
#endif
|
||||||
|
#define EOFVAL -999
|
||||||
|
int peep(ParsingContext& ctx);
|
||||||
|
|
||||||
|
void skip(ParsingContext& ctx);
|
||||||
|
void skip(ParsingContext& ctx, char ch);
|
||||||
|
|
||||||
|
void skipWhitespace(ParsingContext& ctx);
|
||||||
|
|
||||||
|
std::vector<std::string> splitIntoLines(const std::string& str);
|
||||||
|
std::string concatenateLines(const std::vector<std::string>& lines);
|
||||||
|
|
||||||
|
|
||||||
void parse_bare_file(const std::string& filename, const std::string& content,
|
void parse_bare_file(const std::string& filename, const std::string& content,
|
||||||
global_elem_set_t& result);
|
global_elem_set_t& result);
|
||||||
void parse_special_file(const std::string& filename, const std::string& content,
|
void parse_special_file(const std::string& filename, const std::string& content,
|
||||||
global_elem_set_t& result);
|
global_elem_set_t& result);
|
||||||
|
|
||||||
|
/* =================== For rendering ====================*/
|
||||||
struct LocalVarValue {
|
struct LocalVarValue {
|
||||||
bool is_json = false;
|
bool is_json = false;
|
||||||
std::string EL_name;
|
std::string EL_name;
|
||||||
|
@ -21,8 +21,21 @@ namespace nytl {
|
|||||||
str.resize(str.size() - 1);
|
str.resize(str.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string clement_lstrip(const std::string& str) {
|
||||||
|
size_t gone = 0;
|
||||||
|
size_t n = str.size();
|
||||||
|
for (size_t i = 0; i < n; i++) {
|
||||||
|
if (str[i] == '\n') {
|
||||||
|
gone = i + 1;
|
||||||
|
} else if (!isSPACE(str[i])) {
|
||||||
|
return str.substr(gone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
void parse_bare_file(const std::string& filename, const std::string& content,
|
void parse_bare_file(const std::string& filename, const std::string& content,
|
||||||
global_elem_set_t& result)
|
global_elem_set_t& result)
|
||||||
{
|
{
|
||||||
ASSERT(result.count(filename) == 0, "Repeated element " + filename);
|
ASSERT(result.count(filename) == 0, "Repeated element " + filename);
|
||||||
std::vector<std::string> lines;
|
std::vector<std::string> lines;
|
||||||
@ -42,7 +55,6 @@ namespace nytl {
|
|||||||
smallest_tab = tab_sz;
|
smallest_tab = tab_sz;
|
||||||
had_nw_line = true;
|
had_nw_line = true;
|
||||||
}
|
}
|
||||||
rstrip(current_line);
|
|
||||||
lines.push_back(current_line);
|
lines.push_back(current_line);
|
||||||
}
|
}
|
||||||
current_line.clear();
|
current_line.clear();
|
||||||
@ -66,14 +78,62 @@ namespace nytl {
|
|||||||
}
|
}
|
||||||
Element& el = result[filename];
|
Element& el = result[filename];
|
||||||
el.parts = {ElementPart{element_part_types::code}};
|
el.parts = {ElementPart{element_part_types::code}};
|
||||||
std::string lines_cat;
|
std::string lines_cat = concatenateLines(lines);
|
||||||
|
el.parts[0].when_code.lines = mv(lines_cat);
|
||||||
|
}
|
||||||
|
|
||||||
|
int peep(ParsingContext &ctx) {
|
||||||
|
if (ctx.text.size() <= ctx.pos)
|
||||||
|
return EOFVAL;
|
||||||
|
return ctx.text[ctx.pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
void advance(ParsingContext& ctx) {
|
||||||
|
if (ctx.text[ctx.pos] == '\n') {
|
||||||
|
ctx.line++;
|
||||||
|
ctx.column = 0;
|
||||||
|
} else {
|
||||||
|
ctx.column++;
|
||||||
|
}
|
||||||
|
ctx.pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void skip(ParsingContext& ctx) {
|
||||||
|
ASSERT(ctx.pos < ctx.text.size(), "Unexpected EOF");
|
||||||
|
advance(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void skip(ParsingContext& ctx, char ch) {
|
||||||
|
ASSERT(ctx.pos < ctx.text.size(), "Unexpected EOF");
|
||||||
|
ASSERT(ctx.text[ctx.pos] == ch, "Unexpected character");
|
||||||
|
advance(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void skipWhitespace(ParsingContext &ctx) {
|
||||||
|
while (peep(ctx) > 0 && isSPACE((char)peep(ctx)))
|
||||||
|
skip(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> splitIntoLines(const std::string &str) {
|
||||||
|
std::vector<std::string> result;
|
||||||
|
for (char ch: str) {
|
||||||
|
if (ch == '\n')
|
||||||
|
result.emplace_back();
|
||||||
|
else
|
||||||
|
result.back() += ch;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string concatenateLines(const std::vector<std::string>& lines) {
|
||||||
|
std::string result;
|
||||||
size_t n = lines.size();
|
size_t n = lines.size();
|
||||||
for (size_t i = 0; i < n; i++) {
|
for (size_t i = 0; i < n; i++) {
|
||||||
lines_cat += lines[i];
|
if (i)
|
||||||
if (i + 1 < n)
|
result += '\n';
|
||||||
lines_cat += '\n';
|
result += lines[i];
|
||||||
}
|
}
|
||||||
el.parts[0].when_code.lines = mv(lines_cat);
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_special_file(const std::string& filename, const std::string& content,
|
void parse_special_file(const std::string& filename, const std::string& content,
|
||||||
|
@ -13,5 +13,8 @@ int main(int argc, char** argv) {
|
|||||||
templater.update();
|
templater.update();
|
||||||
std::string answer = templater.render("chat", {});
|
std::string answer = templater.render("chat", {});
|
||||||
printf("%s\n<a><f><t><e><r><><l><f>\n", answer.c_str());
|
printf("%s\n<a><f><t><e><r><><l><f>\n", answer.c_str());
|
||||||
|
std::string answer2 = templater.render("test", {});
|
||||||
|
printf("%s\n<a><f><t><e><r><><l><f>\n", answer.c_str());
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user