Document tree generation to HTML output completed
[biaweb2.git] / biawebutil.hpp
1 #ifndef __BIAWEBUTIL__
2 #define __BIAWEBUTIL__
3 #include <string>
4
5 // utility functions for Biaweb that don't fit into any class and can be used by
6 // any class
7 namespace biaweb {
8 // convert a document title to a file title - strip out the non-alpha
9 // chars and spaces
10 std::string convert_title (std::string title)
11 {
12 std::string output;
13 for (char c : title) {
14 if (isalnum (c))
15 output.append (1, c);
16 else if (isspace (c))
17 output.append (1, '_');
18 }
19 return output;
20 }
21
22 // escape HTML special characters
23 std::string escape_html (std::string source)
24 {
25 std::string replace_buf;
26 replace_buf.reserve (source.size());
27 for (char p : source)
28 {
29 switch (p)
30 {
31 case '&' : replace_buf.append ("&amp;"); break;
32 case '<' : replace_buf.append ("&lt;"); break;
33 case '>' : replace_buf.append ("&gt;"); break;
34 case '\"': replace_buf.append ("&quot;"); break;
35 case '\'': replace_buf.append ("&apos;"); break;
36 default : replace_buf.append (1, p);
37 }
38 }
39 return replace_buf;
40 }
41 }
42 #endif