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