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/lstrings.h"
24 #include "support/std_ostream.h"
25 #include "support/convert.h"
27 #include <boost/tuple/tuple.hpp>
32 using lyx::odocstream;
33 using lyx::support::subst;
38 using std::ostringstream;
42 pair<bool, string> sgml::escapeChar(char c)
48 return make_pair(true, string(" "));
50 case '\0': // Ignore :-)
98 return make_pair(false, str);
102 string sgml::escapeString(string const & raw)
106 for(string::size_type i = 0; i < raw.size(); ++i) {
109 boost::tie(ws, str) = sgml::escapeChar(raw[i]);
116 string const sgml::uniqueID(string const label)
118 static unsigned int seed = 1000;
119 return label + convert<string>(++seed);
123 string sgml::cleanID(Buffer const & buf, OutputParams const & runparams, std::string const & orig)
125 // The standard DocBook SGML declaration only allows letters,
126 // digits, '-' and '.' in a name.
127 // Since users might change that declaration one has to cater
128 // for additional allowed characters.
129 // This routine replaces illegal characters by '-' or '.'
130 // and adds a number for uniqueness.
131 // If you know what you are doing, you can set allowed==""
132 // to disable this mangling.
133 LyXTextClass const & tclass = buf.params().getLyXTextClass();
134 string const allowed = runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
139 string::const_iterator it = orig.begin();
140 string::const_iterator end = orig.end();
144 typedef map<string, string> MangledMap;
145 static MangledMap mangledNames;
146 static int mangleID = 1;
148 MangledMap::const_iterator const known = mangledNames.find(orig);
149 if (known != mangledNames.end())
150 return (*known).second;
152 // make sure it starts with a letter
153 if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
157 for (; it != end; ++it) {
159 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
161 else if (c == '_' || c == ' ') {
165 else if (c == ':' || c == ',' || c == ';' || c == '!') {
174 content += "-" + convert<string>(mangleID++);
176 else if (isdigit(content[content.size() - 1])) {
180 mangledNames[orig] = content;
186 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
189 // This should be fixed in layout files later.
190 string param = subst(attribute, "<", "\"");
191 param = subst(param, ">", "\"");
193 if (!name.empty() && name != "!-- --") {
194 os << '<' << lyx::from_ascii(name);
196 os << ' ' << lyx::from_ascii(param);
202 void sgml::closeTag(odocstream & os, string const & name)
205 if (!name.empty() && name != "!-- --")
206 os << "</" << lyx::from_ascii(name) << '>';
210 void sgml::openTag(Buffer const & buf, odocstream & os, OutputParams const & runparams, Paragraph const & par)
212 LyXLayout_ptr const & style = par.layout();
213 string const & name = style->latexname();
214 string param = style->latexparam();
215 Counters & counters = buf.params().getLyXTextClass().counters();
217 string id = par.getID(buf, runparams);
221 if (param.find('#') != string::npos) {
222 string::size_type pos = param.find("id=<");
223 string::size_type end = param.find(">");
224 if( pos != string::npos && end != string::npos)
225 param.erase(pos, end-pos + 1);
227 attribute = id + ' ' + param;
229 if (param.find('#') != string::npos) {
230 if(!style->counter.empty())
231 counters.step(style->counter);
233 counters.step(style->latexname());
234 int i = counters.value(name);
235 attribute = subst(param, "#", convert<string>(i));
240 openTag(os, name, attribute);
244 void sgml::closeTag(odocstream & os, Paragraph const & par)
246 LyXLayout_ptr const & style = par.layout();
247 closeTag(os, style->latexname());