]> git.lyx.org Git - lyx.git/blob - src/insets/insetlabel.C
92f871afab480232dd187174b3e5c2d08cc3779e
[lyx.git] / src / insets / insetlabel.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetlabel.h"
18 #include "support/LOstream.h"
19
20 using std::ostream;
21
22 /* Label. Used to insert a label automatically */
23
24
25 InsetLabel::InsetLabel(string const & cmd)
26 {
27         scanCommand(cmd);
28 }
29
30
31 Inset * InsetLabel::Clone() const
32 {
33         return new InsetLabel(getCommand());
34 }
35
36
37 int InsetLabel::GetNumberOfLabels() const
38 {
39         return 1;
40 }
41
42
43 string InsetLabel::getLabel(int) const
44 {
45         return contents;
46 }
47
48
49 int InsetLabel::Latex(ostream & os,
50                       bool /*fragile*/, bool /*fs*/) const
51 {
52         os << escape(getCommand());
53         return 0;
54 }
55
56
57 int InsetLabel::Linuxdoc(ostream & os) const
58 {
59         os << "<label id=\"" << getContents() << "\" >";
60         return 0;
61 }
62
63
64 int InsetLabel::DocBook(ostream & os) const
65 {
66         os << "<anchor id=\"" << getContents() << "\" >";
67         return 0;
68 }
69
70
71 // This function escapes 8-bit characters and other problematic characters
72 // It's exactly the same code as in insetref.C.
73 string InsetLabel::escape(string const & lab) const {
74         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
75                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
76         string enc;
77         for (string::size_type i= 0; i < lab.length(); ++i) {
78                 unsigned char c = lab[i];
79                 if (c >= 128 || c == '=' || c == '%') {
80                         enc += '=';
81                         enc += hexdigit[c >> 4];
82                         enc += hexdigit[c & 15];
83                 } else {
84                         enc += c;
85                 }
86         }
87         return enc;
88 }