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