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>
40 using std::ostringstream;
44 pair<bool, string> sgml::escapeChar(char c)
50 return make_pair(true, string(" "));
52 case '\0': // Ignore :-)
100 return make_pair(false, str);
104 string sgml::escapeString(string const & raw)
108 for(string::size_type i = 0; i < raw.size(); ++i) {
111 boost::tie(ws, str) = sgml::escapeChar(raw[i]);
118 string const sgml::uniqueID(string const label)
120 static unsigned int seed = 1000;
121 return label + convert<string>(++seed);
125 string sgml::cleanID(Buffer const & buf, OutputParams const & runparams, std::string const & orig)
127 // The standard DocBook SGML declaration only allows letters,
128 // digits, '-' and '.' in a name.
129 // Since users might change that declaration one has to cater
130 // for additional allowed characters.
131 // This routine replaces illegal characters by '-' or '.'
132 // and adds a number for uniqueness.
133 // If you know what you are doing, you can set allowed==""
134 // to disable this mangling.
135 LyXTextClass const & tclass = buf.params().getLyXTextClass();
136 string const allowed = runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
141 string::const_iterator it = orig.begin();
142 string::const_iterator end = orig.end();
146 typedef map<string, string> MangledMap;
147 static MangledMap mangledNames;
148 static int mangleID = 1;
150 MangledMap::const_iterator const known = mangledNames.find(orig);
151 if (known != mangledNames.end())
152 return (*known).second;
154 // make sure it starts with a letter
155 if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
159 for (; it != end; ++it) {
161 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
163 else if (c == '_' || c == ' ') {
167 else if (c == ':' || c == ',' || c == ';' || c == '!') {
176 content += "-" + convert<string>(mangleID++);
178 else if (isdigit(content[content.size() - 1])) {
182 mangledNames[orig] = content;
188 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
191 // This should be fixed in layout files later.
192 string param = subst(attribute, "<", "\"");
193 param = subst(param, ">", "\"");
195 if (!name.empty() && name != "!-- --") {
196 os << '<' << from_ascii(name);
198 os << ' ' << from_ascii(param);
204 void sgml::closeTag(odocstream & os, string const & name)
207 if (!name.empty() && name != "!-- --")
208 os << "</" << from_ascii(name) << '>';
212 void sgml::openTag(Buffer const & buf, odocstream & os, OutputParams const & runparams, Paragraph const & par)
214 LyXLayout_ptr const & style = par.layout();
215 string const & name = style->latexname();
216 string param = style->latexparam();
217 Counters & counters = buf.params().getLyXTextClass().counters();
219 string id = par.getID(buf, runparams);
223 if (param.find('#') != string::npos) {
224 string::size_type pos = param.find("id=<");
225 string::size_type end = param.find(">");
226 if( pos != string::npos && end != string::npos)
227 param.erase(pos, end-pos + 1);
229 attribute = id + ' ' + param;
231 if (param.find('#') != string::npos) {
233 if(!style->counter.empty())
234 counters.step(style->counter);
236 counters.step(from_ascii(name));
237 int i = counters.value(from_ascii(name));
238 attribute = subst(param, "#", convert<string>(i));
243 openTag(os, name, attribute);
247 void sgml::closeTag(odocstream & os, Paragraph const & par)
249 LyXLayout_ptr const & style = par.layout();
250 closeTag(os, style->latexname());