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.
33 /// Attributes will be escaped automatically and so should NOT
34 /// be escaped before being passed to the constructor.
38 explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
40 explicit StartTag(std::string const & tag, std::string const & attr,
41 bool keepempty = false)
42 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
46 virtual docstring asTag() const;
48 virtual docstring asEndTag() const;
53 /// whether to keep things like "<tag></tag>" or discard them
54 /// you would want this for td, e.g, but maybe not for a div
59 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
60 struct ParTag : public StartTag
63 ParTag(std::string const & tag, std::string const & attr,
64 std::string const & parid)
65 : StartTag(tag, attr), parid_(parid)
70 docstring asTag() const;
71 /// the "magic par label" for this paragraph
79 explicit EndTag(std::string tag) : tag_(tag) {}
81 docstring asEndTag() const;
88 /// Attributes will be escaped automatically and so should NOT
89 /// be escaped before being passed to the constructor.
93 explicit CompTag(std::string const & tag)
96 explicit CompTag(std::string const & tag, std::string const & attr)
97 : tag_(tag), attr_(attr) {}
99 docstring asTag() const;
107 // trivial struct for output of newlines
115 explicit XHTMLStream(odocstream & os);
117 odocstream & os() { return os_; }
119 // int & tab() { return tab_; }
120 /// closes any font tags that are eligible to be closed,
121 /// i.e., last on the tag_stack_.
122 /// \return false if there are open font tags we could not close.
123 /// because they are "blocked" by open non-font tags on the stack.
124 bool closeFontTags();
125 /// call at start of paragraph. sets a mark so we know what tags
126 /// to close at the end.
127 void startParagraph(bool keep_empty);
128 /// call at end of paragraph to clear that mark. note that this
129 /// will also close any tags still open.
132 XHTMLStream & operator<<(docstring const &);
134 XHTMLStream & operator<<(const char *);
136 XHTMLStream & operator<<(char_type);
138 XHTMLStream & operator<<(int);
140 XHTMLStream & operator<<(char);
142 XHTMLStream & operator<<(html::StartTag const &);
144 XHTMLStream & operator<<(html::EndTag const &);
146 XHTMLStream & operator<<(html::CompTag const &);
148 XHTMLStream & operator<<(html::ParTag const &);
150 XHTMLStream & operator<<(html::CR const &);
152 enum EscapeSettings {
154 ESCAPE_AND, // meaning &
155 ESCAPE_ALL // meaning <, >, &, at present
157 /// Sets what we are going to escape on the NEXT write.
158 /// Everything is reset for the next time.
159 XHTMLStream & operator<<(EscapeSettings);
161 /// This routine is for debugging the tag stack, etc. Code
162 /// for it is disabled by default, however, so you will need
163 /// to enable it if you want to use it.
164 void dumpTagStack(std::string const & msg) const;
168 void clearTagDeque();
170 bool isTagOpen(std::string const &) const;
172 bool isTagPending(std::string const &) const;
174 void writeError(std::string const &) const;
178 EscapeSettings escape_;
179 // What we would really like to do here is simply use a
180 // deque<StartTag>. But we want to store both StartTags and
181 // sub-classes thereof on this stack, which means we run into the
182 // so-called polymorphic class problem with the STL. We therefore have
183 // to use a deque<StartTag *>, which leads to the question who will
184 // own these pointers and how they will be deleted, so we use shared
187 typedef shared_ptr<html::StartTag> TagPtr;
188 typedef std::deque<TagPtr> TagDeque;
190 template <typename T>
191 shared_ptr<T> makeTagPtr(T const & tag)
192 { return shared_ptr<T>(new T(tag)); }
194 TagDeque pending_tags_;
200 void xhtmlParagraphs(Text const & text,
203 OutputParams const & runparams);
205 /// \return a string appropriate for setting alignment in CSS
206 /// Does NOT return "justify" for "block"
207 std::string alignmentToCSS(LyXAlignment align);
211 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
212 /// converts a string to a form safe for links, etc
213 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
214 /// cleans \param str for use as an atttribute by replacing
215 /// all non-alnum by "_"
216 docstring cleanAttr(docstring const & str);
218 std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
220 std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
222 std::string cleanAttr(std::string const & str);