summaryrefslogtreecommitdiff
path: root/src/producer.cpp
blob: c650459a1d781002b7b42140e74b07172d708bbc (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "producer.hpp"


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Texo Producer
 */
TexoProducer::TexoProducer(TexoExporter &exporter): exporter(exporter)
{}  // Just save exporter for future use.


bool TexoProducer::End()
{  // By default, no checks performed, so no error produced.
    return true;
}


bool TexoProducer::Put(char c)
{  // By default characters are not escaped, error means error in exporter.
    return exporter.Put(c);
}


// Block Signal Handlers
// By default, nothing has to be done, and no error produced,
// except for paragraphs, which must be implemented.
bool TexoProducer::Code()             { return true; }
bool TexoProducer::Header(int level)  { return true; }
bool TexoProducer::Quote()            { return true; }


// Modificator Signal Handlers
// By default, nothing has to be done, and no error produced.
bool TexoProducer::Bold()             { return true; }
bool TexoProducer::Italic()           { return true; }
bool TexoProducer::Mono()             { return true; }
bool TexoProducer::Strike()           { return true; }
bool TexoProducer::Underline()        { return true; }

bool TexoProducer::Link(
    const ScriptVariable &link,
    const ScriptVariable &title
)
{
    return true;
}

bool TexoProducer::Link()
{
    return true;
}


bool TexoProducer::PutImage(
    const ScriptVariable &src,
    const ScriptVariable &alt,
    const ScriptVariable &title
)
{
    return true;
}

bool TexoProducer::PutHorizontalRule()
{
    return true;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Texo Strict Producer
 */
TexoProducerStrict::TexoProducerStrict(TexoExporter &exporter):
    TexoProducer(exporter), block(block_none), opened(0)
{}


bool TexoProducerStrict::End()
{
    bool ok = CloseBlock();
    block   = block_none;
    return ok;
}


bool TexoProducerStrict::Put(char c)
{
    return Start() && TruePut(c);
}


bool TexoProducerStrict::Code()
{
    if (CloseBlock()) {
        block = code;
        return StartCode();
    } else {
        return false;
    }
}

bool TexoProducerStrict::Header(int level)
{
    if (CloseBlock()) {
        block        = header;
        header_level = level;
        return StartHeader(level);
    } else {
        return false;
    }
}

bool TexoProducerStrict::Paragraph()
{
    if (CloseBlock()) {
        block = paragraph;
        return StartParagraph();
    } else {
        return false;
    }
}

bool TexoProducerStrict::Quote()
{
    if (CloseBlock()) {
        block = quote;
        return StartQuote();
    } else {
        return false;
    }
}


bool TexoProducerStrict::Bold()
{
    return SwitchMod(bold);
}

bool TexoProducerStrict::Italic()
{
    return SwitchMod(italic);
}

bool TexoProducerStrict::Mono()
{
    return SwitchMod(mono);
}

bool TexoProducerStrict::Strike()
{
    return SwitchMod(strike);
}

bool TexoProducerStrict::Underline()
{
    return SwitchMod(underline);
}

bool TexoProducerStrict::Link(
    const ScriptVariable &path,
    const ScriptVariable &title
)
{
    if (IsOpened(link)) {
        int closed = CloseMods(link);
        if (closed >= 0) {
            return Reopen(closed);
        } else {
            return false;
        }
    } else {
        link_link  = &path;
        link_title = &title;
        return Start() && OpenMod(link);
    }
}

bool TexoProducerStrict::Link()
{
    if (IsOpened(link)) {
        int closed = CloseMods(link);
        if (closed >= 0) {
            return Reopen(closed);
        } else {
            return false;
        }
    }
    return true;
}

bool TexoProducerStrict::PutImage(
    const ScriptVariable &src,
    const ScriptVariable &alt,
    const ScriptVariable &title
)
{
    return Start() && TruePutImage(src, alt, title);
}

bool TexoProducerStrict::PutHorizontalRule()
{
    return Start() && TruePutHorizontalRule();
}


bool TexoProducerStrict::Start()
{
    if (!block) {
        block = paragraph;
        return StartParagraph();
    } else {
        return true;
    }
}

bool TexoProducerStrict::CloseBlock()
{
    if (CloseMods()) {
        switch (block) {
        case block_none: return true;
        case code:       return CloseCode();
        case header:     return CloseHeader(header_level);
        case paragraph:  return CloseParagraph();
        case quote:      return CloseQuote();
        }
        return true;  // Inpossible, since all cases are in switch.
    } else {
        return false;
    }
}

bool TexoProducerStrict::SwitchMod(Mod mod)
{
    if (IsOpened(mod)) {
        int closed = CloseMods(mod);
        if (closed >= 0) {
            return Reopen(closed);
        } else {
            return false;
        }
    } else {
        return Start() && OpenMod(mod);
    }
}

bool TexoProducerStrict::OpenMod(Mod mod)
{
    mods[opened] = mod;
    ++opened;
    switch (mod) {
    case bold:      return StartBold();
    case italic:    return StartItalic();
    case link:      return StartLink(*link_link, *link_title);
    case mono:      return StartMono();
    case strike:    return StartStrike();
    case underline: return StartUnderline();
    }
    return true;  // Inpossible, since all cases are in switch.
}

bool TexoProducerStrict::CloseMod(Mod mod)
{
    switch (mod) {
    case bold:      return CloseBold();
    case italic:    return CloseItalic();
    case link:      return CloseLink(*link_link, *link_title);
    case mono:      return CloseMono();
    case strike:    return CloseStrike();
    case underline: return CloseUnderline();
    }
    return true;  // Inpossible, since all cases are in switch.
}

int TexoProducerStrict::CloseMods(Mod last)
{
    const int old = opened;
    while (opened) {
        Mod top = mods[--opened];
        if (CloseMod(top)) {
            if (top == last) {
                return old - opened;
            }
        } else {
            return -1;
        }
    }
    return old - opened;
}

bool TexoProducerStrict::CloseMods()
{
    bool ok = true;
    while (ok && opened) {
        ok = CloseMod(mods[--opened]);
    }
    return ok;
}

bool TexoProducerStrict::Reopen(int closed)
{
    bool ok         = true;
    int  new_opened = opened + closed;
    while (ok && opened != new_opened) {
        ok = OpenMod(mods[opened]);
    }
    return ok;
}

bool TexoProducerStrict::IsOpened(Mod mod) const
{
    bool is = false;
    for (int i = opened - 1; !is && i >= 0; --i) {
        is = mods[i] == mod;
    }
    return is;
}