]> git.lyx.org Git - lyx.git/blob - src/sgml.C
compile fix for sgml.C
[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 "outputparams.h"
21 #include "paragraph.h"
22
23 #include "support/docstring.h"
24 #include "support/lstrings.h"
25 #include "support/std_ostream.h"
26 #include "support/convert.h"
27
28 #include <map>
29 #include <sstream>
30
31
32 namespace lyx {
33
34 using support::subst;
35
36 using std::map;
37 using std::ostream;
38 using std::ostringstream;
39 using std::string;
40
41 docstring sgml::escapeChar(char_type c)
42 {
43         docstring str;
44         switch (c) {
45         case ' ':
46                 str += " ";
47                 break;
48         case '&':
49                 str += "&amp;";
50                 break;
51         case '<':
52                 str += "&lt;";
53                 break;
54         case '>':
55                 str += "&gt;";
56                 break;
57 #if 0
58         case '$':
59                 str += "&dollar;";
60                 break;
61         case '#':
62                 str += "&num;";
63                 break;
64         case '%':
65                 str += "&percnt;";
66                 break;
67         case '[':
68                 str += "&lsqb;";
69                 break;
70         case ']':
71                 str += "&rsqb;";
72                 break;
73         case '{':
74                 str += "&lcub;";
75                 break;
76         case '}':
77                 str += "&rcub;";
78                 break;
79         case '~':
80                 str += "&tilde;";
81                 break;
82         case '"':
83                 str += "&quot;";
84                 break;
85         case '\\':
86                 str += "&bsol;";
87                 break;
88 #endif
89         default:
90                 str += c;
91                 break;
92         }
93         return str;
94 }
95
96
97 docstring sgml::escapeString(docstring const & raw)
98 {
99         odocstringstream bin;
100
101         for(docstring::size_type i = 0; i < raw.size(); ++i) {
102                 bin  << sgml::escapeChar(raw[i]);
103         }
104         return bin.str();
105 }
106
107
108 docstring const sgml::uniqueID(docstring const label)
109 {
110         static unsigned int seed = 1000;
111         return label + convert<docstring>(++seed);
112 }
113
114
115 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
116         docstring const & orig)
117 {
118         // The standard DocBook SGML declaration only allows letters,
119         // digits, '-' and '.' in a name.
120         // Since users might change that declaration one has to cater
121         // for additional allowed characters.
122         // This routine replaces illegal characters by '-' or '.'
123         // and adds a number for uniqueness.
124         // If you know what you are doing, you can set allowed==""
125         // to disable this mangling.
126         LyXTextClass const & tclass = buf.params().getLyXTextClass();
127         string const allowed =
128                 runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
129
130         if (allowed.empty())
131                 return orig;
132
133         docstring::const_iterator it  = orig.begin();
134         docstring::const_iterator end = orig.end();
135
136         docstring content;
137
138         typedef map<docstring, docstring> MangledMap;
139         static MangledMap mangledNames;
140         static int mangleID = 1;
141
142         MangledMap::const_iterator const known = mangledNames.find(orig);
143         if (known != mangledNames.end())
144                 return (*known).second;
145
146         // make sure it starts with a letter
147         if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
148                 content += "x";
149
150         bool mangle = false;
151         for (; it != end; ++it) {
152                 char c = *it;
153                 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
154                         content += c;
155                 else if (c == '_' || c == ' ') {
156                         mangle = true;
157                         content += "-";
158                 }
159                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
160                         mangle = true;
161                         content += ".";
162                 }
163                 else {
164                         mangle = true;
165                 }
166         }
167         if (mangle) {
168                 content += "-" + convert<docstring>(mangleID++);
169         }
170         else if (isdigit(content[content.size() - 1])) {
171                 content += ".";
172         }
173
174         mangledNames[orig] = content;
175
176         return content;
177 }
178
179
180 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
181 {
182         // FIXME UNICODE
183         // This should be fixed in layout files later.
184         string param = subst(attribute, "<", "\"");
185         param = subst(param, ">", "\"");
186
187         if (!name.empty() && name != "!-- --") {
188                 os << '<' << from_ascii(name);
189                 if (!param.empty())
190                     os << ' ' << from_ascii(param);
191                 os << '>';
192         }
193 }
194
195
196 void sgml::closeTag(odocstream & os, string const & name)
197 {
198         // FIXME UNICODE
199         if (!name.empty() && name != "!-- --")
200                 os << "</" << from_ascii(name) << '>';
201 }
202
203
204 void sgml::openTag(Buffer const & buf, odocstream & os, OutputParams const & runparams, Paragraph const & par)
205 {
206         LyXLayout_ptr const & style = par.layout();
207         string const & name = style->latexname();
208         string param = style->latexparam();
209         Counters & counters = buf.params().getLyXTextClass().counters();
210
211         string id = par.getID(buf, runparams);
212
213         string attribute;
214         if(!id.empty()) {
215                 if (param.find('#') != string::npos) {
216                         string::size_type pos = param.find("id=<");
217                         string::size_type end = param.find(">");
218                         if( pos != string::npos && end != string::npos)
219                                 param.erase(pos, end-pos + 1);
220                 }
221                 attribute = id + ' ' + param;
222         } else {
223                 if (param.find('#') != string::npos) {
224                         // FIXME UNICODE
225                         if(!style->counter.empty())
226                                 counters.step(style->counter);
227                         else
228                                 counters.step(from_ascii(name));
229                         int i = counters.value(from_ascii(name));
230                         attribute = subst(param, "#", convert<string>(i));
231                 } else {
232                         attribute = param;
233                 }
234         }
235         openTag(os, name, attribute);
236 }
237
238
239 void sgml::closeTag(odocstream & os, Paragraph const & par)
240 {
241         LyXLayout_ptr const & style = par.layout();
242         closeTag(os, style->latexname());
243 }
244
245
246 } // namespace lyx