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 "TextClass.h"
25 #include "support/convert.h"
26 #include "support/docstream.h"
27 #include "support/lstrings.h"
28 #include "support/textutils.h"
33 using namespace lyx::support;
38 docstring sgml::escapeChar(char_type c)
94 docstring sgml::escapeString(docstring const & raw)
97 bin.reserve(raw.size() * 2); // crude approximation is sufficient
98 for (size_t i = 0; i != raw.size(); ++i)
99 bin += sgml::escapeChar(raw[i]);
105 docstring const sgml::uniqueID(docstring const label)
107 static unsigned int seed = 1000;
108 return label + convert<docstring>(++seed);
112 docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
113 docstring const & orig)
115 // The standard DocBook SGML declaration only allows letters,
116 // digits, '-' and '.' in a name.
117 // Since users might change that declaration one has to cater
118 // for additional allowed characters.
119 // This routine replaces illegal characters by '-' or '.'
120 // and adds a number for uniqueness.
121 // If you know what you are doing, you can set allowed==""
122 // to disable this mangling.
123 DocumentClass const & tclass = buf.params().documentClass();
124 docstring const allowed = from_ascii(
125 runparams.flavor == OutputParams::XML ? ".-_:" : tclass.options());
130 docstring::const_iterator it = orig.begin();
131 docstring::const_iterator end = orig.end();
135 typedef map<docstring, docstring> MangledMap;
136 static MangledMap mangledNames;
137 static int mangleID = 1;
139 MangledMap::const_iterator const known = mangledNames.find(orig);
140 if (known != mangledNames.end())
141 return known->second;
143 // make sure it starts with a letter
144 if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
148 for (; it != end; ++it) {
150 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
151 || allowed.find(c) < allowed.size())
153 else if (c == '_' || c == ' ') {
157 else if (c == ':' || c == ',' || c == ';' || c == '!') {
167 content += "-" + convert<docstring>(mangleID++);
168 else if (isDigitASCII(content[content.size() - 1]))
171 mangledNames[orig] = content;
177 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
180 // This should be fixed in layout files later.
181 string param = subst(attribute, "<", "\"");
182 param = subst(param, ">", "\"");
184 // Note: we ignore the name if it empty or if it is a comment "<!-- -->" or
185 // if the name is *dummy*.
186 // We ignore dummy because dummy is not a valid docbook element and it is
187 // the internal name given to single paragraphs in the latex output.
188 // This allow us to simplify the code a lot and is a reasonable compromise.
189 if (!name.empty() && name != "!-- --" && name != "dummy") {
190 os << '<' << from_ascii(name);
192 os << ' ' << from_ascii(param);
198 void sgml::closeTag(odocstream & os, string const & name)
200 if (!name.empty() && name != "!-- --" && name != "dummy")
201 os << "</" << from_ascii(name) << '>';
205 void sgml::openTag(Buffer const & buf, odocstream & os,
206 OutputParams const & runparams, Paragraph const & par)
208 Layout const & style = par.layout();
209 string const & name = style.latexname();
210 string param = style.latexparam();
211 Counters & counters = buf.params().documentClass().counters();
213 string id = par.getID(buf, runparams);
217 if (param.find('#') != string::npos) {
218 string::size_type pos = param.find("id=<");
219 string::size_type end = param.find(">");
220 if( pos != string::npos && end != string::npos)
221 param.erase(pos, end-pos + 1);
223 attribute = id + ' ' + param;
225 if (param.find('#') != string::npos) {
227 if (!style.counter.empty())
228 // This uses InternalUpdate at the moment becuase sgml output
229 // does not do anything with tracked counters, and it would need
230 // to track layouts if it did want to use them.
231 counters.step(style.counter, InternalUpdate);
233 counters.step(from_ascii(name), InternalUpdate);
234 int i = counters.value(from_ascii(name));
235 attribute = subst(param, "#", convert<string>(i));
240 openTag(os, name, attribute);
244 void sgml::closeTag(odocstream & os, Paragraph const & par)
246 Layout const & style = par.layout();
247 closeTag(os, style.latexname());