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 <output_docbook.h>
15 #include "InsetHyperlink.h"
18 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
27 #include "texstream.h"
29 #include "support/docstream.h"
30 #include "support/FileName.h"
31 #include "support/filetools.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "frontends/alert.h"
38 using namespace lyx::support;
43 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
44 : InsetCommand(buf, p)
48 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
50 static ParamInfo param_info_;
51 if (param_info_.empty()) {
52 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
53 ParamInfo::HANDLING_LATEXIFY);
54 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
55 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
56 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
62 docstring InsetHyperlink::screenLabel() const
64 // TODO: replace with unicode hyperlink character = U+1F517
65 docstring const temp = _("Hyperlink: ");
69 url += getParam("name");
71 url += getParam("target");
73 // elide if long and no name was provided
74 if (url.length() > 30) {
75 docstring end = url.substr(url.length() - 17, url.length());
76 support::truncateWithEllipsis(url, 13);
80 // elide if long (approx number of chars in line of article class)
81 if (url.length() > 80) {
82 docstring end = url.substr(url.length() - 67, url.length());
83 support::truncateWithEllipsis(url, 13);
91 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
93 switch (cmd.action()) {
100 InsetCommand::doDispatch(cur, cmd);
106 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
107 FuncStatus & flag) const
109 switch (cmd.action()) {
110 case LFUN_INSET_EDIT:
111 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
115 return InsetCommand::getStatus(cur, cmd, flag);
120 void InsetHyperlink::viewTarget() const
122 if (getParam("type") == "file:") {
123 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
124 string const format = theFormats().getFormatFromFile(url);
125 theFormats().view(buffer(), url, format);
130 void InsetHyperlink::latex(otexstream & os,
131 OutputParams const & runparams) const
133 docstring url = getParam("target");
134 docstring name = getParam("name");
135 static char_type const chars_url[2] = {'%', '#'};
137 // For the case there is no name given, the target is set as name.
138 // Do this before !url.empty() and !name.empty() to handle characters
139 // such as % correctly.
144 // Use URI/URL-style percent-encoded string (hexadecimal).
145 // We exclude some characters that must not be transformed
146 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
147 // or that we need to treat manually: \.
148 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
149 // We handle \ manually since \\ is valid
150 for (size_t i = 0, pos;
151 (pos = url.find('\\', i)) != string::npos;
153 if (url[pos + 1] != '\\')
154 url.replace(pos, 1, from_ascii("%5C"));
157 // The characters in chars_url[] need to be escaped in the url
158 // field because otherwise LaTeX will fail when the hyperlink is
159 // within an argument of another command, e.g. in a \footnote. It
160 // is important that they are escaped as "\#" and not as "\#{}".
161 // FIXME this is not necessary in outside of commands.
162 for (int k = 0; k < 2; k++)
163 for (size_t i = 0, pos;
164 (pos = url.find(chars_url[k], i)) != string::npos;
166 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
168 // add "http://" when the type is web (type = empty)
169 // and no "://" or "run:" is given
170 docstring type = getParam("type");
171 if (url.find(from_ascii("://")) == string::npos
172 && url.find(from_ascii("run:")) == string::npos
174 url = from_ascii("http://") + url;
176 } // end if (!url.empty())
179 name = params().prepareCommand(runparams, name,
180 ParamInfo::HANDLING_LATEXIFY);
181 // replace the tilde by the \sim character as suggested in the
182 // LaTeX FAQ for URLs
183 if (getParam("literal") != from_ascii("true")) {
184 docstring const sim = from_ascii("$\\sim$");
185 for (size_t i = 0, pos;
186 (pos = name.find('~', i)) != string::npos;
188 name.replace(pos, 1, sim);
192 if (runparams.moving_arg)
195 // output the ready \href command
196 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
200 int InsetHyperlink::plaintext(odocstringstream & os,
201 OutputParams const &, size_t) const
203 odocstringstream oss;
205 oss << '[' << getParam("target");
206 if (getParam("name").empty())
209 oss << "||" << getParam("name") << ']';
211 docstring const str = oss.str();
217 void InsetHyperlink::docbook(XMLStream & xs, OutputParams const &) const
219 xs << xml::StartTag("link", "xlink:href=\"" + subst(getParam("target"), from_ascii("&"), from_ascii("&")) + "\"");
220 xs << xml::escapeString(getParam("name"));
221 xs << xml::EndTag("link");
225 docstring InsetHyperlink::xhtml(XMLStream & xs, OutputParams const &) const
227 docstring const & target =
228 xml::xmlize(getParam("target"), XMLStream::ESCAPE_AND);
229 docstring const & name = getParam("name");
230 xs << xml::StartTag("a", to_utf8("href=\"" + target + "\""));
231 xs << (name.empty() ? target : name);
232 xs << xml::EndTag("a");
237 void InsetHyperlink::toString(odocstream & os) const
239 odocstringstream ods;
240 plaintext(ods, OutputParams(0), INT_MAX);
245 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
247 docstring const & n = getParam("name");
252 os += getParam("target");
256 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
258 docstring url = getParam("target");
259 docstring type = getParam("type");
260 docstring guitype = _("www");
261 if (type == "mailto:")
262 guitype = _("email");
263 else if (type == "file:")
265 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
269 void InsetHyperlink::validate(LaTeXFeatures & features) const
271 features.require("hyperref");
272 InsetCommand::validate(features);
276 string InsetHyperlink::contextMenuName() const
278 return "context-hyperlink";