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"
21 #include "OutputParams.h"
22 #include "Paragraph.h"
24 #include "support/docstream.h"
25 #include "support/lstrings.h"
26 #include "support/std_ostream.h"
27 #include "support/convert.h"
28 #include "support/textutils.h"
40 using std::ostringstream;
43 docstring sgml::escapeChar(char_type c)
99 docstring sgml::escapeString(docstring const & raw)
101 odocstringstream bin;
103 for(docstring::size_type 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 == '!') {
171 content += "-" + convert<docstring>(mangleID++);
173 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 if (!name.empty() && name != "!-- --") {
191 os << '<' << from_ascii(name);
193 os << ' ' << from_ascii(param);
199 void sgml::closeTag(odocstream & os, string const & name)
201 if (!name.empty() && name != "!-- --")
202 os << "</" << from_ascii(name) << '>';
206 void sgml::openTag(Buffer const & buf, odocstream & os,
207 OutputParams const & runparams, Paragraph const & par)
209 LayoutPtr const & style = par.layout();
210 string const & name = style->latexname();
211 string param = style->latexparam();
212 Counters & counters = buf.params().getTextClass().counters();
214 string id = par.getID(buf, runparams);
218 if (param.find('#') != string::npos) {
219 string::size_type pos = param.find("id=<");
220 string::size_type end = param.find(">");
221 if( pos != string::npos && end != string::npos)
222 param.erase(pos, end-pos + 1);
224 attribute = id + ' ' + param;
226 if (param.find('#') != string::npos) {
228 if(!style->counter.empty())
229 counters.step(style->counter);
231 counters.step(from_ascii(name));
232 int i = counters.value(from_ascii(name));
233 attribute = subst(param, "#", convert<string>(i));
238 openTag(os, name, attribute);
242 void sgml::closeTag(odocstream & os, Paragraph const & par)
244 LayoutPtr const & style = par.layout();
245 closeTag(os, style->latexname());