From 177a64bdb8830871811ed551e9dbcd8f4451c42b Mon Sep 17 00:00:00 2001
From: Andreev Gregory <grinadand@gmail.com>
Date: Tue, 13 Aug 2024 12:16:05 +0300
Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=BC=D0=B0=D0=BE,=20=D0=90=D0=B4=D0=B5?=
 =?UTF-8?q?=D0=BB=D1=8C,=20=D1=85=D0=B0=D1=80=D0=B5=20carriage=20return=20?=
 =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=B2=20=D0=BA?=
 =?UTF-8?q?=D0=BE=D0=BD=D1=86=D0=B5=20=D0=BA=D0=B0=D0=B6=D0=B4=D0=BE=D0=B9?=
 =?UTF-8?q?=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B8(((?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 assets/HypertextPages/chat.html               | 50 +++++------
 .../new_york_transit_line/alotalot.cpp        |  2 +-
 src/http_server/new_york_transit_line/core.h  |  3 +-
 .../new_york_transit_line/parser.cpp          | 86 ++++++++-----------
 src/http_server/nytl_tests/test0.cpp          |  2 +-
 5 files changed, 65 insertions(+), 78 deletions(-)

diff --git a/assets/HypertextPages/chat.html b/assets/HypertextPages/chat.html
index 809d6b4..61255d5 100644
--- a/assets/HypertextPages/chat.html
+++ b/assets/HypertextPages/chat.html
@@ -1,25 +1,25 @@
-<!DOCTYPE html>
-<html lang="ru">
-<head>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>Веб-Чат</title>
-    <link rel="stylesheet" href="/assets/css/chat.css">
-</head>
-<body>
-    <div class="chat-container">
-        <div class="chat-header">
-            Веб чат
-        </div>
-        <div class="chat-messages" id="chat-messages">
-            <!-- Сообщения чата будут здесь -->
-        </div>
-        <div class="chat-footer">
-            <input type="text" class="chat-input" id="chat-input" placeholder="Введите сообщение...">
-            <button class="chat-send-button" onclick="sendMessage()">Отправить</button>
-        </div>
-    </div>
-
-    <script src="/assets/js/chat.js"></script>
-</body>
-</html>
+<!DOCTYPE html>
+<html lang="ru">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Веб-Чат</title>
+    <link rel="stylesheet" href="/assets/css/chat.css">
+</head>
+<body>
+    <div class="chat-container">
+        <div class="chat-header">
+            Веб чат
+        </div>
+        <div class="chat-messages" id="chat-messages">
+            <!-- Сообщения чата будут здесь -->
+        </div>
+        <div class="chat-footer">
+            <input type="text" class="chat-input" id="chat-input" placeholder="Введите сообщение...">
+            <button class="chat-send-button" onclick="sendMessage()">Отправить</button>
+        </div>
+    </div>
+
+    <script src="/assets/js/chat.js"></script>
+</body>
+</html>
diff --git a/src/http_server/new_york_transit_line/alotalot.cpp b/src/http_server/new_york_transit_line/alotalot.cpp
index 09b9cbd..517b270 100644
--- a/src/http_server/new_york_transit_line/alotalot.cpp
+++ b/src/http_server/new_york_transit_line/alotalot.cpp
@@ -43,7 +43,7 @@ namespace nytl {
     }
 
     bool isSPACE(char ch) {
-        return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\r';
+        return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n';
     }
 
     bool isUname(const std::string &str) {
diff --git a/src/http_server/new_york_transit_line/core.h b/src/http_server/new_york_transit_line/core.h
index 46ff468..384bbdc 100644
--- a/src/http_server/new_york_transit_line/core.h
+++ b/src/http_server/new_york_transit_line/core.h
@@ -26,7 +26,8 @@ namespace nytl {
 
     std::vector<std::string> splitIntoLines(const std::string& str);
     std::string concatenateLines(const std::vector<std::string>& lines);
-
+    void one_part_update_min_start_wsp_non_empty(const std::string& str, bool is_first, size_t& min);
+    std::string one_part_cut_excess_tab(const std::string& str, bool is_first, size_t cut);
 
     void parse_bare_file(const std::string& filename, const std::string& content,
         global_elem_set_t& result);
diff --git a/src/http_server/new_york_transit_line/parser.cpp b/src/http_server/new_york_transit_line/parser.cpp
index 6fbf7f3..9fb7b5b 100644
--- a/src/http_server/new_york_transit_line/parser.cpp
+++ b/src/http_server/new_york_transit_line/parser.cpp
@@ -34,54 +34,6 @@ namespace nytl {
         return "";
     }
 
-    void parse_bare_file(const std::string& filename, const std::string& content,
-                         global_elem_set_t& result)
-    {
-        ASSERT(result.count(filename) == 0, "Repeated element " + filename);
-        std::vector<std::string> lines;
-        bool had_nw_line = false;
-        size_t smallest_tab;
-        std::string current_line;
-        auto finish = [&]() {
-            size_t tab_sz = first_nw_char(current_line);
-            if (tab_sz == current_line.size()) {
-                if (had_nw_line)
-                    lines.emplace_back();
-            } else {
-                if (had_nw_line) {
-                    if (smallest_tab > tab_sz)
-                        smallest_tab = tab_sz;
-                } else {
-                    smallest_tab = tab_sz;
-                    had_nw_line = true;
-                }
-                lines.push_back(current_line);
-            }
-            current_line.clear();
-        };
-        for (char ch: content) {
-            if (ch == '\n') {
-                finish();
-            } else {
-                current_line += ch;
-            }
-        }
-        finish();
-        while (!lines.empty() && lines.back().empty())
-            lines.pop_back();
-
-        for (std::string& line: lines) {
-            if (!line.empty()) {
-                assert(line.size() > smallest_tab);
-                line = line.substr(smallest_tab);
-            }
-        }
-        Element& el = result[filename];
-        el.parts = {ElementPart{element_part_types::code}};
-        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;
@@ -115,7 +67,7 @@ namespace nytl {
     }
 
     std::vector<std::string> splitIntoLines(const std::string &str) {
-        std::vector<std::string> result;
+        std::vector<std::string> result = {""};
         for (char ch: str) {
             if (ch == '\n')
                 result.emplace_back();
@@ -136,9 +88,43 @@ namespace nytl {
         return result;
     }
 
+    void one_part_update_min_start_wsp_non_empty(const std::string& str, bool is_first, size_t& min) {
+        std::vector<std::string> lines = splitIntoLines(str);
+        size_t L = lines.size();
+        for (size_t i = is_first ? 0 : 1; i < L; i++) {
+            size_t first_nw = first_nw_char(lines[i]);
+            if (first_nw < lines[i].size())
+                min = std::min(min, first_nw);
+        }
+    }
+
+    std::string one_part_cut_excess_tab(const std::string& str, bool is_first, size_t cut) {
+        std::vector<std::string> lines = splitIntoLines(str);
+        size_t L = lines.size();
+        for (size_t i = is_first ? 0 : 1; i < L; i++) {
+            if (!is_space_only(lines[i]))
+                lines[i] = lines[i].substr(cut);
+        }
+        return concatenateLines(lines);
+    }
+
+    void parse_bare_file(const std::string& filename, const std::string& content,
+                             global_elem_set_t& result)
+    {
+        ASSERT(result.count(filename) == 0, "Repeated element " + filename);
+        std::string P = clement_lstrip(content);
+        rstrip(P);
+        size_t cut = 9999999999999;
+        one_part_update_min_start_wsp_non_empty(P, true, cut);
+        P = one_part_cut_excess_tab(P, true, cut);
+        Element& el = result[filename];
+        el.parts = {ElementPart{element_part_types::code}};
+        el.parts[0].when_code.lines = mv(P);
+    }
+
     void parse_special_file(const std::string& filename, const std::string& content,
         std::map<std::string, Element>& result)
     {
-        THROW("Don't know how to parse it yet");
+        // THROW("Don't know how to parse it yet");
     }
 }
diff --git a/src/http_server/nytl_tests/test0.cpp b/src/http_server/nytl_tests/test0.cpp
index 4f0423f..9d930f8 100644
--- a/src/http_server/nytl_tests/test0.cpp
+++ b/src/http_server/nytl_tests/test0.cpp
@@ -11,7 +11,7 @@ int main(int argc, char** argv) {
     std::string dir_path = argv[1];
     nytl::Templater templater(nytl::TemplaterSettings{nytl::TemplaterDetourRules{dir_path}});
     templater.update();
-    std::string answer = templater.render("chat", {});
+    std::string answer = templater.render("list-rooms", {});
     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());