From: Harishankar Date: Sat, 16 May 2020 15:53:22 +0000 (+0530) Subject: Added summary to the document tree index X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=f56e2150cc79638c890ef23ef82097a4bdffd2ce;p=biaweb2.git Added summary to the document tree index Added summary field for the document tree index which will add a summary content for the index before the list of documents --- diff --git a/biaweb.cpp b/biaweb.cpp index b1c827b..ca9c059 100644 --- a/biaweb.cpp +++ b/biaweb.cpp @@ -9,6 +9,8 @@ int main (int argc, char *argv[]) { { std::shared_ptr tree (new DocumentTree (argv[1])); tree.get()->set_stub (""); + tree.get()->set_markdown_summary ("# Hello there\n\n\ +These are some contents for this page."); std::shared_ptr a1 (new DocumentTree("Child a1")); std::shared_ptr a2 (new DocumentTree("Child a2")); diff --git a/biawebdocument.hpp b/biawebdocument.hpp index 0c0b74c..b79b346 100644 --- a/biawebdocument.hpp +++ b/biawebdocument.hpp @@ -136,34 +136,7 @@ namespace biaweb { }; void Document::set_markdown_content (std::string str) { - // discount is a C library and it doesn't work well with C++ streams - // and there seems no way to get the output of any of these functions - // into an std::string. - // the only option seems to be to write the output of the markdown() - // function to a temporary working file and then read it back into C++ - // with the normal std::ifstream and feed it into the std::string - // till a cleaner solution can be found. - MMIOT *doc; - doc = mkd_string (str.c_str(), str.size(), 0); - FILE *f = fopen (".biaweb.tmp", "w"); - markdown (doc, f, 0); - fclose (f); - std::ifstream ftmp (".biaweb.tmp"); - std::string tmpl ( (std::istreambuf_iterator (ftmp)), - (std::istreambuf_iterator ()) - ); - - while (! ftmp.eof ()) - { - std::string line; - ftmp >> line; - tmpl.append (line); - tmpl.append (" "); - } - ftmp.close (); - remove (".biaweb.tmp"); - this->content.append (tmpl); - mkd_cleanup (doc); + this->content = convert_to_markdown (str); } void Document::output_to_html (std::string path) diff --git a/biawebdocumenttree.hpp b/biawebdocumenttree.hpp index cf75c86..a65e334 100644 --- a/biawebdocumenttree.hpp +++ b/biawebdocumenttree.hpp @@ -17,6 +17,9 @@ namespace biaweb { std::list children; // title of this tree std::string title; + // summary for this tree - this is displayed in the index.html file of + // this tree before the list of articles in the tree + std::string summary; // file stub of this tree std::string stub; // list of documents in this tree @@ -40,6 +43,20 @@ namespace biaweb { this->parent = nullptr; } + // set the summary for this tree + void set_summary (std::string summary) { + this->summary = summary; + } + + // set the summary for this tree as markdown text + void set_markdown_summary (std::string summary) { + this->summary = convert_to_markdown (summary); + } + + std::string get_summary () { + return this->summary; + } + // create the document index for this tree void create_tree_html (std::string destdir); @@ -196,7 +213,7 @@ namespace biaweb { doc.add_side_bar (*bar2.get()); doc.output_to_html (filepath); } - index.get()->set_content (article_list.to_html()); + index.get()->set_content (this->summary + article_list.to_html()); index.get()->output_to_html (filepath); diff --git a/biawebutil.hpp b/biawebutil.hpp index 36aa0df..b519615 100644 --- a/biawebutil.hpp +++ b/biawebutil.hpp @@ -1,10 +1,40 @@ #ifndef __BIAWEBUTIL__ #define __BIAWEBUTIL__ #include +// "discount" markdown library is a C library and hence requires to be wrapped in +// extern "C" +extern "C" { + #include +} // utility functions for Biaweb that don't fit into any class and can be used by // any class namespace biaweb { + // convert markdown + + std::string convert_to_markdown (std::string str) { + // discount is a C library and it doesn't work well with C++ streams + // and there seems no way to get the output of any of these functions + // into an std::string. + // the only option seems to be to write the output of the markdown() + // function to a temporary working file and then read it back into C++ + // with the normal std::ifstream and feed it into the std::string + // till a cleaner solution can be found. + MMIOT *doc; + doc = mkd_string (str.c_str(), str.size(), 0); + FILE *f = fopen (".biaweb.tmp", "w"); + markdown (doc, f, 0); + fclose (f); + std::ifstream ftmp (".biaweb.tmp"); + std::string tmpl ( (std::istreambuf_iterator (ftmp)), + (std::istreambuf_iterator ()) + ); + ftmp.close (); + mkd_cleanup (doc); + remove (".biaweb.tmp"); + return tmpl; + } + // convert a document title to a file title - strip out the non-alpha // chars and spaces std::string convert_title (std::string title)