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"
20 #include "OutputParams.h"
22 #include "support/lstrings.h"
24 #include "support/std_ostream.h"
36 static char const * const chars_url[2] = {"%", "#"};
38 static char const * const chars_name[6] = {
39 "&", "_", "$", "%", "#", "^"};
42 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
43 : InsetCommand(p, "href")
47 CommandInfo const * InsetHyperlink::findInfo(std::string const & /* cmdName */)
49 static const char * const paramnames[] =
50 {"name", "target", "type", ""};
51 static const bool isoptional[] = {true, false};
52 static const CommandInfo info = {3, paramnames, isoptional};
57 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
59 docstring const temp = from_ascii("Hyperlink: ");
63 url += getParam("name");
65 url += getParam("target");
68 if (url.length() > 30) {
69 url = url.substr(0, 10) + "..."
70 + url.substr(url.length() - 17, url.length());
76 int InsetHyperlink::latex(Buffer const &, odocstream & os,
77 OutputParams const & runparams) const
79 string url = to_utf8(getParam("target"));
81 string backslash = "\\";
84 // The characters in chars_url[] need to be changed to a command when
85 // they are in the url field.
87 // the chars_url[] characters must be handled for both, url and href
88 for (int k = 0; k < 2; k++) {
90 (pos = url.find(chars_url[k], i)) != string::npos;
92 url.replace(pos,1,backslash + chars_url[k]);
95 } // end if (!url.empty())
97 string name = to_utf8(getParam("name"));
99 // The characters in chars_name[] need to be changed to a command when
100 // they are in the name field.
103 // handle the "\" character, but only when the following character
104 // is not also a "\", because "\\" is valid code
106 (pos = name.find("\\", i)) != string::npos;
108 if (name[pos+1] != '\\')
109 name.replace(pos,1,"\\textbackslash{}");
111 for (int k = 0; k < 6; k++) {
113 (pos = name.find(chars_name[k], i)) != string::npos;
115 name.replace(pos,1,backslash + chars_name[k] + braces);
118 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
121 (pos = name.find("~", i)) != string::npos;
123 name.replace(pos,1,"$\\sim$");
125 } // end if (!name.empty())
127 //for the case there is no name given, the target is set as name
128 string urlname = url;
129 // set the hyperlink type
130 url = to_utf8(getParam("type")) + url;
132 if (runparams.moving_arg)
134 //set the target for the name when no name is given
136 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
138 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(urlname) << '}';
143 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
144 OutputParams const &) const
146 odocstringstream oss;
148 oss << '[' << getParam("target");
149 if (getParam("name").empty())
152 oss << "||" << getParam("name") << ']';
154 docstring const str = oss.str();
160 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
161 OutputParams const &) const
163 os << "<ulink url=\""
164 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
172 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
173 OutputParams const & op) const
175 return plaintext(buf, os, op);
179 void InsetHyperlink::validate(LaTeXFeatures & features) const
181 features.require("hyperref");