2 * \file InsetHyperlink.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \authors José Matos, Uwe Stöhr
8 * Full author contact details are available in file CREDITS.
13 #include "InsetHyperlink.h"
15 #include "DispatchResult.h"
16 #include "FuncRequest.h"
17 #include "LaTeXFeatures.h"
19 #include "OutputParams.h"
21 #include "support/lstrings.h"
23 #include "support/std_ostream.h"
35 static char const * const chars_url[2] = {"%", "#"};
37 static char const * const chars_name[6] = {
38 "&", "_", "$", "%", "#", "^"};
41 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
42 : InsetCommand(p, "href")
46 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
48 docstring const temp = from_ascii("Hyperlink: ");
52 url += getParam("name");
54 url += getParam("target");
57 if (url.length() > 30) {
58 url = url.substr(0, 10) + "..."
59 + url.substr(url.length() - 17, url.length());
65 int InsetHyperlink::latex(Buffer const &, odocstream & os,
66 OutputParams const & runparams) const
68 string url = to_utf8(getParam("target"));
70 string backslash = "\\";
73 // The characters in chars_url[] need to be changed to a command when
74 // they are in the url field.
76 // the chars_url[] characters must be handled for both, url and href
77 for (int k = 0; k < 2; k++) {
79 (pos = url.find(chars_url[k], i)) != string::npos;
81 url.replace(pos,1,backslash + chars_url[k]);
84 } // end if (!url.empty())
86 string name = to_utf8(getParam("name"));
88 // The characters in chars_name[] need to be changed to a command when
89 // they are in the name field.
92 // handle the "\" character, but only when the following character
93 // is not also a "\", because "\\" is valid code
95 (pos = name.find("\\", i)) != string::npos;
97 if (name[pos+1] != '\\')
98 name.replace(pos,1,"\\textbackslash{}");
100 for (int k = 0; k < 6; k++) {
102 (pos = name.find(chars_name[k], i)) != string::npos;
104 name.replace(pos,1,backslash + chars_name[k] + braces);
107 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
110 (pos = name.find("~", i)) != string::npos;
112 name.replace(pos,1,"$\\sim$");
114 } // end if (!name.empty())
116 if (runparams.moving_arg)
118 //set the target for the name when no name is given
120 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
122 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(url) << '}';
127 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
128 OutputParams const &) const
130 odocstringstream oss;
132 oss << '[' << getParam("target");
133 if (getParam("name").empty())
136 oss << "||" << getParam("name") << ']';
138 docstring const str = oss.str();
144 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
145 OutputParams const &) const
147 os << "<ulink url=\""
148 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
156 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
157 OutputParams const & op) const
159 return plaintext(buf, os, op);
163 void InsetHyperlink::validate(LaTeXFeatures & features) const
165 features.require("hyperref");