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 docstring const temp = _("Hyperlink: ");
67 url += getParam("name");
69 url += getParam("target");
72 if (url.length() > 30) {
73 docstring end = url.substr(url.length() - 17, url.length());
74 support::truncateWithEllipsis(url, 13);
81 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
83 switch (cmd.action()) {
90 InsetCommand::doDispatch(cur, cmd);
96 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
97 FuncStatus & flag) const
99 switch (cmd.action()) {
100 case LFUN_INSET_EDIT:
101 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
105 return InsetCommand::getStatus(cur, cmd, flag);
110 void InsetHyperlink::viewTarget() const
112 if (getParam("type") == "file:") {
113 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
114 string const format = theFormats().getFormatFromFile(url);
115 theFormats().view(buffer(), url, format);
120 void InsetHyperlink::latex(otexstream & os,
121 OutputParams const & runparams) const
123 docstring url = getParam("target");
124 docstring name = getParam("name");
125 static char_type const chars_url[2] = {'%', '#'};
127 // For the case there is no name given, the target is set as name.
128 // Do this before !url.empty() and !name.empty() to handle characters
129 // such as % correctly.
134 // Use URI/URL-style percent-encoded string (hexadecimal).
135 // We exclude some characters that must not be transformed
136 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
137 // or that we need to treat manually: \.
138 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
139 // We handle \ manually since \\ is valid
140 for (size_t i = 0, pos;
141 (pos = url.find('\\', i)) != string::npos;
143 if (url[pos + 1] != '\\')
144 url.replace(pos, 1, from_ascii("%5C"));
147 // The characters in chars_url[] need to be escaped in the url
148 // field because otherwise LaTeX will fail when the hyperlink is
149 // within an argument of another command, e.g. in a \footnote. It
150 // is important that they are escaped as "\#" and not as "\#{}".
151 // FIXME this is not necessary in outside of commands.
152 for (int k = 0; k < 2; k++)
153 for (size_t i = 0, pos;
154 (pos = url.find(chars_url[k], i)) != string::npos;
156 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
158 // add "http://" when the type is web (type = empty)
159 // and no "://" or "run:" is given
160 docstring type = getParam("type");
161 if (url.find(from_ascii("://")) == string::npos
162 && url.find(from_ascii("run:")) == string::npos
164 url = from_ascii("http://") + url;
166 } // end if (!url.empty())
169 name = params().prepareCommand(runparams, name,
170 ParamInfo::HANDLING_LATEXIFY);
171 // replace the tilde by the \sim character as suggested in the
172 // LaTeX FAQ for URLs
173 if (getParam("literal") != from_ascii("true")) {
174 docstring const sim = from_ascii("$\\sim$");
175 for (size_t i = 0, pos;
176 (pos = name.find('~', i)) != string::npos;
178 name.replace(pos, 1, sim);
182 if (runparams.moving_arg)
185 // output the ready \href command
186 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
190 int InsetHyperlink::plaintext(odocstringstream & os,
191 OutputParams const &, size_t) const
193 odocstringstream oss;
195 oss << '[' << getParam("target");
196 if (getParam("name").empty())
199 oss << "||" << getParam("name") << ']';
201 docstring const str = oss.str();
207 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
209 os << "<ulink url=\""
210 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
212 << sgml::escapeString(getParam("name"))
218 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
220 docstring const & target =
221 html::htmlize(getParam("target"), XHTMLStream::ESCAPE_AND);
222 docstring const & name = getParam("name");
223 xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
224 xs << (name.empty() ? target : name);
225 xs << html::EndTag("a");
230 void InsetHyperlink::toString(odocstream & os) const
232 odocstringstream ods;
233 plaintext(ods, OutputParams(0), INT_MAX);
238 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
240 docstring const & n = getParam("name");
245 os += getParam("target");
249 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
251 docstring url = getParam("target");
252 docstring type = getParam("type");
253 docstring guitype = _("www");
254 if (type == "mailto:")
255 guitype = _("email");
256 else if (type == "file:")
258 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
262 void InsetHyperlink::validate(LaTeXFeatures & features) const
264 features.require("hyperref");
265 InsetCommand::validate(features);
269 string InsetHyperlink::contextMenuName() const
271 return "context-hyperlink";