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 "support/gettext.h"
20 #include "OutputParams.h"
22 #include "support/lstrings.h"
23 #include "support/docstream.h"
26 using namespace lyx::support;
31 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
32 : InsetCommand(p, "href")
36 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
38 static ParamInfo param_info_;
39 if (param_info_.empty()) {
40 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
41 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
42 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
48 docstring InsetHyperlink::screenLabel() const
50 docstring const temp = from_ascii("Hyperlink: ");
54 url += getParam("name");
56 url += getParam("target");
59 if (url.length() > 30) {
60 url = url.substr(0, 10) + "..."
61 + url.substr(url.length() - 17, url.length());
67 int InsetHyperlink::latex(odocstream & os, OutputParams const & runparams) const
69 docstring url = getParam("target");
70 static docstring const backslash = from_ascii("\\");
71 static docstring const braces = from_ascii("{}");
72 static char_type const chars_url[2] = {'%', '#'};
73 static char_type const chars_name[6] = {
74 '&', '_', '$', '%', '#', '^'};
76 // The characters in chars_url[] need to be changed to a command when
77 // they are in the url field.
79 // the chars_url[] characters must be handled for both, url and href
80 for (int k = 0; k < 2; k++) {
81 for (size_t i = 0, pos;
82 (pos = url.find(chars_url[k], i)) != string::npos;
84 url.replace(pos, 1, backslash + chars_url[k]);
87 } // end if (!url.empty())
89 docstring name = getParam("name");
91 // The characters in chars_name[] need to be changed to a command when
92 // they are in the name field.
95 // handle the "\" character, but only when the following character
96 // is not also a "\", because "\\" is valid code
97 docstring const textbackslash = from_ascii("\\textbackslash{}");
98 for (size_t i = 0, pos;
99 (pos = name.find('\\', i)) != string::npos;
101 if (name[pos + 1] != '\\')
102 name.replace(pos, 1, textbackslash);
104 for (int k = 0; k < 6; k++) {
105 for (size_t i = 0, pos;
106 (pos = name.find(chars_name[k], i)) != string::npos;
108 name.replace(pos, 1, backslash + chars_name[k] + braces);
111 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
113 docstring const sim = from_ascii("$\\sim$");
114 for (size_t i = 0, pos;
115 (pos = name.find('~', i)) != string::npos;
117 name.replace(pos, 1, sim);
119 } // end if (!name.empty())
121 if (runparams.moving_arg)
124 //for the case there is no name given, the target is set as name
125 os << "\\href{" << getParam("type") << url << "}{"
126 << (name.empty()? url : name) << '}';
132 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
134 odocstringstream oss;
136 oss << '[' << getParam("target");
137 if (getParam("name").empty())
140 oss << "||" << getParam("name") << ']';
142 docstring const str = oss.str();
148 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
150 os << "<ulink url=\""
151 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
159 void InsetHyperlink::textString(odocstream & os) const
161 plaintext(os, OutputParams(0));
165 void InsetHyperlink::validate(LaTeXFeatures & features) const
167 features.require("hyperref");