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