Changed the rendering code for templating output
[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 items and sidebar item containers which form part of
11 // main document
12 namespace biaweb {
13 // class to represent a sidebar item which can contain a text and link or only
14 // text
15 class SideBarItem {
16 protected:
17 // sidebar text and url
18 std::string sidebar_text;
19 std::string sidebar_url;
20 public:
21 std::string get_sidebar_text () {
22 return this->sidebar_text;
23 }
24 void set_sidebar_text (std::string text) {
25 this->sidebar_text = escape_html (text);
26 }
27 std::string get_sidebar_url () {
28 return this->sidebar_url;
29 }
30 void set_sidebar_url (std::string url) {
31 this->sidebar_url = escape_html (url);
32 }
33 // output to HTML using a template directory specified
34 std::string to_html (std::string templatedir);
35
36 SideBarItem (std::string text = "", std::string url = "") {
37 this->sidebar_text = escape_html (text);
38 this->sidebar_url = escape_html (url);
39 }
40 };
41
42 std::string SideBarItem::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->sidebar_url.empty ()) {
46 if (!this->sidebar_text.empty ())
47 {
48 std::ifstream tpl_linkitem (templatedir + "/sidebarlinkitem.tpl.html");
49 std::string tpl_linkitem_str ( (std::istreambuf_iterator<char> (tpl_linkitem)),
50 (std::istreambuf_iterator<char> ()));
51 tpl_linkitem.close ();
52 html = fmt::format (tpl_linkitem_str,
53 fmt::arg ("itemurl", this->sidebar_url),
54 fmt::arg ("itemtext", this->sidebar_text));
55 }
56 // no text or url - item is empty - so it should be blank
57 else
58 html = "";
59 }
60 // Non link item. Load the normal sidebar item template.
61 else
62 {
63 std::ifstream tpl_item (templatedir + "/sidebaritem.tpl.html");
64 std::string tpl_item_str ( (std::istreambuf_iterator<char> (tpl_item)),
65 (std::istreambuf_iterator<char> ()));
66 tpl_item.close ();
67 html = fmt::format (tpl_item_str, fmt::arg ("itemtext", this->sidebar_text) );
68 }
69 return html;
70 }
71
72 // Class to represent a sidebar, which contains one heading and a list of items
73 // either links or non-link items.
74 class SideBar {
75 protected:
76 std::string sidebar_title;
77 std::list <SideBarItem> items;
78
79 public:
80 void set_title (std::string title) {
81 this->sidebar_title = title;
82 }
83
84 void add_sidebar_item (SideBarItem item) {
85 this->items.insert (this->items.cend(), item);
86 }
87
88 // render the sidebar using the template directory specified
89 std::string to_html (std::string templatedir) ;
90 };
91
92 // render the sidebar to HTML representation from the template.
93 std::string SideBar::to_html (std::string templatedir) {
94 std::ifstream sidetpl (templatedir + "/sidebar.tpl.html");
95 std::string sidetpl_str ( ( std::istreambuf_iterator<char> (sidetpl)) ,
96 (std::istreambuf_iterator<char> ()));
97 sidetpl.close ();
98 std::string listitems;
99 // first get the sidebar items and render them to HTML
100 for (SideBarItem item : this->items) {
101 listitems += item.to_html (templatedir);
102 }
103
104 std::string html = "";
105 // if there are items, sidebar should be rendered otherwise not needed
106 if (items.size () > 0)
107 html = fmt::format (sidetpl_str,
108 fmt::arg ("title", this->sidebar_title),
109 fmt::arg ("items", listitems));
110
111 return html;
112 }
113 }
114
115 #endif