this->mdate = mdate;
}
+ // Constructor to parse the document from a stream instead of manually creating it
+ // File should be of the format
+ // first line: title
+ // second line: Description
+ // third line: Keywords
+ // fourth line: (creation date) YYYY-MM-DD HH:II TZ format
+ // fourth line onwards: Markdown contents
+ Document (std::istream &file) ;
+
// set whether this is the index document
void set_index (bool index = true) {
this->is_index = index;
}
};
+ // Parse and construct a document from a stream instead of individual fields
+ Document::Document (std::istream &infile) {
+ infile.seekg (0);
+ // parse the title, description, keywords, creation time and and contents
+ std::string title, description, keywords, creattime, contents;
+ // read the title
+ std::getline (infile, title);
+ if (infile.eof ()) return;
+ this->title = escape_html (title);
+ this->filename = convert_title (title);
+ // read description
+ std::getline (infile, description);
+ if (infile.eof ()) return;
+ this->meta_desc = escape_html (description);
+ // read the keywords
+ std::getline (infile, keywords);
+ if (infile.eof ()) return;
+ this->meta_keywords = escape_html (keywords);
+ // read the creation date/time and also set the modification time
+ // to creation date/time by default
+ std::getline (infile, creattime);
+ if (infile.eof ()) return;
+ std::stringstream s (creattime);
+ std::tm t;
+ s >> std::get_time (&t, DATE_IN_FORMAT);
+ if (s.fail ())
+ std::cout << WARNING_PARSE_FAILED << this->filename << "\n";
+
+ this->cdate = mktime (&t);
+ this->mdate = this->cdate;
+ // read the rest of contents
+ std::string line;
+ std::getline (infile, line);
+ while (! infile.eof ()) {
+ contents.append (line + "\n");
+ std::getline (infile, line);
+ }
+ this->set_markdown_content (contents);
+ }
+
void Document::set_markdown_content (std::string str) {
this->content = convert_to_markdown (str);
}
+ // output the document using the provided template
void Document::output_to_html (std::string templatedir, std::string path)
{
+ // read the main template file
std::ifstream tpl (templatedir + "/main.tpl.html");
std::string templstr ( (std::istreambuf_iterator<char> (tpl)),
(std::istreambuf_iterator<char> ()) );
tpl.close ();
+ // read the style template file
std::ifstream style (templatedir + "/style.tpl.css");
std::string stylesheet ( (std::istreambuf_iterator<char> (style)),
(std::istreambuf_iterator<char> ()));
sidebartext += bar.to_html (templatedir);
}
+ // time of creation and modification
+ struct std::tm c, m;
+ c = *std::localtime (&this->cdate);
+ m = *std::localtime (&this->mdate);
+
+ // format the template with the values
std::string outputhtml = fmt::format (templstr,
fmt::arg ("title", this->title),
fmt::arg ("keywords", this->meta_keywords),
fmt::arg ("stylesheet", stylesheet),
fmt::arg ("description", this->meta_desc),
- fmt::arg ("cdate", *std::localtime (&this->cdate)),
- fmt::arg ("mdate", *std::localtime (&this->mdate)),
+ fmt::arg ("cdate", c),
+ fmt::arg ("mdate", m),
fmt::arg ("contents", this->content),
fmt::arg ("sidebar", sidebartext)
);
// create the tree - the index file for this tree and all the documents and
// the child trees recursively - using the template specified
void DocumentTree::create_tree_html (std::string templatedir, std::string destdir) {
+ // create a document to represent the index of the tree.
std::unique_ptr<Document> index (new Document (this->title));
index.get()->set_index ();
// set the file name path
// index should contain the summary followed by the article list
index.get()->set_content (this->summary + article_list.get()->to_html(templatedir));
+ // output the index file
index.get()->output_to_html (templatedir, filepath);
// recursively create index for children
}
// add the regular files as documents in the tree
else if (fsitem.is_regular_file ()) {
- // read the contents of the file
- std::ifstream infile (fsitem.path().string());
- std::string infilestr ((std::istreambuf_iterator<char> (infile)),
- (std::istreambuf_iterator<char> ()));
- // if it is an index file (specially named index) add
+ // if it is an index file (specially named as index
+ // or index.md or whatever) directly add
// the contents to the summary of the Doctree
if (fsitem.path().stem().string() == "index")
{
+ std::ifstream infile (fsitem.path());
+ std::string infilestr ( (std::istreambuf_iterator<char> (infile)),
+ (std::istreambuf_iterator<char> ()) );
this->set_markdown_summary (infilestr);
}
// else it is a non-index file-
// create a Document and add it to the tree
else {
+ std::ifstream infile (fsitem.path ());
std::shared_ptr<Document> doc
- (new Document (fsitem.path().stem().string()));
+ (new Document (infile));
+ infile.close ();
- // file creation/modified date from system
+ // file modified date from system
struct stat buf;
if (stat (fsitem.path().string().c_str(), &buf) == 0)
- {
- doc.get()->set_creation_date (buf.st_ctim.tv_sec);
doc.get()->set_modified_date (buf.st_mtim.tv_sec);
- }
- doc.get()->set_markdown_content (infilestr);
this->add_document (doc.get());
}