]> git.lyx.org Git - lyx.git/blob - src/sgml.C
57f89219b487cf0342f1150c7fd7fbe209cd5bcd
[lyx.git] / src / sgml.C
1 /**
2  * \file sgml.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "sgml.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "counters.h"
19 #include "lyxtext.h"
20 #include "paragraph.h"
21
22 #include "support/lstrings.h"
23 #include "support/std_ostream.h"
24 #include "support/tostr.h"
25
26 #include <boost/tuple/tuple.hpp>
27
28 #include <sstream>
29
30 using lyx::support::subst;
31
32 using std::make_pair;
33
34 using std::ostream;
35 using std::ostringstream;
36 using std::pair;
37 using std::string;
38
39 namespace sgml {
40
41 pair<bool, string> escapeChar(char c)
42 {
43         string str;
44
45         switch (c) {
46         case ' ':
47                 return make_pair(true, string(" "));
48                 break;
49         case '\0': // Ignore :-)
50                 str.erase();
51                 break;
52         case '&':
53                 str = "&amp;";
54                 break;
55         case '<':
56                 str = "&lt;";
57                 break;
58         case '>':
59                 str = "&gt;";
60                 break;
61 #if 0
62         case '$':
63                 str = "&dollar;";
64                 break;
65         case '#':
66                 str = "&num;";
67                 break;
68         case '%':
69                 str = "&percnt;";
70                 break;
71         case '[':
72                 str = "&lsqb;";
73                 break;
74         case ']':
75                 str = "&rsqb;";
76                 break;
77         case '{':
78                 str = "&lcub;";
79                 break;
80         case '}':
81                 str = "&rcub;";
82                 break;
83         case '~':
84                 str = "&tilde;";
85                 break;
86         case '"':
87                 str = "&quot;";
88                 break;
89         case '\\':
90                 str = "&bsol;";
91                 break;
92 #endif
93         default:
94                 str = c;
95                 break;
96         }
97         return make_pair(false, str);
98 }
99
100
101 string escapeString(string const & raw)
102 {
103         ostringstream bin;
104
105         for(string::size_type i = 0; i < raw.size(); ++i) {
106                 bool ws;
107                 string str;
108                 boost::tie(ws, str) = sgml::escapeChar(raw[i]);
109                 bin << str;
110         }
111         return bin.str();
112 }
113
114
115 void openTag(ostream & os, string const & name, string const & attribute)
116 {
117         if (!name.empty() && name != "!-- --") {
118                 os << '<' << name;
119                 if (!attribute.empty())
120                         os << " " << attribute;
121                 os << '>';
122         }
123 }
124
125
126 void closeTag(ostream & os, string const & name)
127 {
128         if (!name.empty() && name != "!-- --")
129                 os << "</" << name << '>';
130 }
131
132
133 void openTag(Buffer const & buf, ostream & os, Paragraph const & par)
134 {
135         LyXLayout_ptr const & style = par.layout();
136         string const & name = style->latexname();
137         string param = style->latexparam();
138         Counters & counters = buf.params().getLyXTextClass().counters();
139
140         string id = par.getDocbookId();
141         id = id.empty()? "" : " id = \"" + id + "\"";
142
143         string attribute;
144         if(!id.empty()) {
145                 if (param.find('#') != string::npos) {
146                         string::size_type pos = param.find("id=<");
147                         string::size_type end = param.find(">");
148                         if( pos != string::npos and end != string::npos)
149                                 param.erase(pos, end-pos + 1);
150                 }
151                 attribute = id + ' ' + param;
152         } else {
153                 if (param.find('#') != string::npos) {
154                         if(!style->counter.empty())
155                                 counters.step(style->counter);
156                         else
157                                 counters.step(style->latexname());
158                         int i = counters.value(name);
159                         attribute = subst(param, "#", tostr(i));
160                         attribute = subst(attribute, "<", "\"");
161                         attribute = subst(attribute, ">", "\"");
162                 } else {
163                         attribute = param;
164                 }
165         }
166         openTag(os, name, attribute);
167 }
168
169
170 void closeTag(ostream & os, Paragraph const & par)
171 {
172         LyXLayout_ptr const & style = par.layout();
173         closeTag(os, style->latexname());
174 }
175
176 } // namespace sgml