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