From 26b38b4a38c24955293a7144f1e1d74676601caf Mon Sep 17 00:00:00 2001 From: Harishankar Date: Wed, 20 May 2020 14:46:02 +0530 Subject: [PATCH] Included functionality to describe the document in the input file All input files (except index) require to be of the format Title Description keywords date of creation (YYYY-MM-DD HH:II Z) format --- biawebdoclist.hpp | 8 ++++-- biawebdocument.hpp | 62 ++++++++++++++++++++++++++++++++++++++++-- biawebdocumenttree.hpp | 22 +++++++-------- biawebstrings.hpp | 3 +- biawebutil.hpp | 8 ++++-- 5 files changed, 84 insertions(+), 19 deletions(-) diff --git a/biawebdoclist.hpp b/biawebdoclist.hpp index 7c4efcd..547664a 100644 --- a/biawebdoclist.hpp +++ b/biawebdoclist.hpp @@ -68,11 +68,15 @@ namespace biaweb { std::string templstr ((std::istreambuf_iterator (templ)), (std::istreambuf_iterator ()) ); templ.close (); + + std::tm c, m; + c = *std::localtime (&this->ctime); + m = *std::localtime (&this->mtime); std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url), fmt::arg("doctitle", this->title), - fmt::arg("cdate", *std::localtime (&this->ctime)), - fmt::arg("mdate", *std::localtime (&this->mtime))); + fmt::arg("cdate", c), + fmt::arg("mdate", m)); return outputhtml; } diff --git a/biawebdocument.hpp b/biawebdocument.hpp index 40b0207..221bc4f 100644 --- a/biawebdocument.hpp +++ b/biawebdocument.hpp @@ -48,6 +48,15 @@ namespace biaweb { 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; @@ -134,16 +143,59 @@ namespace biaweb { } }; + // 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 (tpl)), (std::istreambuf_iterator ()) ); tpl.close (); + // read the style template file std::ifstream style (templatedir + "/style.tpl.css"); std::string stylesheet ( (std::istreambuf_iterator (style)), (std::istreambuf_iterator ())); @@ -154,13 +206,19 @@ namespace biaweb { 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) ); diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index 66dcd9f..c58a534 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -163,6 +163,7 @@ namespace biaweb { // 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 index (new Document (this->title)); index.get()->set_index (); // set the file name path @@ -227,6 +228,7 @@ namespace biaweb { // 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 @@ -253,30 +255,28 @@ namespace biaweb { } // 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 (infile)), - (std::istreambuf_iterator ())); - // 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 (infile)), + (std::istreambuf_iterator ()) ); 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 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()); } diff --git a/biawebstrings.hpp b/biawebstrings.hpp index 702e5a9..76c4f2b 100644 --- a/biawebstrings.hpp +++ b/biawebstrings.hpp @@ -7,8 +7,9 @@ namespace biaweb { const char* SUB_CAT = "Sub categories: "; const char* ART_LIST = "List of Articles"; const char* INDEX = "Index Page"; + const char* WARNING_PARSE_FAILED = "Warning: date parse failed on " ; // DATE FORMAT - const char* DATE_FORMAT = "%d %b %Y, %H:%M %Z"; + const char* DATE_IN_FORMAT = "%Y-%m-%d %H:%M %Z"; } #endif diff --git a/biawebutil.hpp b/biawebutil.hpp index ee6cd5a..6a87827 100644 --- a/biawebutil.hpp +++ b/biawebutil.hpp @@ -23,16 +23,18 @@ namespace biaweb { // till a cleaner solution can be found. MMIOT *doc; doc = mkd_string (str.c_str(), str.size(), 0); - FILE *f = fopen (".biaweb.tmp", "w"); + char tempfile[20]; + strcpy (tempfile, "/tmp/biawebXXXXXX"); + mkstemp (tempfile); + FILE *f = fopen (tempfile, "w"); markdown (doc, f, 0); fclose (f); - std::ifstream ftmp (".biaweb.tmp"); + std::ifstream ftmp (tempfile); std::string tmpl ( (std::istreambuf_iterator (ftmp)), (std::istreambuf_iterator ()) ); ftmp.close (); mkd_cleanup (doc); - remove (".biaweb.tmp"); return tmpl; } -- 2.20.1