]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
d1118d11662bf9426bd088d558ec0c9f20c1046b
[lyx.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 "OutputParams.h"
20
21 #include "support/docstream.h"
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29
30
31 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
32         : InsetCommand(p, "href")
33 {}
34
35
36 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
37 {
38         static ParamInfo param_info_;
39         if (param_info_.empty()) {
40                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
41                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
42                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
43         }
44         return param_info_;
45 }
46
47
48 docstring InsetHyperlink::screenLabel() const
49 {
50         docstring const temp = from_ascii("Hyperlink: ");
51
52         docstring url;
53
54         url += getParam("name");
55         if (url.empty())
56                 url += getParam("target");
57
58         // elide if long
59         if (url.length() > 30) {
60                 url = url.substr(0, 10) + "..."
61                         + url.substr(url.length() - 17, url.length());
62         }
63         return temp + url;
64 }
65
66
67 int InsetHyperlink::latex(odocstream & os, OutputParams const & runparams) const
68 {
69         docstring url = getParam("target");
70         docstring name = getParam("name");
71         static docstring const backslash = from_ascii("\\");
72         static docstring const braces = from_ascii("{}");
73         static char_type const chars_name[6] = {
74                 '&', '_', '$', '%', '#', '^'};
75
76         // For the case there is no name given, the target is set as name.
77         // Do this before !url.empty() and !name.empty() to handle characters
78         // like the "%" correctly.
79         if (name.empty())
80                 name = url;
81
82         // The characters in chars_url[] need to be changed to a command when
83         // they are in the url field.
84         if (!url.empty()) {
85                 // Replace the "\" character by its ASCII code according to the URL specifications
86                 // because "\" is not allowed in URLs and by \href. Only do this when the
87                 // following character is not also a "\", because "\\" is valid code
88                 for (size_t i = 0, pos;
89                         (pos = url.find('\\', i)) != string::npos;
90                         i = pos + 2) {
91                         if (url[pos + 1] != '\\')
92                                 url.replace(pos, 1, from_ascii("%5C"));
93                 }
94                 // "#" needs to be escapes to "\#", therefore the treatment
95                 // of "\" must be done before
96                 for (size_t i = 0, pos;
97                         (pos = url.find('#', i)) != string::npos;
98                         i = pos + 2) {
99                         if (url[pos + 1] != '\\')
100                                 url.replace(pos, 1, from_ascii("\\#"));
101                 }
102                 
103                 // add "http://" when the type is web (type = empty)
104                 // and no "://" or "run:" is given
105                 docstring type = getParam("type");
106                 if (url.find(from_ascii("://")) == string::npos
107                         && url.find(from_ascii("run:")) == string::npos
108                         && type.empty())
109                         url = from_ascii("http://") + url;
110
111         } // end if (!url.empty())
112
113         // The characters in chars_name[] need to be changed to a command when
114         // they are in the name field.
115         if (!name.empty()) {
116                 // handle the "\" character, but only when the following character
117                 // is not also a "\", because "\\" is valid code
118                 docstring const textbackslash = from_ascii("\\textbackslash{}");
119                 for (size_t i = 0, pos;
120                         (pos = name.find('\\', i)) != string::npos;
121                         i = pos + 2) {
122                         if (name[pos + 1] != '\\')
123                                 name.replace(pos, 1, textbackslash);
124                 }
125                 // The characters in chars_name[] need to be changed to a command when
126                 // they are in the name field.
127                 // Therefore the treatment of "\" must be the first thing
128                 for (int k = 0; k < 6; k++) {
129                         for (size_t i = 0, pos;
130                                 (pos = name.find(chars_name[k], i)) != string::npos;
131                                 i = pos + 2) {
132                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
133                         }
134                 }
135                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
136                 // for URLs
137                 docstring const sim = from_ascii("$\\sim$");
138                 for (size_t i = 0, pos;
139                         (pos = name.find('~', i)) != string::npos;
140                         i = pos + 1)
141                         name.replace(pos, 1, sim);
142
143         }  // end if (!name.empty())
144         
145         if (runparams.moving_arg)
146                 os << "\\protect";
147
148         // output the ready \href command
149         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
150
151         return 0;
152 }
153
154
155 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
156 {
157         odocstringstream oss;
158
159         oss << '[' << getParam("target");
160         if (getParam("name").empty())
161                 oss << ']';
162         else
163                 oss << "||" << getParam("name") << ']';
164
165         docstring const str = oss.str();
166         os << str;
167         return str.size();
168 }
169
170
171 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
172 {
173         os << "<ulink url=\""
174            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
175            << "\">"
176            << getParam("name")
177            << "</ulink>";
178         return 0;
179 }
180
181
182 void InsetHyperlink::tocString(odocstream & os) const
183 {
184         plaintext(os, OutputParams(0));
185 }
186
187
188 void InsetHyperlink::validate(LaTeXFeatures & features) const
189 {
190         features.require("hyperref");
191 }
192
193
194 } // namespace lyx