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"
23 #include "support/docstream.h"
35 //FIXME: these should be lists of char_type and not char
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 //FIXME: all strings in this routine should be docstrings
80 string url = to_utf8(getParam("target"));
82 string backslash = "\\";
85 // The characters in chars_url[] need to be changed to a command when
86 // they are in the url field.
88 // the chars_url[] characters must be handled for both, url and href
89 for (int k = 0; k < 2; k++) {
90 for (size_t i = 0, pos;
91 (pos = url.find(chars_url[k], i)) != string::npos;
93 url.replace(pos,1,backslash + chars_url[k]);
96 } // end if (!url.empty())
98 string name = to_utf8(getParam("name"));
100 // The characters in chars_name[] need to be changed to a command when
101 // they are in the name field.
104 // handle the "\" character, but only when the following character
105 // is not also a "\", because "\\" is valid code
106 for (size_t i = 0, pos;
107 (pos = name.find("\\", i)) != string::npos;
109 if (name[pos+1] != '\\')
110 name.replace(pos,1,"\\textbackslash{}");
112 for (int k = 0; k < 6; k++) {
113 for (size_t i = 0, pos;
114 (pos = name.find(chars_name[k], i)) != string::npos;
116 name.replace(pos,1,backslash + chars_name[k] + braces);
119 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
122 (pos = name.find("~", i)) != string::npos;
124 name.replace(pos,1,"$\\sim$");
126 } // end if (!name.empty())
128 //for the case there is no name given, the target is set as name
129 string urlname = url;
130 // set the hyperlink type
131 url = to_utf8(getParam("type")) + url;
133 if (runparams.moving_arg)
135 //set the target for the name when no name is given
137 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
139 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(urlname) << '}';
144 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
145 OutputParams const &) const
147 odocstringstream oss;
149 oss << '[' << getParam("target");
150 if (getParam("name").empty())
153 oss << "||" << getParam("name") << ']';
155 docstring const str = oss.str();
161 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
162 OutputParams const &) const
164 os << "<ulink url=\""
165 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
173 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
174 OutputParams const & op) const
176 return plaintext(buf, os, op);
180 void InsetHyperlink::validate(LaTeXFeatures & features) const
182 features.require("hyperref");