Document tree generation to HTML output completed
[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 std::string to_html ();
33
34 SideBarItem (std::string text = "", std::string url = "") {
35 this->sidebar_text = text;
36 this->sidebar_url = url;
37 }
38 };
39
40 std::string SideBarItem::to_html () {
41 std::string html;
42 // if url is not empty it is a link item so load the sidebar link template
43 if (! this->sidebar_url.empty ()) {
44 if (!this->sidebar_text.empty ())
45 {
46 std::ifstream tpl_linkitem ("templates/sidebarlinkitem.tpl.html");
47 std::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
48 (std::istreambuf_iterator<char> ()));
49 tpl_linkitem.close ();
50 std::unique_ptr<char[]> linktxt (new char[tpl_linkitem_str.size()
51 + this->sidebar_text.size ()
52 + this->sidebar_url.size ()] );
53 std::sprintf (linktxt.get(), tpl_linkitem_str.c_str (),
54 this->sidebar_url.c_str (),
55 this->sidebar_text.c_str ());
56 html.append (linktxt.get ());
57 }
58 // no text or url - item is empty - so it should be blank
59 else
60 html = "";
61 }
62 // Non link item. Load the normal sidebar item template.
63 else
64 {
65 std::ifstream tpl_item ("templates/sidebaritem.tpl.html");
66 std::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
67 (std::istreambuf_iterator<char> ()));
68 tpl_item.close ();
69 std::unique_ptr<char[]> txt (new char [tpl_item_str.size () +
70 this->sidebar_text.size ()]);
71 std::sprintf (txt.get (), tpl_item_str.c_str(), this->sidebar_text.c_str());
72 html.append (txt.get ());
73 }
74 return html;
75 }
76
77 // Class to represent a sidebar, which contains one heading and a list of items
78 // either links or non-link items.
79 class SideBar {
80 protected:
81 std::string sidebar_title;
82 std::list <SideBarItem> items;
83
84 public:
85 void set_title (std::string title) {
86 this->sidebar_title = title;
87 }
88
89 void add_sidebar_item (SideBarItem item) {
90 items.insert (items.cend(), item);
91 }
92
93 // render the sidebar
94 std::string to_html () ;
95 };
96
97 // render the sidebar to HTML representation from the template.
98 std::string SideBar::to_html () {
99 std::ifstream sidetpl ("templates/sidebar.tpl.html");
100 std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) ,
101 (std::istreambuf_iterator<char> ()));
102 sidetpl.close ();
103 std::string listitem;
104 // first get the sidebar items and render them to HTML
105 for (SideBarItem item : items) {
106 listitem += item.to_html ();
107 }
108
109 std::unique_ptr<char[]> tpl_final (new char[sidetpl_str.size() +
110 this->sidebar_title.size () +
111 listitem.size () ]);
112
113 std::string html;
114 // if there are items, sidebar should be rendered
115 if (items.size () > 0)
116 {
117 std::sprintf (tpl_final.get (), sidetpl_str.c_str(),
118 this->sidebar_title.c_str(), listitem.c_str()) ;
119 html.append ( tpl_final.get ());
120 }
121 // no items in the sidebar, render as empty string (even if it has a heading)
122 // since heading becomes meaningless without items
123 else
124 html = "";
125 return html;
126 }
127 }
128
129 #endif