summaryrefslogtreecommitdiff
path: root/tools/quickbook/src/actions.hpp
blob: 4f67aec09b687a25b9d124c18f425af17d41eb39 (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
/*=============================================================================
    Copyright (c) 2002 2004 2006 Joel de Guzman
    Copyright (c) 2004 Eric Niebler
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP)
#define BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP

#include <string>
#include <vector>
#include "fwd.hpp"
#include "template_stack.hpp"
#include "utils.hpp"
#include "values.hpp"
#include "scoped.hpp"
#include "symbols.hpp"
#include <boost/spirit/include/classic_parser.hpp>

namespace quickbook
{
    namespace cl = boost::spirit::classic;

    extern unsigned qbk_version_n; // qbk_major_version * 100 + qbk_minor_version

    struct quickbook_range : cl::parser<quickbook_range> {
        quickbook_range(unsigned min_, unsigned max_)
            : min_(min_), max_(max_) {}
        
        template <typename ScannerT>
        typename cl::parser_result<quickbook_range, ScannerT>::type
        parse(ScannerT const& scan) const
        {
            if (qbk_version_n >= min_ && qbk_version_n < max_)
            {
                return scan.empty_match();
            }
            else
            {
                return scan.no_match();
            }
        }

        unsigned min_, max_;
    };
    
    inline quickbook_range qbk_since(unsigned min_) {
        return quickbook_range(min_, 999);
    }
    
    inline quickbook_range qbk_before(unsigned max_) {
        return quickbook_range(0, max_);
    }

    // Throws load_error
    int load_snippets(fs::path const& file, std::vector<template_symbol>& storage,
        std::string const& extension, value::tag_type load_type);

    std::string syntax_highlight(
        parse_iterator first, parse_iterator last,
        actions& escape_actions,
        std::string const& source_mode);        

    struct xinclude_path {
        xinclude_path(fs::path& path, std::string const& uri) :
            path(path), uri(uri) {}

        fs::path path;
        std::string uri;
    };

    xinclude_path calculate_xinclude_path(value const&, quickbook::actions&);

    struct error_message_action
    {
        // Prints an error message to std::cerr

        error_message_action(quickbook::actions& actions, std::string const& m)
            : actions(actions)
            , message(m)
        {}

        void operator()(parse_iterator, parse_iterator) const;

        quickbook::actions& actions;
        std::string message;
    };

    struct error_action
    {
        // Prints an error message to std::cerr

        error_action(quickbook::actions& actions)
        : actions(actions) {}

        void operator()(parse_iterator first, parse_iterator last) const;

        error_message_action operator()(std::string const& message)
        {
            return error_message_action(actions, message);
        }

        quickbook::actions& actions;
    };

    struct element_action
    {
        element_action(quickbook::actions& actions)
            : actions(actions) {}

        void operator()(parse_iterator, parse_iterator) const;

        quickbook::actions& actions;
    };

    struct paragraph_action
    {
        //  implicit paragraphs
        //  doesn't output the paragraph if it's only whitespace.

        paragraph_action(
            quickbook::actions& actions)
        : actions(actions) {}

        void operator()() const;
        void operator()(parse_iterator, parse_iterator) const { (*this)(); }

        quickbook::actions& actions;
    };

    struct list_item_action
    {
        //  implicit paragraphs
        //  doesn't output the paragraph if it's only whitespace.

        list_item_action(
            quickbook::actions& actions)
        : actions(actions) {}

        void operator()() const;
        void operator()(parse_iterator, parse_iterator) const { (*this)(); }

        quickbook::actions& actions;
    };

    struct phrase_end_action
    {
        phrase_end_action(quickbook::actions& actions) :
            actions(actions) {}

        void operator()() const;
        void operator()(parse_iterator, parse_iterator) const { (*this)(); }

        quickbook::actions& actions;
    };

    struct simple_phrase_action
    {
        //  Handles simple text formats

        simple_phrase_action(
            collector& out
          , quickbook::actions& actions)
        : out(out)
        , actions(actions) {}

        void operator()(char) const;

        collector& out;
        quickbook::actions& actions;
    };

    struct cond_phrase_push : scoped_action_base
    {
        cond_phrase_push(quickbook::actions& x)
            : actions(x) {}

        bool start();
        void cleanup();

        quickbook::actions& actions;
        bool saved_conditional;
        std::vector<std::string> anchors;
    };

    extern char const* quickbook_get_date;
    extern char const* quickbook_get_time;

    struct do_macro_action
    {
        // Handles macro substitutions

        do_macro_action(collector& phrase, quickbook::actions& actions)
            : phrase(phrase)
            , actions(actions) {}

        void operator()(std::string const& str) const;
        collector& phrase;
        quickbook::actions& actions;
    };

    struct raw_char_action
    {
        // Prints a space

        raw_char_action(collector& out)
            : out(out) {}

        void operator()(char ch) const;
        void operator()(parse_iterator first, parse_iterator last) const;

        collector& out;
    };

    struct plain_char_action
    {
        // Prints a single plain char.
        // Converts '<' to "&lt;"... etc See utils.hpp

        plain_char_action(collector& phrase, quickbook::actions& actions)
        : phrase(phrase)
        , actions(actions) {}

        void operator()(char ch) const;
        void operator()(parse_iterator first, parse_iterator last) const;

        collector& phrase;
        quickbook::actions& actions;
    };
    
    struct escape_unicode_action
    {
        escape_unicode_action(collector& phrase, quickbook::actions& actions)
        : phrase(phrase)
        , actions(actions) {}
        void operator()(parse_iterator first, parse_iterator last) const;

        collector& phrase;
        quickbook::actions& actions;
    };

    struct code_action
    {
        enum code_type { block, inline_block, inline_ };
    
        // Does the actual syntax highlighing of code

        code_action(
            code_type type
          , quickbook::actions& actions)
        : type(type)
        , actions(actions)
        {
        }

        void operator()(parse_iterator first, parse_iterator last) const;

        code_type type;
        quickbook::actions& actions;
    };

    struct break_action
    {
        break_action(collector& phrase, quickbook::actions& actions)
        : phrase(phrase), actions(actions) {}

        void operator()(parse_iterator f, parse_iterator) const;

        collector& phrase;
        quickbook::actions& actions;
    };

   struct element_id_warning_action
   {
        element_id_warning_action(quickbook::actions& actions_)
            : actions(actions_) {}

        void operator()(parse_iterator first, parse_iterator last) const;

        quickbook::actions& actions;
   };

    // Returns the doc_type, or an empty string if there isn't one.
    std::string pre(quickbook::actions& actions, parse_iterator pos, value include_doc_id, bool nested_file);
    void post(quickbook::actions& actions, std::string const& doc_type);

    struct to_value_scoped_action : scoped_action_base
    {
        to_value_scoped_action(quickbook::actions& actions)
            : actions(actions) {}

        bool start(value::tag_type = value::default_tag);
        void success(parse_iterator, parse_iterator);
        void cleanup();

        quickbook::actions& actions;
        std::vector<std::string> saved_anchors;
        value::tag_type tag;
    };
}

#endif // BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP