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


TexoPlainProducer::TexoPlainProducer(TexoExporter &exporter):
    TexoProducer(exporter)
{}

void TexoPlainProducer::Put(const Texo &piece)
{
    switch (piece.type) {
    case Texo::character: exporter.Put(piece.c);
    default:              break;
    }
}


TexoPlainImporter::TexoPlainImporter(TexoProducer &producer):
    TexoImporter(producer), first(true), space(0), newline(false)
{}

void TexoPlainImporter::Put(const char c)
{
    if (first) {
        first = false;
        producer.Put(Texo(Texo::paragraph_begin));
    }
    if (c == ' ') {
        ++space;
    } else if (c == '\n') {
        if (newline) {
            newline = false;
            producer.Put(Texo(Texo::paragraph_end));
            producer.Put(Texo(Texo::paragraph_begin));
        } else {
            newline = true;
        }
    } else if (!c) {
        producer.Put(Texo(Texo::paragraph_end));
    } else {
        if (newline) {
            newline = false;
            if (space) {
                space   = 0;
                producer.Put(Texo(Texo::newline));
            } else {
                producer.Put(Texo(' '));
            }
        }
        while (space) {
            producer.Put(Texo(' '));
            --space;
        }
        producer.Put(Texo(c));
    }
}