]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2002 2004 2006 Joel de Guzman | |
3 | Copyright (c) 2004 Eric Niebler | |
4 | Copyright (c) 2005 Thomas Guest | |
5 | http://spirit.sourceforge.net/ | |
6 | ||
7 | Use, modification and distribution is subject to the Boost Software | |
8 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
9 | http://www.boost.org/LICENSE_1_0.txt) | |
10 | =============================================================================*/ | |
11 | #include "state.hpp" | |
12 | #include "state_save.hpp" | |
13 | #include "document_state.hpp" | |
14 | #include "quickbook.hpp" | |
15 | #include "grammar.hpp" | |
b32b8144 | 16 | #include "path.hpp" |
7c673cae FG |
17 | #include "utils.hpp" |
18 | #include "phrase_tags.hpp" | |
19 | #include <boost/foreach.hpp> | |
20 | ||
21 | #if (defined(BOOST_MSVC) && (BOOST_MSVC <= 1310)) | |
22 | #pragma warning(disable:4355) | |
23 | #endif | |
24 | ||
25 | namespace quickbook | |
26 | { | |
27 | char const* quickbook_get_date = "__quickbook_get_date__"; | |
28 | char const* quickbook_get_time = "__quickbook_get_time__"; | |
29 | ||
30 | unsigned qbk_version_n = 0; // qbk_major_version * 100 + qbk_minor_version | |
31 | ||
32 | state::state(fs::path const& filein_, fs::path const& xinclude_base_, | |
b32b8144 | 33 | string_stream& out_, document_state& document_) |
7c673cae FG |
34 | : grammar_() |
35 | ||
36 | , order_pos(0) | |
37 | , xinclude_base(xinclude_base_) | |
38 | ||
39 | , templates() | |
40 | , error_count(0) | |
41 | , anchors() | |
42 | , warned_about_breaks(false) | |
43 | , conditional(true) | |
b32b8144 | 44 | , document(document_) |
7c673cae FG |
45 | , callouts() |
46 | , callout_depth(0) | |
47 | , dependencies() | |
48 | , explicit_list(false) | |
b32b8144 | 49 | , strict_mode(false) |
7c673cae FG |
50 | |
51 | , imported(false) | |
52 | , macro() | |
53 | , source_mode() | |
54 | , source_mode_next() | |
55 | , source_mode_next_pos() | |
56 | , current_file(0) | |
57 | , current_path(filein_, 0, filein_.filename()) | |
58 | ||
59 | , template_depth(0) | |
60 | , min_section_level(1) | |
61 | ||
62 | , in_list(false) | |
63 | , in_list_save() | |
64 | , out(out_) | |
65 | , phrase() | |
66 | ||
67 | , values(¤t_file) | |
68 | { | |
69 | // add the predefined macros | |
70 | macro.add | |
71 | ("__DATE__", std::string(quickbook_get_date)) | |
72 | ("__TIME__", std::string(quickbook_get_time)) | |
73 | ("__FILENAME__", std::string()) | |
74 | ; | |
75 | update_filename_macro(); | |
76 | ||
77 | boost::scoped_ptr<quickbook_grammar> g( | |
78 | new quickbook_grammar(*this)); | |
79 | grammar_.swap(g); | |
80 | } | |
81 | ||
82 | quickbook_grammar& state::grammar() const { | |
83 | return *grammar_; | |
84 | } | |
85 | ||
86 | void state::update_filename_macro() { | |
87 | *boost::spirit::classic::find(macro, "__FILENAME__") | |
88 | = detail::encode_string( | |
89 | detail::path_to_generic(current_path.abstract_file_path)); | |
90 | } | |
91 | ||
92 | unsigned state::get_new_order_pos() { | |
93 | return ++order_pos; | |
94 | } | |
95 | ||
96 | void state::push_output() { | |
97 | out.push(); | |
98 | phrase.push(); | |
99 | in_list_save.push(in_list); | |
100 | } | |
101 | ||
102 | void state::pop_output() { | |
103 | phrase.pop(); | |
104 | out.pop(); | |
105 | in_list = in_list_save.top(); | |
106 | in_list_save.pop(); | |
107 | } | |
108 | ||
109 | source_mode_info state::tagged_source_mode() const { | |
110 | source_mode_info result; | |
111 | ||
112 | BOOST_FOREACH(source_mode_info const& s, tagged_source_mode_stack) { | |
113 | result.update(s); | |
114 | } | |
115 | ||
116 | return result; | |
117 | } | |
118 | ||
119 | source_mode_info state::current_source_mode() const { | |
120 | source_mode_info result = source_mode; | |
121 | ||
122 | result.update(document.section_source_mode()); | |
123 | ||
124 | BOOST_FOREACH(source_mode_info const& s, tagged_source_mode_stack) { | |
125 | result.update(s); | |
126 | } | |
127 | ||
128 | return result; | |
129 | } | |
130 | ||
131 | void state::change_source_mode(source_mode_type s) { | |
132 | source_mode = source_mode_info(s, get_new_order_pos()); | |
133 | } | |
134 | ||
135 | void state::push_tagged_source_mode(source_mode_type s) { | |
136 | tagged_source_mode_stack.push_back( | |
137 | source_mode_info(s, s ? get_new_order_pos() : 0)); | |
138 | } | |
139 | ||
140 | void state::pop_tagged_source_mode() { | |
141 | assert(!tagged_source_mode_stack.empty()); | |
142 | tagged_source_mode_stack.pop_back(); | |
143 | } | |
144 | ||
b32b8144 FG |
145 | state_save::state_save(quickbook::state& state_, scope_flags scope_) |
146 | : state(state_) | |
147 | , scope(scope_) | |
7c673cae FG |
148 | , qbk_version(qbk_version_n) |
149 | , imported(state.imported) | |
150 | , current_file(state.current_file) | |
151 | , current_path(state.current_path) | |
152 | , xinclude_base(state.xinclude_base) | |
153 | , source_mode(state.source_mode) | |
154 | , macro() | |
155 | , template_depth(state.template_depth) | |
156 | , min_section_level(state.min_section_level) | |
157 | { | |
158 | if (scope & scope_macros) macro = state.macro; | |
159 | if (scope & scope_templates) state.templates.push(); | |
160 | if (scope & scope_output) { | |
161 | state.push_output(); | |
162 | } | |
163 | state.values.builder.save(); | |
164 | } | |
165 | ||
166 | state_save::~state_save() | |
167 | { | |
168 | state.values.builder.restore(); | |
169 | boost::swap(qbk_version_n, qbk_version); | |
170 | boost::swap(state.imported, imported); | |
171 | boost::swap(state.current_file, current_file); | |
172 | boost::swap(state.current_path, current_path); | |
173 | boost::swap(state.xinclude_base, xinclude_base); | |
174 | boost::swap(state.source_mode, source_mode); | |
175 | if (scope & scope_output) { | |
176 | state.pop_output(); | |
177 | } | |
178 | if (scope & scope_templates) state.templates.pop(); | |
179 | if (scope & scope_macros) state.macro = macro; | |
180 | boost::swap(state.template_depth, template_depth); | |
181 | boost::swap(state.min_section_level, min_section_level); | |
182 | } | |
183 | } |