]> git.lyx.org Git - lyx.git/blob - src/sgml.C
aabf234a59793feb3bd7f3ba307bc00695b2b855
[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 <sstream>
29
30 using lyx::support::subst;
31
32 using std::make_pair;
33
34 using std::ostream;
35 using std::ostringstream;
36 using std::pair;
37 using std::string;
38
39 namespace sgml {
40
41 pair<bool, string> escapeChar(char c)
42 {
43         string str;
44
45         switch (c) {
46         case ' ':
47                 return make_pair(true, string(" "));
48                 break;
49         case '\0': // Ignore :-)
50                 str.erase();
51                 break;
52         case '&':
53                 str = "&amp;";
54                 break;
55         case '<':
56                 str = "&lt;";
57                 break;
58         case '>':
59                 str = "&gt;";
60                 break;
61 #if 0
62         case '$':
63                 str = "&dollar;";
64                 break;
65         case '#':
66                 str = "&num;";
67                 break;
68         case '%':
69                 str = "&percnt;";
70                 break;
71         case '[':
72                 str = "&lsqb;";
73                 break;
74         case ']':
75                 str = "&rsqb;";
76                 break;
77         case '{':
78                 str = "&lcub;";
79                 break;
80         case '}':
81                 str = "&rcub;";
82                 break;
83         case '~':
84                 str = "&tilde;";
85                 break;
86         case '"':
87                 str = "&quot;";
88                 break;
89         case '\\':
90                 str = "&bsol;";
91                 break;
92 #endif
93         default:
94                 str = c;
95                 break;
96         }
97         return make_pair(false, str);
98 }
99
100
101 string escapeString(string const & raw)
102 {
103         ostringstream bin;
104
105         for(string::size_type i = 0; i < raw.size(); ++i) {
106                 bool ws;
107                 string str;
108                 boost::tie(ws, str) = sgml::escapeChar(raw[i]);
109                 bin << str;
110         }
111         return bin.str();
112 }
113
114
115 int openTag(Buffer const & buf, ostream & os, Paragraph::depth_type depth,
116             bool mixcont, string const & name, string const & param)
117 {
118         Counters & counters = buf.params().getLyXTextClass().counters();
119         LyXLayout_ptr const & defaultstyle = buf.params().getLyXTextClass().defaultLayout();
120
121         string attribute = param;
122         // code for paragraphs like the standard paragraph in AGU.
123         if ( defaultstyle->latexname() == name and !defaultstyle->latexparam().empty()) {
124                 counters.step(name);
125                 int i = counters.value(name);
126                 attribute += "" + subst(defaultstyle->latexparam(), "#", tostr(i));
127         }
128
129         if (!name.empty() && name != "!-- --") {
130                 if (!mixcont)
131                         os << string(depth, ' ');
132                 os << '<' << name;
133                 if (!attribute.empty())
134                         os << " " << attribute;
135                 os << '>';
136         }
137
138         return !mixcont;
139 }
140
141
142 int closeTag(ostream & os, Paragraph::depth_type depth,
143              bool mixcont, string const & name)
144 {
145         if (!name.empty() && name != "!-- --") {
146                 if (!mixcont)
147                         os << '\n' << string(depth, ' ');
148                 os << "</" << name << '>';
149         }
150
151         if (!mixcont)
152                 os << '\n';
153
154         return !mixcont;
155 }
156
157 } // namespace sgml