]> git.lyx.org Git - lyx.git/blob - src/sgml.cpp
remove unused code
[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 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38
39 docstring sgml::escapeChar(char_type c)
40 {
41         docstring str;
42         switch (c) {
43         case ' ':
44                 str += " ";
45                 break;
46         case '&':
47                 str += "&amp;";
48                 break;
49         case '<':
50                 str += "&lt;";
51                 break;
52         case '>':
53                 str += "&gt;";
54                 break;
55 #if 0
56         case '$':
57                 str += "&dollar;";
58                 break;
59         case '#':
60                 str += "&num;";
61                 break;
62         case '%':
63                 str += "&percnt;";
64                 break;
65         case '[':
66                 str += "&lsqb;";
67                 break;
68         case ']':
69                 str += "&rsqb;";
70                 break;
71         case '{':
72                 str += "&lcub;";
73                 break;
74         case '}':
75                 str += "&rcub;";
76                 break;
77         case '~':
78                 str += "&tilde;";
79                 break;
80         case '"':
81                 str += "&quot;";
82                 break;
83         case '\\':
84                 str += "&bsol;";
85                 break;
86 #endif
87         default:
88                 str += c;
89                 break;
90         }
91         return str;
92 }
93
94
95 docstring sgml::escapeString(docstring const & raw)
96 {
97         docstring bin;
98         bin.reserve(raw.size() * 2); // crude approximation is sufficient
99         for (size_t i = 0; i != raw.size(); ++i)
100                 bin += sgml::escapeChar(raw[i]);
101
102         return bin;
103 }
104
105
106 docstring const sgml::uniqueID(docstring const label)
107 {
108         static unsigned int seed = 1000;
109         return label + convert<docstring>(++seed);
110 }
111
112
113 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
114         docstring const & orig)
115 {
116         // The standard DocBook SGML declaration only allows letters,
117         // digits, '-' and '.' in a name.
118         // Since users might change that declaration one has to cater
119         // for additional allowed characters.
120         // This routine replaces illegal characters by '-' or '.'
121         // and adds a number for uniqueness.
122         // If you know what you are doing, you can set allowed==""
123         // to disable this mangling.
124         DocumentClass const & tclass = buf.params().documentClass();
125         docstring const allowed = from_ascii(
126                 runparams.flavor == OutputParams::XML ? ".-_:" : tclass.options());
127
128         if (allowed.empty())
129                 return orig;
130
131         docstring::const_iterator it  = orig.begin();
132         docstring::const_iterator end = orig.end();
133
134         docstring content;
135
136         typedef map<docstring, docstring> 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 (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
146                 content += "x";
147
148         bool mangle = false;
149         for (; it != end; ++it) {
150                 char_type c = *it;
151                 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
152                       || allowed.find(c) < allowed.size())
153                         content += c;
154                 else if (c == '_' || c == ' ') {
155                         mangle = true;
156                         content += "-";
157                 }
158                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
159                         mangle = true;
160                         content += ".";
161                 }
162                 else {
163                         mangle = true;
164                 }
165         }
166
167         if (mangle)
168                 content += "-" + convert<docstring>(mangleID++);
169         else if (isDigitASCII(content[content.size() - 1]))
170                 content += ".";
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         if (!name.empty() && name != "!-- --")
197                 os << "</" << from_ascii(name) << '>';
198 }
199
200
201 void sgml::openTag(Buffer const & buf, odocstream & os,
202         OutputParams const & runparams, Paragraph const & par)
203 {
204         Layout const & style = par.layout();
205         string const & name = style.latexname();
206         string param = style.latexparam();
207         Counters & counters = buf.params().documentClass().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         Layout const & style = par.layout();
240         closeTag(os, style.latexname());
241 }
242
243
244 } // namespace lyx