]> git.lyx.org Git - lyx.git/blob - src/sgml.C
add comment
[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 == '.'
154                       || allowed.find(c) < allowed.size())
155                         content += c;
156                 else if (c == '_' || c == ' ') {
157                         mangle = true;
158                         content += "-";
159                 }
160                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
161                         mangle = true;
162                         content += ".";
163                 }
164                 else {
165                         mangle = true;
166                 }
167         }
168         if (mangle) {
169                 content += "-" + convert<docstring>(mangleID++);
170         }
171         else if (isdigit(content[content.size() - 1])) {
172                 content += ".";
173         }
174
175         mangledNames[orig] = content;
176
177         return content;
178 }
179
180
181 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
182 {
183         // FIXME UNICODE
184         // This should be fixed in layout files later.
185         string param = subst(attribute, "<", "\"");
186         param = subst(param, ">", "\"");
187
188         if (!name.empty() && name != "!-- --") {
189                 os << '<' << from_ascii(name);
190                 if (!param.empty())
191                         os << ' ' << from_ascii(param);
192                 os << '>';
193         }
194 }
195
196
197 void sgml::closeTag(odocstream & os, string const & name)
198 {
199         if (!name.empty() && name != "!-- --")
200                 os << "</" << from_ascii(name) << '>';
201 }
202
203
204 void sgml::openTag(Buffer const & buf, odocstream & os,
205         OutputParams const & runparams, Paragraph const & par)
206 {
207         LyXLayout_ptr const & style = par.layout();
208         string const & name = style->latexname();
209         string param = style->latexparam();
210         Counters & counters = buf.params().getLyXTextClass().counters();
211
212         string id = par.getID(buf, runparams);
213
214         string attribute;
215         if(!id.empty()) {
216                 if (param.find('#') != string::npos) {
217                         string::size_type pos = param.find("id=<");
218                         string::size_type end = param.find(">");
219                         if( pos != string::npos && end != string::npos)
220                                 param.erase(pos, end-pos + 1);
221                 }
222                 attribute = id + ' ' + param;
223         } else {
224                 if (param.find('#') != string::npos) {
225                         // FIXME UNICODE
226                         if(!style->counter.empty())
227                                 counters.step(style->counter);
228                         else
229                                 counters.step(from_ascii(name));
230                         int i = counters.value(from_ascii(name));
231                         attribute = subst(param, "#", convert<string>(i));
232                 } else {
233                         attribute = param;
234                 }
235         }
236         openTag(os, name, attribute);
237 }
238
239
240 void sgml::closeTag(odocstream & os, Paragraph const & par)
241 {
242         LyXLayout_ptr const & style = par.layout();
243         closeTag(os, style->latexname());
244 }
245
246
247 } // namespace lyx