]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.cpp
Make macro xhtml export more secure
[lyx.git] / src / mathed / MacroTable.cpp
1 /**
2  * \file MacroTable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathSqrt.h"
14 #include "MacroTable.h"
15 #include "MathMacroTemplate.h"
16 #include "MathMacroArgument.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "InsetMathNest.h"
21
22 #include "Buffer.h"
23 #include "DocIterator.h"
24 #include "InsetList.h"
25 #include "Text.h"
26
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lassert.h"
30
31 #include <sstream>
32
33 using namespace std;
34
35 namespace lyx {
36
37
38 /////////////////////////////////////////////////////////////////////
39 //
40 // MacroData
41 //
42 /////////////////////////////////////////////////////////////////////
43
44 MacroData::MacroData(Buffer * buf)
45         : buffer_(buf), queried_(true), numargs_(0), sym_(0), optionals_(0),
46           lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
47 {}
48
49
50 MacroData::MacroData(Buffer * buf, DocIterator const & pos)
51         : buffer_(buf), pos_(pos), queried_(false), numargs_(0), sym_(0),
52           optionals_(0), lockCount_(0), redefinition_(false),
53           type_(MacroTypeNewcommand)
54 {
55 }
56
57
58 MacroData::MacroData(Buffer * buf, MathMacroTemplate const & macro)
59         : buffer_(buf), queried_(false), numargs_(0), sym_(0), optionals_(0),
60           lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
61 {
62         queryData(macro);
63 }
64
65
66 bool MacroData::expand(vector<MathData> const & args, MathData & to) const
67 {
68         updateData();
69
70         // Hack. Any inset with a cell would do.
71         static InsetMathSqrt inset(0);
72         inset.setBuffer(const_cast<Buffer &>(*buffer_));
73
74         docstring const & definition(display_.empty() ? definition_ : display_);
75         asArray(definition, inset.cell(0));
76         //lyxerr << "MathData::expand: args: " << args << endl;
77         //LYXERR0("MathData::expand: ar: " << inset.cell(0));
78         for (DocIterator it = doc_iterator_begin(buffer_, &inset); it; it.forwardChar()) {
79                 if (!it.nextInset())
80                         continue;
81                 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
82                         continue;
83                 //it.cell().erase(it.pos());
84                 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
85                 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
86                 if (n <= args.size()) {
87                         it.cell().erase(it.pos());
88                         it.cell().insert(it.pos(), args[n - 1]);
89                 }
90         }
91         //LYXERR0("MathData::expand: res: " << inset.cell(0));
92         to = inset.cell(0);
93         // If the result is equal to the definition then we either have a
94         // recursive loop, or the definition did not contain any macro in the
95         // first place.
96         return asString(to) != definition;
97 }
98
99
100 size_t MacroData::optionals() const
101 {
102         updateData();
103         return optionals_;
104 }
105
106
107 vector<docstring> const & MacroData::defaults() const
108 {
109         updateData();
110         return defaults_;
111 }
112
113
114 string const MacroData::requires() const
115 {
116         if (sym_)
117                 return sym_->requires;
118         return string();
119 }
120
121
122 docstring const MacroData::xmlname() const
123 {
124         if (sym_)
125                 return sym_->xmlname;
126         return docstring();
127 }
128
129
130 char const * MacroData::MathMLtype() const
131 {
132         return sym_ ? sym_->MathMLtype() : 0;
133 }
134
135
136 void MacroData::unlock() const
137 {
138         --lockCount_;
139         LASSERT(lockCount_ >= 0, lockCount_ = 0);
140 }
141
142
143 void MacroData::queryData(MathMacroTemplate const & macro) const
144 {
145         if (queried_)
146                 return;
147
148         queried_ = true;
149         definition_ = macro.definition();
150         numargs_ = macro.numArgs();
151         display_ = macro.displayDefinition();
152         redefinition_ = macro.redefinition();
153         type_ = macro.type();
154         optionals_ = macro.numOptionals();
155
156         macro.getDefaults(defaults_);
157 }
158
159
160 void MacroData::updateData() const
161 {
162         if (queried_)
163                 return;
164
165         LBUFERR(buffer_);
166
167         // Try to fix position DocIterator. Should not do anything in theory.
168         pos_.fixIfBroken();
169
170         // find macro template
171         Inset * inset = pos_.nextInset();
172         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
173                 lyxerr << "BUG: No macro template found by MacroData" << endl;
174                 return;
175         }
176
177         // query the data from the macro template
178         queryData(static_cast<MathMacroTemplate const &>(*inset));
179 }
180
181
182 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
183 {
184         updateData();
185
186         // find macro template
187         Inset * inset = pos_.nextInset();
188         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
189                 lyxerr << "BUG: No macro template found by MacroData" << endl;
190                 return 0;
191         }
192
193         // output template
194         MathMacroTemplate const & tmpl =
195                 static_cast<MathMacroTemplate const &>(*inset);
196         WriteStream wi(os, false, true, WriteStream::wsDefault);
197         return tmpl.write(wi, overwriteRedefinition);
198 }
199
200
201 /////////////////////////////////////////////////////////////////////
202 //
203 // The global table of macros
204 //
205 /////////////////////////////////////////////////////////////////////
206
207 MacroTable & MacroTable::globalMacros()
208 {
209         static MacroTable theGlobalMacros;
210         return theGlobalMacros;
211 }
212
213
214 MacroData const * MacroTable::get(docstring const & name) const
215 {
216         const_iterator it = find(name);
217         return it == end() ? 0 : &it->second;
218 }
219
220
221 MacroTable::iterator
222 MacroTable::insert(docstring const & name, MacroData const & data)
223 {
224         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
225         iterator it = find(name);
226         if (it == end())
227                 it = map<docstring, MacroData>::insert(
228                                 make_pair(name, data)).first;
229         else
230                 it->second = data;
231         return it;
232 }
233
234
235 MacroTable::iterator
236 MacroTable::insert(Buffer * buf, docstring const & def)
237 {
238         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
239         MathMacroTemplate mac(buf, def);
240         MacroData data(buf, mac);
241         return insert(mac.name(), data);
242 }
243
244
245 void MacroTable::getMacroNames(std::set<docstring> & names) const
246 {
247         for (const_iterator it = begin(); it != end(); ++it)
248                 names.insert(it->first);
249 }
250
251
252 void MacroTable::dump()
253 {
254         lyxerr << "\n------------------------------------------" << endl;
255         for (const_iterator it = begin(); it != end(); ++it)
256                 lyxerr << to_utf8(it->first)
257                         << " [" << to_utf8(it->second.definition()) << "] : "
258                         << " [" << to_utf8(it->second.display()) << "] : "
259                         << endl;
260         lyxerr << "------------------------------------------" << endl;
261 }
262
263
264 /////////////////////////////////////////////////////////////////////
265 //
266 // MacroContext
267 //
268 /////////////////////////////////////////////////////////////////////
269
270 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
271         : buf_(buf), pos_(pos)
272 {
273 }
274
275
276 MacroData const * MacroContext::get(docstring const & name) const
277 {
278         return buf_->getMacro(name, pos_);
279 }
280
281 } // namespace lyx