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 this->content = convert_to_markdown (str);
142 void Document::output_to_html (std::string path)
145 tpl.open ("templates/main.tpl.html", std::ios_base::openmode::_S_in);
146 std::string main_tpl ( (std::istreambuf_iterator<char> (tpl)),
147 (std::istreambuf_iterator<char> ()) );
149 // first render the sidebars
150 std::string sidebartext;
151 for (SideBar bar : sidebars) {
152 sidebartext += bar.to_html ();
155 char ctm_str[100], mtm_str[100];
156 std::time_t creat = this->cdate;
157 std::time_t modif = this->cdate;
158 std::strftime (ctm_str, sizeof (ctm_str),
159 "%d %b %Y, %H:%M", std::localtime (&creat));
160 std::strftime (mtm_str, sizeof (mtm_str),
161 "%d %b %Y, %H:%M", std::localtime (&modif));
163 // Allocate enough space for the output buffer
164 std::unique_ptr<char[]> final_templ(
165 new char[main_tpl.size()+
167 this->content.size() +
168 this->meta_desc.size() +
169 this->meta_keywords.size () +
171 sidebartext.size()]);
172 std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(),
174 this->content.c_str(), sidebartext.c_str());
176 std::ofstream f (path + "/" + this->filename + ".html");
177 f << final_templ.get ();