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"
38 using std::ostringstream;
41 docstring sgml::escapeChar(char_type c)
97 docstring sgml::escapeString(docstring const & raw)
101 for(docstring::size_type i = 0; i < raw.size(); ++i) {
102 bin << sgml::escapeChar(raw[i]);
108 string const sgml::uniqueID(string const label)
110 static unsigned int seed = 1000;
111 return label + convert<string>(++seed);
115 string sgml::cleanID(Buffer const & buf, OutputParams const & runparams, std::string const & orig)
117 // The standard DocBook SGML declaration only allows letters,
118 // digits, '-' and '.' in a name.
119 // Since users might change that declaration one has to cater
120 // for additional allowed characters.
121 // This routine replaces illegal characters by '-' or '.'
122 // and adds a number for uniqueness.
123 // If you know what you are doing, you can set allowed==""
124 // to disable this mangling.
125 LyXTextClass const & tclass = buf.params().getLyXTextClass();
126 string const allowed = runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
131 string::const_iterator it = orig.begin();
132 string::const_iterator end = orig.end();
136 typedef map<string, string> MangledMap;
137 static MangledMap mangledNames;
138 static int mangleID = 1;
140 MangledMap::const_iterator const known = mangledNames.find(orig);
141 if (known != mangledNames.end())
142 return (*known).second;
144 // make sure it starts with a letter
145 if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
149 for (; it != end; ++it) {
151 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
153 else if (c == '_' || c == ' ') {
157 else if (c == ':' || c == ',' || c == ';' || c == '!') {
166 content += "-" + convert<string>(mangleID++);
168 else if (isdigit(content[content.size() - 1])) {
172 mangledNames[orig] = content;
178 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
181 // This should be fixed in layout files later.
182 string param = subst(attribute, "<", "\"");
183 param = subst(param, ">", "\"");
185 if (!name.empty() && name != "!-- --") {
186 os << '<' << from_ascii(name);
188 os << ' ' << from_ascii(param);
194 void sgml::closeTag(odocstream & os, string const & name)
197 if (!name.empty() && name != "!-- --")
198 os << "</" << from_ascii(name) << '>';
202 void sgml::openTag(Buffer const & buf, odocstream & os, OutputParams const & runparams, Paragraph const & par)
204 LyXLayout_ptr const & style = par.layout();
205 string const & name = style->latexname();
206 string param = style->latexparam();
207 Counters & counters = buf.params().getLyXTextClass().counters();
209 string id = par.getID(buf, runparams);
213 if (param.find('#') != string::npos) {
214 string::size_type pos = param.find("id=<");
215 string::size_type end = param.find(">");
216 if( pos != string::npos && end != string::npos)
217 param.erase(pos, end-pos + 1);
219 attribute = id + ' ' + param;
221 if (param.find('#') != string::npos) {
223 if(!style->counter.empty())
224 counters.step(style->counter);
226 counters.step(from_ascii(name));
227 int i = counters.value(from_ascii(name));
228 attribute = subst(param, "#", convert<string>(i));
233 openTag(os, name, attribute);
237 void sgml::closeTag(odocstream & os, Paragraph const & par)
239 LyXLayout_ptr const & style = par.layout();
240 closeTag(os, style->latexname());