]> git.lyx.org Git - lyx.git/blob - src/sgml.cpp
Provide proper fallback if a bibliography processor is not found
[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 <atomic>
31 #include <map>
32 #include <QThreadStorage>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39
40 docstring sgml::escapeChar(char_type c)
41 {
42         docstring str;
43         switch (c) {
44         case ' ':
45                 str += " ";
46                 break;
47         case '&':
48                 str += "&amp;";
49                 break;
50         case '<':
51                 str += "&lt;";
52                 break;
53         case '>':
54                 str += "&gt;";
55                 break;
56 #if 0
57         case '$':
58                 str += "&dollar;";
59                 break;
60         case '#':
61                 str += "&num;";
62                 break;
63         case '%':
64                 str += "&percnt;";
65                 break;
66         case '[':
67                 str += "&lsqb;";
68                 break;
69         case ']':
70                 str += "&rsqb;";
71                 break;
72         case '{':
73                 str += "&lcub;";
74                 break;
75         case '}':
76                 str += "&rcub;";
77                 break;
78         case '~':
79                 str += "&tilde;";
80                 break;
81         case '"':
82                 str += "&quot;";
83                 break;
84         case '\\':
85                 str += "&bsol;";
86                 break;
87 #endif
88         default:
89                 str += c;
90                 break;
91         }
92         return str;
93 }
94
95
96 docstring sgml::escapeString(docstring const & raw)
97 {
98         docstring bin;
99         bin.reserve(raw.size() * 2); // crude approximation is sufficient
100         for (size_t i = 0; i != raw.size(); ++i)
101                 bin += sgml::escapeChar(raw[i]);
102
103         return bin;
104 }
105
106
107 docstring const sgml::uniqueID(docstring const & label)
108 {
109         // thread-safe
110         static atomic_uint 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         DocumentClass const & tclass = buf.params().documentClass();
127         docstring const allowed = from_ascii(
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 QThreadStorage<MangledMap> tMangledNames;
140         static QThreadStorage<int> tMangleID;
141
142         MangledMap & mangledNames = tMangledNames.localData();
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
171         if (mangle) {
172                 int & mangleID = tMangleID.localData();
173                 content += "-" + convert<docstring>(mangleID++);
174         } else if (isDigitASCII(content[content.size() - 1]))
175                 content += ".";
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         // Note: we ignore the name if it empty or if it is a comment "<!-- -->" or
191         // if the name is *dummy*.
192         // We ignore dummy because dummy is not a valid docbook element and it is
193         // the internal name given to single paragraphs in the latex output.
194         // This allow us to simplify the code a lot and is a reasonable compromise.
195         if (!name.empty() && name != "!-- --" && name != "dummy") {
196                 os << '<' << from_ascii(name);
197                 if (!param.empty())
198                         os << ' ' << from_ascii(param);
199                 os << '>';
200         }
201 }
202
203
204 void sgml::closeTag(odocstream & os, string const & name)
205 {
206         if (!name.empty() && name != "!-- --" && name != "dummy")
207                 os << "</" << from_ascii(name) << '>';
208 }
209
210
211 void sgml::openTag(Buffer const & buf, odocstream & os,
212         OutputParams const & runparams, Paragraph const & par)
213 {
214         Layout const & style = par.layout();
215         string const & name = style.latexname();
216         string param = style.latexparam();
217         Counters & counters = buf.params().documentClass().counters();
218
219         string id = par.getID(buf, runparams);
220
221         string attribute;
222         if (!id.empty()) {
223                 if (param.find('#') != string::npos) {
224                         string::size_type pos = param.find("id=<");
225                         string::size_type end = param.find(">");
226                         if( pos != string::npos && end != string::npos)
227                                 param.erase(pos, end-pos + 1);
228                 }
229                 attribute = id + ' ' + param;
230         } else {
231                 if (param.find('#') != string::npos) {
232                         // FIXME UNICODE
233                         if (!style.counter.empty())
234                                 // This uses InternalUpdate at the moment becuase sgml output
235                                 // does not do anything with tracked counters, and it would need
236                                 // to track layouts if it did want to use them.
237                                 counters.step(style.counter, InternalUpdate);
238                         else
239                                 counters.step(from_ascii(name), InternalUpdate);
240                         int i = counters.value(from_ascii(name));
241                         attribute = subst(param, "#", convert<string>(i));
242                 } else {
243                         attribute = param;
244                 }
245         }
246         openTag(os, name, attribute);
247 }
248
249
250 void sgml::closeTag(odocstream & os, Paragraph const & par)
251 {
252         Layout const & style = par.layout();
253         closeTag(os, style.latexname());
254 }
255
256
257 } // namespace lyx