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 MathStream and its cousins.
42 explicit XMLStream(odocstream & os): os_(os), escape_(ESCAPE_ALL) {}
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
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;
103 void clearTagDeque();
105 void writeError(std::string const &) const;
107 void writeError(docstring const &) const;
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
120 typedef std::shared_ptr<xml::StartTag> TagPtr;
121 typedef std::deque<TagPtr> TagDeque;
123 template <typename T>
124 TagPtr makeTagPtr(T const & tag) { return std::make_shared<T>(tag); }
126 TagDeque pending_tags_;
133 /// Escape the given character, if necessary, to an entity.
134 docstring escapeChar(char_type c, XMLStream::EscapeSettings e);
136 /// Escape the given character, if necessary, to an entity.
137 docstring escapeChar(char c, XMLStream::EscapeSettings e);
139 /// Escape a word instead of a single character
140 docstring escapeString(docstring const & raw, XMLStream::EscapeSettings e=XMLStream::ESCAPE_ALL);
142 /// cleans \param str for use as an attribute by replacing all non-altnum by "_"
143 docstring cleanAttr(docstring const & str);
145 /// \p c must be ASCII
146 docstring escapeChar(char c, XMLStream::EscapeSettings e);
148 /// replaces illegal characters from ID attributes
149 docstring cleanID(docstring const &orig);
151 /// returns a unique numeric ID
152 docstring uniqueID(docstring const & label);
157 /// Attributes will be escaped automatically and so should NOT
158 /// be escaped before being passed to the constructor.
162 explicit StartTag(std::string const & tag) : tag_(from_ascii(tag)), keepempty_(false) {}
164 explicit StartTag(docstring const & tag) : tag_(tag), keepempty_(false) {}
166 explicit StartTag(docstring const & tag, docstring const & attr,
167 bool keepempty = false)
168 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
170 explicit StartTag(std::string const & tag, std::string const & attr,
171 bool keepempty = false)
172 : tag_(from_ascii(tag)), attr_(from_ascii(attr)), keepempty_(keepempty) {}
174 explicit StartTag(std::string const & tag, docstring const & attr,
175 bool keepempty = false)
176 : tag_(from_ascii(tag)), attr_(attr), keepempty_(keepempty) {}
178 virtual ~StartTag() {}
180 virtual docstring writeTag() const;
182 virtual docstring writeEndTag() const;
184 virtual FontTag const * asFontTag() const { return nullptr; }
186 virtual bool operator==(StartTag const & rhs) const
187 { return tag_ == rhs.tag_; }
189 virtual bool operator!=(StartTag const & rhs) const
190 { return !(*this == rhs); }
192 virtual bool operator==(FontTag const & rhs) const;
197 /// whether to keep things like "<tag></tag>" or discard them
198 /// you would want this for td, e.g, but maybe not for a div
207 explicit EndTag(std::string tag) : tag_(from_ascii(tag)) {}
209 explicit EndTag(docstring tag) : tag_(tag) {}
213 virtual docstring writeEndTag() const;
215 bool operator==(StartTag const & rhs) const
216 { return tag_ == rhs.tag_; }
218 bool operator!=(StartTag const & rhs) const
219 { return !(*this == rhs); }
221 virtual EndFontTag const * asFontTag() const { return 0; }
227 /// Tags like <img />
228 /// Attributes will be escaped automatically and so should NOT
229 /// be escaped before being passed to the constructor.
233 explicit CompTag(std::string const & tag)
236 explicit CompTag(std::string const & tag, std::string const & attr)
237 : tag_(tag), attr_(attr) {}
239 docstring writeTag() const;
247 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
248 /// parid is only used for HTML output; XML is supposed to use attr for this.
249 struct ParTag : public StartTag
252 explicit ParTag(std::string const & tag, const std::string & attr): StartTag(tag, from_utf8(attr)) {}
293 // When updating this list, also update fontToTag in both output_docbook.cpp and output_xhtml.cpp,
294 // fontToRole in output_docbook.cpp, and fontToAttribute in output_xhtml.cpp.
299 struct FontTag : public StartTag
302 FontTag(docstring const & tag, FontTypes type): StartTag(tag), font_type_(type) {}
304 FontTag(std::string const & tag, FontTypes type): StartTag(from_utf8(tag)), font_type_(type) {}
306 FontTag(docstring const & tag, docstring const & attr, FontTypes type): StartTag(tag, attr), font_type_(type) {}
308 FontTag(std::string const & tag, std::string const & attr, FontTypes type): StartTag(from_utf8(tag), from_utf8(attr)), font_type_(type) {}
310 FontTag const * asFontTag() const override { return this; }
312 bool operator==(StartTag const &) const override;
314 FontTypes font_type_;
319 struct EndFontTag : public EndTag
322 EndFontTag(docstring const & tag, FontTypes type): EndTag(tag), font_type_(type) {}
324 EndFontTag(std::string const & tag, FontTypes type): EndTag(from_utf8(tag)), font_type_(type) {}
326 EndFontTag const * asFontTag() const override { return this; }
328 FontTypes font_type_;
332 // trivial struct for output of newlines
335 // an illegal tag for internal use
336 xml::StartTag const parsep_tag("&LyX_parsep_tag&");
339 void openTag(odocstream & os, std::string const & name,
340 std::string const & attribute = std::string());
343 void openTag(Buffer const & buf, odocstream & os,
344 OutputParams const & runparams, Paragraph const & par);
347 void closeTag(odocstream & os, std::string const & name);
350 void closeTag(odocstream & os, Paragraph const & par);