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"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "LaTeXFeatures.h"
22 #include "OutputParams.h"
23 #include "output_xhtml.h"
25 #include "support/docstream.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
32 using namespace lyx::support;
37 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
38 : InsetCommand(buf, p)
42 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
44 static ParamInfo param_info_;
45 if (param_info_.empty()) {
46 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
47 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
48 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
54 docstring InsetHyperlink::screenLabel() const
56 docstring const temp = from_ascii("Hyperlink: ");
60 url += getParam("name");
62 url += getParam("target");
65 if (url.length() > 30) {
66 url = url.substr(0, 10) + "..."
67 + url.substr(url.length() - 17, url.length());
72 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
74 switch (cmd.action()) {
81 InsetCommand::doDispatch(cur, cmd);
87 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
88 FuncStatus & flag) const
90 switch (cmd.action()) {
92 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
96 return InsetCommand::getStatus(cur, cmd, flag);
101 void InsetHyperlink::viewTarget() const
103 if (getParam("type").empty())
104 formats.viewURL(getParam("target"));
106 else if (getParam("type") == "file:") {
107 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
108 string const format = formats.getFormatFromFile(url);
109 formats.view(buffer(), url, format);
114 int InsetHyperlink::latex(odocstream & os,
115 OutputParams const & runparams) const
117 docstring url = getParam("target");
118 docstring name = getParam("name");
119 static docstring const backslash = from_ascii("\\");
120 static docstring const braces = from_ascii("{}");
121 static char_type const chars_url[2] = {'%', '#'};
122 static char_type const chars_name[6] = {
123 '&', '_', '$', '%', '#', '^'};
125 // For the case there is no name given, the target is set as name.
126 // Do this before !url.empty() and !name.empty() to handle characters
127 // like the "%" correctly.
132 // Replace the "\" character by its ASCII code according to the
133 // URL specifications because "\" is not allowed in URLs and by
134 // \href. Only do this when the following character is not also
135 // a "\", because "\\" is valid code
136 for (size_t i = 0, pos;
137 (pos = url.find('\\', i)) != string::npos;
139 if (url[pos + 1] != '\\')
140 url.replace(pos, 1, from_ascii("%5C"));
143 // The characters in chars_url[] need to be escaped in the url
144 // field because otherwise LaTeX will fail when the hyperlink is
145 // within an argument of another command, e.g. in a \footnote. It
146 // is important that they are escaped as "\#" and not as "\#{}".
147 for (int k = 0; k < 2; k++)
148 for (size_t i = 0, pos;
149 (pos = url.find(chars_url[k], i)) != string::npos;
151 url.replace(pos, 1, backslash + chars_url[k]);
153 // add "http://" when the type is web (type = empty)
154 // and no "://" or "run:" is given
155 docstring type = getParam("type");
156 if (url.find(from_ascii("://")) == string::npos
157 && url.find(from_ascii("run:")) == string::npos
159 url = from_ascii("http://") + url;
161 } // end if (!url.empty())
163 // The characters in chars_name[] need to be changed to a command when
164 // they are in the name field.
166 // handle the "\" character, but only when the following character
167 // is not also a "\", because "\\" is valid code
168 docstring const textbackslash = from_ascii("\\textbackslash{}");
169 for (size_t i = 0, pos;
170 (pos = name.find('\\', i)) != string::npos;
172 if (name[pos + 1] != '\\')
173 name.replace(pos, 1, textbackslash);
175 // The characters in chars_name[] need to be changed to a command
176 // when they are in the name field.
177 // Therefore the treatment of "\" must be the first thing
178 for (int k = 0; k < 6; k++)
179 for (size_t i = 0, pos;
180 (pos = name.find(chars_name[k], i)) != string::npos;
182 name.replace(pos, 1, backslash + chars_name[k] + braces);
184 // replace the tilde by the \sim character as suggested in the
185 // LaTeX FAQ for URLs
186 docstring const sim = from_ascii("$\\sim$");
187 for (size_t i = 0, pos;
188 (pos = name.find('~', i)) != string::npos;
190 name.replace(pos, 1, sim);
192 } // end if (!name.empty())
194 if (runparams.moving_arg)
197 // output the ready \href command
198 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
204 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
206 odocstringstream oss;
208 oss << '[' << getParam("target");
209 if (getParam("name").empty())
212 oss << "||" << getParam("name") << ']';
214 docstring const str = oss.str();
220 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
222 os << "<ulink url=\""
223 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
231 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
233 docstring const & target = getParam("target");
234 docstring const & name = getParam("name");
235 xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
236 xs << (name.empty() ? target : name);
237 xs << html::EndTag("a");
242 void InsetHyperlink::tocString(odocstream & os) const
244 plaintext(os, OutputParams(0));
248 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
250 docstring url = getParam("target");
251 docstring type = getParam("type");
252 docstring guitype = _("www");
253 if (type == "mailto:")
254 guitype = _("email");
255 else if (type == "file:")
257 return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
261 void InsetHyperlink::validate(LaTeXFeatures & features) const
263 features.require("hyperref");
267 docstring InsetHyperlink::contextMenu(BufferView const &, int, int) const
269 return from_ascii("context-hyperlink");