9 #include "biawebutil.hpp"
10 #include "biawebsidebar.hpp"
12 // class to represent a biaweb document which can have a file name, title, description,
13 // keywords, content and sidebar items
20 std::string meta_desc;
21 std::string meta_keywords;
23 std::list<SideBar> sidebars;
29 Document (std::string title = "", std::string meta_desc = "",
30 std::string meta_keywords = "", std::string content = "",
31 bool is_index = false, std::time_t cdate= std::time(nullptr),
32 std::time_t mdate = std::time(nullptr))
35 this->meta_desc = meta_desc;
36 this->meta_keywords = meta_keywords;
37 this->content = content;
38 this->is_index = is_index;
40 this->filename = convert_title (title);
42 this->filename = "index";
47 // set whether this is the index document
48 void set_index (bool index = true) {
49 this->is_index = index;
50 index == true ? this->filename = "index" :
51 this->filename = convert_title (this->title);
54 // get whether this is the index document
56 return this->is_index;
60 std::string get_filename () {
61 return this->filename;
64 // set the document modification date
65 void set_modified_date (std::time_t modif) {
69 // get the document modification date
70 std::time_t get_modified_date () {
74 // set the document creation date
75 void set_creation_date (std::time_t creat) {
79 // get the document creation date
80 std::time_t get_creation_date () {
84 // output the document to HTML using the templates in templatedir
85 void output_to_html (std::string templatedir, std::string path);
87 // set the content portion of document as raw HTML content
88 void set_content (std::string content) {
89 this->content = content;
92 // read the contents of marked marked-up content string "str" into the
93 // contents after converting to HTML.
94 void set_markdown_content (std::string str);
96 void set_meta_keywords(std::string meta_keywords) {
97 this->meta_keywords = meta_keywords;
100 void set_meta_desc(std::string meta_desc) {
101 this->meta_desc = meta_desc;
104 void set_title(std::string title) {
107 this->filename = "index";
109 this->filename = convert_title (title);
112 std::string get_content () {
113 return this->content;
116 std::string get_meta_keywords() {
117 return this->meta_keywords;
120 std::string get_meta_desc() {
121 return this->meta_desc;
124 std::string get_title() {
128 void add_side_bar (SideBar bar) {
129 sidebars.insert (sidebars.cend(), bar);
133 void Document::set_markdown_content (std::string str) {
134 this->content = convert_to_markdown (str);
137 void Document::output_to_html (std::string templatedir, std::string path)
140 tpl.open (templatedir + "/main.tpl.html", std::ios_base::openmode::_S_in);
141 std::string main_tpl ( (std::istreambuf_iterator<char> (tpl)),
142 (std::istreambuf_iterator<char> ()) );
144 // first render the sidebars
145 std::string sidebartext;
146 for (SideBar bar : sidebars) {
147 sidebartext += bar.to_html (templatedir);
150 char ctm_str[100], mtm_str[100];
151 std::time_t creat = this->cdate;
152 std::time_t modif = this->cdate;
153 std::strftime (ctm_str, sizeof (ctm_str),
154 "%d %b %Y, %H:%M", std::localtime (&creat));
155 std::strftime (mtm_str, sizeof (mtm_str),
156 "%d %b %Y, %H:%M", std::localtime (&modif));
158 // Allocate enough space for the output buffer
159 std::unique_ptr<char[]> final_templ(
160 new char[main_tpl.size()+
162 this->content.size() +
163 this->meta_desc.size() +
164 this->meta_keywords.size () +
166 sidebartext.size()]);
167 std::sprintf (final_templ.get (), main_tpl.c_str(), this->title.c_str(),
169 this->content.c_str(), sidebartext.c_str());
171 std::ofstream f (path + "/" + this->filename + ".html");
172 f << final_templ.get ();