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"
33 using namespace lyx::support;
38 docstring sgml::escapeChar(char_type c)
94 docstring sgml::escapeString(docstring const & raw)
97 bin.reserve(raw.size() * 2); // crude approximation is sufficient
98 for (size_t i = 0; i != raw.size(); ++i)
99 bin += sgml::escapeChar(raw[i]);
105 docstring const sgml::uniqueID(docstring const label)
108 // It seems unlikely there could be a problem here,
109 // but we could have concurrent access, in principle.
110 static unsigned int seed = 1000;
111 return label + convert<docstring>(++seed);
115 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
116 docstring const & orig)
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());
133 docstring::const_iterator it = orig.begin();
134 docstring::const_iterator end = orig.end();
139 typedef map<docstring, docstring> MangledMap;
140 static MangledMap mangledNames;
141 static int mangleID = 1;
143 MangledMap::const_iterator const known = mangledNames.find(orig);
144 if (known != mangledNames.end())
145 return known->second;
147 // make sure it starts with a letter
148 if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
152 for (; it != end; ++it) {
154 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
155 || allowed.find(c) < allowed.size())
157 else if (c == '_' || c == ' ') {
161 else if (c == ':' || c == ',' || c == ';' || c == '!') {
171 content += "-" + convert<docstring>(mangleID++);
172 else if (isDigitASCII(content[content.size() - 1]))
175 mangledNames[orig] = content;
181 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
184 // This should be fixed in layout files later.
185 string param = subst(attribute, "<", "\"");
186 param = subst(param, ">", "\"");
188 // Note: we ignore the name if it empty or if it is a comment "<!-- -->" or
189 // if the name is *dummy*.
190 // We ignore dummy because dummy is not a valid docbook element and it is
191 // the internal name given to single paragraphs in the latex output.
192 // This allow us to simplify the code a lot and is a reasonable compromise.
193 if (!name.empty() && name != "!-- --" && name != "dummy") {
194 os << '<' << from_ascii(name);
196 os << ' ' << from_ascii(param);
202 void sgml::closeTag(odocstream & os, string const & name)
204 if (!name.empty() && name != "!-- --" && name != "dummy")
205 os << "</" << from_ascii(name) << '>';
209 void sgml::openTag(Buffer const & buf, odocstream & os,
210 OutputParams const & runparams, Paragraph const & par)
212 Layout const & style = par.layout();
213 string const & name = style.latexname();
214 string param = style.latexparam();
215 Counters & counters = buf.params().documentClass().counters();
217 string id = par.getID(buf, runparams);
221 if (param.find('#') != string::npos) {
222 string::size_type pos = param.find("id=<");
223 string::size_type end = param.find(">");
224 if( pos != string::npos && end != string::npos)
225 param.erase(pos, end-pos + 1);
227 attribute = id + ' ' + param;
229 if (param.find('#') != string::npos) {
231 if (!style.counter.empty())
232 // This uses InternalUpdate at the moment becuase sgml output
233 // does not do anything with tracked counters, and it would need
234 // to track layouts if it did want to use them.
235 counters.step(style.counter, InternalUpdate);
237 counters.step(from_ascii(name), InternalUpdate);
238 int i = counters.value(from_ascii(name));
239 attribute = subst(param, "#", convert<string>(i));
244 openTag(os, name, attribute);
248 void sgml::closeTag(odocstream & os, Paragraph const & par)
250 Layout const & style = par.layout();
251 closeTag(os, style.latexname());