9 #include "biawebutil.hpp"
10 #include "biawebsidebar.hpp"
12 // "discount" markdown library is a C library and hence requires to be wrapped in
17 // class to represent a biaweb document which can have a file name, title, description,
18 // keywords, content and sidebar items
25 std::string meta_desc;
26 std::string meta_keywords;
28 std::list<SideBar> sidebars;
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))
40 this->meta_desc = meta_desc;
41 this->meta_keywords = meta_keywords;
42 this->content = content;
43 this->is_index = is_index;
45 this->filename = convert_title (title);
47 this->filename = "index";
52 // set whether this is the index document
53 void set_index (bool index = true) {
54 this->is_index = index;
55 index == true ? this->filename = "index" :
56 this->filename = convert_title (this->title);
59 // get whether this is the index document
61 return this->is_index;
65 std::string get_filename () {
66 return this->filename;
69 // set the document modification date
70 void set_modified_date (std::time_t modif) {
74 // get the document modification date
75 std::time_t get_modified_date () {
79 // set the document creation date
80 void set_creation_date (std::time_t creat) {
84 // get the document creation date
85 std::time_t get_creation_date () {
89 // output the document to HTML using the template
90 void output_to_html (std::string path);
92 // set the content portion of document as raw HTML content
93 void set_content (std::string content) {
94 this->content = content;
97 // read the contents of marked marked-up content string "str" into the
98 // contents after converting to HTML.
99 void set_markdown_content (std::string str);
101 void set_meta_keywords(std::string meta_keywords) {
102 this->meta_keywords = meta_keywords;
105 void set_meta_desc(std::string meta_desc) {
106 this->meta_desc = meta_desc;
109 void set_title(std::string title) {
112 this->filename = "index";
114 this->filename = convert_title (title);
117 std::string get_content () {
118 return this->content;
121 std::string get_meta_keywords() {
122 return this->meta_keywords;
125 std::string get_meta_desc() {
126 return this->meta_desc;
129 std::string get_title() {
133 void add_side_bar (SideBar bar) {
134 sidebars.insert (sidebars.cend(), bar);
138 void Document::set_markdown_content (std::string str) {
139 // discount is a C library and it doesn't work well with C++ streams
140 // and there seems no way to get the output of any of these functions
141 // into an std::string.
142 // the only option seems to be to write the output of the markdown()
143 // function to a temporary working file and then read it back into C++
144 // with the normal std::ifstream and feed it into the std::string
145 // till a cleaner solution can be found.
147 doc = mkd_string (str.c_str(), str.size(), 0);
148 FILE *f = fopen (".biaweb.tmp", "w");
149 markdown (doc, f, 0);
151 std::ifstream ftmp (".biaweb.tmp");
152 std::string tmpl ( (std::istreambuf_iterator<char> (ftmp)),
153 (std::istreambuf_iterator<char> ())
156 while (! ftmp.eof ())
164 remove (".biaweb.tmp");
165 this->content.append (tmpl);
169 void Document::output_to_html (std::string path)
172 tpl.open ("templates/main.tpl.html", std::ios_base::openmode::_S_in);
173 std::string main_tpl ( (std::istreambuf_iterator<char> (tpl)),
174 (std::istreambuf_iterator<char> ()) );
176 // first render the sidebars
177 std::string sidebartext;
178 for (SideBar bar : sidebars) {
179 sidebartext += bar.to_html ();
182 char ctm_str[100], mtm_str[100];
183 std::time_t creat = this->cdate;
184 std::time_t modif = this->cdate;
185 std::strftime (ctm_str, sizeof (ctm_str),
186 "%d %b %Y, %H:%M", std::localtime (&creat));
187 std::strftime (mtm_str, sizeof (mtm_str),
188 "%d %b %Y, %H:%M", std::localtime (&modif));
190 // Allocate enough space for the output buffer
191 std::unique_ptr<char[]> final_templ(
192 new char[main_tpl.size()+
194 this->content.size() +
195 this->meta_desc.size() +
196 this->meta_keywords.size () +
198 sidebartext.size()]);
199 std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(),
201 this->content.c_str(), sidebartext.c_str());
203 std::ofstream f (path + "/" + this->filename + ".html");
204 f << final_templ.get ();