summaryrefslogtreecommitdiff
path: root/src/lines.cpp
blob: 3aafde556adaddab76fabf84c16cf409d8a87d80 (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
#include "lines.hpp"


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Texo Lines Producer
 */
TexoProducerLines::TexoProducerLines(TexoExporter & exporter):
    TexoProducer(exporter), newline(true)
{}


bool TexoProducerLines::End()
{
    if (!newline) {
        exporter.Put('\n');
        newline = true;
    }
    return true;
}


bool TexoProducerLines::Put(char c)
{
    if (c == '\n') {
        if (!newline) {
            exporter.Put(' ');
            newline = true;
        }
    } else {
        exporter.Put(c);
        newline = false;
    }
    return true;
}


bool TexoProducerLines::Paragraph()
{
    if (!newline) {
        exporter.Put('\n');
        newline = true;
    }
    return true;
}

bool TexoProducerLines::Quote()
{
    if (!newline) {
        exporter.Put('\n');
    }
    exporter.Put("> ");
    newline = false;
    return true;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Texo Lines Importer
 */
TexoImporterLines::TexoImporterLines(TexoProducer & producer):
    TexoImporter(producer), newline(true), quote(false)
{}


bool TexoImporterLines::TruePut(char c)
{
    if (c == '\n') {
        if (!newline) {
            newline = true;
            quote   = false;
        }
        return true;
    } else if (newline) {
        newline = false;
        if (c == '>') {
            quote = true;
            return producer.Quote();
        } else {
            return producer.Paragraph() && producer.Put(c);
        }
    } else if (quote) {
        if (c != ' ') {
            quote = false;
            return producer.Put(c);
        } else {
            return true;
        }
    } else {
        return producer.Put(c);
    }
}