Readme.md - added Section for customization and notes
[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 #include "biawebtemplate.hpp"
10
11 // classes to describe the a list of (link) items and sidebar containers which form part of
12 // main document
13 namespace biaweb {
14 // class to represent a generic link item which can contain a text and link or only
15 // text
16 class GenericLinkItem {
17 protected:
18 // sidebar text and url
19 std::string itemtext;
20 std::string itemurl;
21 public:
22 std::string get_item_text () {
23 return this->itemtext;
24 }
25 void set_item_text (std::string text) {
26 this->itemtext = escape_html (text);
27 }
28 std::string get_item_url () {
29 return this->itemurl;
30 }
31 void set_item_url (std::string url) {
32 this->itemurl = escape_html (url);
33 }
34 // output to HTML using a template specified
35 std::string to_html (Template *t);
36
37 GenericLinkItem (std::string text = "", std::string url = "") {
38 this->itemtext = escape_html (text);
39 this->itemurl = escape_html (url);
40 }
41 };
42
43 std::string GenericLinkItem::to_html (Template *t) {
44 std::string html = "";
45 // if url is not empty it is a link item so load the sidebar link template
46 if (! this->itemurl.empty ()) {
47 if (!this->itemtext.empty ())
48 {
49 std::string tpl_linkitem_str = t->get_genericlinklistitem_tpl ();
50 html = fmt::format (tpl_linkitem_str,
51 fmt::arg ("itemurl", this->itemurl),
52 fmt::arg ("itemtext", this->itemtext));
53 }
54 }
55 // Non link item. Load the normal sidebar item template.
56 else if (! this->itemtext.empty ())
57 {
58 std::string tpl_item_str = t->get_genericlistitem_tpl ();
59 html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->itemtext) );
60 }
61 return html;
62 }
63
64 // Class to represent a sidebar, which contains one heading and a list of items
65 // either links or non-link items.
66 class SideBar {
67 protected:
68 std::string sidebar_title;
69 std::list <GenericLinkItem> items;
70
71 public:
72 void set_title (std::string title) {
73 this->sidebar_title = title;
74 }
75
76 void add_sidebar_item (GenericLinkItem item) {
77 this->items.insert (this->items.cend(), item);
78 }
79
80 // render the sidebar using the template specified
81 std::string to_html (Template *t) ;
82 };
83
84 // render the sidebar to HTML representation from the template.
85 std::string SideBar::to_html (Template *t) {
86 std::string sidetpl_str = t->get_sidebar_tpl ();
87
88 std::string listitems;
89 // first get the sidebar items and render them to HTML
90 for (GenericLinkItem item : this->items) {
91 listitems += item.to_html (t);
92 }
93
94 std::string html = "";
95 // if there are items, sidebar should be rendered otherwise not needed
96 if (items.size () > 0)
97 html = fmt::format (sidetpl_str,
98 fmt::arg ("title", this->sidebar_title),
99 fmt::arg ("items", listitems));
100
101 return html;
102 }
103 }
104
105 #endif