4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
12 #ifndef OUTPUT_XHTML_H
13 #define OUTPUT_XHTML_H
15 #include "LayoutEnums.h"
17 #include "support/docstream.h"
18 #include "support/strfwd.h"
30 // Inspiration for the *Tag structs and for XHTMLStream
31 // came from MathStream and its cousins.
38 /// Attributes will be escaped automatically and so should NOT
39 /// be escaped before being passed to the constructor.
43 explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
45 explicit StartTag(std::string const & tag, std::string const & attr,
46 bool keepempty = false)
47 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
49 virtual ~StartTag() {}
51 virtual docstring writeTag() const;
53 virtual docstring writeEndTag() const;
55 virtual FontTag const * asFontTag() const { return 0; }
57 virtual bool operator==(StartTag const & rhs) const
58 { return tag_ == rhs.tag_; }
60 virtual bool operator!=(StartTag const & rhs) const
61 { return !(*this == rhs); }
63 virtual bool operator==(FontTag const & rhs) const;
68 /// whether to keep things like "<tag></tag>" or discard them
69 /// you would want this for td, e.g, but maybe not for a div
78 explicit EndTag(std::string tag) : tag_(tag) {}
82 virtual docstring writeEndTag() const;
84 bool operator==(StartTag const & rhs) const
85 { return tag_ == rhs.tag_; }
87 bool operator!=(StartTag const & rhs) const
88 { return !(*this == rhs); }
90 virtual EndFontTag const * asFontTag() const { return 0; }
97 /// Attributes will be escaped automatically and so should NOT
98 /// be escaped before being passed to the constructor.
102 explicit CompTag(std::string const & tag)
105 explicit CompTag(std::string const & tag, std::string const & attr)
106 : tag_(tag), attr_(attr) {}
108 docstring writeTag() const;
116 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
117 struct ParTag : public StartTag
120 explicit ParTag(std::string const & tag, std::string attr,
121 std::string const & parid);
165 struct FontTag : public StartTag
168 explicit FontTag(FontTypes type);
170 FontTag const * asFontTag() const { return this; }
172 bool operator==(StartTag const &) const;
174 FontTypes font_type_;
179 struct EndFontTag : public EndTag
182 explicit EndFontTag(FontTypes type);
184 EndFontTag const * asFontTag() const { return this; }
186 FontTypes font_type_;
190 // trivial struct for output of newlines
198 explicit XHTMLStream(odocstream & os);
200 odocstream & os() { return os_; }
202 // int & tab() { return tab_; }
203 /// closes any font tags that are eligible to be closed,
204 /// i.e., last on the tag_stack_.
205 /// \return false if there are open font tags we could not close.
206 /// because they are "blocked" by open non-font tags on the stack.
207 bool closeFontTags();
208 /// sets a mark so we know what tags to close at the end.
209 /// normally called at the start of a paragraph.
210 void startDivision(bool keep_empty);
211 /// clears the mark set by previous method.
212 /// there should not be any other tags open before it on the stack,
213 /// but if there are, we will close them.
216 XHTMLStream & operator<<(docstring const &);
218 XHTMLStream & operator<<(const char *);
220 XHTMLStream & operator<<(char_type);
222 XHTMLStream & operator<<(int);
224 XHTMLStream & operator<<(char);
226 XHTMLStream & operator<<(html::StartTag const &);
228 XHTMLStream & operator<<(html::EndTag const &);
230 XHTMLStream & operator<<(html::CompTag const &);
232 XHTMLStream & operator<<(html::ParTag const &);
234 XHTMLStream & operator<<(html::FontTag const &);
236 XHTMLStream & operator<<(html::CR const &);
238 enum EscapeSettings {
240 ESCAPE_AND, // meaning &
241 ESCAPE_ALL // meaning <, >, &, at present
243 /// Sets what we are going to escape on the NEXT write.
244 /// Everything is reset for the next time.
245 XHTMLStream & operator<<(EscapeSettings);
246 /// This routine is for debugging the tag stack, etc. Code
247 /// for it is disabled by default, however, so you will need
248 /// to enable it if you want to use it.
249 void dumpTagStack(std::string const & msg);
252 void clearTagDeque();
254 bool isTagOpen(html::StartTag const &) const;
256 bool isTagOpen(html::EndTag const &) const;
258 bool isTagPending(html::StartTag const &) const;
260 void writeError(std::string const &) const;
264 EscapeSettings escape_;
265 // What we would really like to do here is simply use a
266 // deque<StartTag>. But we want to store both StartTags and
267 // sub-classes thereof on this stack, which means we run into the
268 // so-called polymorphic class problem with the STL. We therefore have
269 // to use a deque<StartTag *>, which leads to the question who will
270 // own these pointers and how they will be deleted, so we use shared
273 typedef std::shared_ptr<html::StartTag> TagPtr;
274 typedef std::deque<TagPtr> TagDeque;
276 template <typename T>
277 TagPtr makeTagPtr(T const & tag) { return std::make_shared<T>(tag); }
279 TagDeque pending_tags_;
285 void xhtmlParagraphs(Text const & text,
288 OutputParams const & runparams);
290 /// \return a string appropriate for setting alignment in CSS
291 /// Does NOT return "justify" for "block"
292 std::string alignmentToCSS(LyXAlignment align);
296 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
297 /// converts a string to a form safe for links, etc
298 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
299 /// cleans \param str for use as an atttribute by replacing
300 /// all non-alnum by "_"
301 docstring cleanAttr(docstring const & str);
302 /// \p c must be ASCII
303 docstring escapeChar(char c, XHTMLStream::EscapeSettings e);