summaryrefslogtreecommitdiff
path: root/tools/quickbook/src/quickbook.cpp
blob: fc197b58c76eeaad1b865fe9e4f5d912f5bd98bb (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*=============================================================================
    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)
=============================================================================*/
#include "grammar.hpp"
#include "quickbook.hpp"
#include "actions_class.hpp"
#include "post_process.hpp"
#include "utils.hpp"
#include "files.hpp"
#include "input_path.hpp"
#include "id_manager.hpp"
#include <boost/program_options.hpp>
#include <boost/filesystem/v3/path.hpp>
#include <boost/filesystem/v3/operations.hpp>
#include <boost/filesystem/v3/fstream.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/ref.hpp>
#include <boost/version.hpp>

#include <stdexcept>
#include <vector>
#include <iterator>

#if defined(_WIN32)
#include <windows.h>
#include <shellapi.h>
#endif

#if (defined(BOOST_MSVC) && (BOOST_MSVC <= 1310))
#pragma warning(disable:4355)
#endif

#define QUICKBOOK_VERSION "Quickbook Version 1.5.7"

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

    tm* current_time; // the current time
    tm* current_gm_time; // the current UTC time
    bool debug_mode; // for quickbook developers only
    bool self_linked_headers;
    bool ms_errors = false; // output errors/warnings as if for VS
    std::vector<fs::path> include_path;
    std::vector<std::string> preset_defines;
    fs::path image_location;

    static void set_macros(actions& actor)
    {
        for(std::vector<std::string>::const_iterator
                it = preset_defines.begin(),
                end = preset_defines.end();
                it != end; ++it)
        {
            parse_iterator first(it->begin());
            parse_iterator last(it->end());

            cl::parse_info<parse_iterator> info =
                cl::parse(first, last, actor.grammar().command_line_macro);

            if (!info.full) {
                detail::outerr()
                    << "Error parsing command line definition: '"
                    << detail::utf8(*it)
                    << "'"
                    << std::endl;
                ++actor.error_count;
            }
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    //
    //  Parse a file
    //
    ///////////////////////////////////////////////////////////////////////////
    void parse_file(actions& actor, value include_doc_id, bool nested_file)
    {
        parse_iterator first(actor.current_file->source.begin());
        parse_iterator last(actor.current_file->source.end());

        cl::parse_info<parse_iterator> info = cl::parse(first, last, actor.grammar().doc_info);
        assert(info.hit);

        if (!actor.error_count)
        {
            parse_iterator pos = info.stop;
            std::string doc_type = pre(actor, pos, include_doc_id, nested_file);

            info = cl::parse(info.hit ? info.stop : first, last, actor.grammar().block);

            post(actor, doc_type);

            if (!info.full)
            {
                file_position const& pos = actor.current_file->position_of(info.stop.base());
                detail::outerr(actor.current_file->path, pos.line)
                    << "Syntax Error near column " << pos.column << ".\n";
                ++actor.error_count;
            }
        }
    }

    static int
    parse_document(
        fs::path const& filein_
      , fs::path const& fileout_
      , fs::path const& xinclude_base_
      , int indent
      , int linewidth
      , bool pretty_print)
    {
        string_stream buffer;
        id_manager ids;

        int result = 0;

        try {
            actions actor(filein_, xinclude_base_, buffer, ids);
            set_macros(actor);

            if (actor.error_count == 0) {
                actor.current_file = load(filein_); // Throws load_error

                parse_file(actor);

                if(actor.error_count) {
                    detail::outerr()
                        << "Error count: " << actor.error_count << ".\n";
                }
            }

            result = actor.error_count ? 1 : 0;
        }
        catch (load_error& e) {
            detail::outerr(filein_) << detail::utf8(e.what()) << std::endl;
            result = 1;
        }

        if (result == 0)
        {
            std::string stage2 = ids.replace_placeholders(buffer.str());

            fs::ofstream fileout(fileout_);

            if (fileout.fail()) {
                ::quickbook::detail::outerr()
                    << "Error opening output file "
                    << fileout_
                    << std::endl;

                return 1;
            }

            if (pretty_print)
            {
                try
                {
                    fileout << post_process(stage2, indent, linewidth);
                }
                catch (quickbook::post_process_failure&)
                {
                    // fallback!
                    ::quickbook::detail::outerr()
                        << "Post Processing Failed."
                        << std::endl;
                    fileout << stage2;
                    return 1;
                }
            }
            else
            {
                fileout << stage2;
            }

            if (fileout.fail()) {
                ::quickbook::detail::outerr()
                    << "Error writing to output file "
                    << fileout_
                    << std::endl;

                return 1;
            }
        }

        return result;
    }
}

///////////////////////////////////////////////////////////////////////////
//
//  Main program
//
///////////////////////////////////////////////////////////////////////////
int
main(int argc, char* argv[])
{
    try
    {
        namespace fs = boost::filesystem;
        namespace po = boost::program_options;

        using boost::program_options::options_description;
        using boost::program_options::variables_map;
        using boost::program_options::store;
        using boost::program_options::parse_command_line;
        using boost::program_options::wcommand_line_parser;
        using boost::program_options::command_line_parser;
        using boost::program_options::notify;
        using boost::program_options::positional_options_description;
        
        using quickbook::detail::input_string;

        // First thing, the filesystem should record the current working directory.
        fs::initial_path<fs::path>();
        
        // Various initialisation methods
        quickbook::detail::initialise_output();
        quickbook::detail::initialise_markups();

        options_description desc("Allowed options");
        options_description hidden("Hidden options");
        options_description all("All options");

#if QUICKBOOK_WIDE_PATHS
#define PO_VALUE po::wvalue
#else
#define PO_VALUE po::value
#endif

        desc.add_options()
            ("help", "produce help message")
            ("version", "print version string")
            ("no-pretty-print", "disable XML pretty printing")
            ("no-self-linked-headers", "stop headers linking to themselves")
            ("indent", PO_VALUE<int>(), "indent spaces")
            ("linewidth", PO_VALUE<int>(), "line width")
            ("input-file", PO_VALUE<input_string>(), "input file")
            ("output-file", PO_VALUE<input_string>(), "output file")
            ("debug", "debug mode (for developers)")
            ("ms-errors", "use Microsoft Visual Studio style error & warn message format")
            ("include-path,I", PO_VALUE< std::vector<input_string> >(), "include path")
            ("define,D", PO_VALUE< std::vector<input_string> >(), "define macro")
            ("image-location", PO_VALUE<input_string>(), "image location")
        ;

        hidden.add_options()
            ("expect-errors",
                "Succeed if the input file contains a correctly handled "
                "error, fail otherwise.")
            ("xinclude-base", PO_VALUE<input_string>(),
                "Generate xincludes as if generating for this target "
                "directory.")
        ;

        all.add(desc).add(hidden);

        positional_options_description p;
        p.add("input-file", -1);

        variables_map vm;
        int indent = -1;
        int linewidth = -1;
        bool pretty_print = true;

#if QUICKBOOK_WIDE_PATHS
        quickbook::ignore_variable(&argc);
        quickbook::ignore_variable(&argv);

        int wide_argc;
        LPWSTR* wide_argv = CommandLineToArgvW(GetCommandLineW(), &wide_argc);
        if (!wide_argv)
        {
            quickbook::detail::outerr() << "Error getting argument values." << std::endl;
            return 1;
        }

        store(
            wcommand_line_parser(wide_argc, wide_argv)
                .options(all)
                .positional(p)
                .run(), vm);

        LocalFree(wide_argv);
#else
        store(command_line_parser(argc, argv)
                .options(all)
                .positional(p)
                .run(), vm);
#endif

        notify(vm);

        bool expect_errors = vm.count("expect-errors");
        int error_count = 0;

        if (vm.count("help"))
        {
            std::ostringstream description_text;
            description_text << desc;

            quickbook::detail::out()
                << quickbook::detail::utf8(description_text.str()) << "\n";

            return 0;
        }

        if (vm.count("version"))
        {
            std::string boost_version = BOOST_LIB_VERSION;
            boost::replace(boost_version, '_', '.');
        
            quickbook::detail::out()
                << QUICKBOOK_VERSION
                << " (Boost "
                << quickbook::detail::utf8(boost_version)
                << ")"
                << std::endl;
            return 0;
        }

        if (vm.count("ms-errors"))
            quickbook::ms_errors = true;

        if (vm.count("no-pretty-print"))
            pretty_print = false;

        quickbook::self_linked_headers = !vm.count("no-self-link-headers");

        if (vm.count("indent"))
            indent = vm["indent"].as<int>();

        if (vm.count("linewidth"))
            linewidth = vm["linewidth"].as<int>();

        if (vm.count("debug"))
        {
            static tm timeinfo;
            timeinfo.tm_year = 2000 - 1900;
            timeinfo.tm_mon = 12 - 1;
            timeinfo.tm_mday = 20;
            timeinfo.tm_hour = 12;
            timeinfo.tm_min = 0;
            timeinfo.tm_sec = 0;
            timeinfo.tm_isdst = -1;
            mktime(&timeinfo);
            quickbook::current_time = &timeinfo;
            quickbook::current_gm_time = &timeinfo;
            quickbook::debug_mode = true;
        }
        else
        {
            time_t t = std::time(0);
            static tm lt = *localtime(&t);
            static tm gmt = *gmtime(&t);
            quickbook::current_time = &lt;
            quickbook::current_gm_time = &gmt;
            quickbook::debug_mode = false;
        }
        
        quickbook::include_path.clear();
        if (vm.count("include-path"))
        {
            boost::transform(
                vm["include-path"].as<std::vector<input_string> >(),
                std::back_inserter(quickbook::include_path),
                quickbook::detail::input_to_path);
        }

        quickbook::preset_defines.clear();
        if (vm.count("define"))
        {
            boost::transform(
                vm["define"].as<std::vector<input_string> >(),
                std::back_inserter(quickbook::preset_defines),
                quickbook::detail::input_to_utf8);
        }

        if (vm.count("input-file"))
        {
            fs::path filein = quickbook::detail::input_to_path(
                vm["input-file"].as<input_string>());
            fs::path fileout;

            if (vm.count("output-file"))
            {
                fileout = quickbook::detail::input_to_path(
                    vm["output-file"].as<input_string>());
            }
            else
            {
                fileout = filein;
                fileout.replace_extension(".xml");
            }
            
            fs::path xinclude_base;
            if (vm.count("xinclude-base"))
            {
                xinclude_base = quickbook::detail::input_to_path(
                    vm["xinclude-base"].as<input_string>());
            }
            else
            {
                xinclude_base = fileout.parent_path();
                if (xinclude_base.empty())
                    xinclude_base = ".";
            }

            if (!fs::is_directory(xinclude_base))
            {
                quickbook::detail::outerr()
                    << (vm.count("xinclude-base") ?
                        "xinclude-base is not a directory" :
                        "parent directory not found for output file");
                ++error_count;
            }

            if (vm.count("image-location"))
            {
                quickbook::image_location = quickbook::detail::input_to_path(
                    vm["image-location"].as<input_string>());
            }
            else
            {
                quickbook::image_location = filein.parent_path() / "html";
            }

            quickbook::detail::out() << "Generating Output File: "
                << quickbook::detail::path_to_stream(fileout)
                << std::endl;

            if (!error_count)
                error_count += quickbook::parse_document(filein, fileout, xinclude_base, indent, linewidth, pretty_print);

            if (expect_errors)
            {
                if (!error_count) quickbook::detail::outerr() << "No errors detected for --expect-errors." << std::endl;
                return !error_count;
            }
            else
            {
                return error_count;
            }
        }
        else
        {
            std::ostringstream description_text;
            description_text << desc;
        
            quickbook::detail::outerr() << "No filename given\n\n"
                << quickbook::detail::utf8(description_text.str()) << std::endl;
            return 1;
        }        
    }

    catch(std::exception& e)
    {
        quickbook::detail::outerr() << quickbook::detail::utf8(e.what()) << "\n";
        return 1;
    }

    catch(...)
    {
        quickbook::detail::outerr() << "Exception of unknown type caught\n";
        return 1;
    }

    return 0;
}