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_type const chars_url[2] = {'%', '#'};
38 static char_type 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 docstring url = getParam("target");
82 docstring backslash = from_ascii("\\");
83 docstring braces = from_ascii("{}");
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 docstring name = 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 docstring textbackslash = from_ascii("\\textbackslash{}");
107 for (size_t i = 0, pos;
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++) {
114 for (size_t i = 0, pos;
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
122 docstring sim = from_ascii("$\\sim$");
124 (pos = name.find('~', i)) != string::npos;
126 name.replace(pos, 1, sim);
128 } // end if (!name.empty())
130 //for the case there is no name given, the target is set as name
131 docstring urlname = url;
132 // set the hyperlink type
133 url += getParam("type");
135 if (runparams.moving_arg)
137 //set the target for the name when no name is given
139 os << "\\href{" << url << "}{" << name << '}';
141 os << "\\href{" << url << "}{" << urlname << '}';
146 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
147 OutputParams const &) const
149 odocstringstream oss;
151 oss << '[' << getParam("target");
152 if (getParam("name").empty())
155 oss << "||" << getParam("name") << ']';
157 docstring const str = oss.str();
163 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
164 OutputParams const &) const
166 os << "<ulink url=\""
167 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
175 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
176 OutputParams const & op) const
178 return plaintext(buf, os, op);
182 void InsetHyperlink::validate(LaTeXFeatures & features) const
184 features.require("hyperref");