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