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"
15 #include <QtGui/QDesktopServices>
19 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "LaTeXFeatures.h"
25 #include "OutputParams.h"
26 #include "output_docbook.h"
27 #include "output_xhtml.h"
29 #include "texstream.h"
31 #include "support/docstream.h"
32 #include "support/FileName.h"
33 #include "support/filetools.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/qstring_helpers.h"
38 #include "frontends/alert.h"
41 using namespace lyx::support;
46 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
47 : InsetCommand(buf, p)
51 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
53 static ParamInfo param_info_;
54 if (param_info_.empty()) {
55 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
56 ParamInfo::HANDLING_LATEXIFY);
57 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
58 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
59 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
65 docstring InsetHyperlink::screenLabel() const
67 // TODO: replace with unicode hyperlink character = U+1F517
68 docstring const temp = _("Hyperlink: ");
72 url += getParam("name");
74 url += getParam("target");
76 // elide if long and no name was provided
77 if (url.length() > 30) {
78 docstring end = url.substr(url.length() - 17, url.length());
79 support::truncateWithEllipsis(url, 13);
83 // elide if long (approx number of chars in line of article class)
84 if (url.length() > 80) {
85 docstring end = url.substr(url.length() - 67, url.length());
86 support::truncateWithEllipsis(url, 13);
94 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
96 switch (cmd.action()) {
103 InsetCommand::doDispatch(cur, cmd);
109 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
110 FuncStatus & flag) const
112 switch (cmd.action()) {
113 case LFUN_INSET_EDIT: {
114 QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
115 bool url_valid = getParam("type").empty() && url.isValid();
117 flag.setEnabled(url_valid || getParam("type") == "file:");
122 return InsetCommand::getStatus(cur, cmd, flag);
127 void InsetHyperlink::viewTarget() const
129 if (getParam("type").empty()) { //==Web
130 QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
131 if (!QDesktopServices::openUrl(url))
132 LYXERR0("Unable to open URL!");
134 } else if (getParam("type") == "file:") {
135 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
136 string const format = theFormats().getFormatFromFile(url);
137 theFormats().view(buffer(), url, format);
142 void InsetHyperlink::latex(otexstream & os,
143 OutputParams const & runparams) const
145 docstring url = getParam("target");
146 docstring name = getParam("name");
147 static char_type const chars_url[2] = {'%', '#'};
149 // For the case there is no name given, the target is set as name.
150 // Do this before !url.empty() and !name.empty() to handle characters
151 // such as % correctly.
156 // Use URI/URL-style percent-encoded string (hexadecimal).
157 // We exclude some characters that must not be transformed
158 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
159 // or that we need to treat manually: \.
160 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
161 // We handle \ manually since \\ is valid
162 for (size_t i = 0, pos;
163 (pos = url.find('\\', i)) != string::npos;
165 if (url[pos + 1] != '\\')
166 url.replace(pos, 1, from_ascii("%5C"));
169 // The characters in chars_url[] need to be escaped in the url
170 // field because otherwise LaTeX will fail when the hyperlink is
171 // within an argument of another command, e.g. in a \footnote. It
172 // is important that they are escaped as "\#" and not as "\#{}".
173 // FIXME this is not necessary in outside of commands.
174 for (int k = 0; k < 2; k++)
175 for (size_t i = 0, pos;
176 (pos = url.find(chars_url[k], i)) != string::npos;
178 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
180 // add "http://" when the type is web (type = empty)
181 // and no "://" or "run:" is given
182 docstring type = getParam("type");
183 if (url.find(from_ascii("://")) == string::npos
184 && url.find(from_ascii("run:")) == string::npos
186 url = from_ascii("http://") + url;
188 } // end if (!url.empty())
191 name = params().prepareCommand(runparams, name,
192 ParamInfo::HANDLING_LATEXIFY);
193 // replace the tilde by the \sim character as suggested in the
194 // LaTeX FAQ for URLs
195 if (getParam("literal") != from_ascii("true")) {
196 docstring const sim = from_ascii("$\\sim$");
197 for (size_t i = 0, pos;
198 (pos = name.find('~', i)) != string::npos;
200 name.replace(pos, 1, sim);
204 if (runparams.moving_arg)
207 // output the ready \href command
208 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
212 int InsetHyperlink::plaintext(odocstringstream & os,
213 OutputParams const &, size_t) const
215 odocstringstream oss;
217 oss << '[' << getParam("target");
218 if (getParam("name").empty())
221 oss << "||" << getParam("name") << ']';
223 docstring const str = oss.str();
229 void InsetHyperlink::docbook(XMLStream & xs, OutputParams const &) const
231 xs << xml::StartTag("link", "xlink:href=\"" + subst(getParam("target"), from_ascii("&"), from_ascii("&")) + "\"");
232 xs << xml::escapeString(getParam("name"));
233 xs << xml::EndTag("link");
237 docstring InsetHyperlink::xhtml(XMLStream & xs, OutputParams const &) const
239 docstring const & target =
240 xml::escapeString(getParam("target"), XMLStream::ESCAPE_AND);
241 docstring const & name = getParam("name");
242 xs << xml::StartTag("a", to_utf8("href=\"" + target + "\""));
243 xs << (name.empty() ? target : name);
244 xs << xml::EndTag("a");
249 void InsetHyperlink::toString(odocstream & os) const
251 odocstringstream ods;
252 plaintext(ods, OutputParams(0), INT_MAX);
257 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
259 docstring const & n = getParam("name");
264 os += getParam("target");
268 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
270 docstring url = getParam("target");
271 docstring type = getParam("type");
272 docstring guitype = _("www");
273 if (type == "mailto:")
274 guitype = _("email");
275 else if (type == "file:")
277 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
281 void InsetHyperlink::validate(LaTeXFeatures & features) const
283 features.require("hyperref");
284 InsetCommand::validate(features);
288 string InsetHyperlink::contextMenuName() const
290 return "context-hyperlink";