summaryrefslogtreecommitdiff
path: root/src/backend/filters/plaintohtml.cpp
blob: edb620836091a5011e3abc8c1cfadd36c7f77efe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2014 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "backend/filters/plaintohtml.h"

#include <QDebug>

// Sword includes:
#include <swbuf.h>


char Filters::PlainToHtml::processText(sword::SWBuf &text,
                                       const sword::SWKey * /*key*/,
                                       const sword::SWModule * /*module*/)
{
    sword::SWBuf orig = text;
    const char * from = orig.c_str();
    bool inFootNote = false;

    for (text = "<p>"; *from; from++) {
        switch (*from) {

        case '\n':
            if (text.size() > 3) { // ignore leading newlines
                if (from[1] == '\n') { // two or more newlines denote a new paragraph
                    text += "</p><p>";
                    do {
                        from++;
                    } while (from[1] == '\n');
                } else { // only one new line
                    text += "<br/>";
                }
            }
            break;

        case '<':
            // This is a special case: Newlines in the plaintext editor are stored as <br />, not as \n
            // we need to let them through
            /// \todo is this quirk necessary?
            if ((from[1] == 'b')
                && (from[2] == 'r')
                && (from[3] == ' ')
                && (from[4] == '/')
                && (from[5] == '>'))
            {
                text += "<br/>";
                from += 5;
            } else {
                text += "&lt;";
            }
            break;

        case '>':
            text += "&gt;";
            break;

        case '&':
            text += "&amp;";
            break;

        case '{': // footnote start
            if (inFootNote) {
                text += *from;
            } else {
                text += "<span class=\"footnote\">";
                inFootNote = true;
            }
            break;

        case '}': // footnote end
            if (inFootNote) {
                text += "</span>";
                inFootNote = false;
            }
            // fall through:

        default:
            text += *from;
            break;

        }
    }

    // Handle missing footnode end:
    if (inFootNote) {
        qWarning() << "PlainToHtml filter detected missing footnote end.";
        text += "</span>";
    }

    text += "</p>";
    return 0;
}