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"
40 using std::ostringstream;
43 docstring sgml::escapeChar(char_type c)
99 docstring sgml::escapeString(docstring const & raw)
102 bin.reserve(raw.size() * 2); // crude approximation is sufficient
103 for (size_t i = 0; i != raw.size(); ++i)
104 bin += sgml::escapeChar(raw[i]);
110 docstring const sgml::uniqueID(docstring const label)
112 static unsigned int seed = 1000;
113 return label + convert<docstring>(++seed);
117 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
118 docstring const & orig)
120 // The standard DocBook SGML declaration only allows letters,
121 // digits, '-' and '.' in a name.
122 // Since users might change that declaration one has to cater
123 // for additional allowed characters.
124 // This routine replaces illegal characters by '-' or '.'
125 // and adds a number for uniqueness.
126 // If you know what you are doing, you can set allowed==""
127 // to disable this mangling.
128 TextClass const & tclass = buf.params().getTextClass();
129 docstring const allowed = from_ascii(
130 runparams.flavor == OutputParams::XML? ".-_:":tclass.options());
135 docstring::const_iterator it = orig.begin();
136 docstring::const_iterator end = orig.end();
140 typedef map<docstring, docstring> MangledMap;
141 static MangledMap mangledNames;
142 static int mangleID = 1;
144 MangledMap::const_iterator const known = mangledNames.find(orig);
145 if (known != mangledNames.end())
146 return (*known).second;
148 // make sure it starts with a letter
149 if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
153 for (; it != end; ++it) {
155 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
156 || allowed.find(c) < allowed.size())
158 else if (c == '_' || c == ' ') {
162 else if (c == ':' || c == ',' || c == ';' || c == '!') {
172 content += "-" + convert<docstring>(mangleID++);
173 else if (isDigitASCII(content[content.size() - 1]))
176 mangledNames[orig] = content;
182 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
185 // This should be fixed in layout files later.
186 string param = subst(attribute, "<", "\"");
187 param = subst(param, ">", "\"");
189 if (!name.empty() && name != "!-- --") {
190 os << '<' << from_ascii(name);
192 os << ' ' << from_ascii(param);
198 void sgml::closeTag(odocstream & os, string const & name)
200 if (!name.empty() && name != "!-- --")
201 os << "</" << from_ascii(name) << '>';
205 void sgml::openTag(Buffer const & buf, odocstream & os,
206 OutputParams const & runparams, Paragraph const & par)
208 LayoutPtr const & style = par.layout();
209 string const & name = style->latexname();
210 string param = style->latexparam();
211 Counters & counters = buf.params().getTextClass().counters();
213 string id = par.getID(buf, runparams);
217 if (param.find('#') != string::npos) {
218 string::size_type pos = param.find("id=<");
219 string::size_type end = param.find(">");
220 if( pos != string::npos && end != string::npos)
221 param.erase(pos, end-pos + 1);
223 attribute = id + ' ' + param;
225 if (param.find('#') != string::npos) {
227 if (!style->counter.empty())
228 counters.step(style->counter);
230 counters.step(from_ascii(name));
231 int i = counters.value(from_ascii(name));
232 attribute = subst(param, "#", convert<string>(i));
237 openTag(os, name, attribute);
241 void sgml::closeTag(odocstream & os, Paragraph const & par)
243 LayoutPtr const & style = par.layout();
244 closeTag(os, style->latexname());