3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
17 #include "BufferParams.h"
20 #include "OutputParams.h"
21 #include "Paragraph.h"
23 #include "TextClass.h"
25 #include "support/convert.h"
26 #include "support/docstream.h"
27 #include "support/lstrings.h"
28 #include "support/textutils.h"
31 #include <QThreadStorage>
34 using namespace lyx::support;
39 docstring sgml::escapeChar(char_type c)
95 docstring sgml::escapeString(docstring const & raw)
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]);
106 docstring const sgml::uniqueID(docstring const & label)
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);
116 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
117 docstring const & orig)
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());
134 docstring::const_iterator it = orig.begin();
135 docstring::const_iterator end = orig.end();
139 typedef map<docstring, docstring> MangledMap;
140 static QThreadStorage<MangledMap> tMangledNames;
141 static QThreadStorage<int> tMangleID;
143 MangledMap & mangledNames = tMangledNames.localData();
144 int & mangleID = tMangleID.localData();
146 MangledMap::const_iterator const known = mangledNames.find(orig);
147 if (known != mangledNames.end())
148 return known->second;
150 // make sure it starts with a letter
151 if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
155 for (; it != end; ++it) {
157 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
158 || allowed.find(c) < allowed.size())
160 else if (c == '_' || c == ' ') {
164 else if (c == ':' || c == ',' || c == ';' || c == '!') {
174 content += "-" + convert<docstring>(mangleID++);
175 else if (isDigitASCII(content[content.size() - 1]))
178 mangledNames[orig] = content;
184 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
187 // This should be fixed in layout files later.
188 string param = subst(attribute, "<", "\"");
189 param = subst(param, ">", "\"");
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);
199 os << ' ' << from_ascii(param);
205 void sgml::closeTag(odocstream & os, string const & name)
207 if (!name.empty() && name != "!-- --" && name != "dummy")
208 os << "</" << from_ascii(name) << '>';
212 void sgml::openTag(Buffer const & buf, odocstream & os,
213 OutputParams const & runparams, Paragraph const & par)
215 Layout const & style = par.layout();
216 string const & name = style.latexname();
217 string param = style.latexparam();
218 Counters & counters = buf.params().documentClass().counters();
220 string id = par.getID(buf, runparams);
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);
230 attribute = id + ' ' + param;
232 if (param.find('#') != string::npos) {
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);
240 counters.step(from_ascii(name), InternalUpdate);
241 int i = counters.value(from_ascii(name));
242 attribute = subst(param, "#", convert<string>(i));
247 openTag(os, name, attribute);
251 void sgml::closeTag(odocstream & os, Paragraph const & par)
253 Layout const & style = par.layout();
254 closeTag(os, style.latexname());