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"
29 #include "support/ThreadStorage.h"
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 ThreadStorage<MangledMap> tMangledNames;
141 MangledMap & mangledNames = *tMangledNames;
142 static ThreadStorage<int> tMangleID;
143 int & mangleID = *tMangleID;
145 MangledMap::const_iterator const known = mangledNames.find(orig);
146 if (known != mangledNames.end())
147 return known->second;
149 // make sure it starts with a letter
150 if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
154 for (; it != end; ++it) {
156 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
157 || allowed.find(c) < allowed.size())
159 else if (c == '_' || c == ' ') {
163 else if (c == ':' || c == ',' || c == ';' || c == '!') {
173 content += "-" + convert<docstring>(mangleID++);
174 else if (isDigitASCII(content[content.size() - 1]))
177 mangledNames[orig] = content;
183 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
186 // This should be fixed in layout files later.
187 string param = subst(attribute, "<", "\"");
188 param = subst(param, ">", "\"");
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);
198 os << ' ' << from_ascii(param);
204 void sgml::closeTag(odocstream & os, string const & name)
206 if (!name.empty() && name != "!-- --" && name != "dummy")
207 os << "</" << from_ascii(name) << '>';
211 void sgml::openTag(Buffer const & buf, odocstream & os,
212 OutputParams const & runparams, Paragraph const & par)
214 Layout const & style = par.layout();
215 string const & name = style.latexname();
216 string param = style.latexparam();
217 Counters & counters = buf.params().documentClass().counters();
219 string id = par.getID(buf, runparams);
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);
229 attribute = id + ' ' + param;
231 if (param.find('#') != string::npos) {
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);
239 counters.step(from_ascii(name), InternalUpdate);
240 int i = counters.value(from_ascii(name));
241 attribute = subst(param, "#", convert<string>(i));
246 openTag(os, name, attribute);
250 void sgml::closeTag(odocstream & os, Paragraph const & par)
252 Layout const & style = par.layout();
253 closeTag(os, style.latexname());