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 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
36 : InsetCommand(p, "href")
40 CommandInfo const * InsetHyperlink::findInfo(string const & /* cmdName */)
42 static const char * const paramnames[] =
43 {"name", "target", "type", ""};
44 static const bool isoptional[] = {true, false};
45 static const CommandInfo info = {3, paramnames, isoptional};
50 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
52 docstring const temp = from_ascii("Hyperlink: ");
56 url += getParam("name");
58 url += getParam("target");
61 if (url.length() > 30) {
62 url = url.substr(0, 10) + "..."
63 + url.substr(url.length() - 17, url.length());
69 int InsetHyperlink::latex(Buffer const &, odocstream & os,
70 OutputParams const & runparams) const
72 docstring url = getParam("target");
73 static docstring const backslash = from_ascii("\\");
74 static docstring const braces = from_ascii("{}");
75 static char_type const chars_url[2] = {'%', '#'};
76 static char_type const chars_name[6] = {
77 '&', '_', '$', '%', '#', '^'};
79 // The characters in chars_url[] need to be changed to a command when
80 // they are in the url field.
82 // the chars_url[] characters must be handled for both, url and href
83 for (int k = 0; k < 2; k++) {
84 for (size_t i = 0, pos;
85 (pos = url.find(chars_url[k], i)) != string::npos;
87 url.replace(pos, 1, backslash + chars_url[k]);
90 } // end if (!url.empty())
92 docstring name = getParam("name");
94 // The characters in chars_name[] need to be changed to a command when
95 // they are in the name field.
98 // handle the "\" character, but only when the following character
99 // is not also a "\", because "\\" is valid code
100 docstring const textbackslash = from_ascii("\\textbackslash{}");
101 for (size_t i = 0, pos;
102 (pos = name.find('\\', i)) != string::npos;
104 if (name[pos + 1] != '\\')
105 name.replace(pos, 1, textbackslash);
107 for (int k = 0; k < 6; k++) {
108 for (size_t i = 0, pos;
109 (pos = name.find(chars_name[k], i)) != string::npos;
111 name.replace(pos, 1, backslash + chars_name[k] + braces);
114 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
116 docstring const sim = from_ascii("$\\sim$");
118 (pos = name.find('~', i)) != string::npos;
120 name.replace(pos, 1, sim);
122 } // end if (!name.empty())
124 //for the case there is no name given, the target is set as name
125 docstring const urlname = url;
126 // set the hyperlink type
127 url += getParam("type");
129 if (runparams.moving_arg)
131 //set the target for the name when no name is given
133 os << "\\href{" << url << "}{" << name << '}';
135 os << "\\href{" << url << "}{" << urlname << '}';
140 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
141 OutputParams const &) const
143 odocstringstream oss;
145 oss << '[' << getParam("target");
146 if (getParam("name").empty())
149 oss << "||" << getParam("name") << ']';
151 docstring const str = oss.str();
157 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
158 OutputParams const &) const
160 os << "<ulink url=\""
161 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
169 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
170 OutputParams const & op) const
172 return plaintext(buf, os, op);
176 void InsetHyperlink::validate(LaTeXFeatures & features) const
178 features.require("hyperref");