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"
24 #include "support/convert.h"
25 #include "support/docstream.h"
26 #include "support/lstrings.h"
27 #include "support/textutils.h"
39 using std::ostringstream;
42 docstring sgml::escapeChar(char_type c)
98 docstring sgml::escapeString(docstring const & raw)
101 bin.reserve(raw.size() * 2); // crude approximation is sufficient
102 for (size_t i = 0; i != raw.size(); ++i)
103 bin += sgml::escapeChar(raw[i]);
109 docstring const sgml::uniqueID(docstring const label)
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 TextClass const & tclass = buf.params().getTextClass();
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 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 if (!name.empty() && name != "!-- --") {
189 os << '<' << from_ascii(name);
191 os << ' ' << from_ascii(param);
197 void sgml::closeTag(odocstream & os, string const & name)
199 if (!name.empty() && name != "!-- --")
200 os << "</" << from_ascii(name) << '>';
204 void sgml::openTag(Buffer const & buf, odocstream & os,
205 OutputParams const & runparams, Paragraph const & par)
207 LayoutPtr const & style = par.layout();
208 string const & name = style->latexname();
209 string param = style->latexparam();
210 Counters & counters = buf.params().getTextClass().counters();
212 string id = par.getID(buf, runparams);
216 if (param.find('#') != string::npos) {
217 string::size_type pos = param.find("id=<");
218 string::size_type end = param.find(">");
219 if( pos != string::npos && end != string::npos)
220 param.erase(pos, end-pos + 1);
222 attribute = id + ' ' + param;
224 if (param.find('#') != string::npos) {
226 if (!style->counter.empty())
227 counters.step(style->counter);
229 counters.step(from_ascii(name));
230 int i = counters.value(from_ascii(name));
231 attribute = subst(param, "#", convert<string>(i));
236 openTag(os, name, attribute);
240 void sgml::closeTag(odocstream & os, Paragraph const & par)
242 LayoutPtr const & style = par.layout();
243 closeTag(os, style->latexname());