2 * \file InsetHyperlink.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
13 #include "InsetHyperlink.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "LaTeXFeatures.h"
20 #include "OutputParams.h"
21 #include "output_docbook.h"
22 #include "output_xhtml.h"
24 #include "texstream.h"
26 #include "support/debug.h"
27 #include "support/docstream.h"
28 #include "support/FileName.h"
29 #include "support/filetools.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/qstring_helpers.h"
34 #include "frontends/alert.h"
36 #include <QtGui/QDesktopServices>
40 using namespace lyx::support;
45 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
46 : InsetCommand(buf, p)
50 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
52 static ParamInfo param_info_;
53 if (param_info_.empty()) {
54 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
55 ParamInfo::HANDLING_LATEXIFY);
56 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
57 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
58 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
64 docstring InsetHyperlink::screenLabel() const
66 // TODO: replace with unicode hyperlink character = U+1F517
67 docstring const temp = _("Hyperlink: ");
71 url += getParam("name");
73 url += getParam("target");
75 // elide if long and no name was provided
76 if (url.length() > 30) {
77 docstring end = url.substr(url.length() - 17, url.length());
78 support::truncateWithEllipsis(url, 13);
82 // elide if long (approx number of chars in line of article class)
83 if (url.length() > 80) {
84 docstring end = url.substr(url.length() - 67, url.length());
85 support::truncateWithEllipsis(url, 13);
93 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
95 switch (cmd.action()) {
102 InsetCommand::doDispatch(cur, cmd);
108 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
109 FuncStatus & flag) const
111 switch (cmd.action()) {
112 case LFUN_INSET_EDIT: {
113 QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
114 bool url_valid = getParam("type").empty() && url.isValid();
116 flag.setEnabled(url_valid || getParam("type") == "file:");
121 return InsetCommand::getStatus(cur, cmd, flag);
126 void InsetHyperlink::viewTarget() const
128 if (getParam("type").empty()) { //==Web
129 QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
130 if (!QDesktopServices::openUrl(url))
131 LYXERR0("Unable to open URL!");
133 } else if (getParam("type") == "file:") {
134 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
135 string const format = theFormats().getFormatFromFile(url);
136 theFormats().view(buffer(), url, format);
141 void InsetHyperlink::latex(otexstream & os,
142 OutputParams const & runparams) const
144 docstring url = getParam("target");
145 docstring name = getParam("name");
146 static char_type const chars_url[2] = {'%', '#'};
148 // For the case there is no name given, the target is set as name.
149 // Do this before !url.empty() and !name.empty() to handle characters
150 // such as % correctly.
155 // Use URI/URL-style percent-encoded string (hexadecimal).
156 // We exclude some characters that must not be transformed
157 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
158 // or that we need to treat manually: \.
159 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
160 // We handle \ manually since \\ is valid
161 for (size_t i = 0, pos;
162 (pos = url.find('\\', i)) != string::npos;
164 if (url[pos + 1] != '\\')
165 url.replace(pos, 1, from_ascii("%5C"));
168 // The characters in chars_url[] need to be escaped in the url
169 // field because otherwise LaTeX will fail when the hyperlink is
170 // within an argument of another command, e.g. in a \footnote. It
171 // is important that they are escaped as "\#" and not as "\#{}".
172 // FIXME this is not necessary in outside of commands.
173 for (int k = 0; k < 2; k++)
174 for (size_t i = 0, pos;
175 (pos = url.find(chars_url[k], i)) != string::npos;
177 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
179 // add "http://" when the type is web (type = empty)
180 // and no "://" or "run:" is given
181 docstring type = getParam("type");
182 if (url.find(from_ascii("://")) == string::npos
183 && url.find(from_ascii("run:")) == string::npos
185 url = from_ascii("http://") + url;
187 } // end if (!url.empty())
190 name = params().prepareCommand(runparams, name,
191 ParamInfo::HANDLING_LATEXIFY);
192 // replace the tilde by the \sim character as suggested in the
193 // LaTeX FAQ for URLs
194 if (getParam("literal") != from_ascii("true")) {
195 docstring const sim = from_ascii("$\\sim$");
196 for (size_t i = 0, pos;
197 (pos = name.find('~', i)) != string::npos;
199 name.replace(pos, 1, sim);
203 if (runparams.moving_arg)
206 // output the ready \href command
207 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
211 int InsetHyperlink::plaintext(odocstringstream & os,
212 OutputParams const &, size_t) const
214 odocstringstream oss;
216 oss << '[' << getParam("target");
217 if (getParam("name").empty())
220 oss << "||" << getParam("name") << ']';
222 docstring const str = oss.str();
228 void InsetHyperlink::docbook(XMLStream & xs, OutputParams const &) const
230 xs << xml::StartTag("link", "xlink:href=\"" + subst(getParam("target"), from_ascii("&"), from_ascii("&")) + "\"");
231 xs << xml::escapeString(getParam("name"));
232 xs << xml::EndTag("link");
236 docstring InsetHyperlink::xhtml(XMLStream & xs, OutputParams const &) const
238 docstring const & target =
239 xml::escapeString(getParam("target"), XMLStream::ESCAPE_AND);
240 docstring const & name = getParam("name");
241 xs << xml::StartTag("a", to_utf8("href=\"" + target + "\""));
242 xs << (name.empty() ? target : name);
243 xs << xml::EndTag("a");
248 void InsetHyperlink::toString(odocstream & os) const
250 odocstringstream ods;
251 plaintext(ods, OutputParams(0), INT_MAX);
256 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
258 docstring const & n = getParam("name");
263 os += getParam("target");
267 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
269 docstring url = getParam("target");
270 docstring type = getParam("type");
271 docstring guitype = _("www");
272 if (type == "mailto:")
273 guitype = _("email");
274 else if (type == "file:")
276 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
280 void InsetHyperlink::validate(LaTeXFeatures & features) const
282 features.require("hyperref");
283 InsetCommand::validate(features);
287 string InsetHyperlink::contextMenuName() const
289 return "context-hyperlink";