1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef CONFLUENCE_OUTPUT
6 #define CONFLUENCE_OUTPUT
7 
8 #include <IceUtil/OutputUtil.h>
9 #include <list>
10 
11 namespace Confluence
12 {
13 
14 // ----------------------------------------------------------------------
15 // ConfluenceOutput
16 // ----------------------------------------------------------------------
17 
18 class ConfluenceOutput : public IceUtilInternal::OutputBase
19 {
20 public:
21 
22     ConfluenceOutput();
23     ConfluenceOutput(std::ostream&);
24     ConfluenceOutput(const char*);
25 
~ConfluenceOutput()26     virtual ~ConfluenceOutput(){};
27 
28     virtual void print(const std::string&); // Print a string.
29 
30     virtual void newline(); // Print newline.
31 
32     void startElement(const std::string&); // Start an element.
33     void endElement(); // End an element.
34     void attr(const std::string&, const std::string&); // Add an attribute to an element.
35 
36     std::string convertCommentHTML(std::string comment);
37     std::string escapeComment(std::string comment);
38 
39     std::string getAnchorMarkup(const std::string&, const std::string& = "");
40     std::string getLinkMarkup(const std::string&, const std::string& = "", const std::string& = "", const std::string& = "");
41     std::string getImageMarkup(const std::string&, const std::string& = "");
42     std::string getNavMarkup(const std::string&, const std::string&);
43 
44     void startEscapes();
45     void endEscapes();
46 
47     std::string currentElement() const;
48 
49     /**
50      * Wrap sections in these markers to prevent them from being confluence-escaped.
51      * The regular confluence-escaping process will remove these markers.
52      */
53     const static std::string TEMP_ESCAPER_START; // wrap sections
54     const static std::string TEMP_ESCAPER_END; // wrap sections
55 
56     /**
57      * Gets the start and end positions of all TEMP_ESCAPED sections of the given string.
58      */
59     std::list<std::pair<unsigned int,unsigned int> > getMarkerLimits(const std::string&);
60 
61     std::string removeMarkers(std::string);
62 
63 private:
64 
65     std::string escape(const ::std::string&) const;
66 
67     std::stack<std::string> _elementStack;
68 
69     bool _se;
70     bool _text;
71 
72     bool _escape;
73 
74     std::string _listMarkers;
75     std::string _commentListMarkers;
76 };
77 
78 template<typename T>
79 inline ConfluenceOutput&
80 operator<<(ConfluenceOutput& out, const T& val)
81 {
82     std::ostringstream s;
83     s << val;
84     out.print(s.str().c_str());
85     return out;
86 }
87 
88 template<>
89 inline ConfluenceOutput&
90 operator<<(ConfluenceOutput& o, const IceUtilInternal::NextLine&)
91 {
92     o.newline();
93     return o;
94 }
95 
96 template<>
97 inline ConfluenceOutput&
98 operator<<(ConfluenceOutput& o, const IceUtilInternal::Separator&)
99 {
100     o.separator();
101     return o;
102 }
103 
104 class EndElement
105 {
106 };
107 extern EndElement ee;
108 
109 template<>
110 inline ConfluenceOutput&
111 operator<<(ConfluenceOutput& o, const EndElement&)
112 {
113     o.endElement();
114     return o;
115 }
116 
117 class StartElement
118 {
119 public:
120 
121     StartElement(const std::string&);
122 
123     const std::string& getName() const;
124 
125 private:
126 
127     const std::string _name;
128 };
129 
130 typedef StartElement se;
131 
132 template<>
133 inline ConfluenceOutput&
134 operator<<(ConfluenceOutput& o, const StartElement& e)
135 {
136     o.startElement(e.getName());
137     return o;
138 }
139 
140 class Attribute
141 {
142 public:
143 
144     Attribute(const ::std::string&, const ::std::string&);
145 
146     const ::std::string& getName() const;
147     const ::std::string& getValue() const;
148 
149 private:
150 
151     const ::std::string _name;
152     const ::std::string _value;
153 };
154 
155 typedef Attribute attr;
156 
157 template<>
158 inline ConfluenceOutput&
159 operator<<(ConfluenceOutput& o, const Attribute& e)
160 {
161     o.attr(e.getName(), e.getValue());
162     return o;
163 }
164 
165 class StartEscapes
166 {
167 };
168 extern StartEscapes startEscapes;
169 
170 class EndEscapes
171 {
172 };
173 extern EndEscapes endEscapes;
174 
175 template<>
176 inline ConfluenceOutput&
177 operator<<(ConfluenceOutput& o, const StartEscapes&)
178 {
179     o.startEscapes();
180     return o;
181 }
182 
183 template<>
184 inline ConfluenceOutput&
185 operator<<(ConfluenceOutput& o, const EndEscapes&)
186 {
187     o.endEscapes();
188     return o;
189 }
190 
191 ConfluenceOutput& operator<<(ConfluenceOutput&, std::ios_base& (*)(std::ios_base&));
192 
193 }
194 
195 #endif
196