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 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
49 docstring const temp = from_ascii("Hyperlink: ");
53 url += getParam("name");
55 url += getParam("target");
58 if (url.length() > 30) {
59 url = url.substr(0, 10) + "..."
60 + url.substr(url.length() - 17, url.length());
66 int InsetHyperlink::latex(Buffer const &, odocstream & os,
67 OutputParams const & runparams) const
69 string url = to_utf8(getParam("target"));
71 string backslash = "\\";
74 // The characters in chars_url[] need to be changed to a command when
75 // they are in the url field.
77 // the chars_url[] characters must be handled for both, url and href
78 for (int k = 0; k < 2; k++) {
80 (pos = url.find(chars_url[k], i)) != string::npos;
82 url.replace(pos,1,backslash + chars_url[k]);
85 } // end if (!url.empty())
87 string name = to_utf8(getParam("name"));
89 // The characters in chars_name[] need to be changed to a command when
90 // they are in the name field.
93 // handle the "\" character, but only when the following character
94 // is not also a "\", because "\\" is valid code
96 (pos = name.find("\\", i)) != string::npos;
98 if (name[pos+1] != '\\')
99 name.replace(pos,1,"\\textbackslash{}");
101 for (int k = 0; k < 6; k++) {
103 (pos = name.find(chars_name[k], i)) != string::npos;
105 name.replace(pos,1,backslash + chars_name[k] + braces);
108 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
111 (pos = name.find("~", i)) != string::npos;
113 name.replace(pos,1,"$\\sim$");
115 } // end if (!name.empty())
117 if (runparams.moving_arg)
119 //set the target for the name when no name is given
121 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
123 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(url) << '}';
128 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
129 OutputParams const &) const
131 odocstringstream oss;
133 oss << '[' << getParam("target");
134 if (getParam("name").empty())
137 oss << "||" << getParam("name") << ']';
139 docstring const str = oss.str();
145 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
146 OutputParams const &) const
148 os << "<ulink url=\""
149 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
157 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
158 OutputParams const & op) const
160 return plaintext(buf, os, op);
164 void InsetHyperlink::validate(LaTeXFeatures & features) const
166 features.require("hyperref");