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