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.
14 #include "InsetHyperlink.h"
17 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "LaTeXFeatures.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
26 #include "texstream.h"
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"
34 #include "frontends/alert.h"
37 using namespace lyx::support;
42 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
43 : InsetCommand(buf, p)
47 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
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);
61 docstring InsetHyperlink::screenLabel() const
63 // TODO: replace with unicode hyperlink character = U+1F517
64 docstring const temp = _("Hyperlink: ");
68 url += getParam("name");
70 url += getParam("target");
72 // elide if long and no name was provided
73 if (url.length() > 30) {
74 docstring end = url.substr(url.length() - 17, url.length());
75 support::truncateWithEllipsis(url, 13);
79 // elide if long (approx number of chars in line of article class)
80 if (url.length() > 80) {
81 docstring end = url.substr(url.length() - 67, url.length());
82 support::truncateWithEllipsis(url, 13);
90 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
92 switch (cmd.action()) {
99 InsetCommand::doDispatch(cur, cmd);
105 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
106 FuncStatus & flag) const
108 switch (cmd.action()) {
109 case LFUN_INSET_EDIT:
110 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
114 return InsetCommand::getStatus(cur, cmd, flag);
119 void InsetHyperlink::viewTarget() const
121 if (getParam("type") == "file:") {
122 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
123 string const format = theFormats().getFormatFromFile(url);
124 theFormats().view(buffer(), url, format);
129 void InsetHyperlink::latex(otexstream & os,
130 OutputParams const & runparams) const
132 docstring url = getParam("target");
133 docstring name = getParam("name");
134 static char_type const chars_url[2] = {'%', '#'};
136 // For the case there is no name given, the target is set as name.
137 // Do this before !url.empty() and !name.empty() to handle characters
138 // such as % correctly.
143 // Use URI/URL-style percent-encoded string (hexadecimal).
144 // We exclude some characters that must not be transformed
145 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
146 // or that we need to treat manually: \.
147 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
148 // We handle \ manually since \\ is valid
149 for (size_t i = 0, pos;
150 (pos = url.find('\\', i)) != string::npos;
152 if (url[pos + 1] != '\\')
153 url.replace(pos, 1, from_ascii("%5C"));
156 // The characters in chars_url[] need to be escaped in the url
157 // field because otherwise LaTeX will fail when the hyperlink is
158 // within an argument of another command, e.g. in a \footnote. It
159 // is important that they are escaped as "\#" and not as "\#{}".
160 // FIXME this is not necessary in outside of commands.
161 for (int k = 0; k < 2; k++)
162 for (size_t i = 0, pos;
163 (pos = url.find(chars_url[k], i)) != string::npos;
165 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
167 // add "http://" when the type is web (type = empty)
168 // and no "://" or "run:" is given
169 docstring type = getParam("type");
170 if (url.find(from_ascii("://")) == string::npos
171 && url.find(from_ascii("run:")) == string::npos
173 url = from_ascii("http://") + url;
175 } // end if (!url.empty())
178 name = params().prepareCommand(runparams, name,
179 ParamInfo::HANDLING_LATEXIFY);
180 // replace the tilde by the \sim character as suggested in the
181 // LaTeX FAQ for URLs
182 if (getParam("literal") != from_ascii("true")) {
183 docstring const sim = from_ascii("$\\sim$");
184 for (size_t i = 0, pos;
185 (pos = name.find('~', i)) != string::npos;
187 name.replace(pos, 1, sim);
191 if (runparams.moving_arg)
194 // output the ready \href command
195 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
199 int InsetHyperlink::plaintext(odocstringstream & os,
200 OutputParams const &, size_t) const
202 odocstringstream oss;
204 oss << '[' << getParam("target");
205 if (getParam("name").empty())
208 oss << "||" << getParam("name") << ']';
210 docstring const str = oss.str();
216 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
218 os << "<ulink url=\""
219 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
221 << sgml::escapeString(getParam("name"))
227 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
229 docstring const & target =
230 html::htmlize(getParam("target"), XHTMLStream::ESCAPE_AND);
231 docstring const & name = getParam("name");
232 xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
233 xs << (name.empty() ? target : name);
234 xs << html::EndTag("a");
239 void InsetHyperlink::toString(odocstream & os) const
241 odocstringstream ods;
242 plaintext(ods, OutputParams(0), INT_MAX);
247 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
249 docstring const & n = getParam("name");
254 os += getParam("target");
258 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
260 docstring url = getParam("target");
261 docstring type = getParam("type");
262 docstring guitype = _("www");
263 if (type == "mailto:")
264 guitype = _("email");
265 else if (type == "file:")
267 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
271 void InsetHyperlink::validate(LaTeXFeatures & features) const
273 features.require("hyperref");
274 InsetCommand::validate(features);
278 string InsetHyperlink::contextMenuName() const
280 return "context-hyperlink";