src/html.cpp

changeset 52
e9edc3bd0301
parent 50
1ebab6df60c2
equal deleted inserted replaced
51:49fdc2eb7cd4 52:e9edc3bd0301
23 */ 23 */
24 24
25 #include "html.h" 25 #include "html.h"
26 26
27 #include <cstdio> 27 #include <cstdio>
28 #include <cassert>
28 29
29 using namespace std::chrono; 30 using namespace std::chrono;
30 31
31 namespace html { 32 namespace html {
32 static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; 33 static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
33 34
34 static unsigned indentation; 35 static unsigned indentation;
35 static const char *tabs = " "; 36 static const char *tabs = " ";
36 static void indent(int change = 0) { 37 static void indent(int change = 0) {
37 indentation += change; 38 indentation += change;
39 assert(indentation >= 0);
40 assert(indentation <= max_indentation);
38 fwrite(tabs, 4, indentation, stdout); 41 fwrite(tabs, 4, indentation, stdout);
39 } 42 }
40 43
41 static std::string encode(const std::string &data) { 44 static std::string encode(const std::string &data) {
42 std::string buffer; 45 std::string buffer;
65 } 68 }
66 return buffer; 69 return buffer;
67 } 70 }
68 } 71 }
69 72
70 void html::open(bool fragment) { 73 void html::open(bool fragment, unsigned char fragment_indent) {
71 if (fragment) { 74 if (fragment) {
75 indent(fragment_indent);
72 puts("<div class=\"heatmap-content\">"); 76 puts("<div class=\"heatmap-content\">");
73 indentation = 1; 77 indentation++;
74 } else { 78 } else {
75 puts(R"(<!DOCTYPE html> 79 puts(R"(<!DOCTYPE html>
76 <html> 80 <html>
77 <head> 81 <head>
78 <style> 82 <style>
126 } 130 }
127 } 131 }
128 132
129 void html::close(bool fragment) { 133 void html::close(bool fragment) {
130 if (fragment) { 134 if (fragment) {
135 indent(-1);
131 puts("</div>"); 136 puts("</div>");
132 } else { 137 } else {
133 puts(" </div>\n </body>\n</html>"); 138 puts(" </div>\n </body>\n</html>");
134 } 139 }
135 } 140 }

mercurial