]> git.lyx.org Git - lyx.git/blob - src/sgml.C
More unicode fixes for docbook.
[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(string const label)
109 {
110         static unsigned int seed = 1000;
111         return from_ascii(label + convert<string>(++seed));
112 }
113
114
115 string sgml::cleanID(Buffer const & buf, OutputParams const & runparams, std::string const & orig)
116 {
117         // The standard DocBook SGML declaration only allows letters,
118         // digits, '-' and '.' in a name.
119         // Since users might change that declaration one has to cater
120         // for additional allowed characters.
121         // This routine replaces illegal characters by '-' or '.'
122         // and adds a number for uniqueness.
123         // If you know what you are doing, you can set allowed==""
124         // to disable this mangling.
125         LyXTextClass const & tclass = buf.params().getLyXTextClass();
126         string const allowed = runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
127
128         if (allowed.empty())
129                 return orig;
130
131         string::const_iterator it  = orig.begin();
132         string::const_iterator end = orig.end();
133
134         string content;
135
136         typedef map<string, string> MangledMap;
137         static MangledMap mangledNames;
138         static int mangleID = 1;
139
140         MangledMap::const_iterator const known = mangledNames.find(orig);
141         if (known != mangledNames.end())
142                 return (*known).second;
143
144         // make sure it starts with a letter
145         if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
146                 content += "x";
147
148         bool mangle = false;
149         for (; it != end; ++it) {
150                 char c = *it;
151                 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
152                         content += c;
153                 else if (c == '_' || c == ' ') {
154                         mangle = true;
155                         content += "-";
156                 }
157                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
158                         mangle = true;
159                         content += ".";
160                 }
161                 else {
162                         mangle = true;
163                 }
164         }
165         if (mangle) {
166                 content += "-" + convert<string>(mangleID++);
167         }
168         else if (isdigit(content[content.size() - 1])) {
169                 content += ".";
170         }
171
172         mangledNames[orig] = content;
173
174         return content;
175 }
176
177
178 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
179 {
180         // FIXME UNICODE
181         // This should be fixed in layout files later.
182         string param = subst(attribute, "<", "\"");
183         param = subst(param, ">", "\"");
184
185         if (!name.empty() && name != "!-- --") {
186                 os << '<' << from_ascii(name);
187                 if (!param.empty())
188                     os << ' ' << from_ascii(param);
189                 os << '>';
190         }
191 }
192
193
194 void sgml::closeTag(odocstream & os, string const & name)
195 {
196         // FIXME UNICODE
197         if (!name.empty() && name != "!-- --")
198                 os << "</" << from_ascii(name) << '>';
199 }
200
201
202 void sgml::openTag(Buffer const & buf, odocstream & os, OutputParams const & runparams, Paragraph const & par)
203 {
204         LyXLayout_ptr const & style = par.layout();
205         string const & name = style->latexname();
206         string param = style->latexparam();
207         Counters & counters = buf.params().getLyXTextClass().counters();
208
209         string id = par.getID(buf, runparams);
210
211         string attribute;
212         if(!id.empty()) {
213                 if (param.find('#') != string::npos) {
214                         string::size_type pos = param.find("id=<");
215                         string::size_type end = param.find(">");
216                         if( pos != string::npos && end != string::npos)
217                                 param.erase(pos, end-pos + 1);
218                 }
219                 attribute = id + ' ' + param;
220         } else {
221                 if (param.find('#') != string::npos) {
222                         // FIXME UNICODE
223                         if(!style->counter.empty())
224                                 counters.step(style->counter);
225                         else
226                                 counters.step(from_ascii(name));
227                         int i = counters.value(from_ascii(name));
228                         attribute = subst(param, "#", convert<string>(i));
229                 } else {
230                         attribute = param;
231                 }
232         }
233         openTag(os, name, attribute);
234 }
235
236
237 void sgml::closeTag(odocstream & os, Paragraph const & par)
238 {
239         LyXLayout_ptr const & style = par.layout();
240         closeTag(os, style->latexname());
241 }
242
243
244 } // namespace lyx