]> git.lyx.org Git - lyx.git/blob - src/xml.h
Replace removed signal
[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 MathMLStream 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 /// \param c must be ASCII
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 /// replaces illegal characters from ID attributes
156 docstring cleanID(docstring const &orig);
157
158 /// returns a unique numeric ID
159 docstring uniqueID(docstring const & label);
160
161 /// determines whether a string only contains space characters
162 bool isNotOnlySpace(docstring const & str);
163
164 /// trims the string to the left, i.e. remove any space-like character at the beginning of the string
165 docstring trimLeft(docstring const & str);
166
167 struct FontTag;
168 struct EndFontTag;
169
170 /// Attributes will be escaped automatically and so should NOT
171 /// be escaped before being passed to the constructor.
172 struct StartTag
173 {
174         ///
175         explicit StartTag(std::string const & tag) : tag_(from_ascii(tag)), keepempty_(false), tagtype_("none") {}
176         ///
177         explicit StartTag(docstring const & tag) : tag_(tag), keepempty_(false), tagtype_("none") {}
178         ///
179         explicit StartTag(docstring const & tag, docstring const & attr,
180                                           bool keepempty = false, std::string const & tagtype = "none")
181                         : tag_(tag), attr_(attr), keepempty_(keepempty), tagtype_(tagtype) {}
182         ///
183         explicit StartTag(std::string const & tag, std::string const & attr,
184                                           bool keepempty = false, std::string const & tagtype = "none")
185                         : tag_(from_ascii(tag)), attr_(from_utf8(attr)), keepempty_(keepempty), tagtype_(tagtype) {}
186         ///
187         explicit StartTag(std::string const & tag, docstring const & attr,
188                                           bool keepempty = false, std::string const & tagtype = "none")
189                         : tag_(from_ascii(tag)), attr_(attr), keepempty_(keepempty), tagtype_(tagtype) {}
190         ///
191         virtual ~StartTag() = default;
192         /// <tag_ attr_>
193         virtual docstring writeTag() const;
194         /// </tag_>
195         virtual docstring writeEndTag() const;
196         ///
197         virtual FontTag const * asFontTag() const { return nullptr; }
198         ///
199         docstring tag_;
200         ///
201         docstring attr_;
202         /// whether to keep things like "<tag></tag>" or discard them
203         /// you would want this for td, e.g, but maybe not for a div
204         bool keepempty_;
205         /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
206         std::string tagtype_;
207 };
208
209
210 ///
211 struct EndTag
212 {
213         ///
214         explicit EndTag(std::string const & tag, std::string const & tagtype = "none")
215                 : tag_(from_ascii(tag)), tagtype_(tagtype) {}
216         ///
217         explicit EndTag(docstring const & tag, std::string const & tagtype = "none")
218                 : tag_(tag), tagtype_(tagtype) {}
219         ///
220         virtual ~EndTag() = default;
221         /// </tag_>
222         virtual docstring writeEndTag() const;
223         ///
224         virtual EndFontTag const * asFontTag() const { return nullptr; }
225         ///
226         docstring tag_;
227         /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
228         /// The value should match that of the corresponding xml::StartTag.
229         std::string tagtype_;
230 };
231
232
233 /// Tags like <img />
234 /// Attributes will be escaped automatically and so should NOT
235 /// be escaped before being passed to the constructor.
236 struct CompTag
237 {
238         ///
239         explicit CompTag(std::string const & tag)
240                         : tag_(from_utf8(tag)), tagtype_("none") {}
241         ///
242         explicit CompTag(docstring const & tag)
243                         : tag_(tag), tagtype_("none") {}
244         ///
245         explicit CompTag(std::string const & tag, std::string const & attr, std::string const & tagtype = "none")
246                         : tag_(from_utf8(tag)), attr_(from_utf8(attr)), tagtype_(tagtype) {}
247         ///
248         explicit CompTag(std::string const & tag, docstring const & attr, std::string const & tagtype = "none")
249                         : tag_(from_utf8(tag)), attr_(attr), tagtype_(tagtype) {}
250         ///
251         explicit CompTag(docstring const & tag, std::string const & attr, std::string const & tagtype = "none")
252                         : tag_(tag), attr_(from_utf8(attr)), tagtype_(tagtype) {}
253         ///
254         explicit CompTag(docstring const & tag, docstring const & attr, std::string const & tagtype = "none")
255                         : tag_(tag), attr_(attr), tagtype_(tagtype) {}
256         /// <tag_ attr_ />
257         docstring writeTag() const;
258         ///
259         docstring tag_;
260         ///
261         docstring attr_;
262         /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
263         std::string tagtype_;
264 };
265
266
267 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
268 struct ParTag : public StartTag
269 {
270         ///
271         explicit ParTag(std::string const & tag, const std::string & attr): StartTag(tag, from_utf8(attr)) {}
272         ///
273         ~ParTag() override = default;
274 };
275
276
277 ///
278 enum FontTypes {
279         // ranges
280         FT_EMPH,
281         FT_NOUN,
282         FT_UBAR,
283         FT_DBAR,
284         FT_WAVE,
285         FT_SOUT,
286         FT_XOUT,
287         // bold
288         FT_BOLD,
289         // shapes
290         FT_UPRIGHT,
291         FT_ITALIC,
292         FT_SLANTED,
293         FT_SMALLCAPS,
294         // families
295         FT_ROMAN,
296         FT_SANS,
297         FT_TYPE,
298         // sizes
299         FT_SIZE_TINY,
300         FT_SIZE_SCRIPT,
301         FT_SIZE_FOOTNOTE,
302         FT_SIZE_SMALL,
303         FT_SIZE_NORMAL,
304         FT_SIZE_LARGE,
305         FT_SIZE_LARGER,
306         FT_SIZE_LARGEST,
307         FT_SIZE_HUGE,
308         FT_SIZE_HUGER,
309         FT_SIZE_INCREASE,
310         FT_SIZE_DECREASE
311
312         // When updating this list, also update fontToTag in both output_docbook.cpp and output_xhtml.cpp,
313         // fontToRole in output_docbook.cpp, and fontToAttribute in output_xhtml.cpp.
314 };
315
316
317 ///
318 struct FontTag : public StartTag
319 {
320         ///
321         FontTag(docstring const & tag, FontTypes type): StartTag(tag), font_type_(type) {}
322         ///
323         FontTag(std::string const & tag, FontTypes type): StartTag(from_utf8(tag)), font_type_(type) {}
324         ///
325         FontTag(docstring const & tag, docstring const & attr, FontTypes type): StartTag(tag, attr), font_type_(type) {}
326         ///
327         FontTag(std::string const & tag, std::string const & attr, FontTypes type): StartTag(from_utf8(tag), from_utf8(attr)), font_type_(type) {}
328         ///
329         FontTag const * asFontTag() const override { return this; }
330         ///
331         FontTypes font_type_;
332 };
333
334
335 ///
336 struct EndFontTag : public EndTag
337 {
338         ///
339         EndFontTag(docstring const & tag, FontTypes type): EndTag(tag), font_type_(type) {}
340         ///
341         EndFontTag(std::string const & tag, FontTypes type): EndTag(from_utf8(tag)), font_type_(type) {}
342         ///
343         EndFontTag const * asFontTag() const override { return this; }
344         ///
345         FontTypes font_type_;
346 };
347
348
349 // trivial struct for output of newlines
350 struct CR{};
351
352 // an illegal tag for internal use
353 xml::StartTag const parsep_tag("&LyX_parsep_tag&");
354
355 /// Open tag
356 void openTag(odocstream & os, std::string const & name,
357              std::string const & attribute = std::string());
358
359 /// Open tag
360 void openTag(Buffer const & buf, odocstream & os,
361              OutputParams const & runparams, Paragraph const & par);
362
363 /// Close tag
364 void closeTag(odocstream & os, std::string const & name);
365
366 /// Close tag
367 void closeTag(odocstream & os, Paragraph const & par);
368
369 // Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
370 // Block style:
371 //        Content before
372 //        <blocktag>
373 //          Contents of the block.
374 //        </blocktag>
375 //        Content after
376 // Paragraph style:
377 //        Content before
378 //          <paratag>Contents of the paragraph.</paratag>
379 //        Content after
380 // Inline style:
381 //    Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
382
383 ///
384 void openTag(XMLStream & xs, const docstring & tag, const docstring & attr, const std::string & tagtype);
385 ///
386 void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype);
387 ///
388 void openTag(XMLStream & xs, const docstring & tag, const std::string & attr, const std::string & tagtype);
389 ///
390 void openTag(XMLStream & xs, const std::string & tag, const docstring & attr, const std::string & tagtype);
391 ///
392 void closeTag(XMLStream & xs, const docstring & tag, const std::string & tagtype);
393 ///
394 void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype);
395 ///
396 void compTag(XMLStream & xs, const docstring & tag, const docstring & attr, const std::string & tagtype);
397 ///
398 void compTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype);
399 ///
400 void compTag(XMLStream & xs, const docstring & tag, const std::string & attr, const std::string & tagtype);
401 ///
402 void compTag(XMLStream & xs, const std::string & tag, const docstring & attr, const std::string & tagtype);
403
404 } // namespace xml
405
406
407 /// Comparison operators for tags. They are defined as free functions, otherwise comparison of casts does not work.
408 /// For font tags, do not only compare the XML tag, but also the font type: several fonts can be using the same tag.
409 /// In XHTML, <span>; in DocBook, <emphasis>.
410 bool operator==(xml::StartTag const & lhs, xml::StartTag const & rhs);
411 bool operator==(xml::EndTag const & lhs, xml::StartTag const & rhs);
412 bool operator!=(xml::EndTag const & lhs, xml::StartTag const & rhs);
413 bool operator!=(xml::StartTag const & lhs, xml::StartTag const & rhs);
414
415 } // namespace lyx
416
417 #endif // XML_H