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 //FIXME: these should be lists of char_type and not char
37 static char const * const chars_url[2] = {"%", "#"};
39 static char const * const chars_name[6] = {
40 "&", "_", "$", "%", "#", "^"};
43 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
44 : InsetCommand(p, "href")
48 CommandInfo const * InsetHyperlink::findInfo(std::string const & /* cmdName */)
50 static const char * const paramnames[] =
51 {"name", "target", "type", ""};
52 static const bool isoptional[] = {true, false};
53 static const CommandInfo info = {3, paramnames, isoptional};
58 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
60 docstring const temp = from_ascii("Hyperlink: ");
64 url += getParam("name");
66 url += getParam("target");
69 if (url.length() > 30) {
70 url = url.substr(0, 10) + "..."
71 + url.substr(url.length() - 17, url.length());
77 int InsetHyperlink::latex(Buffer const &, odocstream & os,
78 OutputParams const & runparams) const
80 //FIXME: all strings in this routine should be docstrings
81 string url = to_utf8(getParam("target"));
83 string backslash = "\\";
86 // The characters in chars_url[] need to be changed to a command when
87 // they are in the url field.
89 // the chars_url[] characters must be handled for both, url and href
90 for (int k = 0; k < 2; k++) {
92 (pos = url.find(chars_url[k], i)) != string::npos;
94 url.replace(pos,1,backslash + chars_url[k]);
97 } // end if (!url.empty())
99 string name = to_utf8(getParam("name"));
101 // The characters in chars_name[] need to be changed to a command when
102 // they are in the name field.
105 // handle the "\" character, but only when the following character
106 // is not also a "\", because "\\" is valid code
108 (pos = name.find("\\", i)) != string::npos;
110 if (name[pos+1] != '\\')
111 name.replace(pos,1,"\\textbackslash{}");
113 for (int k = 0; k < 6; k++) {
115 (pos = name.find(chars_name[k], i)) != string::npos;
117 name.replace(pos,1,backslash + chars_name[k] + braces);
120 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
123 (pos = name.find("~", i)) != string::npos;
125 name.replace(pos,1,"$\\sim$");
127 } // end if (!name.empty())
129 //for the case there is no name given, the target is set as name
130 string urlname = url;
131 // set the hyperlink type
132 url = to_utf8(getParam("type")) + url;
134 if (runparams.moving_arg)
136 //set the target for the name when no name is given
138 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
140 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(urlname) << '}';
145 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
146 OutputParams const &) const
148 odocstringstream oss;
150 oss << '[' << getParam("target");
151 if (getParam("name").empty())
154 oss << "||" << getParam("name") << ']';
156 docstring const str = oss.str();
162 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
163 OutputParams const &) const
165 os << "<ulink url=\""
166 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
174 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
175 OutputParams const & op) const
177 return plaintext(buf, os, op);
181 void InsetHyperlink::validate(LaTeXFeatures & features) const
183 features.require("hyperref");