diff --git a/assets/HypertextPages/test.nytl.html b/assets/HypertextPages/test.nytl.html
new file mode 100644
index 0000000..2c78ec3
--- /dev/null
+++ b/assets/HypertextPages/test.nytl.html
@@ -0,0 +1,10 @@
+
+
+
+
+ {% WRITE arg1 %}
+
+
+ {% WRITE arg1 %}
+
+
\ No newline at end of file
diff --git a/src/http_server/new_york_transit_line/core.h b/src/http_server/new_york_transit_line/core.h
index e3b698d..46ff468 100644
--- a/src/http_server/new_york_transit_line/core.h
+++ b/src/http_server/new_york_transit_line/core.h
@@ -5,11 +5,35 @@
#include
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 splitIntoLines(const std::string& str);
+ std::string concatenateLines(const std::vector& lines);
+
+
void parse_bare_file(const std::string& filename, const std::string& content,
global_elem_set_t& result);
void parse_special_file(const std::string& filename, const std::string& content,
global_elem_set_t& result);
+ /* =================== For rendering ====================*/
struct LocalVarValue {
bool is_json = false;
std::string EL_name;
diff --git a/src/http_server/new_york_transit_line/parser.cpp b/src/http_server/new_york_transit_line/parser.cpp
index 2dee160..6fbf7f3 100644
--- a/src/http_server/new_york_transit_line/parser.cpp
+++ b/src/http_server/new_york_transit_line/parser.cpp
@@ -21,8 +21,21 @@ namespace nytl {
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,
- global_elem_set_t& result)
+ global_elem_set_t& result)
{
ASSERT(result.count(filename) == 0, "Repeated element " + filename);
std::vector lines;
@@ -42,7 +55,6 @@ namespace nytl {
smallest_tab = tab_sz;
had_nw_line = true;
}
- rstrip(current_line);
lines.push_back(current_line);
}
current_line.clear();
@@ -66,14 +78,62 @@ namespace nytl {
}
Element& el = result[filename];
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 splitIntoLines(const std::string &str) {
+ std::vector result;
+ for (char ch: str) {
+ if (ch == '\n')
+ result.emplace_back();
+ else
+ result.back() += ch;
+ }
+ return result;
+ }
+
+ std::string concatenateLines(const std::vector& lines) {
+ std::string result;
size_t n = lines.size();
for (size_t i = 0; i < n; i++) {
- lines_cat += lines[i];
- if (i + 1 < n)
- lines_cat += '\n';
+ if (i)
+ result += '\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,
diff --git a/src/http_server/nytl_tests/test0.cpp b/src/http_server/nytl_tests/test0.cpp
index 5336d0b..4f0423f 100644
--- a/src/http_server/nytl_tests/test0.cpp
+++ b/src/http_server/nytl_tests/test0.cpp
@@ -13,5 +13,8 @@ int main(int argc, char** argv) {
templater.update();
std::string answer = templater.render("chat", {});
printf("%s\n<>\n", answer.c_str());
+ std::string answer2 = templater.render("test", {});
+ printf("%s\n<>\n", answer.c_str());
+
return 0;
}
\ No newline at end of file