Changed the rendering code for templating output
[biaweb2.git] / biawebdocument.hpp
1 #ifndef __BIAWEB__
2 #define __BIAWEB__
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 #include <list>
7 #include <memory>
8 #include <iomanip>
9 #include <ctime>
10 #include <fmt/format.h>
11 #include <fmt/chrono.h>
12 #include "biawebutil.hpp"
13 #include "biawebsidebar.hpp"
14 #include "biawebstrings.hpp"
15
16 // class to represent a biaweb document which can have a file name, title, description,
17 // keywords, content and sidebar items
18 namespace biaweb {
19 class Document
20 {
21 protected:
22 std::string filename;
23 std::string title;
24 std::string meta_desc;
25 std::string meta_keywords;
26 std::string content;
27 std::list<SideBar> sidebars;
28 std::time_t cdate;
29 std::time_t mdate;
30 bool is_index;
31
32 public:
33 Document (std::string title = "", std::string meta_desc = "",
34 std::string meta_keywords = "", std::string content = "",
35 bool is_index = false, std::time_t cdate= std::time(nullptr),
36 std::time_t mdate = std::time(nullptr))
37 {
38 this->title = escape_html (title);
39 this->meta_desc = escape_html (meta_desc);
40 this->meta_keywords = escape_html (meta_keywords);
41 this->content = content;
42 this->is_index = is_index;
43 if (! is_index)
44 this->filename = convert_title (title);
45 else
46 this->filename = "index";
47 this->cdate = cdate;
48 this->mdate = mdate;
49 }
50
51 // set whether this is the index document
52 void set_index (bool index = true) {
53 this->is_index = index;
54 index == true ? this->filename = "index" :
55 this->filename = convert_title (this->title);
56 }
57
58 // get whether this is the index document
59 bool get_index () {
60 return this->is_index;
61 }
62
63 // get the file name
64 std::string get_filename () {
65 return this->filename;
66 }
67
68 // set the document modification date
69 void set_modified_date (std::time_t modif) {
70 this->mdate = modif;
71 }
72
73 // get the document modification date
74 std::time_t get_modified_date () {
75 return this->mdate;
76 }
77
78 // set the document creation date
79 void set_creation_date (std::time_t creat) {
80 this->cdate = creat;
81 }
82
83 // get the document creation date
84 std::time_t get_creation_date () {
85 return this->cdate;
86 }
87
88 // output the document to HTML using the templates in templatedir
89 void output_to_html (std::string templatedir, std::string path);
90
91 // set the content portion of document as raw HTML content
92 void set_content (std::string content) {
93 this->content = content;
94 }
95
96 // read the contents of marked marked-up content string "str" into the
97 // contents after converting to HTML.
98 void set_markdown_content (std::string str);
99
100 void set_meta_keywords(std::string meta_keywords) {
101 this->meta_keywords = escape_html (meta_keywords);
102 }
103
104 void set_meta_desc(std::string meta_desc) {
105 this->meta_desc = escape_html (meta_desc);
106 }
107
108 void set_title(std::string title) {
109 this->title = title;
110 if (this->is_index)
111 this->filename = "index";
112 else
113 this->filename = convert_title (title);
114 }
115
116 std::string get_content () {
117 return this->content;
118 }
119
120 std::string get_meta_keywords() {
121 return this->meta_keywords;
122 }
123
124 std::string get_meta_desc() {
125 return this->meta_desc;
126 }
127
128 std::string get_title() {
129 return this->title;
130 }
131
132 void add_side_bar (SideBar bar) {
133 sidebars.insert (sidebars.cend(), bar);
134 }
135 };
136
137 void Document::set_markdown_content (std::string str) {
138 this->content = convert_to_markdown (str);
139 }
140
141 void Document::output_to_html (std::string templatedir, std::string path)
142 {
143 std::ifstream tpl (templatedir + "/main.tpl.html");
144 std::string templstr ( (std::istreambuf_iterator<char> (tpl)),
145 (std::istreambuf_iterator<char> ()) );
146 tpl.close ();
147 std::ifstream style (templatedir + "/style.tpl.css");
148 std::string stylesheet ( (std::istreambuf_iterator<char> (style)),
149 (std::istreambuf_iterator<char> ()));
150 style.close ();
151 // first render the sidebars
152 std::string sidebartext;
153 for (SideBar bar : sidebars) {
154 sidebartext += bar.to_html (templatedir);
155 }
156
157 std::string outputhtml = fmt::format (templstr,
158 fmt::arg ("title", this->title),
159 fmt::arg ("keywords", this->meta_keywords),
160 fmt::arg ("stylesheet", stylesheet),
161 fmt::arg ("description", this->meta_desc),
162 fmt::arg ("cdate", *std::localtime (&this->cdate)),
163 fmt::arg ("mdate", *std::localtime (&this->mdate)),
164 fmt::arg ("contents", this->content),
165 fmt::arg ("sidebar", sidebartext)
166 );
167
168 std::ofstream f (path + "/" + this->filename + ".html");
169 f << outputhtml;
170 f.close ();
171 }
172 }
173
174 #endif