Another minor change, but this should almost get us to the point that we
[lyx.git] / src / sgml.cpp
blob7b899f223cfe3124a64b8e753dd6fa7db7c0c91c
1 /**
2 * \file sgml.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author José Matos
7 * \author John Levon
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "sgml.h"
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "Counters.h"
19 #include "Layout.h"
20 #include "OutputParams.h"
21 #include "Paragraph.h"
22 #include "Text.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"
30 #include <map>
32 using namespace std;
33 using namespace lyx::support;
35 namespace lyx {
38 docstring sgml::escapeChar(char_type c)
40 docstring str;
41 switch (c) {
42 case ' ':
43 str += " ";
44 break;
45 case '&':
46 str += "&amp;";
47 break;
48 case '<':
49 str += "&lt;";
50 break;
51 case '>':
52 str += "&gt;";
53 break;
54 #if 0
55 case '$':
56 str += "&dollar;";
57 break;
58 case '#':
59 str += "&num;";
60 break;
61 case '%':
62 str += "&percnt;";
63 break;
64 case '[':
65 str += "&lsqb;";
66 break;
67 case ']':
68 str += "&rsqb;";
69 break;
70 case '{':
71 str += "&lcub;";
72 break;
73 case '}':
74 str += "&rcub;";
75 break;
76 case '~':
77 str += "&tilde;";
78 break;
79 case '"':
80 str += "&quot;";
81 break;
82 case '\\':
83 str += "&bsol;";
84 break;
85 #endif
86 default:
87 str += c;
88 break;
90 return str;
94 docstring sgml::escapeString(docstring const & raw)
96 docstring bin;
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]);
101 return bin;
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());
127 if (allowed.empty())
128 return orig;
130 docstring::const_iterator it = orig.begin();
131 docstring::const_iterator end = orig.end();
133 docstring content;
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())
145 content += "x";
147 bool mangle = false;
148 for (; it != end; ++it) {
149 char_type c = *it;
150 if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
151 || allowed.find(c) < allowed.size())
152 content += c;
153 else if (c == '_' || c == ' ') {
154 mangle = true;
155 content += "-";
157 else if (c == ':' || c == ',' || c == ';' || c == '!') {
158 mangle = true;
159 content += ".";
161 else {
162 mangle = true;
166 if (mangle)
167 content += "-" + convert<docstring>(mangleID++);
168 else if (isDigitASCII(content[content.size() - 1]))
169 content += ".";
171 mangledNames[orig] = content;
173 return content;
177 void sgml::openTag(odocstream & os, string const & name, string const & attribute)
179 // FIXME UNICODE
180 // This should be fixed in layout files later.
181 string param = subst(attribute, "<", "\"");
182 param = subst(param, ">", "\"");
184 if (!name.empty() && name != "!-- --") {
185 os << '<' << from_ascii(name);
186 if (!param.empty())
187 os << ' ' << from_ascii(param);
188 os << '>';
193 void sgml::closeTag(odocstream & os, string const & name)
195 if (!name.empty() && name != "!-- --")
196 os << "</" << from_ascii(name) << '>';
200 void sgml::openTag(Buffer const & buf, odocstream & os,
201 OutputParams const & runparams, Paragraph const & par)
203 Layout const & style = par.layout();
204 string const & name = style.latexname();
205 string param = style.latexparam();
206 Counters & counters = buf.params().documentClass().counters();
208 string id = par.getID(buf, runparams);
210 string attribute;
211 if (!id.empty()) {
212 if (param.find('#') != string::npos) {
213 string::size_type pos = param.find("id=<");
214 string::size_type end = param.find(">");
215 if( pos != string::npos && end != string::npos)
216 param.erase(pos, end-pos + 1);
218 attribute = id + ' ' + param;
219 } else {
220 if (param.find('#') != string::npos) {
221 // FIXME UNICODE
222 if (!style.counter.empty())
223 counters.step(style.counter);
224 else
225 counters.step(from_ascii(name));
226 int i = counters.value(from_ascii(name));
227 attribute = subst(param, "#", convert<string>(i));
228 } else {
229 attribute = param;
232 openTag(os, name, attribute);
236 void sgml::closeTag(odocstream & os, Paragraph const & par)
238 Layout const & style = par.layout();
239 closeTag(os, style.latexname());
243 } // namespace lyx