]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.h
New methods in LaTeXFeatures specifically for collection of CSS
[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 #include "support/docstream.h"
17 #include "support/strfwd.h"
18
19 #include <deque>
20
21 namespace lyx {
22
23 class Buffer;
24 class OutputParams;
25 class Text;
26
27 // Inspiration for the *Tag structs and for XHTMLStream
28 // came from MathStream and its cousins.
29
30 namespace html {
31 /// Attributes will be escaped automatically and so should NOT
32 /// be escaped before being passed to the constructor.
33 struct StartTag {
34         ///
35         explicit StartTag(std::string const & tag) : tag_(tag), keepempty_(false) {}
36         ///
37         explicit StartTag(std::string const & tag, std::string const & attr, 
38                 bool keepempty = false) 
39                 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
40         /// <tag_ attr_>
41         docstring asTag() const;
42         /// </tag_>
43         docstring asEndTag() const;
44         ///
45         std::string tag_;
46         ///
47         std::string attr_;
48         /// whether to keep things like "<tag></tag>" or discard them
49         /// you would want this for td, e.g, but maybe not for a div
50         bool keepempty_;
51 };
52
53
54 struct EndTag {
55         ///
56         explicit EndTag(std::string tag) : tag_(tag) {}
57         /// </tag_>
58         docstring asEndTag() const;
59         ///
60         std::string tag_;
61 };
62
63
64 // FIXME XHTML
65 // We need to allow these to be deferrable, which means it should
66 // inherit from StartTag. This is probably better, anyway, but we'll
67 // need to re-work a bit of code....
68 /// Tags like <img />
69 /// Attributes will be escaped automatically and so should NOT
70 /// be escaped before being passed to the constructor.
71 struct CompTag {
72         ///
73         explicit CompTag(std::string const & tag)
74                 : tag_(tag) {}
75         ///
76         explicit CompTag(std::string const & tag, std::string const & attr)
77                 : tag_(tag), attr_(attr) {}
78         /// <tag_ attr_ />
79         docstring asTag() const;
80         ///
81         std::string tag_;
82         ///
83         std::string attr_;
84 };
85
86 // trivial struct for output of newlines
87 struct CR{};
88
89 } // namespace html
90
91 class XHTMLStream {
92 public:
93         ///
94         explicit XHTMLStream(odocstream & os);
95         ///
96         odocstream & os() { return os_; }
97         ///
98         // int & tab() { return tab_; }
99         /// closes any font tags that are eligible to be closed,
100         /// i.e., last on the tag_stack_.
101         /// \return false if there are open font tags we could not close.
102         /// because they are "blocked" by open non-font tags on the stack.
103         bool closeFontTags();
104         /// call at start of paragraph. sets a mark so we know what tags
105         /// to close at the end. 
106         void startParagraph(bool keep_empty);
107         /// call at end of paragraph to clear that mark. note that this
108         /// will also close any tags still open. 
109         void endParagraph();
110         ///
111         XHTMLStream & operator<<(docstring const &);
112         ///
113         XHTMLStream & operator<<(const char *);
114         ///
115         XHTMLStream & operator<<(char_type);
116         ///
117         XHTMLStream & operator<<(int);
118         ///
119         XHTMLStream & operator<<(char);
120         ///
121         XHTMLStream & operator<<(html::StartTag const &);
122         ///
123         XHTMLStream & operator<<(html::EndTag const &);
124         ///
125         XHTMLStream & operator<<(html::CompTag const &);
126         ///
127         XHTMLStream & operator<<(html::CR const &);
128         ///
129         enum EscapeSettings {
130                 ESCAPE_NONE,
131                 ESCAPE_AND, // meaning &
132                 ESCAPE_ALL  // meaning <, >, &, at present
133         };
134         /// Sets what we are going to escape on the NEXT write.
135         /// Everything is reset for the next time.
136         XHTMLStream & operator<<(EscapeSettings);
137 private:
138         ///
139         void clearTagDeque();
140         ///
141         bool isTagOpen(std::string const &);
142         ///
143         void writeError(std::string const &);
144         ///
145         odocstream & os_;
146         ///
147         typedef std::deque<html::StartTag> TagStack;
148         /// holds start tags until we know there is content in them.
149         TagStack pending_tags_;
150         /// remembers the history, so we can make sure we nest properly.
151         TagStack tag_stack_;
152         /// 
153         EscapeSettings escape_;
154 };
155
156 ///
157 void xhtmlParagraphs(Text const & text,
158                        Buffer const & buf,
159                        XHTMLStream & xs,
160                        OutputParams const & runparams);
161
162 /// \return a string appropriate for setting alignment in CSS
163 /// Does NOT return "justify" for "block"
164 std::string alignmentToCSS(LyXAlignment align);
165
166 namespace html {
167 ///
168 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
169 /// converts a string to a form safe for links, etc
170 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
171 /// cleans \param str for use as an atttribute by replacing
172 /// all non-alnum by "_"
173 docstring cleanAttr(docstring const & str);
174 ///
175 std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
176 /// 
177 std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
178 /// 
179 std::string cleanAttr(std::string const & str);
180
181 } // namespace html
182 } // namespace lyx
183
184 #endif