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 const InsetHyperlink::getScreenLabel(Buffer const &) 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(Buffer const &, odocstream & os,
68 OutputParams const & runparams) const
70 docstring url = getParam("target");
71 static docstring const backslash = from_ascii("\\");
72 static docstring const braces = from_ascii("{}");
73 static char_type const chars_url[2] = {'%', '#'};
74 static char_type const chars_name[6] = {
75 '&', '_', '$', '%', '#', '^'};
77 // The characters in chars_url[] need to be changed to a command when
78 // they are in the url field.
80 // the chars_url[] characters must be handled for both, url and href
81 for (int k = 0; k < 2; k++) {
82 for (size_t i = 0, pos;
83 (pos = url.find(chars_url[k], i)) != string::npos;
85 url.replace(pos, 1, backslash + chars_url[k]);
88 } // end if (!url.empty())
90 docstring name = getParam("name");
92 // The characters in chars_name[] need to be changed to a command when
93 // they are in the name field.
96 // handle the "\" character, but only when the following character
97 // is not also a "\", because "\\" is valid code
98 docstring const textbackslash = from_ascii("\\textbackslash{}");
99 for (size_t i = 0, pos;
100 (pos = name.find('\\', i)) != string::npos;
102 if (name[pos + 1] != '\\')
103 name.replace(pos, 1, textbackslash);
105 for (int k = 0; k < 6; k++) {
106 for (size_t i = 0, pos;
107 (pos = name.find(chars_name[k], i)) != string::npos;
109 name.replace(pos, 1, backslash + chars_name[k] + braces);
112 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
114 docstring const sim = from_ascii("$\\sim$");
115 for (size_t i = 0, pos;
116 (pos = name.find('~', i)) != string::npos;
118 name.replace(pos, 1, sim);
120 } // end if (!name.empty())
122 if (runparams.moving_arg)
125 //for the case there is no name given, the target is set as name
126 os << "\\href{" << getParam("type") << url << "}{"
127 << (name.empty()? url : name) << '}';
133 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
134 OutputParams const &) const
136 odocstringstream oss;
138 oss << '[' << getParam("target");
139 if (getParam("name").empty())
142 oss << "||" << getParam("name") << ']';
144 docstring const str = oss.str();
150 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
151 OutputParams const &) const
153 os << "<ulink url=\""
154 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
162 void InsetHyperlink::textString(Buffer const & buf, odocstream & os) const
164 plaintext(buf, os, OutputParams(0));
168 void InsetHyperlink::validate(LaTeXFeatures & features) const
170 features.require("hyperref");