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