]> git.lyx.org Git - lyx.git/blob - src/sgml.C
12f3ae53e374117c499202236635028575ec2e4a
[lyx.git] / src / sgml.C
1 /**
2  * \file sgml.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "sgml.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "counters.h"
19 #include "lyxtext.h"
20 #include "paragraph.h"
21
22 #include "support/lstrings.h"
23 #include "support/std_ostream.h"
24 #include "support/tostr.h"
25
26 #include <boost/tuple/tuple.hpp>
27
28 #include <map>
29 #include <sstream>
30
31 using lyx::support::subst;
32
33 using std::make_pair;
34 using std::map;
35 using std::ostream;
36 using std::ostringstream;
37 using std::pair;
38 using std::string;
39
40 namespace sgml {
41
42 pair<bool, string> escapeChar(char c)
43 {
44         string str;
45
46         switch (c) {
47         case ' ':
48                 return make_pair(true, string(" "));
49                 break;
50         case '\0': // Ignore :-)
51                 str.erase();
52                 break;
53         case '&':
54                 str = "&amp;";
55                 break;
56         case '<':
57                 str = "&lt;";
58                 break;
59         case '>':
60                 str = "&gt;";
61                 break;
62 #if 0
63         case '$':
64                 str = "&dollar;";
65                 break;
66         case '#':
67                 str = "&num;";
68                 break;
69         case '%':
70                 str = "&percnt;";
71                 break;
72         case '[':
73                 str = "&lsqb;";
74                 break;
75         case ']':
76                 str = "&rsqb;";
77                 break;
78         case '{':
79                 str = "&lcub;";
80                 break;
81         case '}':
82                 str = "&rcub;";
83                 break;
84         case '~':
85                 str = "&tilde;";
86                 break;
87         case '"':
88                 str = "&quot;";
89                 break;
90         case '\\':
91                 str = "&bsol;";
92                 break;
93 #endif
94         default:
95                 str = c;
96                 break;
97         }
98         return make_pair(false, str);
99 }
100
101
102 string escapeString(string const & raw)
103 {
104         ostringstream bin;
105
106         for(string::size_type i = 0; i < raw.size(); ++i) {
107                 bool ws;
108                 string str;
109                 boost::tie(ws, str) = sgml::escapeChar(raw[i]);
110                 bin << str;
111         }
112         return bin.str();
113 }
114
115
116 string const uniqueID(string const label)
117 {
118         static unsigned int seed = 1000;
119         return label + tostr(++seed);
120 }
121
122
123 string cleanID(std::string const & orig, std::string const & allowed)
124 {
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.
133         
134         string::const_iterator it  = orig.begin();
135         string::const_iterator end = orig.end();
136
137         string content;
138
139         if (allowed.empty()) {
140                 return orig;
141         }
142
143         typedef map<string, string> MangledMap;
144         static MangledMap mangledNames;
145         static int mangleID = 1;
146
147         MangledMap::const_iterator const known = mangledNames.find(orig);
148         if (known != mangledNames.end())
149                 return (*known).second;
150
151         // make sure it starts with a letter
152         if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
153                 content += "x";
154         
155         bool mangle = false;    
156         for (; it != end; ++it) {
157                 char c = *it;
158                 if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
159                         content += c;
160                 else if (c == '_' || c == ' ') {
161                         mangle = true;
162                         content += "-";
163                 }
164                 else if (c == ':' || c == ',' || c == ';' || c == '!') {
165                         mangle = true;
166                         content += ".";
167                 }
168                 else {
169                         mangle = true;
170                 }
171         }
172         if (mangle) {
173                 content += "-" + tostr(mangleID++);
174         }
175         else if (isdigit(content[content.size()-1])) {
176                 content += ".";
177         }
178
179         mangledNames[orig] = content;
180
181         return content;
182 }
183
184
185 void openTag(ostream & os, string const & name, string const & attribute)
186 {
187         // This should be fixed in layout files later.
188         string param = subst(attribute, "<", "\"");
189         param = subst(param, ">", "\"");
190
191         if (!name.empty() && name != "!-- --") {
192                 os << '<' << name;
193                 if (!param.empty())
194                         os << " " << param;
195                 os << '>';
196         }
197 }
198
199
200 void closeTag(ostream & os, string const & name)
201 {
202         if (!name.empty() && name != "!-- --")
203                 os << "</" << name << '>';
204 }
205
206
207 void openTag(Buffer const & buf, ostream & os, Paragraph const & par)
208 {
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();
213
214         string id = par.getID();
215
216         string attribute;
217         if(!id.empty()) {
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);
223                 }
224                 attribute = id + ' ' + param;
225         } else {
226                 if (param.find('#') != string::npos) {
227                         if(!style->counter.empty())
228                                 counters.step(style->counter);
229                         else
230                                 counters.step(style->latexname());
231                         int i = counters.value(name);
232                         attribute = subst(param, "#", tostr(i));
233                 } else {
234                         attribute = param;
235                 }
236         }
237         openTag(os, name, attribute);
238 }
239
240
241 void closeTag(ostream & os, Paragraph const & par)
242 {
243         LyXLayout_ptr const & style = par.layout();
244         closeTag(os, style->latexname());
245 }
246
247 } // namespace sgml