9 #include "biawebutil.hpp"
10 #include "biawebsidebar.hpp"
11 #include "biawebstrings.hpp"
13 // class to represent a biaweb document which can have a file name, title, description,
14 // keywords, content and sidebar items
21 std::string meta_desc;
22 std::string meta_keywords;
24 std::list<SideBar> sidebars;
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))
36 this->meta_desc = meta_desc;
37 this->meta_keywords = meta_keywords;
38 this->content = content;
39 this->is_index = is_index;
41 this->filename = convert_title (title);
43 this->filename = "index";
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);
55 // get whether this is the index document
57 return this->is_index;
61 std::string get_filename () {
62 return this->filename;
65 // set the document modification date
66 void set_modified_date (std::time_t modif) {
70 // get the document modification date
71 std::time_t get_modified_date () {
75 // set the document creation date
76 void set_creation_date (std::time_t creat) {
80 // get the document creation date
81 std::time_t get_creation_date () {
85 // output the document to HTML using the templates in templatedir
86 void output_to_html (std::string templatedir, std::string path);
88 // set the content portion of document as raw HTML content
89 void set_content (std::string content) {
90 this->content = content;
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);
97 void set_meta_keywords(std::string meta_keywords) {
98 this->meta_keywords = meta_keywords;
101 void set_meta_desc(std::string meta_desc) {
102 this->meta_desc = meta_desc;
105 void set_title(std::string title) {
108 this->filename = "index";
110 this->filename = convert_title (title);
113 std::string get_content () {
114 return this->content;
117 std::string get_meta_keywords() {
118 return this->meta_keywords;
121 std::string get_meta_desc() {
122 return this->meta_desc;
125 std::string get_title() {
129 void add_side_bar (SideBar bar) {
130 sidebars.insert (sidebars.cend(), bar);
134 void Document::set_markdown_content (std::string str) {
135 this->content = convert_to_markdown (str);
138 void Document::output_to_html (std::string templatedir, std::string path)
141 tpl.open (templatedir + "/main.tpl.html");
142 std::string main_tpl ( (std::istreambuf_iterator<char> (tpl)),
143 (std::istreambuf_iterator<char> ()) );
145 // first render the sidebars
146 std::string sidebartext;
147 for (SideBar bar : sidebars) {
148 sidebartext += bar.to_html (templatedir);
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));
159 // Allocate enough space for the output buffer
160 std::unique_ptr<char[]> final_templ(
161 new char[main_tpl.size()+
163 this->content.size() +
164 this->meta_desc.size() +
165 this->meta_keywords.size () +
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 (),
171 this->content.c_str(), sidebartext.c_str());
173 std::ofstream f (path + "/" + this->filename + ".html");
174 f << final_templ.get ();