summaryrefslogtreecommitdiff
path: root/src/producer.hpp
blob: 499a7f346b99ad8589e3817ee14f933fb93e5cd7 (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
#ifndef TEXO_INCLUDED_PRODUCER
#define TEXO_INCLUDED_PRODUCER

#include "exporter.hpp"


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Texo Producer
 * Texo interface for producing output based on inner representation.
 */
class TexoProducer {
public:
    TexoProducer(TexoExporter &exporter);  // Setup exporter used for output.


    virtual bool End();  // Notify producer, what input is ended.


    virtual bool Put(char c);  // Output c, possibly escaping it.


    // Block Signal Handlers
    // Only one can be opened at one moment.
    virtual bool Header(int level);
    virtual bool Code();
    virtual bool Paragraph() = 0;
    virtual bool Quote();


    // Modificator Signal Handlers
    // Independently switch one of text modificators.
    virtual bool Bold();
    virtual bool Italic();
    virtual bool Mono();
    virtual bool Strike();
    virtual bool Underline();

    // Switch link to given or switch it off, if no one is given.
    virtual bool Link(const char *link, const char *title);
    virtual bool Link();


    virtual bool PutImage(const char *src, const char *alt, const char *title);
    virtual bool PutHorizontalRule();


protected:
    TexoExporter &exporter;
};


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Texo Strict Producer
 * Converts switch-signal interface of Texo Producer into
 * sequence of start/close signals with disjoint modificator sections,
 * as well as block sections, which are provided on top level.
 */
class TexoProducerStrict: public TexoProducer {
public:
    TexoProducerStrict(TexoExporter & exporter);


    bool End();


    bool Put(char c);


    bool Header(int level);
    bool Code();
    bool Paragraph();
    bool Quote();


    bool Bold();
    bool Italic();
    bool Mono();
    bool Strike();
    bool Underline();

    bool Link(const char *path, const char *title);
    bool Link();

    bool PutImage(const char *src, const char *alt, const char *title);

    bool PutHorizontalRule();


protected:
    virtual bool TruePut(char c) = 0;


    virtual bool StartHeader(int level) = 0;
    virtual bool CloseHeader(int level) = 0;

    virtual bool StartCode()            = 0;
    virtual bool CloseCode()            = 0;

    virtual bool StartParagraph()       = 0;
    virtual bool CloseParagraph()       = 0;

    virtual bool StartQuote()           = 0;
    virtual bool CloseQuote()           = 0;


    virtual bool StartBold()      = 0;
    virtual bool CloseBold()      = 0;

    virtual bool StartItalic()    = 0;
    virtual bool CloseItalic()    = 0;

    virtual bool StartMono()      = 0;
    virtual bool CloseMono()      = 0;

    virtual bool StartStrike()    = 0;
    virtual bool CloseStrike()    = 0;

    virtual bool StartUnderline() = 0;
    virtual bool CloseUnderline() = 0;

    virtual bool StartLink(const char *link, const char *title) = 0;
    virtual bool CloseLink(const char *link, const char *title) = 0;


    virtual bool TruePutImage(const char *src,
                              const char *alt,
                              const char *title) = 0;

    virtual bool TruePutHorizontalRule() = 0;


private:
    enum Block { block_none = 0, code, header, paragraph, quote } block;

    int header_level;


    enum Mod { bold, italic, link, mono, strike, underline } mods[6];
    int opened;

    const char **link_link;
    const char **link_title;


    bool Start();  // Open paragraph if no block is opened.

    bool CloseBlock();

    bool SwitchMod(Mod mod);

    bool OpenMod(Mod mod);

    bool CloseMod(Mod mod);

    // Close all opened mods sequentially, down to (including) last,
    // returning count of closed mods, or negative number in case of error.
    int CloseMods(Mod last);

    // Close all opened mods.
    bool CloseMods();

    bool Reopen(int closed);

    bool IsOpened(Mod mod) const;
};


#endif