BibleTime
plaintohtml.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "plaintohtml.h"
14 
15 #include <QDebug>
16 
17 // Sword includes:
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wextra-semi"
20 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
21 #include <swbuf.h>
22 #pragma GCC diagnostic pop
23 
24 
25 char Filters::PlainToHtml::processText(sword::SWBuf &text,
26  const sword::SWKey * /*key*/,
27  const sword::SWModule * /*module*/)
28 {
29  sword::SWBuf orig = text;
30  const char * from = orig.c_str();
31  bool inFootNote = false;
32 
33  for (text = "<p>"; *from; from++) {
34  switch (*from) {
35 
36  case '\n':
37  if (text.size() > 3) { // ignore leading newlines
38  if (from[1] == '\n') { // two or more newlines denote a new paragraph
39  text += "</p><p>";
40  do {
41  from++;
42  } while (from[1] == '\n');
43  } else { // only one new line
44  text += "<br/>";
45  }
46  }
47  break;
48 
49  case '<':
50  // This is a special case: Newlines in the plaintext editor are stored as <br />, not as \n
51  // we need to let them through
52  /// \todo is this quirk necessary?
53  if ((from[1] == 'b')
54  && (from[2] == 'r')
55  && (from[3] == ' ')
56  && (from[4] == '/')
57  && (from[5] == '>'))
58  {
59  text += "<br/>";
60  from += 5;
61  } else {
62  text += "&lt;";
63  }
64  break;
65 
66  case '>':
67  text += "&gt;";
68  break;
69 
70  case '&':
71  text += "&amp;";
72  break;
73 
74  case '{': // footnote start
75  if (inFootNote) {
76  text += *from;
77  } else {
78  text += "<span class=\"footnote\">";
79  inFootNote = true;
80  }
81  break;
82 
83  case '}': // footnote end
84  if (inFootNote) {
85  text += "</span>";
86  inFootNote = false;
87  }
88  [[fallthrough]];
89 
90  default:
91  text += *from;
92  break;
93 
94  }
95  }
96 
97  // Handle missing footnode end:
98  if (inFootNote) {
99  qWarning() << "PlainToHtml filter detected missing footnote end.";
100  text += "</span>";
101  }
102 
103  text += "</p>";
104  return 0;
105 }
char processText(sword::SWBuf &buf, const sword::SWKey *key, const sword::SWModule *module=nullptr) override
Definition: plaintohtml.cpp:25