]> git.lyx.org Git - lyx.git/blob - src/xml.h
6da53ada99cffe9c869c82ab517ea264258c3bfd
[lyx.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 /// determines whether a string only contains space characters
164 bool isNotOnlySpace(docstring const & str);
165
166 /// trims the string to the left, i.e. remove any space-like character at the beginning of the string
167 docstring trimLeft(docstring const & str);
168
169 struct FontTag;
170 struct EndFontTag;
171
172 /// Attributes will be escaped automatically and so should NOT
173 /// be escaped before being passed to the constructor.
174 struct StartTag
175 {
176         ///
177         explicit StartTag(std::string const & tag) : tag_(from_ascii(tag)), keepempty_(false), tagtype_("none") {}
178         ///
179         explicit StartTag(docstring const & tag) : tag_(tag), keepempty_(false), tagtype_("none") {}
180         ///
181         explicit StartTag(docstring const & tag, docstring const & attr,
182                                           bool keepempty = false, std::string const & tagtype = "none")
183                         : tag_(tag), attr_(attr), keepempty_(keepempty), tagtype_(tagtype) {}
184         ///
185         explicit StartTag(std::string const & tag, std::string const & attr,
186                                           bool keepempty = false, std::string const & tagtype = "none")
187                         : tag_(from_ascii(tag)), attr_(from_utf8(attr)), keepempty_(keepempty), tagtype_(tagtype) {}
188         ///
189         explicit StartTag(std::string const & tag, docstring const & attr,
190                                           bool keepempty = false, std::string const & tagtype = "none")
191                         : tag_(from_ascii(tag)), attr_(attr), keepempty_(keepempty), tagtype_(tagtype) {}
192         ///
193         virtual ~StartTag() = default;
194         /// <tag_ attr_>
195         virtual docstring writeTag() const;
196         /// </tag_>
197         virtual docstring writeEndTag() const;
198         ///
199         virtual FontTag const * asFontTag() const { return nullptr; }
200         ///
201         virtual bool operator==(StartTag const & rhs) const
202         { return tag_ == rhs.tag_; }
203         ///
204         virtual bool operator!=(StartTag const & rhs) const
205         { return !(*this == rhs); }
206         ///
207         virtual bool operator==(FontTag const & rhs) const;
208         ///
209         docstring tag_;
210         ///
211         docstring attr_;
212         /// whether to keep things like "<tag></tag>" or discard them
213         /// you would want this for td, e.g, but maybe not for a div
214         bool keepempty_;
215         /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
216         std::string tagtype_;
217 };
218
219
220 ///
221 struct EndTag
222 {
223         ///
224         explicit EndTag(std::string const & tag, std::string const & tagtype = "none")
225                 : tag_(from_ascii(tag)), tagtype_(tagtype) {}
226         ///
227         explicit EndTag(docstring const & tag, std::string const & tagtype = "none")
228                 : tag_(tag), tagtype_(tagtype) {}
229         ///
230         virtual ~EndTag() = default;
231         /// </tag_>
232         virtual docstring writeEndTag() const;
233         ///
234         bool operator==(StartTag const & rhs) const
235         { return tag_ == rhs.tag_; }
236         ///
237         bool operator!=(StartTag const & rhs) const
238         { return !(*this == rhs); }
239         ///
240         virtual EndFontTag const * asFontTag() const { return nullptr; }
241         ///
242         docstring tag_;
243         /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
244         /// The value should match that of the corresponding xml::StartTag.
245         std::string tagtype_;
246 };
247
248
249 /// Tags like <img />
250 /// Attributes will be escaped automatically and so should NOT
251 /// be escaped before being passed to the constructor.
252 struct CompTag
253 {
254         ///
255         explicit CompTag(std::string const & tag)
256                         : tag_(from_utf8(tag)), tagtype_("none") {}
257         ///
258         explicit CompTag(std::string const & tag, std::string const & attr, std::string const & tagtype = "none")
259                         : tag_(from_utf8(tag)), attr_(from_utf8(attr)), tagtype_(tagtype) {}
260         ///
261         explicit CompTag(std::string const & tag, docstring const & attr, std::string const & tagtype = "none")
262                         : tag_(from_utf8(tag)), attr_(attr), tagtype_(tagtype) {}
263         /// <tag_ attr_ />
264         docstring writeTag() const;
265         ///
266         docstring tag_;
267         ///
268         docstring attr_;
269         /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
270         std::string tagtype_;
271 };
272
273
274 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
275 struct ParTag : public StartTag
276 {
277         ///
278         explicit ParTag(std::string const & tag, const std::string & attr): StartTag(tag, from_utf8(attr)) {}
279         ///
280         ~ParTag() override = default;
281 };
282
283
284 ///
285 enum FontTypes {
286         // ranges
287         FT_EMPH,
288         FT_NOUN,
289         FT_UBAR,
290         FT_DBAR,
291         FT_WAVE,
292         FT_SOUT,
293         FT_XOUT,
294         // bold
295         FT_BOLD,
296         // shapes
297         FT_UPRIGHT,
298         FT_ITALIC,
299         FT_SLANTED,
300         FT_SMALLCAPS,
301         // families
302         FT_ROMAN,
303         FT_SANS,
304         FT_TYPE,
305         // sizes
306         FT_SIZE_TINY,
307         FT_SIZE_SCRIPT,
308         FT_SIZE_FOOTNOTE,
309         FT_SIZE_SMALL,
310         FT_SIZE_NORMAL,
311         FT_SIZE_LARGE,
312         FT_SIZE_LARGER,
313         FT_SIZE_LARGEST,
314         FT_SIZE_HUGE,
315         FT_SIZE_HUGER,
316         FT_SIZE_INCREASE,
317         FT_SIZE_DECREASE
318
319         // When updating this list, also update fontToTag in both output_docbook.cpp and output_xhtml.cpp,
320         // fontToRole in output_docbook.cpp, and fontToAttribute in output_xhtml.cpp.
321 };
322
323
324 ///
325 struct FontTag : public StartTag
326 {
327         ///
328         FontTag(docstring const & tag, FontTypes type): StartTag(tag), font_type_(type) {}
329         ///
330         FontTag(std::string const & tag, FontTypes type): StartTag(from_utf8(tag)), font_type_(type) {}
331         ///
332         FontTag(docstring const & tag, docstring const & attr, FontTypes type): StartTag(tag, attr), font_type_(type) {}
333         ///
334         FontTag(std::string const & tag, std::string const & attr, FontTypes type): StartTag(from_utf8(tag), from_utf8(attr)), font_type_(type) {}
335         ///
336         FontTag const * asFontTag() const override { return this; }
337         ///
338         bool operator==(StartTag const &) const override;
339         ///
340         FontTypes font_type_;
341 };
342
343
344 ///
345 struct EndFontTag : public EndTag
346 {
347         ///
348         EndFontTag(docstring const & tag, FontTypes type): EndTag(tag), font_type_(type) {}
349         ///
350         EndFontTag(std::string const & tag, FontTypes type): EndTag(from_utf8(tag)), font_type_(type) {}
351         ///
352         EndFontTag const * asFontTag() const override { return this; }
353         ///
354         FontTypes font_type_;
355 };
356
357
358 // trivial struct for output of newlines
359 struct CR{};
360
361 // an illegal tag for internal use
362 xml::StartTag const parsep_tag("&LyX_parsep_tag&");
363
364 /// Open tag
365 void openTag(odocstream & os, std::string const & name,
366              std::string const & attribute = std::string());
367
368 /// Open tag
369 void openTag(Buffer const & buf, odocstream & os,
370              OutputParams const & runparams, Paragraph const & par);
371
372 /// Close tag
373 void closeTag(odocstream & os, std::string const & name);
374
375 /// Close tag
376 void closeTag(odocstream & os, Paragraph const & par);
377
378 } // namespace xml
379
380 } // namespace lyx
381
382 #endif // XML_H