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-2026 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#ifdef __GNUC__
19#pragma GCC diagnostic push
20#pragma GCC diagnostic ignored "-Wextra-semi"
21#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
22#endif
23#include <swbuf.h>
24#ifdef __GNUC__
25#pragma GCC diagnostic pop
26#endif
27
28
29char Filters::PlainToHtml::processText(sword::SWBuf &text,
30 const sword::SWKey * /*key*/,
31 const sword::SWModule * /*module*/)
32{
33 sword::SWBuf orig = text;
34 const char * from = orig.c_str();
35 bool inFootNote = false;
36
37 for (text = "<p>"; *from; from++) {
38 switch (*from) {
39
40 case '\n':
41 if (text.size() > 3) { // ignore leading newlines
42 if (from[1] == '\n') { // two or more newlines denote a new paragraph
43 text += "</p><p>";
44 do {
45 from++;
46 } while (from[1] == '\n');
47 } else { // only one new line
48 text += "<br/>";
49 }
50 }
51 break;
52
53 case '<':
54 // This is a special case: Newlines in the plaintext editor are stored as <br />, not as \n
55 // we need to let them through
56 /// \todo is this quirk necessary?
57 if ((from[1] == 'b')
58 && (from[2] == 'r')
59 && (from[3] == ' ')
60 && (from[4] == '/')
61 && (from[5] == '>'))
62 {
63 text += "<br/>";
64 from += 5;
65 } else {
66 text += "&lt;";
67 }
68 break;
69
70 case '>':
71 text += "&gt;";
72 break;
73
74 case '&':
75 text += "&amp;";
76 break;
77
78 case '{': // footnote start
79 if (inFootNote) {
80 text += *from;
81 } else {
82 text += "<span class=\"footnote\">";
83 inFootNote = true;
84 }
85 break;
86
87 case '}': // footnote end
88 if (inFootNote) {
89 text += "</span>";
90 inFootNote = false;
91 }
92 [[fallthrough]];
93
94 default:
95 text += *from;
96 break;
97
98 }
99 }
100
101 // Handle missing footnode end:
102 if (inFootNote) {
103 qWarning() << "PlainToHtml filter detected missing footnote end.";
104 text += "</span>";
105 }
106
107 text += "</p>";
108 return 0;
109}
char processText(sword::SWBuf &buf, const sword::SWKey *key, const sword::SWModule *module=nullptr) override