From: Harishankar Date: Thu, 21 May 2020 07:05:24 +0000 (+0530) Subject: Separated the strings used for document tree generation from code X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=44d5fb1ff24147859c5f198dd47f6956ec5fea51;p=biaweb2.git Separated the strings used for document tree generation from code Separated the string bits used for document tree generation from constant in-code strings to a separate template file stringbits.txt. This will allow for customizability of common strings. --- diff --git a/biawebdocument.hpp b/biawebdocument.hpp index 3fc15e1..65005dd 100644 --- a/biawebdocument.hpp +++ b/biawebdocument.hpp @@ -182,10 +182,9 @@ namespace biaweb { 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); + contents.append (line + "\n"); } this->set_markdown_content (contents); } diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index c070131..9122feb 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -2,7 +2,9 @@ #define __BIAWEBDOCUMENTTREE__ #include #include +#include #include +#include #include #include #include "biawebdocument.hpp" @@ -27,12 +29,17 @@ namespace biaweb { std::string stub; // list of documents in this tree std::list docs; + // common strings used for template generation + std::array doc_strings; // set the parent - protected function as this has to be // called only by add_child void set_parent (DocumentTree *parent) { this->parent = parent; } + // load document generation strings from file stringbits.txt from template dir + void load_doc_strings (std::string templatedir); + public: // method to build a document tree from a path void document_tree_builder (std::string srcpath); @@ -129,6 +136,22 @@ namespace biaweb { return lev; } + // load document generation strings from file stringbits.txt from template dir + void DocumentTree::load_doc_strings (std::string templatedir) { + // load the template file + std::ifstream stringsfile (templatedir + "/stringbits.txt"); + + std::string line; + // read line by line and append it to doc_strings + int i = 0; + while (! stringsfile.eof () && i < this->doc_strings.size ()) { + std::getline (stringsfile, line); + this->doc_strings[i] = line; + i ++; + } + stringsfile.close (); + } + // get the stub hierarchy for this tree std::string DocumentTree::stub_hierarchy () { std::list levels; @@ -163,6 +186,9 @@ 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) { + // load the document string bits to be used in templates + this->load_doc_strings (templatedir); + // create a document to represent the index of the tree. std::unique_ptr index (new Document (this->title)); index.get()->set_index (); @@ -182,13 +208,13 @@ namespace biaweb { // If this tree has a parent, create a sidebar link to the level up std::unique_ptr bar1 (new SideBar()); GenericLinkItem item0; - bar1.get()->set_title (NAVIGATE); - item0.set_item_text (INDEX); + bar1.get()->set_title (this->doc_strings [NAVIGATION]); + item0.set_item_text (this->doc_strings[INDEX]); item0.set_item_url (urlpath + "index.html"); bar1.get()->add_sidebar_item (item0); if (this->get_parent() != nullptr) { GenericLinkItem item1; - item1.set_item_text (GO_UP); + item1.set_item_text (this->doc_strings[GO_UP]); item1.set_item_url (this->stub_hierarchy() + "index.html"); bar1.get()->add_sidebar_item (item1); } @@ -196,7 +222,7 @@ namespace biaweb { // create a sidebar for the child levels if there are children std::unique_ptr bar2 (new SideBar ()); - bar2.get()->set_title (SUB_CAT + this->title); + bar2.get()->set_title (this->doc_strings[SUB_CAT] + this->title); for (DocumentTree tree : this->children) { // we use site relative URLs that rely on the base href tag // so for biaweb generated sites, the base href tag should be @@ -212,7 +238,7 @@ namespace biaweb { // Create the list of documents in this tree with links std::unique_ptr article_list (new DocList ()); - article_list.get()->set_title (this->title + ": " + ART_LIST); + article_list.get()->set_title (this->title + ": " + this->doc_strings[ARTICLES_LIST]); // sort the documents as per creation time and then add the document // links - newest documents should appear above older ones. sort_documents_creation_time (); @@ -227,7 +253,7 @@ namespace biaweb { navbit.get()->add_link_item (GenericLinkItem(par1->stub, par1->stub_hierarchy() + par1->stub + "/index.html")); else - navbit.get()->add_link_item (GenericLinkItem(HOME, "index.html")); + navbit.get()->add_link_item (GenericLinkItem(this->doc_strings[HOME], "index.html")); par1 = par1->parent; } @@ -305,7 +331,7 @@ namespace biaweb { } } catch (std::filesystem::filesystem_error) { - std::cout << "No such path! Specify an existing file path" << std::endl; + std::cout << NO_SUCH_PATH_ERROR << std::endl; } // add the trees for the children recursively diff --git a/biawebstrings.hpp b/biawebstrings.hpp index 1e15e57..c87f882 100644 --- a/biawebstrings.hpp +++ b/biawebstrings.hpp @@ -2,16 +2,18 @@ #define __BIAWEBSTRINGS__ namespace biaweb { - // COMMON STRINGS - const char* GO_UP = "Go up"; - const char* SUB_CAT = "Sub categories: "; - const char* ART_LIST = "List of Articles"; - const char* INDEX = "Index Page"; - const char* NAVIGATE = "Navigation"; + // COMMON APPLICATION STRINGS const char* WARNING_PARSE_FAILED = "Warning: date parse failed on " ; - const char* HOME = "Home"; - // DATE FORMAT + const char* NO_SUCH_PATH_ERROR = "No such path! Specify an existing file path" ; const char* DATE_IN_FORMAT = "%Y-%m-%d %H:%M %Z"; + + // NAMED INDEX FOR DOCUMENT STRING BITS (LOADED FROM TEMPLATE FILE stringbits.txt) + const unsigned int HOME = 0; + const unsigned int ARTICLES_LIST = 1; + const unsigned int INDEX = 2; + const unsigned int NAVIGATION = 3; + const unsigned int SUB_CAT = 4; + const unsigned int GO_UP = 5; } #endif diff --git a/templates/stringbits.txt b/templates/stringbits.txt new file mode 100644 index 0000000..70341af --- /dev/null +++ b/templates/stringbits.txt @@ -0,0 +1,6 @@ +Home +List of Articles +Index Page +Navigation +Sub categories: +Go up \ No newline at end of file