]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
InsetHyperlink: Fix replacement of empty name with target
[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 "Buffer.h"
17 #include "DispatchResult.h"
18 #include "Encoding.h"
19 #include "Format.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "LaTeXFeatures.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
25 #include "sgml.h"
26 #include "texstream.h"
27
28 #include "support/docstream.h"
29 #include "support/FileName.h"
30 #include "support/filetools.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33
34 #include "frontends/alert.h"
35
36 using namespace std;
37 using namespace lyx::support;
38
39 namespace lyx {
40
41
42 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
43         : InsetCommand(buf, p)
44 {}
45
46
47 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
48 {
49         static ParamInfo param_info_;
50         if (param_info_.empty()) {
51                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
52                                 ParamInfo::HANDLING_LATEXIFY);
53                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
54                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
55                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
56         }
57         return param_info_;
58 }
59
60
61 docstring InsetHyperlink::screenLabel() const
62 {
63         docstring const temp = _("Hyperlink: ");
64
65         docstring url;
66
67         url += getParam("name");
68         if (url.empty())
69                 url += getParam("target");
70
71         // elide if long
72         if (url.length() > 30) {
73                 docstring end = url.substr(url.length() - 17, url.length());
74                 support::truncateWithEllipsis(url, 13);
75                 url += end;
76         }
77         return temp + url;
78 }
79
80 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
81 {
82         switch (cmd.action()) {
83
84         case LFUN_INSET_EDIT:
85                 viewTarget();
86                 break;
87
88         default:
89                 InsetCommand::doDispatch(cur, cmd);
90                 break;
91         }
92 }
93
94
95 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
96                 FuncStatus & flag) const
97 {
98         switch (cmd.action()) {
99         case LFUN_INSET_EDIT:
100                 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
101                 return true;
102
103         default:
104                 return InsetCommand::getStatus(cur, cmd, flag);
105         }
106 }
107
108
109 void InsetHyperlink::viewTarget() const
110 {
111         if (getParam("type") == "file:") {
112                 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
113                 string const format = formats.getFormatFromFile(url);
114                 formats.view(buffer(), url, format);
115         }
116 }
117
118
119 void InsetHyperlink::latex(otexstream & os,
120                            OutputParams const & runparams) const
121 {
122         docstring url = getParam("target");
123         docstring name = getParam("name");
124         static char_type const chars_url[2] = {'%', '#'};
125
126         // For the case there is no name given, the target is set as name.
127         // Do this before !url.empty() and !name.empty() to handle characters
128         // like the "%" correctly.
129         if (name.empty())
130                 name = url;
131
132         if (!url.empty()) {
133                 // Replace the "\" character by its ASCII code according to the
134                 // URL specifications because "\" is not allowed in URLs and by
135                 // \href. Only do this when the following character is not also
136                 // a "\", because "\\" is valid code
137                 for (size_t i = 0, pos;
138                         (pos = url.find('\\', i)) != string::npos;
139                         i = pos + 2) {
140                         if (url[pos + 1] != '\\')
141                                 url.replace(pos, 1, from_ascii("%5C"));
142                 }
143
144                 // The characters in chars_url[] need to be escaped in the url
145                 // field because otherwise LaTeX will fail when the hyperlink is
146                 // within an argument of another command, e.g. in a \footnote. It
147                 // is important that they are escaped as "\#" and not as "\#{}".
148                 for (int k = 0; k < 2; k++)
149                         for (size_t i = 0, pos;
150                                 (pos = url.find(chars_url[k], i)) != string::npos;
151                                 i = pos + 2)
152                                 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
153                 
154                 // add "http://" when the type is web (type = empty)
155                 // and no "://" or "run:" is given
156                 docstring type = getParam("type");
157                 if (url.find(from_ascii("://")) == string::npos
158                         && url.find(from_ascii("run:")) == string::npos
159                         && type.empty())
160                         url = from_ascii("http://") + url;
161
162         } // end if (!url.empty())
163
164         if (!name.empty()) {
165                 name = params().prepareCommand(runparams, name,
166                                         ParamInfo::HANDLING_LATEXIFY);
167                 // replace the tilde by the \sim character as suggested in the
168                 // LaTeX FAQ for URLs
169                 if (getParam("literal") != from_ascii("true")) {
170                         docstring const sim = from_ascii("$\\sim$");
171                         for (size_t i = 0, pos;
172                                 (pos = name.find('~', i)) != string::npos;
173                                 i = pos + 1)
174                                 name.replace(pos, 1, sim);
175                 }
176         }
177         
178         if (runparams.moving_arg)
179                 os << "\\protect";
180
181         // output the ready \href command
182         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
183 }
184
185
186 int InsetHyperlink::plaintext(odocstringstream & os,
187         OutputParams const &, size_t) const
188 {
189         odocstringstream oss;
190
191         oss << '[' << getParam("target");
192         if (getParam("name").empty())
193                 oss << ']';
194         else
195                 oss << "||" << getParam("name") << ']';
196
197         docstring const str = oss.str();
198         os << str;
199         return str.size();
200 }
201
202
203 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
204 {
205         os << "<ulink url=\""
206            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
207            << "\">"
208            << sgml::escapeString(getParam("name"))
209            << "</ulink>";
210         return 0;
211 }
212
213
214 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
215 {
216         docstring const & target = 
217                 html::htmlize(getParam("target"), XHTMLStream::ESCAPE_AND);
218         docstring const & name   = getParam("name");
219         xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
220         xs << (name.empty() ? target : name);
221         xs << html::EndTag("a");
222         return docstring();
223 }
224
225
226 void InsetHyperlink::toString(odocstream & os) const
227 {
228         odocstringstream ods;
229         plaintext(ods, OutputParams(0), INT_MAX);
230         os << ods.str();
231 }
232
233
234 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
235 {
236         docstring const & n = getParam("name");
237         if (!n.empty()) {
238                 os += n;
239                 return;
240         }
241         os += getParam("target");
242 }
243
244
245 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
246 {
247         docstring url = getParam("target");
248         docstring type = getParam("type");
249         docstring guitype = _("www");
250         if (type == "mailto:")
251                 guitype = _("email");
252         else if (type == "file:")
253                 guitype = _("file");
254         return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
255 }
256
257
258 void InsetHyperlink::validate(LaTeXFeatures & features) const
259 {
260         features.require("hyperref");
261         InsetCommand::validate(features);
262 }
263
264
265 string InsetHyperlink::contextMenuName() const
266 {
267         return "context-hyperlink";
268 }
269
270
271 } // namespace lyx