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 "paragraph.h"
22 #include "support/lstrings.h"
23 #include "support/std_ostream.h"
24 #include "support/tostr.h"
26 #include <boost/tuple/tuple.hpp>
31 using lyx::support::subst;
36 using std::ostringstream;
42 pair<bool, string> escapeChar(char c)
48 return make_pair(true, string(" "));
50 case '\0': // Ignore :-)
98 return make_pair(false, str);
102 string 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 uniqueID(string const label)
118 static unsigned int seed = 1000;
119 return label + tostr(++seed);
123 string cleanID(std::string const & orig, std::string const & allowed)
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.
134 string::const_iterator it = orig.begin();
135 string::const_iterator end = orig.end();
139 if (allowed.empty()) {
143 typedef map<string, string> MangledMap;
144 static MangledMap mangledNames;
145 static int mangleID = 1;
147 MangledMap::const_iterator const known = mangledNames.find(orig);
148 if (known != mangledNames.end())
149 return (*known).second;
151 // make sure it starts with a letter
152 if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
156 for (; it != end; ++it) {
158 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
160 else if (c == '_' || c == ' ') {
164 else if (c == ':' || c == ',' || c == ';' || c == '!') {
173 content += "-" + tostr(mangleID++);
175 else if (isdigit(content[content.size()-1])) {
179 mangledNames[orig] = content;
185 void openTag(ostream & os, string const & name, string const & attribute)
187 // This should be fixed in layout files later.
188 string param = subst(attribute, "<", "\"");
189 param = subst(param, ">", "\"");
191 if (!name.empty() && name != "!-- --") {
200 void closeTag(ostream & os, string const & name)
202 if (!name.empty() && name != "!-- --")
203 os << "</" << name << '>';
207 void openTag(Buffer const & buf, ostream & os, Paragraph const & par)
209 LyXLayout_ptr const & style = par.layout();
210 string const & name = style->latexname();
211 string param = style->latexparam();
212 Counters & counters = buf.params().getLyXTextClass().counters();
214 string id = par.getID();
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 and end != string::npos)
222 param.erase(pos, end-pos + 1);
224 attribute = id + ' ' + param;
226 if (param.find('#') != string::npos) {
227 if(!style->counter.empty())
228 counters.step(style->counter);
230 counters.step(style->latexname());
231 int i = counters.value(name);
232 attribute = subst(param, "#", tostr(i));
237 openTag(os, name, attribute);
241 void closeTag(ostream & os, Paragraph const & par)
243 LyXLayout_ptr const & style = par.layout();
244 closeTag(os, style->latexname());