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"
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "LaTeXFeatures.h"
19 #include "OutputParams.h"
20 #include "output_xhtml.h"
22 #include "support/docstream.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
27 using namespace lyx::support;
32 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
33 : InsetCommand(buf, p, "href")
37 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
39 static ParamInfo param_info_;
40 if (param_info_.empty()) {
41 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
42 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
43 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
49 docstring InsetHyperlink::screenLabel() const
51 docstring const temp = from_ascii("Hyperlink: ");
55 url += getParam("name");
57 url += getParam("target");
60 if (url.length() > 30) {
61 url = url.substr(0, 10) + "..."
62 + url.substr(url.length() - 17, url.length());
68 int InsetHyperlink::latex(odocstream & os,
69 OutputParams const & runparams) const
71 docstring url = getParam("target");
72 docstring name = getParam("name");
73 static docstring const backslash = from_ascii("\\");
74 static docstring const braces = from_ascii("{}");
75 static char_type const chars_url[2] = {'%', '#'};
76 static char_type const chars_name[6] = {
77 '&', '_', '$', '%', '#', '^'};
79 // For the case there is no name given, the target is set as name.
80 // Do this before !url.empty() and !name.empty() to handle characters
81 // like the "%" correctly.
86 // Replace the "\" character by its ASCII code according to the
87 // URL specifications because "\" is not allowed in URLs and by
88 // \href. Only do this when the following character is not also
89 // a "\", because "\\" is valid code
90 for (size_t i = 0, pos;
91 (pos = url.find('\\', i)) != string::npos;
93 if (url[pos + 1] != '\\')
94 url.replace(pos, 1, from_ascii("%5C"));
97 // The characters in chars_url[] need to be escaped in the url
98 // field because otherwise LaTeX will fail when the hyperlink is
99 // within an argument of another command, e.g. in a \footnote. It
100 // is important that they are escaped as "\#" and not as "\#{}".
101 for (int k = 0; k < 2; k++)
102 for (size_t i = 0, pos;
103 (pos = url.find(chars_url[k], i)) != string::npos;
105 url.replace(pos, 1, backslash + chars_url[k]);
107 // add "http://" when the type is web (type = empty)
108 // and no "://" or "run:" is given
109 docstring type = getParam("type");
110 if (url.find(from_ascii("://")) == string::npos
111 && url.find(from_ascii("run:")) == string::npos
113 url = from_ascii("http://") + url;
115 } // end if (!url.empty())
117 // The characters in chars_name[] need to be changed to a command when
118 // they are in the name field.
120 // handle the "\" character, but only when the following character
121 // is not also a "\", because "\\" is valid code
122 docstring const textbackslash = from_ascii("\\textbackslash{}");
123 for (size_t i = 0, pos;
124 (pos = name.find('\\', i)) != string::npos;
126 if (name[pos + 1] != '\\')
127 name.replace(pos, 1, textbackslash);
129 // The characters in chars_name[] need to be changed to a command
130 // when they are in the name field.
131 // Therefore the treatment of "\" must be the first thing
132 for (int k = 0; k < 6; k++)
133 for (size_t i = 0, pos;
134 (pos = name.find(chars_name[k], i)) != string::npos;
136 name.replace(pos, 1, backslash + chars_name[k] + braces);
138 // replace the tilde by the \sim character as suggested in the
139 // LaTeX FAQ for URLs
140 docstring const sim = from_ascii("$\\sim$");
141 for (size_t i = 0, pos;
142 (pos = name.find('~', i)) != string::npos;
144 name.replace(pos, 1, sim);
146 } // end if (!name.empty())
148 if (runparams.moving_arg)
151 // output the ready \href command
152 os << "\\href{" << getParam("type") << url << "}{" << name << '}';
158 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
160 odocstringstream oss;
162 oss << '[' << getParam("target");
163 if (getParam("name").empty())
166 oss << "||" << getParam("name") << ']';
168 docstring const str = oss.str();
174 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
176 os << "<ulink url=\""
177 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
185 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
187 docstring const & target = getParam("target");
188 docstring const & name = getParam("name");
189 xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
190 xs << (name.empty() ? target : name);
191 xs << html::EndTag("a");
196 void InsetHyperlink::tocString(odocstream & os) const
198 plaintext(os, OutputParams(0));
202 void InsetHyperlink::validate(LaTeXFeatures & features) const
204 features.require("hyperref");