Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/tools/quickbook/detail/quickbook.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 18 years ago

added boost

  • Property svn:executable set to *
File size: 13.5 KB
Line 
1/*=============================================================================
2    Copyright (c) 2002 2004 Joel de Guzman
3    Copyright (c) 2004 Eric Niebler
4    http://spirit.sourceforge.net/
5
6    Use, modification and distribution is subject to the Boost Software
7    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8    http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10#include "../quickbook.hpp"
11#include "../doc_info.hpp"
12#include "utils.hpp"
13#include "actions.hpp"
14#include <boost/spirit/iterator/position_iterator.hpp>
15#include <boost/program_options.hpp>
16
17#include <fstream>
18#include <iostream>
19#include <sstream>
20
21#if (defined(BOOST_MSVC) && (BOOST_MSVC <= 1310))
22#pragma warning(disable:4355)
23#endif
24
25#define QUICKBOOK_VERSION "Quickbook Version 1.1"
26
27namespace quickbook
28{
29    using namespace boost::spirit;
30
31    ///////////////////////////////////////////////////////////////////////////
32    //
33    //  Our actions
34    //
35    ///////////////////////////////////////////////////////////////////////////
36    struct actions
37    {
38        actions(char const* filein_, char const* fileout_)
39            : filename(filein_)
40            , out(fileout_)
41            , table_span(0)
42            , table_header()
43            , source_mode("c++")
44            , code(out, source_mode, macro)
45            , inline_code(phrase, source_mode, macro)
46            , paragraph(out, phrase, section_id, paragraph_pre, paragraph_post)
47            , h1(out, phrase, section_id, h1_pre, h1_post, true)
48            , h2(out, phrase, section_id, h2_pre, h2_post, true)
49            , h3(out, phrase, section_id, h3_pre, h3_post, true)
50            , h4(out, phrase, section_id, h4_pre, h4_post, true)
51            , h5(out, phrase, section_id, h5_pre, h5_post, true)
52            , h6(out, phrase, section_id, h6_pre, h6_post, true)
53            , hr(out, hr_)
54            , blurb(out, phrase, section_id, blurb_pre, blurb_post)
55            , blockquote(out, phrase, section_id, blockquote_pre, blockquote_post)
56            , preformatted(out, phrase, section_id, preformatted_pre, preformatted_post)
57            , plain_char(phrase)
58            , raw_char(phrase)
59            , image(phrase)
60            , list_buffer()
61            , list_marks()
62            , indent(-1)
63            , list(out, list_buffer, indent, list_marks)
64            , list_format(list_buffer, indent, list_marks)
65            , list_item(list_buffer, phrase, section_id, list_item_pre, list_item_post)
66            , funcref_pre(phrase, funcref_pre_)
67            , funcref_post(phrase, funcref_post_)
68            , classref_pre(phrase, classref_pre_)
69            , classref_post(phrase, classref_post_)
70            , memberref_pre(phrase, memberref_pre_)
71            , memberref_post(phrase, memberref_post_)
72            , enumref_pre(phrase, enumref_pre_)
73            , enumref_post(phrase, enumref_post_)
74            , headerref_pre(phrase, headerref_pre_)
75            , headerref_post(phrase, headerref_post_)
76            , bold_pre(phrase, bold_pre_)
77            , bold_post(phrase, bold_post_)
78            , italic_pre(phrase, italic_pre_)
79            , italic_post(phrase, italic_post_)
80            , underline_pre(phrase, underline_pre_)
81            , underline_post(phrase, underline_post_)
82            , teletype_pre(phrase, teletype_pre_)
83            , teletype_post(phrase, teletype_post_)
84            , simple_bold(phrase, bold_pre_, bold_post_)
85            , simple_italic(phrase, italic_pre_, italic_post_)
86            , simple_underline(phrase, underline_pre_, underline_post_)
87            , simple_teletype(phrase, teletype_pre_, teletype_post_)
88            , variablelist(*this)
89            , start_varlistentry(phrase, start_varlistentry_)
90            , end_varlistentry(phrase, end_varlistentry_)
91            , start_varlistterm(phrase, start_varlistterm_)
92            , end_varlistterm(phrase, end_varlistterm_)
93            , start_varlistitem(phrase, start_varlistitem_)
94            , end_varlistitem(phrase, end_varlistitem_)
95            , break_(phrase, break_mark)
96            , identifier(*this)
97            , macro_def(*this)
98            , do_macro(phrase)
99            , url_pre(phrase, url_pre_)
100            , url_post(phrase, url_post_)
101            , link_pre(phrase, link_pre_)
102            , link_post(phrase, link_post_)
103            , table(*this)
104            , start_row(phrase, table_span, table_header)
105            , end_row(phrase, end_row_)
106            , start_cell(phrase, table_span)
107            , end_cell(phrase, end_cell_)
108            , anchor(out)
109            , begin_section(out, doc_id, section_id)
110            , end_section(out, "</section>")
111            , xinclude(out)
112        {
113            std::cout << "Generating Output File: "
114                << fileout_
115                << std::endl;
116
117            // add the predefined macros
118            macro.add
119                ("__DATE__", std::string(quickbook_get_date))
120                ("__TIME__", std::string(quickbook_get_time))
121                ("__FILENAME__", std::string(filein_))
122            ;
123        }
124
125        typedef indentifier_action<actions> indentifier_action;
126        typedef macro_def_action<actions> macro_def_action;
127        typedef table_action<actions> table_action;
128        typedef variablelist_action<actions> variablelist_action;
129
130        char const*             filename;
131        std::string             directory;
132        std::string             macro_id;
133        std::string             phrase_save;
134        std::string             table_title;
135        std::ofstream           out;
136        error_action            error;
137
138        typedef std::vector<std::string> copyright_list;
139        typedef std::vector<std::pair<std::string, std::string> > author_list;
140
141        std::string             doc_type;
142        std::string             doc_title;
143        std::string             doc_version;
144        std::string             doc_id;
145        std::string             doc_dirname;
146        copyright_list          doc_copyright_years;
147        std::string             doc_copyright_holder;
148        std::string             doc_purpose;
149        std::string             doc_category;
150        author_list             doc_authors;
151        std::string             doc_license;
152        std::string             doc_last_revision;
153
154        std::string             page_title;
155        std::string             section_id;
156        std::string             previous;
157        std::stringstream       phrase;
158        unsigned                table_span;
159        std::string             table_header;
160
161        symbols<std::string>    macro;
162        std::string             source_mode;
163        code_action             code;
164        inline_code_action      inline_code;
165        phrase_action           paragraph, h1, h2, h3, h4, h5, h6;
166        markup_action           hr;
167        phrase_action           blurb, blockquote, preformatted;
168        plain_char_action       plain_char;
169        raw_char_action         raw_char;
170        image_action            image;
171
172        typedef std::pair<char, int> mark_type;
173        std::stringstream       list_buffer;
174        std::stack<mark_type>   list_marks;
175        int                     indent;
176        list_action             list;
177        list_format_action      list_format;
178        phrase_action           list_item;
179
180        link_action             funcref_pre;
181        markup_action           funcref_post;
182        link_action             classref_pre;
183        markup_action           classref_post;
184        link_action             memberref_pre;
185        markup_action           memberref_post;
186        link_action             enumref_pre;
187        markup_action           enumref_post;
188        link_action             headerref_pre;
189        markup_action           headerref_post;
190
191        markup_action           bold_pre;
192        markup_action           bold_post;
193        markup_action           italic_pre;
194        markup_action           italic_post;
195        markup_action           underline_pre;
196        markup_action           underline_post;
197        markup_action           teletype_pre;
198        markup_action           teletype_post;
199
200        simple_phrase_action    simple_bold;
201        simple_phrase_action    simple_italic;
202        simple_phrase_action    simple_underline;
203        simple_phrase_action    simple_teletype;
204
205        variablelist_action     variablelist;
206        markup_action           start_varlistentry;
207        markup_action           end_varlistentry;
208        markup_action           start_varlistterm;
209        markup_action           end_varlistterm;
210        markup_action           start_varlistitem;
211        markup_action           end_varlistitem;
212
213        markup_action           break_;
214        indentifier_action      identifier;
215        macro_def_action        macro_def;
216        do_macro_action         do_macro;
217        link_action             url_pre;
218        markup_action           url_post;
219        link_action             link_pre;
220        markup_action           link_post;
221        table_action            table;
222        start_row_action        start_row;
223        markup_action           end_row;
224        start_col_action        start_cell;
225        markup_action           end_cell;
226        anchor_action           anchor;
227
228        begin_section_action    begin_section;
229        markup_action           end_section;
230        xinclude_action         xinclude;
231    };
232
233    ///////////////////////////////////////////////////////////////////////////
234    //
235    //  Load a file
236    //
237    ///////////////////////////////////////////////////////////////////////////
238    typedef std::vector<char> file_storage;
239
240    static int
241    load(char const* filename, file_storage& storage)
242    {
243        using std::cerr;
244        using std::endl;
245        using std::ios;
246        using std::ifstream;
247        using std::istream_iterator;
248
249        ifstream in(filename);
250
251        if (!in)
252        {
253            cerr << "Could not open input file: " << filename << endl;
254            return 1;
255        }
256
257        // Turn off white space skipping on the stream
258        in.unsetf(ios::skipws);
259
260        std::copy(
261            istream_iterator<char>(in),
262            istream_iterator<char>(),
263            std::back_inserter(storage));
264
265        //  ensure that we have enough trailing newlines to eliminate
266        //  the need to check for end of file in the grammar.
267        storage.push_back('\n');
268        storage.push_back('\n');
269        return 0;
270    }
271
272    ///////////////////////////////////////////////////////////////////////////
273    //
274    //  Parse a file
275    //
276    ///////////////////////////////////////////////////////////////////////////
277    static int
278    parse(char const* filein_, char const* fileout_)
279    {
280        using std::cerr;
281        using std::vector;
282        using std::string;
283
284        file_storage storage;
285        int err = quickbook::load(filein_, storage);
286        if (err != 0)
287            return err;
288
289        typedef position_iterator<file_storage::const_iterator> iterator_type;
290        iterator_type first(storage.begin(), storage.end(), filein_);
291        iterator_type last(storage.end(), storage.end());
292
293        actions actor(filein_, fileout_);
294
295        doc_info_grammar<actions> l(actor);
296        parse_info<iterator_type> info = parse(first, last, l);
297
298        if (info.hit)
299        {
300            pre(actor.out, actor);
301
302            first = info.stop;
303            quickbook_grammar<actions> g(actor);
304            info = parse(first, last, g);
305            if (info.full)
306            {
307                post(actor.out, actor);
308            }
309        }
310
311        if (!info.full)
312        {
313            file_position const pos = info.stop.get_position();
314            cerr
315                << "Syntax error at \"" << pos.file
316                << "\" line " << pos.line
317                << ", column " << pos.column << ".\n";
318            return 1;
319        }
320
321        return 0;
322    }
323}
324
325///////////////////////////////////////////////////////////////////////////
326//
327//  Main program
328//
329///////////////////////////////////////////////////////////////////////////
330int
331main(int argc, char* argv[])
332{
333    try 
334    {
335        using boost::program_options::options_description;
336        using boost::program_options::variables_map;
337        using boost::program_options::store;
338        using boost::program_options::parse_command_line;
339        using boost::program_options::notify;
340
341        options_description desc("Allowed options");
342        desc.add_options()
343            ("help", "produce help message")
344            ("version,v", "print version string")
345        ;
346   
347        variables_map vm;
348        store(parse_command_line(argc, argv, desc), vm);
349        notify(vm);   
350   
351        if (vm.count("help")) 
352        {
353            std::cout << desc << "\n";
354            return 0;
355        }
356
357        if (vm.count("version"))
358        {
359            std::cout << QUICKBOOK_VERSION << std::endl;
360            return 0;
361        }
362
363        if (argc > 1)
364        {
365            std::string fileout;
366            if (argc == 2)
367            {
368                fileout = quickbook::detail::remove_extension(argv[1]);
369                fileout += ".xml";
370            }
371            else
372            {
373                fileout = argv[2];
374            }
375   
376            return quickbook::parse(argv[1], fileout.c_str());
377        }
378        else
379        {
380            std::cerr << "Error: No filename given" << std::endl;
381        }
382    }
383   
384    catch(std::exception& e) 
385    {
386        std::cerr << "Error: " << e.what() << "\n";
387        return 1;
388    }
389
390    catch(...) 
391    {
392        std::cerr << "Error: Exception of unknown type caught\n";
393    }
394
395    return 0;
396}
Note: See TracBrowser for help on using the repository browser.