f001373307fcb5400c12b61bb7ff0589df283ad5
[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 // "discount" markdown library is a C library and hence requires to be wrapped in
13 // extern "C"
14 extern "C" {
15 #include <mkdio.h>
16 }
17 // class to represent a biaweb document which can have a file name, title, description,
18 // keywords, content and sidebar items
19 namespace biaweb {
20 class Document
21 {
22 protected:
23 std::string filename;
24 std::string title;
25 std::string meta_desc;
26 std::string meta_keywords;
27 std::string content;
28 std::list<SideBar> sidebars;
29 std::time_t cdate;
30 std::time_t mdate;
31 bool is_index;
32
33 public:
34 Document (std::string title = "", std::string meta_desc = "",
35 std::string meta_keywords = "", std::string content = "",
36 bool is_index = false, std::time_t cdate= std::time(nullptr),
37 std::time_t mdate = std::time(nullptr))
38 {
39 this->title = title;
40 this->meta_desc = meta_desc;
41 this->meta_keywords = meta_keywords;
42 this->content = content;
43 this->is_index = is_index;
44 if (is_index)
45 this->filename = convert_title (title);
46 else
47 this->filename = "index";
48 this->cdate = cdate;
49 this->mdate = mdate;
50 }
51
52 // set whether this is the index document
53 void set_index (bool index = true) {
54 this->is_index = index;
55 }
56
57 // get whether this is the index document
58 bool get_index () {
59 return this->is_index;
60 }
61
62 // set the document modification date
63 void set_modified_date (std::time_t modif) {
64 this->mdate = modif;
65 }
66
67 // get the document modification date
68 std::time_t get_modified_date () {
69 return this->mdate;
70 }
71
72 // set the document creation date
73 void set_creation_date (std::time_t creat) {
74 this->cdate = creat;
75 }
76
77 // get the document creation date
78 std::time_t get_creation_date () {
79 return this->cdate;
80 }
81
82 // output the document to HTML using the template
83 void output_to_html (std::string path);
84
85 // set the content portion of document as raw HTML content
86 void set_content (std::string content) {
87 this->content = content;
88 }
89
90 // read the contents of marked marked-up content string "str" into the
91 // contents after converting to HTML.
92 void set_markdown_content (std::string str);
93
94 void set_meta_keywords(std::string meta_keywords) {
95 this->meta_keywords = meta_keywords;
96 }
97
98 void set_meta_desc(std::string meta_desc) {
99 this->meta_desc = meta_desc;
100 }
101
102 void set_title(std::string title) {
103 this->title = title;
104 if (this->is_index)
105 this->filename = "index";
106 else
107 this->filename = convert_title (title);
108 }
109
110 std::string get_content () {
111 return this->content;
112 }
113
114 std::string get_meta_keywords() {
115 return this->meta_keywords;
116 }
117
118 std::string get_meta_desc() {
119 return this->meta_desc;
120 }
121
122 std::string get_title() {
123 return this->title;
124 }
125
126 void add_side_bar (SideBar bar) {
127 sidebars.insert (sidebars.cend(), bar);
128 }
129 };
130
131 void Document::set_markdown_content (std::string str) {
132 // discount is a C library and it doesn't work well with C++ streams
133 // and there seems no way to get the output of any of these functions
134 // into an std::string.
135 // the only option seems to be to write the output of the markdown()
136 // function to a temporary working file and then read it back into C++
137 // with the normal std::ifstream and feed it into the std::string
138 // till a cleaner solution can be found.
139 MMIOT *doc;
140 doc = mkd_string (str.c_str(), str.size(), 0);
141 FILE *f = fopen (".biaweb.tmp", "w");
142 markdown (doc, f, 0);
143 fclose (f);
144 std::ifstream ftmp (".biaweb.tmp");
145 std::string tmpl ( (std::istreambuf_iterator<char> (ftmp)),
146 (std::istreambuf_iterator<char> ())
147 );
148
149 while (! ftmp.eof ())
150 {
151 std::string line;
152 ftmp >> line;
153 tmpl.append (line);
154 tmpl.append (" ");
155 }
156 ftmp.close ();
157 remove (".biaweb.tmp");
158 this->content.append (tmpl);
159 mkd_cleanup (doc);
160 }
161
162 void Document::output_to_html (std::string path)
163 {
164 std::ifstream tpl;
165 tpl.open ("templates/main.tpl.html", std::ios_base::openmode::_S_in);
166 std::string main_tpl ( (std::istreambuf_iterator<char> (tpl)),
167 (std::istreambuf_iterator<char> ()) );
168 tpl.close ();
169 // first render the sidebars
170 std::string sidebartext;
171 for (SideBar bar : sidebars) {
172 sidebartext += bar.to_html ();
173 }
174
175 char ctm_str[100], mtm_str[100];
176 std::time_t creat = this->cdate;
177 std::time_t modif = this->cdate;
178 std::strftime (ctm_str, sizeof (ctm_str),
179 "%d %b %Y, %H:%M", std::localtime (&creat));
180 std::strftime (mtm_str, sizeof (mtm_str),
181 "%d %b %Y, %H:%M", std::localtime (&modif));
182
183 // Allocate enough space for the output buffer
184 std::unique_ptr<char[]> final_templ(
185 new char[main_tpl.size()+
186 this->title.size()+
187 this->content.size() +
188 this->meta_desc.size() +
189 this->meta_keywords.size () +
190 200 +
191 sidebartext.size()]);
192 std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(),
193 ctm_str, mtm_str,
194 this->content.c_str(), sidebartext.c_str());
195
196 std::ofstream f (path + "/" + this->filename + ".html");
197 f << final_templ.get ();
198 f.close ();
199 }
200 }
201
202 #endif