]> git.lyx.org Git - lyx.git/blob - src/sgml.cpp
prepare Qt 5.6 builds
[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 <QThreadStorage>
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         // FIXME THREAD
109         // It seems unlikely there could be a problem here,
110         // but we could have concurrent access, in principle.
111         static unsigned int seed = 1000;
112         return label + convert<docstring>(++seed);
113 }
114
115
116 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
117         docstring const & orig)
118 {
119         // The standard DocBook SGML declaration only allows letters,
120         // digits, '-' and '.' in a name.
121         // Since users might change that declaration one has to cater
122         // for additional allowed characters.
123         // This routine replaces illegal characters by '-' or '.'
124         // and adds a number for uniqueness.
125         // If you know what you are doing, you can set allowed==""
126         // to disable this mangling.
127         DocumentClass const & tclass = buf.params().documentClass();
128         docstring const allowed = from_ascii(
129                 runparams.flavor == OutputParams::XML ? ".-_:" : tclass.options());
130
131         if (allowed.empty())
132                 return orig;
133
134         docstring::const_iterator it  = orig.begin();
135         docstring::const_iterator end = orig.end();
136
137         docstring content;
138
139         typedef map<docstring, docstring> MangledMap;
140         static QThreadStorage<MangledMap> tMangledNames;
141         static QThreadStorage<int> tMangleID;
142
143         MangledMap & mangledNames = tMangledNames.localData();
144
145         MangledMap::const_iterator const known = mangledNames.find(orig);
146         if (known != mangledNames.end())
147                 return known->second;
148
149         // make sure it starts with a letter
150         if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
151                 content += "x";
152
153         bool mangle = false;
154         for (; it != end; ++it) {
155                 char_type c = *it;
156                 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
157                       || allowed.find(c) < allowed.size())
158                         content += c;
159                 else if (c == '_' || c == ' ') {
160                         mangle = true;
161                         content += "-";
162                 }
163                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
164                         mangle = true;
165                         content += ".";
166                 }
167                 else {
168                         mangle = true;
169                 }
170         }
171
172         if (mangle) {
173                 int & mangleID = tMangleID.localData();
174                 content += "-" + convert<docstring>(mangleID++);
175         } else if (isDigitASCII(content[content.size() - 1]))
176                 content += ".";
177
178         mangledNames[orig] = content;
179
180         return content;
181 }
182
183
184 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
185 {
186         // FIXME UNICODE
187         // This should be fixed in layout files later.
188         string param = subst(attribute, "<", "\"");
189         param = subst(param, ">", "\"");
190
191         // Note: we ignore the name if it empty or if it is a comment "<!-- -->" or
192         // if the name is *dummy*.
193         // We ignore dummy because dummy is not a valid docbook element and it is
194         // the internal name given to single paragraphs in the latex output.
195         // This allow us to simplify the code a lot and is a reasonable compromise.
196         if (!name.empty() && name != "!-- --" && name != "dummy") {
197                 os << '<' << from_ascii(name);
198                 if (!param.empty())
199                         os << ' ' << from_ascii(param);
200                 os << '>';
201         }
202 }
203
204
205 void sgml::closeTag(odocstream & os, string const & name)
206 {
207         if (!name.empty() && name != "!-- --" && name != "dummy")
208                 os << "</" << from_ascii(name) << '>';
209 }
210
211
212 void sgml::openTag(Buffer const & buf, odocstream & os,
213         OutputParams const & runparams, Paragraph const & par)
214 {
215         Layout const & style = par.layout();
216         string const & name = style.latexname();
217         string param = style.latexparam();
218         Counters & counters = buf.params().documentClass().counters();
219
220         string id = par.getID(buf, runparams);
221
222         string attribute;
223         if (!id.empty()) {
224                 if (param.find('#') != string::npos) {
225                         string::size_type pos = param.find("id=<");
226                         string::size_type end = param.find(">");
227                         if( pos != string::npos && end != string::npos)
228                                 param.erase(pos, end-pos + 1);
229                 }
230                 attribute = id + ' ' + param;
231         } else {
232                 if (param.find('#') != string::npos) {
233                         // FIXME UNICODE
234                         if (!style.counter.empty())
235                                 // This uses InternalUpdate at the moment becuase sgml output
236                                 // does not do anything with tracked counters, and it would need
237                                 // to track layouts if it did want to use them.
238                                 counters.step(style.counter, InternalUpdate);
239                         else
240                                 counters.step(from_ascii(name), InternalUpdate);
241                         int i = counters.value(from_ascii(name));
242                         attribute = subst(param, "#", convert<string>(i));
243                 } else {
244                         attribute = param;
245                 }
246         }
247         openTag(os, name, attribute);
248 }
249
250
251 void sgml::closeTag(odocstream & os, Paragraph const & par)
252 {
253         Layout const & style = par.layout();
254         closeTag(os, style.latexname());
255 }
256
257
258 } // namespace lyx