]> git.lyx.org Git - features.git/blob - src/xml.h
DocBook: fix float tags (was unduly overridden).
[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::deque<TagPtr> TagDeque;
129         ///
130         template <typename T>
131         TagPtr makeTagPtr(T const & tag) { return std::make_shared<T>(tag); }
132         ///
133         TagDeque pending_tags_;
134         ///
135         TagDeque tag_stack_;
136         ///
137         bool is_last_tag_cr_;
138 };
139
140 namespace xml {
141
142 /// Escape the given character, if necessary, to an entity.
143 docstring escapeChar(char_type c, XMLStream::EscapeSettings e);
144
145 /// Escape the given character, if necessary, to an entity.
146 docstring escapeChar(char c, XMLStream::EscapeSettings e);
147
148 /// Escape a word instead of a single character
149 docstring escapeString(docstring const & raw, XMLStream::EscapeSettings e=XMLStream::ESCAPE_ALL);
150
151 /// cleans \param str for use as an attribute by replacing all non-altnum by "_"
152 docstring cleanAttr(docstring const & str);
153
154 /// \p c must be ASCII
155 docstring escapeChar(char c, XMLStream::EscapeSettings e);
156
157 /// replaces illegal characters from ID attributes
158 docstring cleanID(docstring const &orig);
159
160 /// returns a unique numeric ID
161 docstring uniqueID(docstring const & label);
162
163 struct FontTag;
164 struct EndFontTag;
165
166 /// Attributes will be escaped automatically and so should NOT
167 /// be escaped before being passed to the constructor.
168 struct StartTag
169 {
170         ///
171         explicit StartTag(std::string const & tag) : tag_(from_ascii(tag)), keepempty_(false) {}
172         ///
173         explicit StartTag(docstring const & tag) : tag_(tag), keepempty_(false) {}
174         ///
175         explicit StartTag(docstring const & tag, docstring const & attr,
176                                           bool keepempty = false)
177                         : tag_(tag), attr_(attr), keepempty_(keepempty) {}
178         ///
179         explicit StartTag(std::string const & tag, std::string const & attr,
180                                           bool keepempty = false)
181                         : tag_(from_ascii(tag)), attr_(from_ascii(attr)), keepempty_(keepempty) {}
182         ///
183         explicit StartTag(std::string const & tag, docstring const & attr,
184                                           bool keepempty = false)
185                         : tag_(from_ascii(tag)), attr_(attr), keepempty_(keepempty) {}
186         ///
187         virtual ~StartTag() {}
188         /// <tag_ attr_>
189         virtual docstring writeTag() const;
190         /// </tag_>
191         virtual docstring writeEndTag() const;
192         ///
193         virtual FontTag const * asFontTag() const { return nullptr; }
194         ///
195         virtual bool operator==(StartTag const & rhs) const
196         { return tag_ == rhs.tag_; }
197         ///
198         virtual bool operator!=(StartTag const & rhs) const
199         { return !(*this == rhs); }
200         ///
201         virtual bool operator==(FontTag const & rhs) const;
202         ///
203         docstring tag_;
204         ///
205         docstring attr_;
206         /// whether to keep things like "<tag></tag>" or discard them
207         /// you would want this for td, e.g, but maybe not for a div
208         bool keepempty_;
209 };
210
211
212 ///
213 struct EndTag
214 {
215         ///
216         explicit EndTag(std::string tag) : tag_(from_ascii(tag)) {}
217         ///
218         explicit EndTag(docstring tag) : tag_(tag) {}
219         ///
220         virtual ~EndTag() {}
221         /// </tag_>
222         virtual docstring writeEndTag() const;
223         ///
224         bool operator==(StartTag const & rhs) const
225         { return tag_ == rhs.tag_; }
226         ///
227         bool operator!=(StartTag const & rhs) const
228         { return !(*this == rhs); }
229         ///
230         virtual EndFontTag const * asFontTag() const { return 0; }
231         ///
232         docstring tag_;
233 };
234
235
236 /// Tags like <img />
237 /// Attributes will be escaped automatically and so should NOT
238 /// be escaped before being passed to the constructor.
239 struct CompTag
240 {
241         ///
242         explicit CompTag(std::string const & tag)
243                         : tag_(tag) {}
244         ///
245         explicit CompTag(std::string const & tag, std::string const & attr)
246                         : tag_(tag), attr_(attr) {}
247         /// <tag_ attr_ />
248         docstring writeTag() const;
249         ///
250         std::string tag_;
251         ///
252         std::string attr_;
253 };
254
255
256 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
257 /// parid is only used for HTML output; XML is supposed to use attr for this.
258 struct ParTag : public StartTag
259 {
260         ///
261         explicit ParTag(std::string const & tag, const std::string & attr): StartTag(tag, from_utf8(attr)) {}
262         ///
263         ~ParTag() {}
264 };
265
266
267 ///
268 enum FontTypes {
269         // ranges
270         FT_EMPH,
271         FT_NOUN,
272         FT_UBAR,
273         FT_DBAR,
274         FT_WAVE,
275         FT_SOUT,
276         FT_XOUT,
277         // bold
278         FT_BOLD,
279         // shapes
280         FT_UPRIGHT,
281         FT_ITALIC,
282         FT_SLANTED,
283         FT_SMALLCAPS,
284         // families
285         FT_ROMAN,
286         FT_SANS,
287         FT_TYPE,
288         // sizes
289         FT_SIZE_TINY,
290         FT_SIZE_SCRIPT,
291         FT_SIZE_FOOTNOTE,
292         FT_SIZE_SMALL,
293         FT_SIZE_NORMAL,
294         FT_SIZE_LARGE,
295         FT_SIZE_LARGER,
296         FT_SIZE_LARGEST,
297         FT_SIZE_HUGE,
298         FT_SIZE_HUGER,
299         FT_SIZE_INCREASE,
300         FT_SIZE_DECREASE
301
302         // When updating this list, also update fontToTag in both output_docbook.cpp and output_xhtml.cpp,
303         // fontToRole in output_docbook.cpp, and fontToAttribute in output_xhtml.cpp.
304 };
305
306
307 ///
308 struct FontTag : public StartTag
309 {
310         ///
311         FontTag(docstring const & tag, FontTypes type): StartTag(tag), font_type_(type) {}
312         ///
313         FontTag(std::string const & tag, FontTypes type): StartTag(from_utf8(tag)), font_type_(type) {}
314         ///
315         FontTag(docstring const & tag, docstring const & attr, FontTypes type): StartTag(tag, attr), font_type_(type) {}
316         ///
317         FontTag(std::string const & tag, std::string const & attr, FontTypes type): StartTag(from_utf8(tag), from_utf8(attr)), font_type_(type) {}
318         ///
319         FontTag const * asFontTag() const override { return this; }
320         ///
321         bool operator==(StartTag const &) const override;
322         ///
323         FontTypes font_type_;
324 };
325
326
327 ///
328 struct EndFontTag : public EndTag
329 {
330         ///
331         EndFontTag(docstring const & tag, FontTypes type): EndTag(tag), font_type_(type) {}
332         ///
333         EndFontTag(std::string const & tag, FontTypes type): EndTag(from_utf8(tag)), font_type_(type) {}
334         ///
335         EndFontTag const * asFontTag() const override { return this; }
336         ///
337         FontTypes font_type_;
338 };
339
340
341 // trivial struct for output of newlines
342 struct CR{};
343
344 // an illegal tag for internal use
345 xml::StartTag const parsep_tag("&LyX_parsep_tag&");
346
347 /// Open tag
348 void openTag(odocstream & os, std::string const & name,
349              std::string const & attribute = std::string());
350
351 /// Open tag
352 void openTag(Buffer const & buf, odocstream & os,
353              OutputParams const & runparams, Paragraph const & par);
354
355 /// Close tag
356 void closeTag(odocstream & os, std::string const & name);
357
358 /// Close tag
359 void closeTag(odocstream & os, Paragraph const & par);
360
361 } // namespace xml
362
363 } // namespace lyx
364
365 #endif // XML_H