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 "output_docbook.h"
21 #include "output_xhtml.h"
23 #include "texstream.h"
25 #include "support/debug.h"
26 #include "support/docstream.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/qstring_helpers.h"
33 #include "frontends/alert.h"
35 #include <QtGui/QDesktopServices>
39 using namespace lyx::support;
44 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
45 : InsetCommand(buf, p)
49 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
51 static ParamInfo param_info_;
52 if (param_info_.empty()) {
53 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
54 ParamInfo::HANDLING_LATEXIFY);
55 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
56 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
57 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
63 docstring InsetHyperlink::screenLabel() const
65 // TODO: replace with unicode hyperlink character = U+1F517
66 docstring const temp = _("Hyperlink: ");
70 url += getParam("name");
72 url += getParam("target");
74 // elide if long and no name was provided
75 if (url.length() > 30) {
76 docstring end = url.substr(url.length() - 17, url.length());
77 support::truncateWithEllipsis(url, 13);
81 // elide if long (approx number of chars in line of article class)
82 if (url.length() > 80) {
83 docstring end = url.substr(url.length() - 67, url.length());
84 support::truncateWithEllipsis(url, 13);
92 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
94 switch (cmd.action()) {
101 InsetCommand::doDispatch(cur, cmd);
107 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
108 FuncStatus & flag) const
110 switch (cmd.action()) {
111 case LFUN_INSET_EDIT: {
112 docstring const & utype = getParam("type");
113 QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
114 bool url_valid = utype.empty() && url.isValid();
115 flag.setEnabled(url_valid || utype == "file:");
120 return InsetCommand::getStatus(cur, cmd, flag);
125 void InsetHyperlink::viewTarget() const
127 if (getParam("type").empty()) { //==Web
128 QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
129 if (!QDesktopServices::openUrl(url))
130 LYXERR0("Unable to open URL!");
131 } else if (getParam("type") == "file:") {
132 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
133 string const format = theFormats().getFormatFromFile(url);
134 theFormats().view(buffer(), url, format);
139 docstring makeURL(docstring const & url, docstring const & type) {
140 if (type == "other" ||
141 (!type.empty() && url.find(type) == 0))
147 void InsetHyperlink::latex(otexstream & os,
148 OutputParams const & runparams) const
150 docstring url = getParam("target");
151 docstring name = getParam("name");
152 docstring const & utype = getParam("type");
153 static char_type const chars_url[2] = {'%', '#'};
155 // For the case there is no name given, the target is set as name.
156 // Do this before !url.empty() and !name.empty() to handle characters
157 // such as % correctly.
162 // Use URI/URL-style percent-encoded string (hexadecimal).
163 // We exclude some characters that must not be transformed
164 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
165 // or that we need to treat manually: \.
166 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
167 // We handle \ manually since \\ is valid
168 for (size_t i = 0, pos;
169 (pos = url.find('\\', i)) != string::npos;
171 if (url[pos + 1] != '\\')
172 url.replace(pos, 1, from_ascii("%5C"));
175 // The characters in chars_url[] need to be escaped in the url
176 // field because otherwise LaTeX will fail when the hyperlink is
177 // within an argument of another command, e.g. in a \footnote. It
178 // is important that they are escaped as "\#" and not as "\#{}".
179 // FIXME this is not necessary in outside of commands.
180 for (int k = 0; k < 2; k++)
181 for (size_t i = 0, pos;
182 (pos = url.find(chars_url[k], i)) != string::npos;
184 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
186 // add "http://" when the type is web (type = empty)
187 // and no "://" is given
188 if (url.find(from_ascii("://")) == string::npos
190 url = from_ascii("http://") + url;
192 } // end if (!url.empty())
195 name = params().prepareCommand(runparams, name,
196 ParamInfo::HANDLING_LATEXIFY);
197 // replace the tilde by the \sim character as suggested in the
198 // LaTeX FAQ for URLs
199 if (getParam("literal") != "true") {
200 docstring const sim = from_ascii("$\\sim$");
201 for (size_t i = 0, pos;
202 (pos = name.find('~', i)) != string::npos;
204 name.replace(pos, 1, sim);
208 if (runparams.moving_arg)
211 // output the ready \href command
212 os << "\\href{" << makeURL(url, utype) << "}{" << name << '}';
216 int InsetHyperlink::plaintext(odocstringstream & os,
217 OutputParams const &, size_t) const
219 odocstringstream oss;
221 oss << '[' << getParam("target");
222 if (getParam("name").empty())
225 oss << "||" << getParam("name") << ']';
227 docstring const str = oss.str();
233 void InsetHyperlink::docbook(XMLStream & xs, OutputParams const &) const
235 docstring target = subst(getParam("target"), from_ascii("&"), from_ascii("&")) ;
236 xs << xml::StartTag("link", "xlink:href=\"" + makeURL(target, getParam("type")) + "\"");
237 xs << xml::escapeString(getParam("name"));
238 xs << xml::EndTag("link");
242 docstring InsetHyperlink::xhtml(XMLStream & xs, OutputParams const &) const
244 docstring const & target =
245 xml::escapeString(getParam("target"), XMLStream::ESCAPE_AND);
246 docstring const & name = getParam("name");
247 xs << xml::StartTag("a", to_utf8("href=\"" + makeURL(target, getParam("type")) + "\""));
248 xs << (name.empty() ? target : name);
249 xs << xml::EndTag("a");
254 void InsetHyperlink::toString(odocstream & os) const
256 odocstringstream ods;
257 plaintext(ods, OutputParams(0), INT_MAX);
262 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
264 docstring const & n = getParam("name");
269 os += getParam("target");
273 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
275 docstring const & url = getParam("target");
276 docstring const & type = getParam("type");
277 docstring guitype = _("www");
278 if (type == "mailto:")
279 guitype = _("email");
280 else if (type == "file:")
282 else if (type == "other")
283 guitype = _("other[[Hyperlink Type]]");
284 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
288 void InsetHyperlink::validate(LaTeXFeatures & features) const
290 features.require("hyperref");
291 InsetCommand::validate(features);
295 pair<int, int> InsetHyperlink::isWords() const
297 docstring const label = getParam("name");
298 return pair<int, int>(label.size(), wordCount(label));
302 string InsetHyperlink::contextMenuName() const
304 return "context-hyperlink";