]> git.lyx.org Git - features.git/blob - src/xml.h
45c62aba324dcbe282ece1092978fb7551cc86e4
[features.git] / src / xml.h
1 // -*- C++ -*-
2 /**
3  * \file xml.h
4  * This file is part of LyX, the document processor.
5  * License details can be found in the file COPYING.
6  *
7  * \author José Matos
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef XML_H
14 #define XML_H
15
16 #include "support/docstring.h"
17
18 #include <deque>
19 #include <memory>
20
21 namespace lyx {
22
23 class Buffer;
24 class Paragraph;
25 class OutputParams;
26
27 // Inspiration for the *Tag structs and for XMLStream
28 // came from MathStream and its cousins.
29
30 namespace xml {
31 struct StartTag;
32 struct EndTag;
33 struct CompTag;
34 struct ParTag;
35 struct FontTag;
36 struct CR;
37 }
38
39 class XMLStream {
40 public:
41         ///
42         explicit XMLStream(odocstream & os): os_(os), escape_(ESCAPE_ALL), is_last_tag_cr_(true) {}
43         ///
44         odocstream & os() { return os_; }
45         ///
46         // int & tab() { return tab_; }
47         /// closes any font tags that are eligible to be closed,
48         /// i.e., last on the tag_stack_.
49         /// \return false if there are open font tags we could not close.
50         /// because they are "blocked" by open non-font tags on the stack.
51         bool closeFontTags();
52         /// sets a mark so we know what tags to close at the end.
53         /// normally called at the start of a paragraph.
54         void startDivision(bool keep_empty);
55         /// clears the mark set by previous method.
56         /// there should not be any other tags open before it on the stack,
57         /// but if there are, we will close them.
58         void endDivision();
59         ///
60         XMLStream & operator<<(docstring const &);
61         ///
62         XMLStream & operator<<(const char *);
63         ///
64         XMLStream & operator<<(char_type);
65         ///
66         XMLStream & operator<<(int);
67         ///
68         XMLStream & operator<<(char);
69         ///
70         XMLStream & operator<<(xml::StartTag const &);
71         ///
72         XMLStream & operator<<(xml::EndTag const &);
73         ///
74         XMLStream & operator<<(xml::CompTag const &);
75         ///
76         XMLStream & operator<<(xml::ParTag const &);
77         ///
78         XMLStream & operator<<(xml::FontTag const &);
79         ///
80         XMLStream & operator<<(xml::CR const &);
81         ///
82         enum EscapeSettings {
83                 ESCAPE_NONE,
84                 ESCAPE_AND, // meaning &
85                 ESCAPE_ALL, // meaning <, >, &, at present, except things that are forbidden in comments
86                 ESCAPE_COMMENTS // Anything that is forbidden within comments
87         };
88         /// Sets what we are going to escape on the NEXT write.
89         /// Everything is reset for the next time.
90         XMLStream & operator<<(EscapeSettings);
91         /// This routine is for debugging the tag stack, etc. Code
92         /// for it is disabled by default, however, so you will need
93         /// to enable it if you want to use it.
94         void dumpTagStack(std::string const & msg);
95         ///
96         bool isTagOpen(xml::StartTag const &, int maxdepth = -1) const;
97         ///
98         bool isTagOpen(xml::EndTag const &, int maxdepth = -1) const;
99         ///
100         bool isTagPending(xml::StartTag const &, int maxdepth = -1) const;
101         /// Is the last tag that was added to the stream a new line (CR)? This is mostly to known
102         /// whether a new line must be added. Therefore, consider that an empty stream just had a CR,
103         /// that simplifies the logic using this code.
104         bool isLastTagCR() const { return is_last_tag_cr_; };
105         ///
106         void writeError(std::string const &);
107         ///
108         void writeError(docstring const &);
109         ///
110         typedef std::shared_ptr<xml::StartTag> TagPtr;
111         /// Returns the last element on the tag stack. XMLStream keeps ownership of the item.
112         TagPtr getLastStackTag();
113 private:
114         ///
115         void clearTagDeque();
116         ///
117         odocstream & os_;
118         ///
119         EscapeSettings escape_;
120         // What we would really like to do here is simply use a
121         // deque<StartTag>. But we want to store both StartTags and
122         // sub-classes thereof on this stack, which means we run into the
123         // so-called polymorphic class problem with the STL. We therefore have
124         // to use a deque<StartTag *>, which leads to the question who will
125         // own these pointers and how they will be deleted, so we use shared
126         // pointers.
127         ///
128         typedef std::shared_ptr<xml::StartTag> TagPtr;
129         typedef std::deque<TagPtr> TagDeque;
130         ///
131         template <typename T>
132         TagPtr makeTagPtr(T const & tag) { return std::make_shared<T>(tag); }
133         ///
134         TagDeque pending_tags_;
135         ///
136         TagDeque tag_stack_;
137         ///
138         bool is_last_tag_cr_;
139 };
140
141 namespace xml {
142
143 /// Escape the given character, if necessary, to an entity.
144 docstring escapeChar(char_type c, XMLStream::EscapeSettings e);
145
146 /// Escape the given character, if necessary, to an entity.
147 docstring escapeChar(char c, XMLStream::EscapeSettings e);
148
149 /// Escape a word instead of a single character
150 docstring escapeString(docstring const & raw, XMLStream::EscapeSettings e=XMLStream::ESCAPE_ALL);
151
152 /// cleans \param str for use as an attribute by replacing all non-altnum by "_"
153 docstring cleanAttr(docstring const & str);
154
155 /// \p c must be ASCII
156 docstring escapeChar(char c, XMLStream::EscapeSettings e);
157
158 /// replaces illegal characters from ID attributes
159 docstring cleanID(docstring const &orig);
160
161 /// returns a unique numeric ID
162 docstring uniqueID(docstring const & label);
163
164 struct FontTag;
165 struct EndFontTag;
166
167 /// Attributes will be escaped automatically and so should NOT
168 /// be escaped before being passed to the constructor.
169 struct StartTag
170 {
171         ///
172         explicit StartTag(std::string const & tag) : tag_(from_ascii(tag)), keepempty_(false) {}
173         ///
174         explicit StartTag(docstring const & tag) : tag_(tag), keepempty_(false) {}
175         ///
176         explicit StartTag(docstring const & tag, docstring const & attr,
177                                           bool keepempty = false)
178                         : tag_(tag), attr_(attr), keepempty_(keepempty) {}
179         ///
180         explicit StartTag(std::string const & tag, std::string const & attr,
181                                           bool keepempty = false)
182                         : tag_(from_ascii(tag)), attr_(from_ascii(attr)), keepempty_(keepempty) {}
183         ///
184         explicit StartTag(std::string const & tag, docstring const & attr,
185                                           bool keepempty = false)
186                         : tag_(from_ascii(tag)), attr_(attr), keepempty_(keepempty) {}
187         ///
188         virtual ~StartTag() {}
189         /// <tag_ attr_>
190         virtual docstring writeTag() const;
191         /// </tag_>
192         virtual docstring writeEndTag() const;
193         ///
194         virtual FontTag const * asFontTag() const { return nullptr; }
195         ///
196         virtual bool operator==(StartTag const & rhs) const
197         { return tag_ == rhs.tag_; }
198         ///
199         virtual bool operator!=(StartTag const & rhs) const
200         { return !(*this == rhs); }
201         ///
202         virtual bool operator==(FontTag const & rhs) const;
203         ///
204         docstring tag_;
205         ///
206         docstring attr_;
207         /// whether to keep things like "<tag></tag>" or discard them
208         /// you would want this for td, e.g, but maybe not for a div
209         bool keepempty_;
210 };
211
212
213 ///
214 struct EndTag
215 {
216         ///
217         explicit EndTag(std::string tag) : tag_(from_ascii(tag)) {}
218         ///
219         explicit EndTag(docstring tag) : tag_(tag) {}
220         ///
221         virtual ~EndTag() {}
222         /// </tag_>
223         virtual docstring writeEndTag() const;
224         ///
225         bool operator==(StartTag const & rhs) const
226         { return tag_ == rhs.tag_; }
227         ///
228         bool operator!=(StartTag const & rhs) const
229         { return !(*this == rhs); }
230         ///
231         virtual EndFontTag const * asFontTag() const { return 0; }
232         ///
233         docstring tag_;
234 };
235
236
237 /// Tags like <img />
238 /// Attributes will be escaped automatically and so should NOT
239 /// be escaped before being passed to the constructor.
240 struct CompTag
241 {
242         ///
243         explicit CompTag(std::string const & tag)
244                         : tag_(tag) {}
245         ///
246         explicit CompTag(std::string const & tag, std::string const & attr)
247                         : tag_(tag), attr_(attr) {}
248         /// <tag_ attr_ />
249         docstring writeTag() const;
250         ///
251         std::string tag_;
252         ///
253         std::string attr_;
254 };
255
256
257 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
258 /// parid is only used for HTML output; XML is supposed to use attr for this.
259 struct ParTag : public StartTag
260 {
261         ///
262         explicit ParTag(std::string const & tag, const std::string & attr): StartTag(tag, from_utf8(attr)) {}
263         ///
264         ~ParTag() {}
265 };
266
267
268 ///
269 enum FontTypes {
270         // ranges
271         FT_EMPH,
272         FT_NOUN,
273         FT_UBAR,
274         FT_DBAR,
275         FT_WAVE,
276         FT_SOUT,
277         FT_XOUT,
278         // bold
279         FT_BOLD,
280         // shapes
281         FT_UPRIGHT,
282         FT_ITALIC,
283         FT_SLANTED,
284         FT_SMALLCAPS,
285         // families
286         FT_ROMAN,
287         FT_SANS,
288         FT_TYPE,
289         // sizes
290         FT_SIZE_TINY,
291         FT_SIZE_SCRIPT,
292         FT_SIZE_FOOTNOTE,
293         FT_SIZE_SMALL,
294         FT_SIZE_NORMAL,
295         FT_SIZE_LARGE,
296         FT_SIZE_LARGER,
297         FT_SIZE_LARGEST,
298         FT_SIZE_HUGE,
299         FT_SIZE_HUGER,
300         FT_SIZE_INCREASE,
301         FT_SIZE_DECREASE
302
303         // When updating this list, also update fontToTag in both output_docbook.cpp and output_xhtml.cpp,
304         // fontToRole in output_docbook.cpp, and fontToAttribute in output_xhtml.cpp.
305 };
306
307
308 ///
309 struct FontTag : public StartTag
310 {
311         ///
312         FontTag(docstring const & tag, FontTypes type): StartTag(tag), font_type_(type) {}
313         ///
314         FontTag(std::string const & tag, FontTypes type): StartTag(from_utf8(tag)), font_type_(type) {}
315         ///
316         FontTag(docstring const & tag, docstring const & attr, FontTypes type): StartTag(tag, attr), font_type_(type) {}
317         ///
318         FontTag(std::string const & tag, std::string const & attr, FontTypes type): StartTag(from_utf8(tag), from_utf8(attr)), font_type_(type) {}
319         ///
320         FontTag const * asFontTag() const override { return this; }
321         ///
322         bool operator==(StartTag const &) const override;
323         ///
324         FontTypes font_type_;
325 };
326
327
328 ///
329 struct EndFontTag : public EndTag
330 {
331         ///
332         EndFontTag(docstring const & tag, FontTypes type): EndTag(tag), font_type_(type) {}
333         ///
334         EndFontTag(std::string const & tag, FontTypes type): EndTag(from_utf8(tag)), font_type_(type) {}
335         ///
336         EndFontTag const * asFontTag() const override { return this; }
337         ///
338         FontTypes font_type_;
339 };
340
341
342 // trivial struct for output of newlines
343 struct CR{};
344
345 // an illegal tag for internal use
346 xml::StartTag const parsep_tag("&LyX_parsep_tag&");
347
348 /// Open tag
349 void openTag(odocstream & os, std::string const & name,
350              std::string const & attribute = std::string());
351
352 /// Open tag
353 void openTag(Buffer const & buf, odocstream & os,
354              OutputParams const & runparams, Paragraph const & par);
355
356 /// Close tag
357 void closeTag(odocstream & os, std::string const & name);
358
359 /// Close tag
360 void closeTag(odocstream & os, Paragraph const & par);
361
362 } // namespace xml
363
364 } // namespace lyx
365
366 #endif // XML_H