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.
37 explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
39 explicit StartTag(std::string const & tag, std::string const & attr,
40 bool keepempty = false)
41 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
43 docstring asTag() const;
45 docstring asEndTag() const;
50 /// whether to keep things like "<tag></tag>" or discard them
51 /// you would want this for td, e.g, but maybe not for a div
58 explicit EndTag(std::string tag) : tag_(tag) {}
60 docstring asEndTag() const;
67 // We need to allow these to be deferrable, which means it should
68 // inherit from StartTag. This is probably better, anyway, but we'll
69 // need to re-work a bit of code....
71 /// Attributes will be escaped automatically and so should NOT
72 /// be escaped before being passed to the constructor.
75 explicit CompTag(std::string const & tag)
78 explicit CompTag(std::string const & tag, std::string const & attr)
79 : tag_(tag), attr_(attr) {}
81 docstring asTag() const;
89 // trivial struct for output of newlines
97 explicit XHTMLStream(odocstream & os);
99 odocstream & os() { return os_; }
101 // int & tab() { return tab_; }
102 /// closes any font tags that are eligible to be closed,
103 /// i.e., last on the tag_stack_.
104 /// \return false if there are open font tags we could not close.
105 /// because they are "blocked" by open non-font tags on the stack.
106 bool closeFontTags();
107 /// call at start of paragraph. sets a mark so we know what tags
108 /// to close at the end.
109 void startParagraph(bool keep_empty);
110 /// call at end of paragraph to clear that mark. note that this
111 /// will also close any tags still open.
114 XHTMLStream & operator<<(docstring const &);
116 XHTMLStream & operator<<(const char *);
118 XHTMLStream & operator<<(char_type);
120 XHTMLStream & operator<<(int);
122 XHTMLStream & operator<<(char);
124 XHTMLStream & operator<<(html::StartTag const &);
126 XHTMLStream & operator<<(html::EndTag const &);
128 XHTMLStream & operator<<(html::CompTag const &);
130 XHTMLStream & operator<<(html::CR const &);
132 enum EscapeSettings {
134 ESCAPE_AND, // meaning &
135 ESCAPE_ALL // meaning <, >, &, at present
137 /// Sets what we are going to escape on the NEXT write.
138 /// Everything is reset for the next time.
139 XHTMLStream & operator<<(EscapeSettings);
141 /// This routine is for debugging the tag stack, etc. Code
142 /// for it is disabled by default, however, so you will need
143 /// to enable it if you want to use it.
144 void dumpTagStack(std::string const & msg) const;
148 void clearTagDeque();
150 bool isTagOpen(std::string const &) const;
152 bool isTagPending(std::string const &) const;
154 void writeError(std::string const &) const;
158 EscapeSettings escape_;
159 // What we would really like to do here is simply use a
160 // deque<StartTag>. But we want to store both StartTags and
161 // sub-classes thereof on this stack, which means we run into the
162 // so-called polymorphic class problem with the STL. We therefore have
163 // to use a deque<StartTag *>, which leads to the question who will
164 // own these pointers and how they will be deleted, so we use shared
167 typedef shared_ptr<html::StartTag> TagPtr;
168 typedef std::deque<TagPtr> TagDeque;
170 template <typename T>
171 shared_ptr<T> makeTagPtr(T const & tag)
172 { return shared_ptr<T>(new T(tag)); }
174 TagDeque pending_tags_;
180 void xhtmlParagraphs(Text const & text,
183 OutputParams const & runparams);
185 /// \return a string appropriate for setting alignment in CSS
186 /// Does NOT return "justify" for "block"
187 std::string alignmentToCSS(LyXAlignment align);
191 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
192 /// converts a string to a form safe for links, etc
193 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
194 /// cleans \param str for use as an atttribute by replacing
195 /// all non-alnum by "_"
196 docstring cleanAttr(docstring const & str);
198 std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
200 std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
202 std::string cleanAttr(std::string const & str);