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"
34 static char_type const chars_url[2] = {'%', '#'};
36 static char_type const chars_name[6] = {
37 '&', '_', '$', '%', '#', '^'};
40 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
41 : InsetCommand(p, "href")
45 CommandInfo const * InsetHyperlink::findInfo(string const & /* cmdName */)
47 static const char * const paramnames[] =
48 {"name", "target", "type", ""};
49 static const bool isoptional[] = {true, false};
50 static const CommandInfo info = {3, paramnames, isoptional};
55 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
57 docstring const temp = from_ascii("Hyperlink: ");
61 url += getParam("name");
63 url += getParam("target");
66 if (url.length() > 30) {
67 url = url.substr(0, 10) + "..."
68 + url.substr(url.length() - 17, url.length());
74 int InsetHyperlink::latex(Buffer const &, odocstream & os,
75 OutputParams const & runparams) const
77 docstring url = getParam("target");
78 docstring backslash = from_ascii("\\");
79 docstring braces = from_ascii("{}");
81 // The characters in chars_url[] need to be changed to a command when
82 // they are in the url field.
84 // the chars_url[] characters must be handled for both, url and href
85 for (int k = 0; k < 2; k++) {
86 for (size_t i = 0, pos;
87 (pos = url.find(chars_url[k], i)) != string::npos;
89 url.replace(pos, 1, backslash + chars_url[k]);
92 } // end if (!url.empty())
94 docstring name = getParam("name");
96 // The characters in chars_name[] need to be changed to a command when
97 // they are in the name field.
100 // handle the "\" character, but only when the following character
101 // is not also a "\", because "\\" is valid code
102 docstring textbackslash = from_ascii("\\textbackslash{}");
103 for (size_t i = 0, pos;
104 (pos = name.find('\\', i)) != string::npos;
106 if (name[pos + 1] != '\\')
107 name.replace(pos, 1, textbackslash);
109 for (int k = 0; k < 6; k++) {
110 for (size_t i = 0, pos;
111 (pos = name.find(chars_name[k], i)) != string::npos;
113 name.replace(pos, 1, backslash + chars_name[k] + braces);
116 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
118 docstring sim = from_ascii("$\\sim$");
120 (pos = name.find('~', i)) != string::npos;
122 name.replace(pos, 1, sim);
124 } // end if (!name.empty())
126 //for the case there is no name given, the target is set as name
127 docstring urlname = url;
128 // set the hyperlink type
129 url += getParam("type");
131 if (runparams.moving_arg)
133 //set the target for the name when no name is given
135 os << "\\href{" << url << "}{" << name << '}';
137 os << "\\href{" << url << "}{" << urlname << '}';
142 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
143 OutputParams const &) const
145 odocstringstream oss;
147 oss << '[' << getParam("target");
148 if (getParam("name").empty())
151 oss << "||" << getParam("name") << ']';
153 docstring const str = oss.str();
159 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
160 OutputParams const &) const
162 os << "<ulink url=\""
163 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
171 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
172 OutputParams const & op) const
174 return plaintext(buf, os, op);
178 void InsetHyperlink::validate(LaTeXFeatures & features) const
180 features.require("hyperref");