-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
155 lines (114 loc) · 4.35 KB
/
Copy pathrenderer.cpp
File metadata and controls
155 lines (114 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "renderer.hpp"
#include <fstream>
#include "data.hpp"
#include "publisher.hpp"
#include "book.hpp"
#include "config.hpp"
#include "utils.hpp"
std::string Renderer::header(const std::string& directory, const std::string& active)
{
std::string content = open("header.tpl.html");
Utils::replaceAll(content, "{{title}}", Config::get("title"));
Utils::replaceAll(content, "{{root}}", directory);
Utils::replaceAll(content, "{{" + active + "}}", "active");
return content;
}
std::string Renderer::footer()
{
return open("footer.tpl.html");
}
std::string Renderer::pagination(std::size_t page, std::size_t n) {
std::size_t offset = std::stoi(Config::get("items_per_page"));
std::size_t pages = n / offset + 1;
std::string pagination;
if(pages > 1) {
std::string content;
if(page != 0) {
content += open("pagination.first.tpl.html");
std::string prev = open("pagination.prev.tpl.html");
std::string index = (page-1 == 0) ? "." : std::to_string(page-1) + ".html";
Utils::replaceAll(prev, "{{page}}", index);
content += prev;
}
for(std::size_t i = 0; i < pages; ++i) {
if(i < 2 or i >= pages-2 or (i >= page-1 and i < page+2) or (i == page-2 and i == 2) or (i == page+2 and i == pages-3)) {
std::string current = open("pagination.link.tpl.html");
std::string index = (i == 0) ? "." : std::to_string(i) + ".html";
Utils::replaceAll(current, "{{link}}", index);
Utils::replaceAll(current, "{{page}}", i+1);
Utils::replaceAll(current, "{{active}}", (page == i) ? "active" : "");
content += current;
} else if((i == 3 and page-2 < i) or (i == pages-3 and page+2 > i) or i == page-2 or i == page+2)
content += open("pagination.hellip.tpl.html");
}
if(page != pages-1) {
std::string next = open("pagination.next.tpl.html");
std::string index = std::to_string(page+1) + ".html";
Utils::replaceAll(next, "{{page}}", index);
content += next;
std::string last = open("pagination.last.tpl.html");
Utils::replaceAll(last, "{{page}}", pages-1);
content += last;
}
pagination = open("pagination.tpl.html");
Utils::replaceAll(pagination, "{{pages}}", content);
}
return pagination;
}
std::string Renderer::renderItem(
std::string const &title,
std::string const &id,
std::string const &url,
std::string const &image,
int count
) {
std::string item = open("item.tpl.html");
Utils::replaceAll(item, "{{title}}", title);
Utils::replaceAll(item, "{{id}}", id);
Utils::replaceAll(item, "{{url}}", url);
Utils::replaceAll(item, "{{image}}", image);
Utils::replaceAll(item, "{{count}}", (count > 0) ? "(" + std::to_string(count) + ")" : "");
return item;
}
std::string Renderer::renderHomeItem(
std::string const &type,
int count
) {
return renderItem(
Utils::titlize(type),
type,
type,
type + "/icon.png",
count
);
}
std::string Renderer::renderLink(
std::string const& title,
std::string const& url
) {
if(title.empty())
return "";
std::string link = open("link.tpl.html");
Utils::replaceAll(link, "{{name}}", title);
Utils::replaceAll(link, "{{url}}", url);
return link;
}
std::string Renderer::open(const std::string& filename) {
auto it = _cache.find(filename);
if(it == _cache.end()) {
std::string path = Config::get("template") + "/" + filename;
std::ifstream file(path.c_str());
if(!file)
throw std::runtime_error("Renderer: can't open template " + path);
std::string content;
std::string line;
for(int i = 0; std::getline(file, line); ++i) {
if(i != 0)
content += "\n";
content += line;
}
file.close();
it = _cache.insert(std::pair<std::string, std::string>(filename, content)).first;
}
return it->second;
}