Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/tools/quickbook/detail/actions.hpp @ 50

Last change on this file since 50 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 25.7 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#if !defined(BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP)
11#define BOOST_SPIRIT_QUICKBOOK_ACTIONS_HPP
12
13#include <time.h>
14#include <map>
15#include <string>
16#include <vector>
17#include <stack>
18#include <algorithm>
19#include <sstream>
20#include <boost/spirit/iterator/position_iterator.hpp>
21#include <boost/filesystem/operations.hpp>
22#include "../syntax_highlight.hpp"
23#include "utils.hpp"
24
25#ifdef BOOST_MSVC
26// disable copy/assignment could not be generated, unreferenced formal params
27#pragma warning(disable : 4511 4512 4100)
28#endif
29
30namespace quickbook
31{
32    namespace fs = boost::filesystem;
33    typedef std::vector<char> file_storage;
34    typedef position_iterator<file_storage::const_iterator> iterator;
35    typedef std::string::const_iterator string_iterator;
36    struct actions;
37    extern tm* current_time; // the current time
38    extern tm* current_gm_time; // the current UTC time
39    extern bool debug_mode; 
40    extern unsigned qbk_major_version;
41    extern unsigned qbk_minor_version;
42    extern unsigned qbk_version_n; // qbk_major_version * 100 + qbk_minor_version
43
44    // forward declarations
45    struct actions;
46    int parse(char const* filein_, actions& actor, bool ignore_docinfo = false);
47
48    namespace
49    {
50        // Some markups
51   
52        const char* paragraph_pre   = "<para>\n";
53        const char* paragraph_post  = "</para>\n";
54        const char* h1_pre          = "<bridgehead renderas=\"sect1\">";
55        const char* h1_post         = "</bridgehead>";
56        const char* h2_pre          = "<bridgehead renderas=\"sect2\">";
57        const char* h2_post         = "</bridgehead>";
58        const char* h3_pre          = "<bridgehead renderas=\"sect3\">";
59        const char* h3_post         = "</bridgehead>";
60        const char* h4_pre          = "<bridgehead renderas=\"sect4\">";
61        const char* h4_post         = "</bridgehead>";
62        const char* h5_pre          = "<bridgehead renderas=\"sect5\">";
63        const char* h5_post         = "</bridgehead>";
64        const char* h6_pre          = "<bridgehead renderas=\"sect6\">";
65        const char* h6_post         = "</bridgehead>";
66        const char* hr_             = "<para/>";
67   
68        const char* blurb_pre =
69            "<informaltable frame=\"all\">\n"
70            "<?dbhtml table-width=\"74%\" ?>\n"
71            "<tgroup cols=\"1\">\n"
72            "<tbody>\n"
73            "<row>\n"
74            "<entry role=\"blurb\">\n"
75            ;
76   
77        const char* blurb_post =
78            "</entry>\n"
79            "</row>\n"
80            "</tbody>\n"
81            "</tgroup>\n"
82            "</informaltable>\n"
83            ;
84   
85        const char* blockquote_pre      = "<blockquote><para>";
86        const char* blockquote_post     = "</para></blockquote>";
87        const char* preformatted_pre    = "<programlisting>";
88        const char* preformatted_post   = "</programlisting>";
89        const char* warning_pre         = "<warning><para>";
90        const char* warning_post        = "</para></warning>";
91        const char* caution_pre         = "<caution><para>";
92        const char* caution_post        = "</para></caution>";
93        const char* important_pre     = "<important><para>";
94        const char* important_post    = "</para></important>";
95        const char* note_pre            = "<note><para>";
96        const char* note_post           = "</para></note>";
97        const char* tip_pre             = "<tip><para>";
98        const char* tip_post            = "</para></tip>";
99        const char* list_item_pre       = "<listitem>\n";
100        const char* list_item_post      = "\n</listitem>";
101        const char* bold_pre_           = "<emphasis role=\"bold\">";
102        const char* bold_post_          = "</emphasis>";
103        const char* italic_pre_         = "<emphasis>";
104        const char* italic_post_        = "</emphasis>";
105        const char* underline_pre_      = "<emphasis role=\"underline\">";
106        const char* underline_post_     = "</emphasis>";
107        const char* teletype_pre_       = "<literal>";
108        const char* teletype_post_      = "</literal>";
109        const char* strikethrough_pre_  = "<emphasis role=\"strikethrough\">";
110        const char* strikethrough_post_ = "</emphasis>";
111        const char* quote_pre_          = "<quote>";
112        const char* quote_post_         = "</quote>";
113        const char* break_mark          = "<sbr/>\n";
114        const char* url_pre_            = "<ulink url=\"";
115        const char* url_post_           = "</ulink>";
116        const char* link_pre_           = "<link linkend=\"";
117        const char* link_post_          = "</link>";
118        const char* start_varlistentry_ = "<varlistentry>";
119        const char* end_varlistentry_   = "</varlistentry>\n";
120        const char* start_varlistterm_  = "<term>";
121        const char* end_varlistterm_    = "</term>";
122        const char* start_varlistitem_  = "<listitem>";
123        const char* end_varlistitem_    = "</listitem>";
124        const char* start_header_       = "<thead>";
125        const char* end_header_         = "</thead>\n";
126        const char* start_row_          = "<row>";
127        const char* end_row_            = "</row>\n";
128        const char* start_cell_         = "<entry>";
129        const char* end_cell_           = "</entry>";
130        const char* funcref_pre_        = "<functionname alt=\"";
131        const char* funcref_post_       = "</functionname>";
132        const char* classref_pre_       = "<classname alt=\"";
133        const char* classref_post_      = "</classname>";
134        const char* memberref_pre_      = "<methodname alt=\"";
135        const char* memberref_post_     = "</methodname>";
136        const char* enumref_pre_        = "<enumname alt=\"";
137        const char* enumref_post_       = "</enumname>";
138        const char* headerref_pre_      = "<headername alt=\"";
139        const char* headerref_post_     = "</headername>";
140        const char* footnote_pre_       = "<footnote><para>";
141        const char* footnote_post_      = "</para></footnote>";
142        const char* escape_pre_         = "<!--quickbook-escape-prefix-->";
143        const char* escape_post_        = "<!--quickbook-escape-postfix-->";
144        const char* replaceable_pre_    = "<replaceable>";
145        const char* replaceable_post_   = "</replaceable>";
146    }
147   
148    struct error_action
149    {
150        // Prints an error message to std::cerr
151
152        void operator()(iterator const& first, iterator const& /*last*/) const;
153    };
154
155    struct phrase_action
156    {
157        //  blurb, blockquote, preformatted, list_item,
158        //  unordered_list, ordered_list
159
160        phrase_action(
161            std::ostream&       out,
162            std::stringstream&  phrase,
163            std::string const&  pre,
164            std::string const&  post)
165        : out(out)
166        , phrase(phrase)
167        , pre(pre)
168        , post(post) {}
169
170        void operator()(iterator const& first, iterator const& last) const;
171
172        std::ostream&       out;
173        std::stringstream&  phrase;
174        std::string         pre;
175        std::string         post;
176    };
177
178    struct anchored_phrase_action
179    {
180        //  Handles paragraph, h1, h2, h3, h4, h5, h6,
181
182        anchored_phrase_action(
183            std::ostream&       out,
184            std::stringstream&  phrase,
185            std::string const&  library_id,
186            std::string const&  section_id,
187            std::string const&  qualified_section_id,
188            std::string const&  pre,
189            std::string const&  post)
190        : out(out)
191        , phrase(phrase)
192        , library_id(library_id)
193        , section_id(section_id)
194        , qualified_section_id(qualified_section_id)
195        , pre(pre)
196        , post(post) {}
197
198        void operator()(iterator const& first, iterator const& last) const;
199
200        std::ostream&       out;
201        std::stringstream&  phrase;
202        std::string const&  library_id;
203        std::string const&  section_id;
204        std::string const&  qualified_section_id;
205        std::string         pre;
206        std::string         post;
207    };
208
209    struct simple_phrase_action
210    {
211        //  Handles simple text formats
212
213        simple_phrase_action(
214            std::ostream&       out,
215            std::string const&  pre,
216            std::string const&  post)
217        : out(out)
218        , pre(pre)
219        , post(post) {}
220
221        void operator()(iterator first, iterator const& last) const;
222
223        std::ostream&   out;
224        std::string     pre;
225        std::string     post;
226    };
227
228    struct list_action
229    {
230        //  Handles lists
231
232        typedef std::pair<char, int> mark_type;
233        list_action(
234            std::ostream& out
235          , std::stringstream& list_buffer
236          , int& indent
237          , std::stack<mark_type>& list_marks)
238        : out(out)
239        , list_buffer(list_buffer)
240        , indent(indent)
241        , list_marks(list_marks) {}
242
243        void operator()(iterator const& first, iterator const& last) const;
244
245        std::ostream& out;
246        std::stringstream& list_buffer;
247        int& indent;
248        std::stack<mark_type>& list_marks;
249    };
250
251    struct list_format_action
252    {
253        //  Handles list formatting and hierarchy
254
255        typedef std::pair<char, int> mark_type;
256        list_format_action(
257            std::stringstream& out
258          , int& indent
259          , std::stack<mark_type>& list_marks)
260        : out(out)
261        , indent(indent)
262        , list_marks(list_marks) {}
263
264        void operator()(iterator first, iterator const& last) const;
265
266        std::stringstream& out;
267        int& indent;
268        std::stack<mark_type>& list_marks;
269    };
270
271    struct span
272    {
273        // Decorates c++ code fragments
274
275        span(char const* name, std::ostream& out)
276        : name(name), out(out) {}
277
278        void operator()(iterator first, iterator const& last) const;
279
280        char const* name;
281        std::ostream& out;
282    };
283
284    struct unexpected_char
285    {
286        // Handles unexpected chars in c++ syntax
287
288        unexpected_char(std::ostream& out)
289        : out(out) {}
290
291        void operator()(char) const;
292
293        std::ostream& out;
294    };
295
296    struct anchor_action
297    {
298        // Handles anchors
299
300        anchor_action(std::ostream& out)
301            : out(out) {}
302
303        void operator()(iterator first, iterator const& last) const;
304
305        std::ostream& out;
306    };
307
308    namespace
309    {
310        char const* quickbook_get_date = "__quickbook_get_date__";
311        char const* quickbook_get_time = "__quickbook_get_time__";
312    }
313
314    struct do_macro_action
315    {
316        // Handles macro substitutions
317
318        do_macro_action(std::ostream& phrase)
319        : phrase(phrase) {}
320
321        void operator()(std::string const& str) const;
322        std::ostream& phrase;
323    };
324
325    struct space
326    {
327        // Prints a space
328
329        space(std::ostream& out)
330        : out(out) {}
331
332        void operator()(iterator first, iterator const& last) const;
333        void operator()(char ch) const;
334
335        std::ostream& out;
336    };
337
338    struct pre_escape_back
339    {
340        // Escapes back from code to quickbook (Pre)
341
342        pre_escape_back(actions& escape_actions, std::string& save)
343            : escape_actions(escape_actions), save(save) {}
344
345        void operator()(iterator const& first, iterator const& last) const;
346
347        actions& escape_actions;
348        std::string& save;
349    };
350
351    struct post_escape_back
352    {
353        // Escapes back from code to quickbook (Post)
354
355        post_escape_back(std::ostream& out, actions& escape_actions, std::string& save)
356            : out(out), escape_actions(escape_actions), save(save) {}
357
358        void operator()(iterator const& first, iterator const& last) const;
359
360        std::ostream& out;
361        actions& escape_actions;
362        std::string& save;
363    };
364
365    typedef symbols<std::string> macros_type;
366    typedef cpp_highlight<
367        span
368      , space
369      , macros_type
370      , do_macro_action
371      , pre_escape_back
372      , post_escape_back
373      , actions
374      , unexpected_char
375      , std::ostream>
376    cpp_p_type;
377       
378    typedef python_highlight<
379        span
380      , space
381      , macros_type
382      , do_macro_action
383      , pre_escape_back
384      , post_escape_back
385      , actions
386      , unexpected_char
387      , std::ostream>
388    python_p_type;
389
390    struct code_action
391    {
392        // Does the actual syntax highlighing of code
393
394        code_action(std::ostream& out,
395                    std::stringstream& phrase,
396                    std::stringstream& temp,
397                    std::string const& source_mode,
398                    macros_type const& macro,
399                    actions& escape_actions)
400        : out(out)
401        , phrase(phrase)
402        , temp(temp)
403        , source_mode(source_mode)
404        , cpp_p(temp, macro, do_macro_action(temp), escape_actions)
405        , python_p(temp, macro, do_macro_action(temp), escape_actions)
406        {
407        }
408
409        void operator()(iterator const& first, iterator const& last) const;
410
411        std::ostream& out;
412        std::stringstream& phrase;
413        std::stringstream& temp;
414        std::string const& source_mode;
415
416        cpp_p_type cpp_p;
417        python_p_type python_p;
418    };
419
420    struct inline_code_action
421    {
422        // Does the actual syntax highlighing of code inlined in text
423
424        inline_code_action(std::stringstream& out,
425                           std::stringstream& temp,
426                           std::string const& source_mode,
427                           macros_type const& macro,
428                           actions& escape_actions)
429        : out(out)
430        , source_mode(source_mode)
431        , temp(temp)
432        , cpp_p(temp, macro, do_macro_action(temp), escape_actions)
433        , python_p(temp, macro, do_macro_action(temp), escape_actions)
434        {}
435
436        void operator()(iterator const& first, iterator const& last) const;
437
438        std::stringstream& out;
439        std::string const& source_mode;
440        std::stringstream& temp;
441
442        cpp_p_type cpp_p;
443        python_p_type python_p;
444    };
445
446    struct raw_char_action
447    {
448        // Prints a single raw (unprocessed) char.
449        // Allows '<', '>'... etc.
450
451        raw_char_action(std::ostream& phrase)
452        : phrase(phrase) {}
453
454        void operator()(char ch) const;
455        void operator()(iterator const& first, iterator const& /*last*/) const;
456
457        std::ostream& phrase;
458    };
459
460    struct plain_char_action
461    {
462        // Prints a single plain char.
463        // Converts '<' to "&lt;"... etc See utils.hpp
464
465        plain_char_action(std::ostream& phrase)
466        : phrase(phrase) {}
467
468        void operator()(char ch) const;
469        void operator()(iterator const& first, iterator const& /*last*/) const;
470
471        std::ostream& phrase;
472    };
473
474    struct image_action
475    {
476        // Handles inline images
477
478        image_action(std::ostream& phrase)
479        : phrase(phrase) {}
480
481        void operator()(iterator first, iterator const& last) const;
482
483        std::ostream& phrase;
484    };
485   
486    struct markup_action
487    {
488        // A generic markup action
489
490        markup_action(std::ostream& phrase, std::string const& str)
491        : phrase(phrase), str(str) {}
492
493        template <typename T>
494        void operator()(T const&) const
495        {
496            phrase << str;
497        }
498
499        template <typename T>
500        void operator()(T const&, T const&) const
501        {
502            phrase << str;
503        }
504
505        std::ostream& phrase;
506        std::string str;
507    };
508
509    struct indentifier_action
510    {
511        // Handles macro identifiers
512
513        indentifier_action(quickbook::actions& actions)
514        : actions(actions) {}
515
516        void operator()(iterator const& first, iterator const& last) const;
517
518        quickbook::actions& actions;
519    };
520
521    struct macro_def_action
522    {
523        // Handles macro definitions
524
525        macro_def_action(quickbook::actions& actions)
526        : actions(actions) {}
527
528        void operator()(iterator const& first, iterator const& last) const;
529
530        quickbook::actions& actions;
531    };
532
533    struct link_action
534    {
535        // Handles links (URL, XML refentry, function, class, member)
536
537        link_action(std::ostream& phrase, char const* tag)
538        : phrase(phrase), tag(tag) {}
539
540        void operator()(iterator first, iterator const& last) const;
541
542        std::ostream& phrase;
543        char const* tag;
544    };
545
546    struct variablelist_action
547    {
548        // Handles variable lists
549
550        variablelist_action(quickbook::actions& actions)
551        : actions(actions) {}
552
553        void operator()(iterator, iterator) const;
554
555        quickbook::actions& actions;
556    };
557
558    struct table_action
559    {
560        // Handles tables
561
562        table_action(quickbook::actions& actions)
563        : actions(actions) {}
564
565        void operator()(iterator, iterator) const;
566
567        quickbook::actions& actions;
568    };
569
570    struct start_row_action
571    {
572        // Handles table rows
573
574        start_row_action(std::stringstream& phrase, unsigned& span, std::string& header)
575            : phrase(phrase), span(span), header(header) {}
576
577        void operator()(char) const;
578        void operator()(iterator f, iterator) const;
579
580        std::stringstream& phrase;
581        unsigned& span;
582        std::string& header;
583    };
584
585    struct start_col_action
586    {
587        // Handles table columns
588
589        start_col_action(std::ostream& phrase, unsigned& span)
590        : phrase(phrase), span(span) {}
591
592        void operator()(char) const;
593
594        std::ostream& phrase;
595        unsigned& span;
596    };
597
598    struct begin_section_action
599    {
600        // Handles begin page
601
602        begin_section_action(
603            std::ostream& out
604          , std::stringstream& phrase
605          , std::string& library_id
606          , std::string& section_id
607          , int& level
608          , std::string& qualified_section_id)
609        : out(out)
610        , phrase(phrase)
611        , library_id(library_id)
612        , section_id(section_id)
613        , level(level)
614        , qualified_section_id(qualified_section_id) {}
615
616        void operator()(iterator first, iterator const& last) const;
617
618        std::ostream& out;
619        std::stringstream& phrase;
620        std::string& library_id;
621        std::string& section_id;
622        int& level;
623        std::string& qualified_section_id;
624    };
625
626    struct end_section_action
627    {
628        end_section_action(
629            std::ostream& out
630          , int& level
631          , std::string& qualified_section_id)
632        : out(out)
633        , level(level)
634        , qualified_section_id(qualified_section_id) {}
635
636        void operator()(iterator const& first, iterator const& last) const;
637
638        std::ostream& out;
639        int& level;
640        std::string& qualified_section_id;
641   };
642
643    struct xinclude_action
644    {
645        // Handles XML includes
646        xinclude_action(std::ostream& out_, quickbook::actions& actions_)
647            : out(out_), actions(actions_) {}
648
649        void operator()(iterator const& first, iterator const& last) const;
650
651        std::ostream& out;
652        quickbook::actions& actions;
653    };
654
655    struct include_action
656    {
657        // Handles QBK includes
658
659        include_action(quickbook::actions& actions_)
660            : actions(actions_) {}
661
662        void operator()(iterator const& first, iterator const& last) const;
663
664        quickbook::actions& actions;
665    };
666
667    struct xml_author
668    {
669        // Handles xml author
670
671        xml_author(std::ostream& out)
672        : out(out) {}
673
674        void operator()(std::pair<std::string, std::string> const& author) const;
675
676        std::ostream& out;
677    };
678
679    struct xml_year
680    {
681        // Handles xml year
682
683        xml_year(std::ostream& out)
684            : out(out) {}
685
686        void operator()(std::string const &year) const;
687
688        std::ostream& out;
689    };
690
691    void pre(std::ostream& out, quickbook::actions& actions, bool ignore_docinfo = false);
692    void post(std::ostream& out, quickbook::actions& actions, bool ignore_docinfo = false);
693
694    struct phrase_to_string_action
695    {
696        phrase_to_string_action(std::string& out, std::stringstream& phrase)
697            : out(out) , phrase(phrase) {}
698
699        void operator()(iterator const& first, iterator const& last) const;
700
701        std::string&        out;
702        std::stringstream&  phrase;
703    };
704
705    ///////////////////////////////////////////////////////////////////////////
706    //
707    //  Our actions
708    //
709    ///////////////////////////////////////////////////////////////////////////
710    struct actions
711    {
712        actions(char const* filein_, fs::path const& outdir, std::ostream &out_);
713
714        fs::path                filename;
715        fs::path                outdir;
716        std::string             macro_id;
717        std::string             phrase_save;
718        std::string             table_title;
719        std::ostream&           out;
720        error_action            error;
721
722        typedef std::vector<std::string> copyright_list;
723        typedef std::vector<std::pair<std::string, std::string> > author_list;
724
725        std::string             doc_type;
726        std::string             doc_title;
727        std::string             doc_version;
728        std::string             doc_id;
729        std::string             doc_dirname;
730        copyright_list          doc_copyright_years;
731        std::string             doc_copyright_holder;
732        std::string             doc_purpose;
733        std::string             doc_category;
734        author_list             doc_authors;
735        std::string             doc_license;
736        std::string             doc_last_revision;
737        std::string             include_doc_id;
738        phrase_to_string_action extract_doc_license;
739        phrase_to_string_action extract_doc_purpose;
740        std::string             doc_license_1_1;
741        std::string             doc_purpose_1_1;
742
743        std::string             page_title;
744        std::string             section_id;
745        std::string             previous;
746        std::stringstream       phrase;
747        std::stringstream       temp;
748        unsigned                table_span;
749        std::string             table_header;
750
751        macros_type             macro;
752        std::string             source_mode;
753        code_action             code;
754        code_action             code_block;
755        inline_code_action      inline_code;
756        phrase_action           paragraph;
757        anchored_phrase_action  h1, h2, h3, h4, h5, h6;
758        markup_action           hr;
759        phrase_action           blurb, blockquote, preformatted;
760        phrase_action           warning, caution, important, note, tip;
761        plain_char_action       plain_char;
762        raw_char_action         raw_char;
763        image_action            image;
764
765        typedef std::pair<char, int> mark_type;
766        std::stringstream       list_buffer;
767        std::stack<mark_type>   list_marks;
768        int                     indent;
769        list_action             list;
770        list_format_action      list_format;
771        phrase_action           list_item;
772
773        link_action             funcref_pre;
774        markup_action           funcref_post;
775        link_action             classref_pre;
776        markup_action           classref_post;
777        link_action             memberref_pre;
778        markup_action           memberref_post;
779        link_action             enumref_pre;
780        markup_action           enumref_post;
781        link_action             headerref_pre;
782        markup_action           headerref_post;
783
784        markup_action           bold_pre;
785        markup_action           bold_post;
786        markup_action           italic_pre;
787        markup_action           italic_post;
788        markup_action           underline_pre;
789        markup_action           underline_post;
790        markup_action           teletype_pre;
791        markup_action           teletype_post;
792        markup_action           strikethrough_pre;
793        markup_action           strikethrough_post;
794        markup_action           quote_pre;
795        markup_action           quote_post;
796        markup_action           replaceable_pre;
797        markup_action           replaceable_post;
798        markup_action           footnote_pre;
799        markup_action           footnote_post;
800
801        simple_phrase_action    simple_bold;
802        simple_phrase_action    simple_italic;
803        simple_phrase_action    simple_underline;
804        simple_phrase_action    simple_teletype;
805        simple_phrase_action    simple_strikethrough;
806
807        variablelist_action     variablelist;
808        markup_action           start_varlistentry;
809        markup_action           end_varlistentry;
810        markup_action           start_varlistterm;
811        markup_action           end_varlistterm;
812        markup_action           start_varlistitem;
813        markup_action           end_varlistitem;
814
815        markup_action           break_;
816        indentifier_action      identifier;
817        macro_def_action        macro_def;
818        do_macro_action         do_macro;
819        link_action             url_pre;
820        markup_action           url_post;
821        link_action             link_pre;
822        markup_action           link_post;
823        table_action            table;
824        start_row_action        start_row;
825        markup_action           end_row;
826        start_col_action        start_cell;
827        markup_action           end_cell;
828        anchor_action           anchor;
829
830        begin_section_action    begin_section;
831        end_section_action      end_section;
832        xinclude_action         xinclude;
833        include_action          include;
834
835        markup_action           escape_pre;
836        markup_action           escape_post;
837
838        int                     level;
839        std::string             qualified_section_id;
840    };
841}
842
843#endif // BOOST_SPIRIT_QUICKBOOK_UTILS_HPP
844
Note: See TracBrowser for help on using the repository browser.