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::support::subst;
37 using std::ostringstream;
43 pair<bool, string> escapeChar(char c)
49 return make_pair(true, string(" "));
51 case '\0': // Ignore :-)
99 return make_pair(false, str);
103 string escapeString(string const & raw)
107 for(string::size_type i = 0; i < raw.size(); ++i) {
110 boost::tie(ws, str) = sgml::escapeChar(raw[i]);
117 string const uniqueID(string const label)
119 static unsigned int seed = 1000;
120 return label + convert<string>(++seed);
124 string cleanID(Buffer const & buf, OutputParams const & runparams, std::string const & orig)
126 // The standard DocBook SGML declaration only allows letters,
127 // digits, '-' and '.' in a name.
128 // Since users might change that declaration one has to cater
129 // for additional allowed characters.
130 // This routine replaces illegal characters by '-' or '.'
131 // and adds a number for uniqueness.
132 // If you know what you are doing, you can set allowed==""
133 // to disable this mangling.
134 LyXTextClass const & tclass = buf.params().getLyXTextClass();
135 string const allowed = runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
140 string::const_iterator it = orig.begin();
141 string::const_iterator end = orig.end();
145 typedef map<string, string> MangledMap;
146 static MangledMap mangledNames;
147 static int mangleID = 1;
149 MangledMap::const_iterator const known = mangledNames.find(orig);
150 if (known != mangledNames.end())
151 return (*known).second;
153 // make sure it starts with a letter
154 if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
158 for (; it != end; ++it) {
160 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
162 else if (c == '_' || c == ' ') {
166 else if (c == ':' || c == ',' || c == ';' || c == '!') {
175 content += "-" + convert<string>(mangleID++);
177 else if (isdigit(content[content.size() - 1])) {
181 mangledNames[orig] = content;
187 void openTag(ostream & 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 != "!-- --") {
202 void closeTag(ostream & os, string const & name)
204 if (!name.empty() && name != "!-- --")
205 os << "</" << name << '>';
209 void openTag(Buffer const & buf, ostream & os, OutputParams const & runparams, Paragraph const & par)
211 LyXLayout_ptr const & style = par.layout();
212 string const & name = style->latexname();
213 string param = style->latexparam();
214 Counters & counters = buf.params().getLyXTextClass().counters();
216 string id = par.getID(buf, runparams);
220 if (param.find('#') != string::npos) {
221 string::size_type pos = param.find("id=<");
222 string::size_type end = param.find(">");
223 if( pos != string::npos && end != string::npos)
224 param.erase(pos, end-pos + 1);
226 attribute = id + ' ' + param;
228 if (param.find('#') != string::npos) {
229 if(!style->counter.empty())
230 counters.step(style->counter);
232 counters.step(style->latexname());
233 int i = counters.value(name);
234 attribute = subst(param, "#", convert<string>(i));
239 openTag(os, name, attribute);
243 void closeTag(ostream & os, Paragraph const & par)
245 LyXLayout_ptr const & style = par.layout();
246 closeTag(os, style->latexname());