-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.hpp
More file actions
154 lines (119 loc) · 4.27 KB
/
Copy pathrenderer.hpp
File metadata and controls
154 lines (119 loc) · 4.27 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
#ifndef __RENDERER_H__
#define __RENDERER_H__
#include <set>
#include <vector>
#include <string>
#include <map>
#include "config.hpp"
#include "data.hpp"
#include "utils.hpp"
class Data;
class Book;
class Renderer {
public:
static Renderer& instance() {
static Renderer instance;
return instance;
}
std::string renderItem(
std::string const &title,
std::string const &id,
std::string const &url,
std::string const &image,
int count = -1
);
std::string renderHomeItem(
std::string const &type,
int count = -1
);
std::string renderLink(
std::string const &title,
std::string const &url
);
template<typename T>
std::string renderSection(
std::vector<std::set<Data*, T>> const& vector,
std::string const& dir
) {
std::string sections;
for(std::set<Data*, T> const& dataset : vector) {
if(dataset.empty())
continue;
std::string type = (*dataset.begin())->type();
std::string items;
for(Data const* data: dataset)
items += data->renderItem(dir);
std::string section = open("section.tpl.html");
Utils::replaceAll(section, "{{id}}", type);
Utils::replaceAll(section, "{{title}}", Utils::plural(Utils::titlize(type)));
Utils::replaceAll(section, "{{items}}", items);
sections += section;
}
return header(dir) + sections + footer();
}
template<typename T>
std::string renderSection(
std::map<std::string, std::set<Data*, T>> const& map,
std::string const& type,
std::string const& title,
std::string const& dir,
std::size_t page = 0,
std::size_t offset = std::stoi(Config::get("ITEMS_PER_PAGE"))
) {
if(map.empty())
return "";
std::string items;
std::size_t max = offset*page + offset;
int i;
std::map<std::string, std::set<Data*>>::const_iterator it;
for(i = offset*page, it = map.begin(); it != map.end() and i < max; ++it, ++i) {
items += renderItem(
(!it->first.empty()) ? Utils::titlize(it->first) : "Unknown",
it->first,
dir + type + "/" + std::to_string(i),
dir + type + "/icon.png",
it->second.size()
);
}
std::string section = open("section.tpl.html");
Utils::replaceAll(section, "{{id}}", type);
Utils::replaceAll(section, "{{title}}", title);
Utils::replaceAll(section, "{{items}}", items);
section += pagination(page, map.size());
return header(dir, type) + section + footer();
}
template<typename T>
std::string renderSection(
std::set<Data*, T> const &set,
std::string const &type,
std::string const& title,
std::string const& dir,
std::size_t page = 0,
std::size_t offset = std::stoi(Config::get("ITEMS_PER_PAGE"))
) {
std::string items;
std::size_t max = offset*page + offset;
std::size_t i;
std::set<Data*>::const_iterator it;
for(i = offset*page, it = std::next(set.begin(), i); it != set.end() and i < max; ++it, ++i)
items += (*it)->renderItem(dir);
std::string section = open("section.tpl.html");
Utils::replaceAll(section, "{{id}}", type);
Utils::replaceAll(section, "{{title}}", title);
Utils::replaceAll(section, "{{items}}", items);
section += pagination(page, set.size());
return header(dir, type) + section + footer();
}
std::string open(const std::string& filename);
std::string pagination(std::size_t page, std::size_t n);
void clearCache() {
_cache.clear();
}
std::string header(std::string const& directory = std::string(), std::string const& active = std::string());
std::string footer();
protected:
Renderer() {}
protected:
std::map<std::string, std::string> _cache;
};
#endif