]> git.lyx.org Git - lyx.git/blob - src/output_xhtml.h
1b68261be34f34db94f61c56a715dd895d15bee2
[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
17 #include <deque>
18 #include <vector>
19
20 namespace lyx {
21
22 class Buffer;
23 class OutputParams;
24 class Text;
25
26 // Inspiration for the *Tag structs and for XHTMLStream
27 // came from MathStream and its cousins.
28
29 struct StartTag {
30         ///
31         StartTag(std::string const & tag) : tag_(tag) {}
32         ///
33         StartTag(std::string const & tag, std::string const & attr, 
34                 bool keepempty = false) 
35                 : tag_(tag), attr_(attr), keepempty_(keepempty) {}
36         /// <tag_ attr_>
37         docstring asTag() const;
38         /// </tag_>
39         docstring asEndTag() const;
40         ///
41         std::string tag_;
42         ///
43         std::string attr_;
44         /// whether to keep things like "<tag></tag>" or discard them
45         /// you would want this for td, e.g, but maybe not for a div
46         bool keepempty_;
47 };
48
49
50 struct EndTag {
51         ///
52         EndTag(std::string tag) : tag_(tag) {}
53         /// </tag_>
54         docstring asEndTag() const;
55         ///
56         std::string tag_;
57 };
58
59
60 // Tags like <img />
61 struct CompTag {
62         ///
63         CompTag(std::string const & tag)
64                 : tag_(tag) {}
65         ///
66         CompTag(std::string const & tag, std::string const & attr)
67                 : tag_(tag), attr_(attr) {}
68         /// <tag_ attr_ />
69         docstring asTag() const;
70         ///
71         std::string tag_;
72         ///
73         std::string attr_;
74 };
75
76
77 class XHTMLStream {
78 public:
79         ///
80         explicit XHTMLStream(odocstream & os);
81         ///
82         void cr();
83         ///
84         odocstream & os() { return os_; }
85         ///
86         // int & tab() { return tab_; }
87         /// closes any font tags that are eligible to be closed,
88         /// i.e., last on the tag_stack_.
89         /// \return false if there are open font tags we could not close.
90         /// because they are "blocked" by open non-font tags on the stack.
91         bool closeFontTags();
92         ///
93         XHTMLStream & operator<<(docstring const &);
94         ///
95         XHTMLStream & operator<<(const char *);
96         ///
97         XHTMLStream & operator<<(char_type);
98         ///
99         XHTMLStream & operator<<(StartTag const &);
100         ///
101         XHTMLStream & operator<<(EndTag const &);
102         ///
103         XHTMLStream & operator<<(CompTag const &);
104         /// A trivial struct that functions as a stream modifier.
105         /// << NextRaw() causes the next string-like thing sent to the
106         /// stream not to be escaped.
107         struct NextRaw {};
108         ///
109         XHTMLStream & operator<<(NextRaw const &);
110 private:
111         ///
112         void clearTagDeque();
113         ///
114         bool isTagOpen(std::string const &);
115         ///
116         void writeError(std::string const &);
117         ///
118         odocstream & os_;
119         ///
120         // int tab_;
121         ///
122         typedef std::deque<StartTag> TagDeque;
123         ///
124         typedef std::vector<StartTag> TagStack;
125         /// holds start tags until we know there is content in them.
126         TagDeque pending_tags_;
127         /// remembers the history, so we can make sure we nest properly.
128         TagStack tag_stack_;
129         /// 
130         bool nextraw_;
131 };
132
133 ///
134 void xhtmlParagraphs(Text const & text,
135                        Buffer const & buf,
136                        XHTMLStream & xs,
137                        OutputParams const & runparams);
138
139 namespace html {
140 ///
141 docstring escapeChar(char_type c);
142 /// converts a string to a form safe for links, etc
143 docstring htmlize(docstring const & str);
144
145 // to be removed
146 /// \return true if tag was opened, false if not 
147 bool openTag(odocstream & os, std::string const & tag, 
148                                                  std::string const & attr);
149 /// \return true if tag was opened, false if not 
150 bool closeTag(odocstream & os, std::string const & tag);
151 }
152 } // namespace lyx
153
154 #endif