]> git.lyx.org Git - features.git/blob - src/output_xhtml.h
21ec8f61bbbcfe02bb123c52de2a8b938a41ed54
[features.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 "support/docstream.h"
16 #include "support/strfwd.h"
17
18 #include <deque>
19 #include <vector>
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 /// Attributes will be escaped automatically and so should NOT
31 /// be escaped before passing to the constructor.
32 struct StartTag {
33         ///
34         explicit StartTag(std::string const & tag) : tag_(tag) {}
35         ///
36         explicit StartTag(std::string const & tag, std::string const & attr, 
37                 bool keepempty = false) 
38                 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
39         /// <tag_ attr_>
40         docstring asTag() const;
41         /// </tag_>
42         docstring asEndTag() const;
43         ///
44         std::string tag_;
45         ///
46         std::string attr_;
47         /// whether to keep things like "<tag></tag>" or discard them
48         /// you would want this for td, e.g, but maybe not for a div
49         bool keepempty_;
50 };
51
52
53 struct EndTag {
54         ///
55         explicit EndTag(std::string tag) : tag_(tag) {}
56         /// </tag_>
57         docstring asEndTag() const;
58         ///
59         std::string tag_;
60 };
61
62
63 // FIXME XHTML
64 // We need to allow these to be deferrable, which means it should
65 // inherit from StartTag. This is probably better, anyway, but we'll
66 // need to re-work a bit of code....
67 /// Tags like <img />
68 /// Attributes will be escaped automatically and so should NOT
69 /// be escaped before passing to the constructor.
70 struct CompTag {
71         ///
72         explicit CompTag(std::string const & tag)
73                 : tag_(tag) {}
74         ///
75         explicit CompTag(std::string const & tag, std::string const & attr)
76                 : tag_(tag), attr_(attr) {}
77         /// <tag_ attr_ />
78         docstring asTag() const;
79         ///
80         std::string tag_;
81         ///
82         std::string attr_;
83 };
84
85
86 class XHTMLStream {
87 public:
88         ///
89         explicit XHTMLStream(odocstream & os);
90         ///
91         void cr();
92         ///
93         odocstream & os() { return os_; }
94         ///
95         // int & tab() { return tab_; }
96         /// closes any font tags that are eligible to be closed,
97         /// i.e., last on the tag_stack_.
98         /// \return false if there are open font tags we could not close.
99         /// because they are "blocked" by open non-font tags on the stack.
100         bool closeFontTags();
101         ///
102         XHTMLStream & operator<<(docstring const &);
103         ///
104         XHTMLStream & operator<<(const char *);
105         ///
106         XHTMLStream & operator<<(char_type);
107         ///
108         XHTMLStream & operator<<(int);
109         ///
110         XHTMLStream & operator<<(StartTag const &);
111         ///
112         XHTMLStream & operator<<(EndTag const &);
113         ///
114         XHTMLStream & operator<<(CompTag const &);
115         /// A trivial struct that functions as a stream modifier.
116         /// << NextRaw() causes the next string-like thing sent to the
117         /// stream not to be escaped.
118         struct NextRaw {};
119         ///
120         XHTMLStream & operator<<(NextRaw const &);
121 private:
122         ///
123         void clearTagDeque();
124         ///
125         bool isTagOpen(std::string const &);
126         ///
127         void writeError(std::string const &);
128         ///
129         odocstream & os_;
130         ///
131         // int tab_;
132         ///
133         typedef std::deque<StartTag> TagDeque;
134         ///
135         typedef std::vector<StartTag> TagStack;
136         /// holds start tags until we know there is content in them.
137         TagDeque pending_tags_;
138         /// remembers the history, so we can make sure we nest properly.
139         TagStack tag_stack_;
140         /// 
141         bool nextraw_;
142 };
143
144 ///
145 void xhtmlParagraphs(Text const & text,
146                        Buffer const & buf,
147                        XHTMLStream & xs,
148                        OutputParams const & runparams);
149
150 namespace html {
151 ///
152 docstring escapeChar(char_type c);
153 /// converts a string to a form safe for links, etc
154 docstring htmlize(docstring const & str);
155 /// cleans \param str for use as an atttribute by replacing
156 /// all non-alnum by "_"
157 docstring cleanAttr(docstring const & str);
158 ///
159 std::string escapeChar(char c);
160 /// 
161 std::string htmlize(std::string const & str);
162 /// 
163 std::string cleanAttr(std::string const & str);
164
165 // to be removed
166 /// \return true if tag was opened, false if not 
167 bool openTag(odocstream & os, std::string const & tag, 
168                                                  std::string const & attr);
169 /// \return true if tag was opened, false if not 
170 bool closeTag(odocstream & os, std::string const & tag);
171 }
172 } // namespace lyx
173
174 #endif