X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebutil.hpp;h=279d394471fab1e2fcdb5adf3aac2d14c5afbb39;hp=36aa0df16242a20f6aebfb8d324dd65e4b1e8aba;hb=HEAD;hpb=9dd960dd37c92af1ca6531c72b82849ea59a354c diff --git a/biawebutil.hpp b/biawebutil.hpp index 36aa0df..279d394 100644 --- a/biawebutil.hpp +++ b/biawebutil.hpp @@ -1,10 +1,48 @@ #ifndef __BIAWEBUTIL__ #define __BIAWEBUTIL__ #include +#include + +// "discount" markdown library is a C library and hence requires to be wrapped in +// extern "C" +extern "C" { + #include +} // utility functions for Biaweb that don't fit into any class and can be used by // any class namespace biaweb { + // load a string from file + std::string load_from_file (std::string filename) { + std::ifstream f (filename); + std::string r ((std::istreambuf_iterator (f)), + (std::istreambuf_iterator ())); + return r; + } + + // convert markdown + std::string convert_to_markdown (std::string str) { + // discount is a C library and it doesn't work well with C++ streams + // and there seems no way to get the output of any of these functions + // into an std::string. + // the only option seems to be to write the output of the markdown() + // function to a temporary working file and then read it back into C++ + // with the normal std::ifstream and feed it into the std::string + // till a cleaner solution can be found. + MMIOT *doc; + doc = mkd_string (str.c_str(), str.size(), 0); + char tempfile[20]; + strcpy (tempfile, "/tmp/biawebXXXXXX"); + int fd = mkstemp (tempfile); + FILE *f = fdopen (fd, "w"); + markdown (doc, f, 0); + fclose (f); + std::string tmpl = load_from_file (tempfile); + mkd_cleanup (doc); + remove (tempfile); + return tmpl; + } + // convert a document title to a file title - strip out the non-alpha // chars and spaces std::string convert_title (std::string title)