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;
57 // get whether this is the index document
59 return this->is_index;
62 // set the document modification date
63 void set_modified_date (std::time_t modif) {
67 // get the document modification date
68 std::time_t get_modified_date () {
72 // set the document creation date
73 void set_creation_date (std::time_t creat) {
77 // get the document creation date
78 std::time_t get_creation_date () {
82 // output the document to HTML using the template
83 void output_to_html (std::string path);
85 // set the content portion of document as raw HTML content
86 void set_content (std::string content) {
87 this->content = content;
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);
94 void set_meta_keywords(std::string meta_keywords) {
95 this->meta_keywords = meta_keywords;
98 void set_meta_desc(std::string meta_desc) {
99 this->meta_desc = meta_desc;
102 void set_title(std::string title) {
105 this->filename = "index";
107 this->filename = convert_title (title);
110 std::string get_content () {
111 return this->content;
114 std::string get_meta_keywords() {
115 return this->meta_keywords;
118 std::string get_meta_desc() {
119 return this->meta_desc;
122 std::string get_title() {
126 void add_side_bar (SideBar bar) {
127 sidebars.insert (sidebars.cend(), bar);
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.
140 doc = mkd_string (str.c_str(), str.size(), 0);
141 FILE *f = fopen (".biaweb.tmp", "w");
142 markdown (doc, f, 0);
144 std::ifstream ftmp (".biaweb.tmp");
145 std::string tmpl ( (std::istreambuf_iterator<char> (ftmp)),
146 (std::istreambuf_iterator<char> ())
149 while (! ftmp.eof ())
157 remove (".biaweb.tmp");
158 this->content.append (tmpl);
162 void Document::output_to_html (std::string path)
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> ()) );
169 // first render the sidebars
170 std::string sidebartext;
171 for (SideBar bar : sidebars) {
172 sidebartext += bar.to_html ();
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));
183 // Allocate enough space for the output buffer
184 std::unique_ptr<char[]> final_templ(
185 new char[main_tpl.size()+
187 this->content.size() +
188 this->meta_desc.size() +
189 this->meta_keywords.size () +
191 sidebartext.size()]);
192 std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(),
194 this->content.c_str(), sidebartext.c_str());
196 std::ofstream f (path + "/" + this->filename + ".html");
197 f << final_templ.get ();