]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.h
Rework how paragraph ids are handled.
[lyx.git] / src / output_xhtml.h
1 // -*- C++ -*-
2 /**
3  * \file output_xhtml.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Heck
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef OUTPUT_XHTML_H
13 #define OUTPUT_XHTML_H
14
15 #include "LayoutEnums.h"
16
17 #include "support/docstream.h"
18 #include "support/shared_ptr.h"
19 #include "support/strfwd.h"
20
21 #include <deque>
22
23 namespace lyx {
24
25 class Buffer;
26 class OutputParams;
27 class Text;
28
29 // Inspiration for the *Tag structs and for XHTMLStream
30 // came from MathStream and its cousins.
31
32 namespace html {
33 /// Attributes will be escaped automatically and so should NOT
34 /// be escaped before being passed to the constructor.
35 struct StartTag
36 {
37         ///
38         explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
39         ///
40         explicit StartTag(std::string const & tag, std::string const & attr, 
41                 bool keepempty = false) 
42                 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
43         ///
44         ~StartTag() {}
45         /// <tag_ attr_>
46         virtual docstring asTag() const;
47         /// </tag_>
48         virtual docstring asEndTag() const;
49         ///
50         std::string tag_;
51         ///
52         std::string attr_;
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
55         bool keepempty_;
56 };
57
58
59 /// A special case of StartTag, used exclusively for tags that wrap paragraphs.
60 struct ParTag : public StartTag
61 {
62         ///
63         ParTag(std::string const & tag, std::string const & attr,
64                std::string const & parid)
65           : StartTag(tag, attr), parid_(parid)
66         {}
67         ///
68         ~ParTag() {}
69         ///
70         docstring asTag() const;
71         /// the "magic par label" for this paragraph
72         std::string parid_;
73 };
74
75
76 struct EndTag
77 {
78         ///
79         explicit EndTag(std::string tag) : tag_(tag) {}
80         /// </tag_>
81         docstring asEndTag() const;
82         ///
83         std::string tag_;
84 };
85
86
87 /// Tags like <img />
88 /// Attributes will be escaped automatically and so should NOT
89 /// be escaped before being passed to the constructor.
90 struct CompTag
91 {
92         ///
93         explicit CompTag(std::string const & tag)
94                 : tag_(tag) {}
95         ///
96         explicit CompTag(std::string const & tag, std::string const & attr)
97                 : tag_(tag), attr_(attr) {}
98         /// <tag_ attr_ />
99         docstring asTag() const;
100         ///
101         std::string tag_;
102         ///
103         std::string attr_;
104 };
105
106
107 // trivial struct for output of newlines
108 struct CR{};
109
110 } // namespace html
111
112 class XHTMLStream {
113 public:
114         ///
115         explicit XHTMLStream(odocstream & os);
116         ///
117         odocstream & os() { return os_; }
118         ///
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. 
130         void endParagraph();
131         ///
132         XHTMLStream & operator<<(docstring const &);
133         ///
134         XHTMLStream & operator<<(const char *);
135         ///
136         XHTMLStream & operator<<(char_type);
137         ///
138         XHTMLStream & operator<<(int);
139         ///
140         XHTMLStream & operator<<(char);
141         ///
142         XHTMLStream & operator<<(html::StartTag const &);
143         ///
144         XHTMLStream & operator<<(html::EndTag const &);
145         ///
146         XHTMLStream & operator<<(html::CompTag const &);
147         ///
148         XHTMLStream & operator<<(html::ParTag const &);
149         ///
150         XHTMLStream & operator<<(html::CR const &);
151         ///
152         enum EscapeSettings {
153                 ESCAPE_NONE,
154                 ESCAPE_AND, // meaning &
155                 ESCAPE_ALL  // meaning <, >, &, at present
156         };
157         /// Sets what we are going to escape on the NEXT write.
158         /// Everything is reset for the next time.
159         XHTMLStream & operator<<(EscapeSettings);
160 #if 0
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;
165 #endif
166 private:
167         ///
168         void clearTagDeque();
169         ///
170         bool isTagOpen(std::string const &) const;
171         ///
172         bool isTagPending(std::string const &) const;
173         ///
174         void writeError(std::string const &) const;
175         ///
176         odocstream & os_;
177         /// 
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
185         // pointers.
186         ///
187         typedef shared_ptr<html::StartTag> TagPtr;
188         typedef std::deque<TagPtr> TagDeque;
189         ///
190         template <typename T> 
191         shared_ptr<T> makeTagPtr(T const & tag) 
192                 { return shared_ptr<T>(new T(tag)); }
193         ///
194         TagDeque pending_tags_;
195         ///
196         TagDeque tag_stack_;
197 };
198
199 ///
200 void xhtmlParagraphs(Text const & text,
201                        Buffer const & buf,
202                        XHTMLStream & xs,
203                        OutputParams const & runparams);
204
205 /// \return a string appropriate for setting alignment in CSS
206 /// Does NOT return "justify" for "block"
207 std::string alignmentToCSS(LyXAlignment align);
208
209 namespace html {
210 ///
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);
217 ///
218 std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
219 /// 
220 std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
221 /// 
222 std::string cleanAttr(std::string const & str);
223
224 } // namespace html
225 } // namespace lyx
226
227 #endif