]> git.lyx.org Git - lyx.git/blob - src/sgml.cpp
use FileName::isDirectory()
[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 "Text.h"
20 #include "Layout.h"
21 #include "OutputParams.h"
22 #include "Paragraph.h"
23
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
26 #include "support/std_ostream.h"
27 #include "support/convert.h"
28 #include "support/textutils.h"
29
30 #include <map>
31 #include <sstream>
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         odocstringstream bin;
102
103         for(docstring::size_type i = 0; i < raw.size(); ++i) {
104                 bin  << sgml::escapeChar(raw[i]);
105         }
106         return bin.str();
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         if (mangle) {
171                 content += "-" + convert<docstring>(mangleID++);
172         }
173         else if (isDigitASCII(content[content.size() - 1])) {
174                 content += ".";
175         }
176
177         mangledNames[orig] = content;
178
179         return content;
180 }
181
182
183 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
184 {
185         // FIXME UNICODE
186         // This should be fixed in layout files later.
187         string param = subst(attribute, "<", "\"");
188         param = subst(param, ">", "\"");
189
190         if (!name.empty() && name != "!-- --") {
191                 os << '<' << from_ascii(name);
192                 if (!param.empty())
193                         os << ' ' << from_ascii(param);
194                 os << '>';
195         }
196 }
197
198
199 void sgml::closeTag(odocstream & os, string const & name)
200 {
201         if (!name.empty() && name != "!-- --")
202                 os << "</" << from_ascii(name) << '>';
203 }
204
205
206 void sgml::openTag(Buffer const & buf, odocstream & os,
207         OutputParams const & runparams, Paragraph const & par)
208 {
209         LayoutPtr const & style = par.layout();
210         string const & name = style->latexname();
211         string param = style->latexparam();
212         Counters & counters = buf.params().getTextClass().counters();
213
214         string id = par.getID(buf, runparams);
215
216         string attribute;
217         if(!id.empty()) {
218                 if (param.find('#') != string::npos) {
219                         string::size_type pos = param.find("id=<");
220                         string::size_type end = param.find(">");
221                         if( pos != string::npos && end != string::npos)
222                                 param.erase(pos, end-pos + 1);
223                 }
224                 attribute = id + ' ' + param;
225         } else {
226                 if (param.find('#') != string::npos) {
227                         // FIXME UNICODE
228                         if(!style->counter.empty())
229                                 counters.step(style->counter);
230                         else
231                                 counters.step(from_ascii(name));
232                         int i = counters.value(from_ascii(name));
233                         attribute = subst(param, "#", convert<string>(i));
234                 } else {
235                         attribute = param;
236                 }
237         }
238         openTag(os, name, attribute);
239 }
240
241
242 void sgml::closeTag(odocstream & os, Paragraph const & par)
243 {
244         LayoutPtr const & style = par.layout();
245         closeTag(os, style->latexname());
246 }
247
248
249 } // namespace lyx