]> git.lyx.org Git - lyx.git/blob - src/sgml.cpp
simplification
[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 "Layout.h"
20 #include "OutputParams.h"
21 #include "Paragraph.h"
22 #include "Text.h"
23 #include "TextClass.h"
24
25 #include "support/convert.h"
26 #include "support/docstream.h"
27 #include "support/lstrings.h"
28 #include "support/textutils.h"
29
30 #include <map>
31 #include <ostream>
32
33
34 namespace lyx {
35
36 using support::subst;
37
38 using std::map;
39 using std::ostream;
40 using std::ostringstream;
41 using std::string;
42
43 docstring sgml::escapeChar(char_type c)
44 {
45         docstring str;
46         switch (c) {
47         case ' ':
48                 str += " ";
49                 break;
50         case '&':
51                 str += "&amp;";
52                 break;
53         case '<':
54                 str += "&lt;";
55                 break;
56         case '>':
57                 str += "&gt;";
58                 break;
59 #if 0
60         case '$':
61                 str += "&dollar;";
62                 break;
63         case '#':
64                 str += "&num;";
65                 break;
66         case '%':
67                 str += "&percnt;";
68                 break;
69         case '[':
70                 str += "&lsqb;";
71                 break;
72         case ']':
73                 str += "&rsqb;";
74                 break;
75         case '{':
76                 str += "&lcub;";
77                 break;
78         case '}':
79                 str += "&rcub;";
80                 break;
81         case '~':
82                 str += "&tilde;";
83                 break;
84         case '"':
85                 str += "&quot;";
86                 break;
87         case '\\':
88                 str += "&bsol;";
89                 break;
90 #endif
91         default:
92                 str += c;
93                 break;
94         }
95         return str;
96 }
97
98
99 docstring sgml::escapeString(docstring const & raw)
100 {
101         docstring bin;
102         bin.reserve(raw.size() * 2); // crude approximation is sufficient
103         for (size_t i = 0; i != raw.size(); ++i)
104                 bin += sgml::escapeChar(raw[i]);
105
106         return bin;
107 }
108
109
110 docstring const sgml::uniqueID(docstring const label)
111 {
112         static unsigned int seed = 1000;
113         return label + convert<docstring>(++seed);
114 }
115
116
117 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
118         docstring const & orig)
119 {
120         // The standard DocBook SGML declaration only allows letters,
121         // digits, '-' and '.' in a name.
122         // Since users might change that declaration one has to cater
123         // for additional allowed characters.
124         // This routine replaces illegal characters by '-' or '.'
125         // and adds a number for uniqueness.
126         // If you know what you are doing, you can set allowed==""
127         // to disable this mangling.
128         TextClass const & tclass = buf.params().getTextClass();
129         docstring const allowed = from_ascii(
130                 runparams.flavor == OutputParams::XML? ".-_:":tclass.options());
131
132         if (allowed.empty())
133                 return orig;
134
135         docstring::const_iterator it  = orig.begin();
136         docstring::const_iterator end = orig.end();
137
138         docstring content;
139
140         typedef map<docstring, docstring> MangledMap;
141         static MangledMap mangledNames;
142         static int mangleID = 1;
143
144         MangledMap::const_iterator const known = mangledNames.find(orig);
145         if (known != mangledNames.end())
146                 return (*known).second;
147
148         // make sure it starts with a letter
149         if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
150                 content += "x";
151
152         bool mangle = false;
153         for (; it != end; ++it) {
154                 char_type c = *it;
155                 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
156                       || allowed.find(c) < allowed.size())
157                         content += c;
158                 else if (c == '_' || c == ' ') {
159                         mangle = true;
160                         content += "-";
161                 }
162                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
163                         mangle = true;
164                         content += ".";
165                 }
166                 else {
167                         mangle = true;
168                 }
169         }
170
171         if (mangle)
172                 content += "-" + convert<docstring>(mangleID++);
173         else if (isDigitASCII(content[content.size() - 1]))
174                 content += ".";
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