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/shared_ptr.h"
19 #include "support/strfwd.h"
29 // Inspiration for the *Tag structs and for XHTMLStream
30 // came from MathStream and its cousins.
37 /// Attributes will be escaped automatically and so should NOT
38 /// be escaped before being passed to the constructor.
42 explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
44 explicit StartTag(std::string const & tag, std::string const & attr,
45 bool keepempty = false)
46 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
50 virtual docstring writeTag() const;
52 virtual docstring writeEndTag() const;
54 virtual FontTag const * asFontTag() const { return 0; }
56 virtual bool operator==(StartTag const & rhs) const
57 { return tag_ == rhs.tag_; }
59 virtual bool operator!=(StartTag const & rhs) const
60 { return !(*this == rhs); }
62 virtual bool operator==(FontTag const & rhs) const;
67 /// whether to keep things like "<tag></tag>" or discard them
68 /// you would want this for td, e.g, but maybe not for a div
77 explicit EndTag(std::string tag) : tag_(tag) {}
79 virtual docstring writeEndTag() const;
81 bool operator==(StartTag const & rhs) const
82 { return tag_ == rhs.tag_; }
84 bool operator!=(StartTag const & rhs) const
85 { return !(*this == rhs); }
87 virtual EndFontTag const * asFontTag() const { return 0; }
94 /// Attributes will be escaped automatically and so should NOT
95 /// be escaped before being passed to the constructor.
99 explicit CompTag(std::string const & tag)
102 explicit CompTag(std::string const & tag, std::string const & attr)
103 : tag_(tag), attr_(attr) {}
105 docstring writeTag() const;
113 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
114 struct ParTag : public StartTag
117 explicit ParTag(std::string const & tag, std::string const & attr,
118 std::string const & parid)
119 : StartTag(tag, attr), parid_(parid)
124 docstring writeTag() const;
125 /// the "magic par label" for this paragraph
155 struct FontTag : public StartTag
158 explicit FontTag(FontTypes type);
160 FontTag const * asFontTag() const { return this; }
162 bool operator==(StartTag const &) const;
164 FontTypes font_type_;
169 struct EndFontTag : public EndTag
172 explicit EndFontTag(FontTypes type);
174 EndFontTag const * asFontTag() const { return this; }
176 FontTypes font_type_;
180 // trivial struct for output of newlines
188 explicit XHTMLStream(odocstream & os);
190 odocstream & os() { return os_; }
192 // int & tab() { return tab_; }
193 /// closes any font tags that are eligible to be closed,
194 /// i.e., last on the tag_stack_.
195 /// \return false if there are open font tags we could not close.
196 /// because they are "blocked" by open non-font tags on the stack.
197 bool closeFontTags();
198 /// call at start of paragraph. sets a mark so we know what tags
199 /// to close at the end.
200 void startParagraph(bool keep_empty);
201 /// call at end of paragraph to clear that mark. note that this
202 /// will also close any tags still open.
205 XHTMLStream & operator<<(docstring const &);
207 XHTMLStream & operator<<(const char *);
209 XHTMLStream & operator<<(char_type);
211 XHTMLStream & operator<<(int);
213 XHTMLStream & operator<<(char);
215 XHTMLStream & operator<<(html::StartTag const &);
217 XHTMLStream & operator<<(html::EndTag const &);
219 XHTMLStream & operator<<(html::CompTag const &);
221 XHTMLStream & operator<<(html::ParTag const &);
223 XHTMLStream & operator<<(html::FontTag const &);
225 XHTMLStream & operator<<(html::CR const &);
227 enum EscapeSettings {
229 ESCAPE_AND, // meaning &
230 ESCAPE_ALL // meaning <, >, &, at present
232 /// Sets what we are going to escape on the NEXT write.
233 /// Everything is reset for the next time.
234 XHTMLStream & operator<<(EscapeSettings);
236 /// This routine is for debugging the tag stack, etc. Code
237 /// for it is disabled by default, however, so you will need
238 /// to enable it if you want to use it.
239 void dumpTagStack(std::string const & msg) const;
243 void clearTagDeque();
245 bool isTagOpen(html::StartTag const &) const;
247 bool isTagOpen(html::EndTag const &) const;
249 bool isTagPending(html::StartTag const &) const;
251 void writeError(std::string const &) const;
255 EscapeSettings escape_;
256 // What we would really like to do here is simply use a
257 // deque<StartTag>. But we want to store both StartTags and
258 // sub-classes thereof on this stack, which means we run into the
259 // so-called polymorphic class problem with the STL. We therefore have
260 // to use a deque<StartTag *>, which leads to the question who will
261 // own these pointers and how they will be deleted, so we use shared
264 typedef shared_ptr<html::StartTag> TagPtr;
265 typedef std::deque<TagPtr> TagDeque;
267 template <typename T>
268 shared_ptr<T> makeTagPtr(T const & tag)
269 { return shared_ptr<T>(new T(tag)); }
271 TagDeque pending_tags_;
277 void xhtmlParagraphs(Text const & text,
280 OutputParams const & runparams);
282 /// \return a string appropriate for setting alignment in CSS
283 /// Does NOT return "justify" for "block"
284 std::string alignmentToCSS(LyXAlignment align);
288 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
289 /// converts a string to a form safe for links, etc
290 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
291 /// cleans \param str for use as an atttribute by replacing
292 /// all non-alnum by "_"
293 docstring cleanAttr(docstring const & str);
295 std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
297 std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
299 std::string cleanAttr(std::string const & str);