]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.h
* sk.po
[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 } // namespace html
87
88 class XHTMLStream {
89 public:
90         ///
91         explicit XHTMLStream(odocstream & os);
92         ///
93         void cr();
94         ///
95         odocstream & os() { return os_; }
96         ///
97         // int & tab() { return tab_; }
98         /// closes any font tags that are eligible to be closed,
99         /// i.e., last on the tag_stack_.
100         /// \return false if there are open font tags we could not close.
101         /// because they are "blocked" by open non-font tags on the stack.
102         bool closeFontTags();
103         ///
104         XHTMLStream & operator<<(docstring const &);
105         ///
106         XHTMLStream & operator<<(const char *);
107         ///
108         XHTMLStream & operator<<(char_type);
109         ///
110         XHTMLStream & operator<<(int);
111         ///
112         XHTMLStream & operator<<(char);
113         ///
114         XHTMLStream & operator<<(html::StartTag const &);
115         ///
116         XHTMLStream & operator<<(html::EndTag const &);
117         ///
118         XHTMLStream & operator<<(html::CompTag const &);
119         ///
120         enum EscapeSettings {
121                 ESCAPE_NONE,
122                 ESCAPE_AND, // meaning &
123                 ESCAPE_ALL  // meaning <, >, &, at present
124         };
125         /// Sets what we are going to escape on the NEXT write.
126         /// Everything is reset for the next time.
127         XHTMLStream & operator<<(EscapeSettings);
128 private:
129         ///
130         void clearTagDeque();
131         ///
132         bool isTagOpen(std::string const &);
133         ///
134         void writeError(std::string const &);
135         ///
136         odocstream & os_;
137         ///
138         typedef std::deque<html::StartTag> TagStack;
139         /// holds start tags until we know there is content in them.
140         TagStack pending_tags_;
141         /// remembers the history, so we can make sure we nest properly.
142         TagStack tag_stack_;
143         /// 
144         EscapeSettings escape_;
145 };
146
147 ///
148 void xhtmlParagraphs(Text const & text,
149                        Buffer const & buf,
150                        XHTMLStream & xs,
151                        OutputParams const & runparams);
152
153 /// \return a string appropriate for setting alignment in CSS
154 /// Does NOT return "justify" for "block"
155 std::string alignmentToCSS(LyXAlignment align);
156
157 namespace html {
158 ///
159 docstring escapeChar(char_type c, XHTMLStream::EscapeSettings e);
160 /// converts a string to a form safe for links, etc
161 docstring htmlize(docstring const & str, XHTMLStream::EscapeSettings e);
162 /// cleans \param str for use as an atttribute by replacing
163 /// all non-alnum by "_"
164 docstring cleanAttr(docstring const & str);
165 ///
166 std::string escapeChar(char c, XHTMLStream::EscapeSettings e);
167 /// 
168 std::string htmlize(std::string const & str, XHTMLStream::EscapeSettings e);
169 /// 
170 std::string cleanAttr(std::string const & str);
171
172 } // namespace html
173 } // namespace lyx
174
175 #endif