Separated the strings used for document tree generation from code
[biaweb2.git] / biawebsidebar.hpp
1 #ifndef __BIAWEBSIDEBAR__
2 #define __BIAWEBSIDEBAR__
3 #include <string>
4 #include <list>
5 #include <iostream>
6 #include <fstream>
7 #include <fmt/format.h>
8 #include "biawebutil.hpp"
9
10 // classes to describe the a list of (link) items and sidebar containers which form part of
11 // main document
12 namespace biaweb {
13 // class to represent a generic link item which can contain a text and link or only
14 // text
15 class GenericLinkItem {
16 protected:
17 // sidebar text and url
18 std::string itemtext;
19 std::string itemurl;
20 public:
21 std::string get_item_text () {
22 return this->itemtext;
23 }
24 void set_item_text (std::string text) {
25 this->itemtext = escape_html (text);
26 }
27 std::string get_item_url () {
28 return this->itemurl;
29 }
30 void set_item_url (std::string url) {
31 this->itemurl = escape_html (url);
32 }
33 // output to HTML using a template directory specified
34 std::string to_html (std::string templatedir);
35
36 GenericLinkItem (std::string text = "", std::string url = "") {
37 this->itemtext = escape_html (text);
38 this->itemurl = escape_html (url);
39 }
40 };
41
42 std::string GenericLinkItem::to_html (std::string templatedir) {
43 std::string html = "";
44 // if url is not empty it is a link item so load the sidebar link template
45 if (! this->itemurl.empty ()) {
46 if (!this->itemtext.empty ())
47 {
48 std::ifstream tpl_linkitem (templatedir + "/genericlinklistitem.tpl.html");
49
50 std::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
51 (std::istreambuf_iterator<char> ()));
52 tpl_linkitem.close ();
53 html = fmt::format (tpl_linkitem_str,
54 fmt::arg ("itemurl", this->itemurl),
55 fmt::arg ("itemtext", this->itemtext));
56 }
57 }
58 // Non link item. Load the normal sidebar item template.
59 else if (! this->itemtext.empty ())
60 {
61 std::ifstream tpl_item (templatedir + "/genericlistitem.tpl.html");
62
63 std::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
64 (std::istreambuf_iterator<char> ()));
65 tpl_item.close ();
66 html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->itemtext) );
67 }
68 return html;
69 }
70
71 // Class to represent a sidebar, which contains one heading and a list of items
72 // either links or non-link items.
73 class SideBar {
74 protected:
75 std::string sidebar_title;
76 std::list <GenericLinkItem> items;
77
78 public:
79 void set_title (std::string title) {
80 this->sidebar_title = title;
81 }
82
83 void add_sidebar_item (GenericLinkItem item) {
84 this->items.insert (this->items.cend(), item);
85 }
86
87 // render the sidebar using the template directory specified
88 std::string to_html (std::string templatedir) ;
89 };
90
91 // render the sidebar to HTML representation from the template.
92 std::string SideBar::to_html (std::string templatedir) {
93 std::ifstream sidetpl (templatedir + "/sidebar.tpl.html");
94 std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) ,
95 (std::istreambuf_iterator<char> ()));
96 sidetpl.close ();
97 std::string listitems;
98 // first get the sidebar items and render them to HTML
99 for (GenericLinkItem item : this->items) {
100 listitems += item.to_html (templatedir);
101 }
102
103 std::string html = "";
104 // if there are items, sidebar should be rendered otherwise not needed
105 if (items.size () > 0)
106 html = fmt::format (sidetpl_str,
107 fmt::arg ("title", this->sidebar_title),
108 fmt::arg ("items", listitems));
109
110 return html;
111 }
112 }
113
114 #endif