]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.h
More notes.
[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 "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 /// Tags like <img />
64 /// Attributes will be escaped automatically and so should NOT
65 /// be escaped before passing to the constructor.
66 struct CompTag {
67         ///
68         explicit CompTag(std::string const & tag)
69                 : tag_(tag) {}
70         ///
71         explicit CompTag(std::string const & tag, std::string const & attr)
72                 : tag_(tag), attr_(attr) {}
73         /// <tag_ attr_ />
74         docstring asTag() const;
75         ///
76         std::string tag_;
77         ///
78         std::string attr_;
79 };
80
81
82 class XHTMLStream {
83 public:
84         ///
85         explicit XHTMLStream(odocstream & os);
86         ///
87         void cr();
88         ///
89         odocstream & os() { return os_; }
90         ///
91         // int & tab() { return tab_; }
92         /// closes any font tags that are eligible to be closed,
93         /// i.e., last on the tag_stack_.
94         /// \return false if there are open font tags we could not close.
95         /// because they are "blocked" by open non-font tags on the stack.
96         bool closeFontTags();
97         ///
98         XHTMLStream & operator<<(docstring const &);
99         ///
100         XHTMLStream & operator<<(const char *);
101         ///
102         XHTMLStream & operator<<(char_type);
103         ///
104         XHTMLStream & operator<<(StartTag const &);
105         ///
106         XHTMLStream & operator<<(EndTag const &);
107         ///
108         XHTMLStream & operator<<(CompTag const &);
109         /// A trivial struct that functions as a stream modifier.
110         /// << NextRaw() causes the next string-like thing sent to the
111         /// stream not to be escaped.
112         struct NextRaw {};
113         ///
114         XHTMLStream & operator<<(NextRaw const &);
115 private:
116         ///
117         void clearTagDeque();
118         ///
119         bool isTagOpen(std::string const &);
120         ///
121         void writeError(std::string const &);
122         ///
123         odocstream & os_;
124         ///
125         // int tab_;
126         ///
127         typedef std::deque<StartTag> TagDeque;
128         ///
129         typedef std::vector<StartTag> TagStack;
130         /// holds start tags until we know there is content in them.
131         TagDeque pending_tags_;
132         /// remembers the history, so we can make sure we nest properly.
133         TagStack tag_stack_;
134         /// 
135         bool nextraw_;
136 };
137
138 ///
139 void xhtmlParagraphs(Text const & text,
140                        Buffer const & buf,
141                        XHTMLStream & xs,
142                        OutputParams const & runparams);
143
144 namespace html {
145 ///
146 docstring escapeChar(char_type c);
147 /// converts a string to a form safe for links, etc
148 docstring htmlize(docstring const & str);
149 /// cleans \param str for use as an atttribute by replacing
150 /// all non-alnum by "_"
151 docstring cleanAttr(docstring const & str);
152 ///
153 std::string escapeChar(char c);
154 /// 
155 std::string htmlize(std::string const & str);
156 /// 
157 std::string cleanAttr(std::string const & str);
158
159 // to be removed
160 /// \return true if tag was opened, false if not 
161 bool openTag(odocstream & os, std::string const & tag, 
162                                                  std::string const & attr);
163 /// \return true if tag was opened, false if not 
164 bool closeTag(odocstream & os, std::string const & tag);
165 }
166 } // namespace lyx
167
168 #endif