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