4 * This file is part of LyX, the document processor.
5 * License details can be found in the file COPYING.
10 * Full author contact details are available in file CREDITS.
16 #include "support/docstring.h"
27 // Inspiration for the *Tag structs and for XMLStream
28 // came from MathMLStream and its cousins.
42 explicit XMLStream(odocstream & os): os_(os), escape_(ESCAPE_ALL), is_last_tag_cr_(true) {}
44 odocstream & os() { return os_; }
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.
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.
60 XMLStream & operator<<(docstring const &);
62 XMLStream & operator<<(const char *);
64 XMLStream & operator<<(char_type);
66 XMLStream & operator<<(int);
68 XMLStream & operator<<(char);
70 XMLStream & operator<<(xml::StartTag const &);
72 XMLStream & operator<<(xml::EndTag const &);
74 XMLStream & operator<<(xml::CompTag const &);
76 XMLStream & operator<<(xml::ParTag const &);
78 XMLStream & operator<<(xml::FontTag const &);
80 XMLStream & operator<<(xml::CR const &);
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
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);
96 bool isTagOpen(xml::StartTag const &, int maxdepth = -1) const;
98 bool isTagOpen(xml::EndTag const &, int maxdepth = -1) const;
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_; };
106 void writeError(std::string const &);
108 void writeError(docstring const &);
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();
115 void clearTagDeque();
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
128 typedef std::deque<TagPtr> TagDeque;
130 template <typename T>
131 TagPtr makeTagPtr(T const & tag) { return std::make_shared<T>(tag); }
133 TagDeque pending_tags_;
137 bool is_last_tag_cr_;
142 /// Escape the given character, if necessary, to an entity.
143 docstring escapeChar(char_type c, XMLStream::EscapeSettings e);
145 /// Escape the given character, if necessary, to an entity.
146 docstring escapeChar(char c, XMLStream::EscapeSettings e);
148 /// Escape a word instead of a single character
149 docstring escapeString(docstring const & raw, XMLStream::EscapeSettings e=XMLStream::ESCAPE_ALL);
151 /// cleans \param str for use as an attribute by replacing all non-altnum by "_"
152 docstring cleanAttr(docstring const & str);
154 /// \p c must be ASCII
155 docstring escapeChar(char c, XMLStream::EscapeSettings e);
157 /// replaces illegal characters from ID attributes
158 docstring cleanID(docstring const &orig);
160 /// returns a unique numeric ID
161 docstring uniqueID(docstring const & label);
163 /// determines whether a string only contains space characters
164 bool isNotOnlySpace(docstring const & str);
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);
172 /// Attributes will be escaped automatically and so should NOT
173 /// be escaped before being passed to the constructor.
177 explicit StartTag(std::string const & tag) : tag_(from_ascii(tag)), keepempty_(false), tagtype_("none") {}
179 explicit StartTag(docstring const & tag) : tag_(tag), keepempty_(false), tagtype_("none") {}
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) {}
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) {}
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) {}
193 virtual ~StartTag() = default;
195 virtual docstring writeTag() const;
197 virtual docstring writeEndTag() const;
199 virtual FontTag const * asFontTag() const { return nullptr; }
201 virtual bool operator==(StartTag const & rhs) const
202 { return tag_ == rhs.tag_; }
204 virtual bool operator!=(StartTag const & rhs) const
205 { return !(*this == rhs); }
207 virtual bool operator==(FontTag const & rhs) const;
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
215 /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
216 std::string tagtype_;
224 explicit EndTag(std::string const & tag, std::string const & tagtype = "none")
225 : tag_(from_ascii(tag)), tagtype_(tagtype) {}
227 explicit EndTag(docstring const & tag, std::string const & tagtype = "none")
228 : tag_(tag), tagtype_(tagtype) {}
230 virtual ~EndTag() = default;
232 virtual docstring writeEndTag() const;
234 bool operator==(StartTag const & rhs) const
235 { return tag_ == rhs.tag_; }
237 bool operator!=(StartTag const & rhs) const
238 { return !(*this == rhs); }
240 virtual EndFontTag const * asFontTag() const { return nullptr; }
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_;
249 /// Tags like <img />
250 /// Attributes will be escaped automatically and so should NOT
251 /// be escaped before being passed to the constructor.
255 explicit CompTag(std::string const & tag)
256 : tag_(from_utf8(tag)), tagtype_("none") {}
258 explicit CompTag(docstring const & tag)
259 : tag_(tag), tagtype_("none") {}
261 explicit CompTag(std::string const & tag, std::string const & attr, std::string const & tagtype = "none")
262 : tag_(from_utf8(tag)), attr_(from_utf8(attr)), tagtype_(tagtype) {}
264 explicit CompTag(std::string const & tag, docstring const & attr, std::string const & tagtype = "none")
265 : tag_(from_utf8(tag)), attr_(attr), tagtype_(tagtype) {}
267 explicit CompTag(docstring const & tag, std::string const & attr, std::string const & tagtype = "none")
268 : tag_(tag), attr_(from_utf8(attr)), tagtype_(tagtype) {}
270 explicit CompTag(docstring const & tag, docstring const & attr, std::string const & tagtype = "none")
271 : tag_(tag), attr_(attr), tagtype_(tagtype) {}
273 docstring writeTag() const;
278 /// Type of tag for new-line behaviour. Either "paragraph", "inline", "block", or "none" (default).
279 std::string tagtype_;
283 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
284 struct ParTag : public StartTag
287 explicit ParTag(std::string const & tag, const std::string & attr): StartTag(tag, from_utf8(attr)) {}
289 ~ParTag() override = default;
328 // When updating this list, also update fontToTag in both output_docbook.cpp and output_xhtml.cpp,
329 // fontToRole in output_docbook.cpp, and fontToAttribute in output_xhtml.cpp.
334 struct FontTag : public StartTag
337 FontTag(docstring const & tag, FontTypes type): StartTag(tag), font_type_(type) {}
339 FontTag(std::string const & tag, FontTypes type): StartTag(from_utf8(tag)), font_type_(type) {}
341 FontTag(docstring const & tag, docstring const & attr, FontTypes type): StartTag(tag, attr), font_type_(type) {}
343 FontTag(std::string const & tag, std::string const & attr, FontTypes type): StartTag(from_utf8(tag), from_utf8(attr)), font_type_(type) {}
345 FontTag const * asFontTag() const override { return this; }
347 bool operator==(StartTag const &) const override;
349 FontTypes font_type_;
354 struct EndFontTag : public EndTag
357 EndFontTag(docstring const & tag, FontTypes type): EndTag(tag), font_type_(type) {}
359 EndFontTag(std::string const & tag, FontTypes type): EndTag(from_utf8(tag)), font_type_(type) {}
361 EndFontTag const * asFontTag() const override { return this; }
363 FontTypes font_type_;
367 // trivial struct for output of newlines
370 // an illegal tag for internal use
371 xml::StartTag const parsep_tag("&LyX_parsep_tag&");
374 void openTag(odocstream & os, std::string const & name,
375 std::string const & attribute = std::string());
378 void openTag(Buffer const & buf, odocstream & os,
379 OutputParams const & runparams, Paragraph const & par);
382 void closeTag(odocstream & os, std::string const & name);
385 void closeTag(odocstream & os, Paragraph const & par);
387 // Convenience functions to open and close tags. First, very low-level ones to ensure a consistent new-line behaviour.
391 // Contents of the block.
396 // <paratag>Contents of the paragraph.</paratag>
399 // Content before<inlinetag>Contents of the paragraph.</inlinetag>Content after
402 void openTag(XMLStream & xs, const docstring & tag, const docstring & attr, const std::string & tagtype);
404 void openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype);
406 void openTag(XMLStream & xs, const docstring & tag, const std::string & attr, const std::string & tagtype);
408 void openTag(XMLStream & xs, const std::string & tag, const docstring & attr, const std::string & tagtype);
410 void closeTag(XMLStream & xs, const docstring & tag, const std::string & tagtype);
412 void closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype);
414 void compTag(XMLStream & xs, const docstring & tag, const docstring & attr, const std::string & tagtype);
416 void compTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype);
418 void compTag(XMLStream & xs, const docstring & tag, const std::string & attr, const std::string & tagtype);
420 void compTag(XMLStream & xs, const std::string & tag, const docstring & attr, const std::string & tagtype);