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"
25 #include "texstream.h"
27 #include "support/docstream.h"
28 #include "support/FileName.h"
29 #include "support/filetools.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include "frontends/alert.h"
36 using namespace lyx::support;
41 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
42 : InsetCommand(buf, p)
46 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
48 static ParamInfo param_info_;
49 if (param_info_.empty()) {
50 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
51 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
52 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
58 docstring InsetHyperlink::screenLabel() const
60 docstring const temp = _("Hyperlink: ");
64 url += getParam("name");
66 url += getParam("target");
69 if (url.length() > 30) {
70 docstring end = url.substr(url.length() - 17, url.length());
71 support::truncateWithEllipsis(url, 13);
77 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
79 switch (cmd.action()) {
86 InsetCommand::doDispatch(cur, cmd);
92 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
93 FuncStatus & flag) const
95 switch (cmd.action()) {
97 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
101 return InsetCommand::getStatus(cur, cmd, flag);
106 void InsetHyperlink::viewTarget() const
108 if (getParam("type") == "file:") {
109 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
110 string const format = formats.getFormatFromFile(url);
111 formats.view(buffer(), url, format);
116 void InsetHyperlink::latex(otexstream & os,
117 OutputParams const & runparams) const
119 docstring url = getParam("target");
120 docstring name = getParam("name");
121 static docstring const backslash = from_ascii("\\");
122 static docstring const braces = from_ascii("{}");
123 static char_type const chars_url[2] = {'%', '#'};
124 static char_type const chars_name[6] = {
125 '&', '_', '$', '%', '#', '^'};
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 // like the "%" correctly.
134 // Replace the "\" character by its ASCII code according to the
135 // URL specifications because "\" is not allowed in URLs and by
136 // \href. Only do this when the following character is not also
137 // a "\", because "\\" is valid code
138 for (size_t i = 0, pos;
139 (pos = url.find('\\', i)) != string::npos;
141 if (url[pos + 1] != '\\')
142 url.replace(pos, 1, from_ascii("%5C"));
145 // The characters in chars_url[] need to be escaped in the url
146 // field because otherwise LaTeX will fail when the hyperlink is
147 // within an argument of another command, e.g. in a \footnote. It
148 // is important that they are escaped as "\#" and not as "\#{}".
149 for (int k = 0; k < 2; k++)
150 for (size_t i = 0, pos;
151 (pos = url.find(chars_url[k], i)) != string::npos;
153 url.replace(pos, 1, backslash + chars_url[k]);
155 // add "http://" when the type is web (type = empty)
156 // and no "://" or "run:" is given
157 docstring type = getParam("type");
158 if (url.find(from_ascii("://")) == string::npos
159 && url.find(from_ascii("run:")) == string::npos
161 url = from_ascii("http://") + url;
163 } // end if (!url.empty())
165 // The characters in chars_name[] need to be changed to a command when
166 // they are in the name field.
168 // handle the "\" character, but only when the following character
169 // is not also a "\", because "\\" is valid code
170 docstring const textbackslash = from_ascii("\\textbackslash{}");
171 for (size_t i = 0, pos;
172 (pos = name.find('\\', i)) != string::npos;
174 if (name[pos + 1] != '\\')
175 name.replace(pos, 1, textbackslash);
177 // The characters in chars_name[] need to be changed to a command
178 // when they are in the name field.
179 // Therefore the treatment of "\" must be the first thing
180 for (int k = 0; k < 6; k++)
181 for (size_t i = 0, pos;
182 (pos = name.find(chars_name[k], i)) != string::npos;
184 name.replace(pos, 1, backslash + chars_name[k] + braces);
186 // replace the tilde by the \sim character as suggested in the
187 // LaTeX FAQ for URLs
188 docstring const sim = from_ascii("$\\sim$");
189 for (size_t i = 0, pos;
190 (pos = name.find('~', i)) != string::npos;
192 name.replace(pos, 1, sim);
193 pair<docstring, docstring> name_latexed =
194 runparams.encoding->latexString(name, runparams.dryrun);
195 name = name_latexed.first;
196 if (!name_latexed.second.empty() && !runparams.silent) {
197 // issue a warning about omitted characters
198 // FIXME: should be passed to the error dialog
199 frontend::Alert::warning(_("Uncodable characters"),
200 bformat(_("The following characters that are used in the href inset are not\n"
201 "representable in the current encoding and therefore have been omitted:\n%1$s."),
202 name_latexed.second));
204 } // end if (!name.empty())
206 if (runparams.moving_arg)
209 // output the ready \href command
210 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
214 int InsetHyperlink::plaintext(odocstringstream & os,
215 OutputParams const &, size_t) const
217 odocstringstream oss;
219 oss << '[' << getParam("target");
220 if (getParam("name").empty())
223 oss << "||" << getParam("name") << ']';
225 docstring const str = oss.str();
231 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
233 os << "<ulink url=\""
234 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
242 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
244 docstring const & target =
245 html::htmlize(getParam("target"), XHTMLStream::ESCAPE_AND);
246 docstring const & name = getParam("name");
247 xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
248 xs << (name.empty() ? target : name);
249 xs << html::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 url = getParam("target");
276 docstring type = getParam("type");
277 docstring guitype = _("www");
278 if (type == "mailto:")
279 guitype = _("email");
280 else if (type == "file:")
282 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
286 void InsetHyperlink::validate(LaTeXFeatures & features) const
288 features.require("hyperref");
292 string InsetHyperlink::contextMenuName() const
294 return "context-hyperlink";