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 "support/docstring.h"
24 #include "support/lstrings.h"
25 #include "support/std_ostream.h"
26 #include "support/convert.h"
27 #include "support/textutils.h"
39 using std::ostringstream;
42 docstring sgml::escapeChar(char_type c)
98 docstring sgml::escapeString(docstring const & raw)
100 odocstringstream bin;
102 for(docstring::size_type 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 string const allowed =
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 == '!') {
170 content += "-" + convert<docstring>(mangleID++);
172 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 Layout_ptr 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 Layout_ptr const & style = par.layout();
244 closeTag(os, style->latexname());