]> git.lyx.org Git - features.git/blob - src/insets/InsetHyperlink.cpp
'using namespace std' instead of 'using std::xxx'
[features.git] / src / insets / InsetHyperlink.cpp
1 /**
2  * \file InsetHyperlink.cpp
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 Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetHyperlink.h"
15
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "LaTeXFeatures.h"
19 #include "support/gettext.h"
20 #include "OutputParams.h"
21
22 #include "support/lstrings.h"
23 #include "support/docstream.h"
24
25 using namespace std;
26
27 namespace lyx {
28
29 using support::subst;
30
31
32 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
33         : InsetCommand(p, "href")
34 {}
35
36
37 CommandInfo const * InsetHyperlink::findInfo(string const & /* cmdName */)
38 {
39         static const char * const paramnames[] =
40                 {"name", "target", "type", ""};
41         static const bool isoptional[] = {true, false};
42         static const CommandInfo info = {3, paramnames, isoptional};
43         return &info;
44 }
45
46
47 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
48 {
49         docstring const temp = from_ascii("Hyperlink: ");
50
51         docstring url;
52
53         url += getParam("name");
54         if (url.empty())
55                 url += getParam("target");
56
57         // elide if long
58         if (url.length() > 30) {
59                 url = url.substr(0, 10) + "..."
60                         + url.substr(url.length() - 17, url.length());
61         }
62         return temp + url;
63 }
64
65
66 int InsetHyperlink::latex(Buffer const &, odocstream & os,
67                     OutputParams const & runparams) const
68 {
69         docstring url = getParam("target");
70         static docstring const backslash = from_ascii("\\");
71         static docstring const braces = from_ascii("{}");
72         static char_type const chars_url[2] = {'%', '#'};
73         static char_type const chars_name[6] = {
74                 '&', '_', '$', '%', '#', '^'};
75
76         // The characters in chars_url[] need to be changed to a command when
77         // they are in the url field.
78         if (!url.empty()) {
79                 // the chars_url[] characters must be handled for both, url and href
80                 for (int k = 0; k < 2; k++) {
81                         for (size_t i = 0, pos;
82                                 (pos = url.find(chars_url[k], i)) != string::npos;
83                                 i = pos + 2) {
84                                 url.replace(pos, 1, backslash + chars_url[k]);
85                         }
86                 }
87         } // end if (!url.empty())
88
89         docstring name = getParam("name");
90
91         // The characters in chars_name[] need to be changed to a command when
92         // they are in the name field.
93         if (!name.empty()) {
94
95                 // handle the "\" character, but only when the following character
96                 // is not also a "\", because "\\" is valid code
97                 docstring const textbackslash = from_ascii("\\textbackslash{}");
98                 for (size_t i = 0, pos;
99                         (pos = name.find('\\', i)) != string::npos;
100                         i = pos + 2) {
101                         if (name[pos + 1] != '\\')
102                                 name.replace(pos, 1, textbackslash);
103                 }
104                 for (int k = 0; k < 6; k++) {
105                         for (size_t i = 0, pos;
106                                 (pos = name.find(chars_name[k], i)) != string::npos;
107                                 i = pos + 2) {
108                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
109                         }
110                 }
111                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
112                 // for URLs
113                 docstring const sim = from_ascii("$\\sim$");
114                 for (size_t i = 0, pos;
115                         (pos = name.find('~', i)) != string::npos;
116                         i = pos + 1)
117                         name.replace(pos, 1, sim);
118
119         }  // end if (!name.empty())
120         
121         if (runparams.moving_arg)
122                 os << "\\protect";
123
124         //for the case there is no name given, the target is set as name
125         os << "\\href{" << getParam("type") << url << "}{"
126                 << (name.empty()? url : name) << '}';
127
128         return 0;
129 }
130
131
132 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
133                         OutputParams const &) const
134 {
135         odocstringstream oss;
136
137         oss << '[' << getParam("target");
138         if (getParam("name").empty())
139                 oss << ']';
140         else
141                 oss << "||" << getParam("name") << ']';
142
143         docstring const str = oss.str();
144         os << str;
145         return str.size();
146 }
147
148
149 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
150                       OutputParams const &) const
151 {
152         os << "<ulink url=\""
153            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
154            << "\">"
155            << getParam("name")
156            << "</ulink>";
157         return 0;
158 }
159
160
161 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
162                        OutputParams const & op) const
163 {
164         return plaintext(buf, os, op);
165 }
166
167
168 void InsetHyperlink::validate(LaTeXFeatures & features) const
169 {
170         features.require("hyperref");
171 }
172
173
174 } // namespace lyx