Fawkes API  Fawkes Development Version
tracwiki.cpp
1 
2 /***************************************************************************
3  * tracwiki.cpp - Trac wiki style formatter
4  *
5  * Created: Wed May 11 17:04:04 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <webview/formatters/tracwiki.h>
24 
25 #include <core/exception.h>
26 #include <sstream>
27 #include <cstring>
28 #include <cstdio>
29 
30 namespace fawkes {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class TracWikiHeadingFormatter <webview/formatters/tracwiki.h>
36  * Translate Trac wiki heading syntax to HTML.
37  * This class translates Trac wiki style heading identifications and
38  * translates them into HTML.
39  * @author Tim Niemueller
40  */
41 
42 
43 /** Constructor. */
45 {
46  if ( regcomp(&__re_heading, "^(=+) (.*) \\1$", REG_EXTENDED) != 0 ) {
47  throw Exception("Failed to compile heading regex");
48  }
49 
50 }
51 
52 
53 /** Destructor. */
55 {
56  regfree(&__re_heading);
57 }
58 
59 
60 /** Format string.
61  * @param text input text in Trac wiki markup language
62  * @return formatted text
63  */
64 std::string
66 {
67  std::string rv = "";
68 
69  bool in_paragraph = false;
70 
71  std::istringstream iss(text);
72  while (!iss.eof() && !iss.fail()) {
73  std::string line;
74  getline(iss, line);
75  const char *sl = line.c_str();
76 
77  int num_matches = 3;
78  regmatch_t matches[num_matches];
79 
80  if (line == "") {
81  if (in_paragraph) {
82  rv += "</p>\n";
83  in_paragraph = false;
84  }
85  } else if (regexec(&__re_heading, sl, num_matches, matches, 0) == 0) {
86  unsigned int h_depth = matches[1].rm_eo - matches[1].rm_so;
87 
88  if (in_paragraph) {
89  rv += "</p>\n";
90  in_paragraph = false;
91  }
92 
93  char title[matches[2].rm_eo - matches[2].rm_so + 1];
94  title[matches[2].rm_eo - matches[2].rm_so] = 0;
95  strncpy(title, &line.c_str()[matches[2].rm_so],
96  matches[2].rm_eo - matches[2].rm_so);
97  char *tmp;
98  if (asprintf(&tmp, "<h%u>%s</h%u>\n", h_depth, title, h_depth) != -1) {
99  rv += tmp;
100  } else {
101  rv += line;
102  }
103  } else {
104  if (!in_paragraph) {
105  rv += "<p>";
106  in_paragraph = true;
107  }
108  rv += line;
109  }
110  }
111 
112  if (in_paragraph) {
113  rv += "</p>\n";
114  in_paragraph = false;
115  }
116 
117  return rv;
118 }
119 
120 } // end namespace fawkes
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36
TracWikiHeadingFormatter()
Constructor.
Definition: tracwiki.cpp:44
virtual ~TracWikiHeadingFormatter()
Destructor.
Definition: tracwiki.cpp:54
virtual std::string format(std::string &text)
Format string.
Definition: tracwiki.cpp:65