6 // "discount" markdown library is a C library and hence requires to be wrapped in
12 // utility functions for Biaweb that don't fit into any class and can be used by
15 // load a string from file
16 std::string load_from_file (std::string filename) {
17 std::ifstream f (filename);
18 std::string r ((std::istreambuf_iterator<char> (f)),
19 (std::istreambuf_iterator<char> ()));
24 std::string convert_to_markdown (std::string str) {
25 // discount is a C library and it doesn't work well with C++ streams
26 // and there seems no way to get the output of any of these functions
27 // into an std::string.
28 // the only option seems to be to write the output of the markdown()
29 // function to a temporary working file and then read it back into C++
30 // with the normal std::ifstream and feed it into the std::string
31 // till a cleaner solution can be found.
33 doc = mkd_string (str.c_str(), str.size(), 0);
35 strcpy (tempfile, "/tmp/biawebXXXXXX");
36 int fd = mkstemp (tempfile);
37 FILE *f = fdopen (fd, "w");
40 std::string tmpl = load_from_file (tempfile);
46 // convert a document title to a file title - strip out the non-alpha
48 std::string convert_title (std::string title)
51 for (char c : title) {
55 output.append (1, '_');
60 // escape HTML special characters
61 std::string escape_html (std::string source)
63 std::string replace_buf;
64 replace_buf.reserve (source.size());
69 case '&' : replace_buf.append ("&"); break;
70 case '<' : replace_buf.append ("<"); break;
71 case '>' : replace_buf.append (">"); break;
72 case '\"': replace_buf.append ("""); break;
73 case '\'': replace_buf.append ("'"); break;
74 default : replace_buf.append (1, p);