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