4aef44f1a687588496b383e3d30461d896f18020
[biaweb2.git] / biawebsidebar.hpp
1 #ifndef __BIAWEBSIDEBAR__
2 #define __BIAWEBSIDEBAR__
3 #include <string>
4 #include <list>
5 #include <iostream>
6 #include <fstream>
7 #include "biawebutil.hpp"
8
9 // classes to describe the a list of items and sidebar item containers which form part of
10 // main document
11 namespace biaweb {
12 // class to represent a sidebar item which can contain a text and link or only
13 // text
14 class SideBarItem {
15 protected:
16 // sidebar text and url
17 std::string sidebar_text;
18 std::string sidebar_url;
19 public:
20 std::string get_sidebar_text () {
21 return this->sidebar_text;
22 }
23 void set_sidebar_text (std::string text) {
24 this->sidebar_text = text;
25 }
26 std::string get_sidebar_url () {
27 return this->sidebar_url;
28 }
29 void set_sidebar_url (std::string url) {
30 this->sidebar_url = url;
31 }
32 // output to HTML using a template directory specified
33 std::string to_html (std::string templatedir);
34
35 SideBarItem (std::string text = "", std::string url = "") {
36 this->sidebar_text = text;
37 this->sidebar_url = url;
38 }
39 };
40
41 std::string SideBarItem::to_html (std::string templatedir) {
42 std::string html;
43 // if url is not empty it is a link item so load the sidebar link template
44 if (! this->sidebar_url.empty ()) {
45 if (!this->sidebar_text.empty ())
46 {
47 std::ifstream tpl_linkitem (templatedir + "/sidebarlinkitem.tpl.html");
48 std::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
49 (std::istreambuf_iterator<char> ()));
50 tpl_linkitem.close ();
51 std::unique_ptr<char[]> linktxt (new char[tpl_linkitem_str.size()
52 + this->sidebar_text.size ()
53 + this->sidebar_url.size ()] );
54 std::sprintf (linktxt.get(), tpl_linkitem_str.c_str (),
55 this->sidebar_url.c_str (),
56 this->sidebar_text.c_str ());
57 html.append (linktxt.get ());
58 }
59 // no text or url - item is empty - so it should be blank
60 else
61 html = "";
62 }
63 // Non link item. Load the normal sidebar item template.
64 else
65 {
66 std::ifstream tpl_item (templatedir + "/sidebaritem.tpl.html");
67 std::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
68 (std::istreambuf_iterator<char> ()));
69 tpl_item.close ();
70 std::unique_ptr<char[]> txt (new char [tpl_item_str.size () +
71 this->sidebar_text.size ()]);
72 std::sprintf (txt.get (), tpl_item_str.c_str(), this->sidebar_text.c_str());
73 html.append (txt.get ());
74 }
75 return html;
76 }
77
78 // Class to represent a sidebar, which contains one heading and a list of items
79 // either links or non-link items.
80 class SideBar {
81 protected:
82 std::string sidebar_title;
83 std::list <SideBarItem> items;
84
85 public:
86 void set_title (std::string title) {
87 this->sidebar_title = title;
88 }
89
90 void add_sidebar_item (SideBarItem item) {
91 this->items.insert (this->items.cend(), item);
92 }
93
94 // render the sidebar using the template directory specified
95 std::string to_html (std::string templatedir) ;
96 };
97
98 // render the sidebar to HTML representation from the template.
99 std::string SideBar::to_html (std::string templatedir) {
100 std::ifstream sidetpl (templatedir + "/sidebar.tpl.html");
101 std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) ,
102 (std::istreambuf_iterator<char> ()));
103 sidetpl.close ();
104 std::string listitem;
105 // first get the sidebar items and render them to HTML
106 for (SideBarItem item : this->items) {
107 listitem += item.to_html (templatedir);
108 }
109
110 std::unique_ptr<char[]> tpl_final (new char[sidetpl_str.size() +
111 this->sidebar_title.size () +
112 listitem.size () ]);
113
114 std::string html;
115 // if there are items, sidebar should be rendered
116 if (items.size () > 0)
117 {
118 std::sprintf (tpl_final.get (), sidetpl_str.c_str(),
119 this->sidebar_title.c_str(), listitem.c_str()) ;
120 html.append ( tpl_final.get ());
121 }
122 // no items in the sidebar, render as empty string (even if it has a heading)
123 // since heading becomes meaningless without items
124 else
125 html = "";
126 return html;
127 }
128 }
129
130 #endif