10 #include <fmt/format.h>
11 #include <fmt/chrono.h>
12 #include "biawebutil.hpp"
13 #include "biawebsidebar.hpp"
14 #include "biawebstrings.hpp"
16 // class to represent a biaweb document which can have a file name, title, description,
17 // keywords, content and sidebar items
24 std::string meta_desc;
25 std::string meta_keywords;
27 std::list<SideBar> sidebars;
33 Document (std::string title = "", std::string meta_desc = "",
34 std::string meta_keywords = "", std::string content = "",
35 bool is_index = false, std::time_t cdate= std::time(nullptr),
36 std::time_t mdate = std::time(nullptr))
38 this->title = escape_html (title);
39 this->meta_desc = escape_html (meta_desc);
40 this->meta_keywords = escape_html (meta_keywords);
41 this->content = content;
42 this->is_index = is_index;
44 this->filename = convert_title (title);
46 this->filename = "index";
51 // set whether this is the index document
52 void set_index (bool index = true) {
53 this->is_index = index;
54 index == true ? this->filename = "index" :
55 this->filename = convert_title (this->title);
58 // get whether this is the index document
60 return this->is_index;
64 std::string get_filename () {
65 return this->filename;
68 // set the document modification date
69 void set_modified_date (std::time_t modif) {
73 // get the document modification date
74 std::time_t get_modified_date () {
78 // set the document creation date
79 void set_creation_date (std::time_t creat) {
83 // get the document creation date
84 std::time_t get_creation_date () {
88 // output the document to HTML using the templates in templatedir
89 void output_to_html (std::string templatedir, std::string path);
91 // set the content portion of document as raw HTML content
92 void set_content (std::string content) {
93 this->content = content;
96 // read the contents of marked marked-up content string "str" into the
97 // contents after converting to HTML.
98 void set_markdown_content (std::string str);
100 void set_meta_keywords(std::string meta_keywords) {
101 this->meta_keywords = escape_html (meta_keywords);
104 void set_meta_desc(std::string meta_desc) {
105 this->meta_desc = escape_html (meta_desc);
108 void set_title(std::string title) {
111 this->filename = "index";
113 this->filename = convert_title (title);
116 std::string get_content () {
117 return this->content;
120 std::string get_meta_keywords() {
121 return this->meta_keywords;
124 std::string get_meta_desc() {
125 return this->meta_desc;
128 std::string get_title() {
132 void add_side_bar (SideBar bar) {
133 sidebars.insert (sidebars.cend(), bar);
137 void Document::set_markdown_content (std::string str) {
138 this->content = convert_to_markdown (str);
141 void Document::output_to_html (std::string templatedir, std::string path)
143 std::ifstream tpl (templatedir + "/main.tpl.html");
144 std::string templstr ( (std::istreambuf_iterator<char> (tpl)),
145 (std::istreambuf_iterator<char> ()) );
147 std::ifstream style (templatedir + "/style.tpl.css");
148 std::string stylesheet ( (std::istreambuf_iterator<char> (style)),
149 (std::istreambuf_iterator<char> ()));
151 // first render the sidebars
152 std::string sidebartext;
153 for (SideBar bar : sidebars) {
154 sidebartext += bar.to_html (templatedir);
157 std::string outputhtml = fmt::format (templstr,
158 fmt::arg ("title", this->title),
159 fmt::arg ("keywords", this->meta_keywords),
160 fmt::arg ("stylesheet", stylesheet),
161 fmt::arg ("description", this->meta_desc),
162 fmt::arg ("cdate", *std::localtime (&this->cdate)),
163 fmt::arg ("mdate", *std::localtime (&this->mdate)),
164 fmt::arg ("contents", this->content),
165 fmt::arg ("sidebar", sidebartext)
168 std::ofstream f (path + "/" + this->filename + ".html");