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 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
35 : InsetCommand(p, "href")
39 CommandInfo const * InsetHyperlink::findInfo(string const & /* cmdName */)
41 static const char * const paramnames[] =
42 {"name", "target", "type", ""};
43 static const bool isoptional[] = {true, false};
44 static const CommandInfo info = {3, paramnames, isoptional};
49 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
51 docstring const temp = from_ascii("Hyperlink: ");
55 url += getParam("name");
57 url += getParam("target");
60 if (url.length() > 30) {
61 url = url.substr(0, 10) + "..."
62 + url.substr(url.length() - 17, url.length());
68 int InsetHyperlink::latex(Buffer const &, odocstream & os,
69 OutputParams const & runparams) const
71 docstring url = getParam("target");
72 static docstring const backslash = from_ascii("\\");
73 static docstring const braces = from_ascii("{}");
74 static char_type const chars_url[2] = {'%', '#'};
75 static char_type const chars_name[6] = {
76 '&', '_', '$', '%', '#', '^'};
78 // The characters in chars_url[] need to be changed to a command when
79 // they are in the url field.
81 // the chars_url[] characters must be handled for both, url and href
82 for (int k = 0; k < 2; k++) {
83 for (size_t i = 0, pos;
84 (pos = url.find(chars_url[k], i)) != string::npos;
86 url.replace(pos, 1, backslash + chars_url[k]);
89 } // end if (!url.empty())
91 docstring name = getParam("name");
93 // The characters in chars_name[] need to be changed to a command when
94 // they are in the name field.
97 // handle the "\" character, but only when the following character
98 // is not also a "\", because "\\" is valid code
99 docstring const textbackslash = from_ascii("\\textbackslash{}");
100 for (size_t i = 0, pos;
101 (pos = name.find('\\', i)) != string::npos;
103 if (name[pos + 1] != '\\')
104 name.replace(pos, 1, textbackslash);
106 for (int k = 0; k < 6; k++) {
107 for (size_t i = 0, pos;
108 (pos = name.find(chars_name[k], i)) != string::npos;
110 name.replace(pos, 1, backslash + chars_name[k] + braces);
113 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
115 docstring const sim = from_ascii("$\\sim$");
116 for (size_t i = 0, pos;
117 (pos = name.find('~', i)) != string::npos;
119 name.replace(pos, 1, sim);
121 } // end if (!name.empty())
123 if (runparams.moving_arg)
126 //for the case there is no name given, the target is set as name
127 os << "\\href{" << getParam("type") << url << "}{"
128 << (name.empty()? url : name) << '}';
134 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
135 OutputParams const &) const
137 odocstringstream oss;
139 oss << '[' << getParam("target");
140 if (getParam("name").empty())
143 oss << "||" << getParam("name") << ']';
145 docstring const str = oss.str();
151 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
152 OutputParams const &) const
154 os << "<ulink url=\""
155 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
163 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
164 OutputParams const & op) const
166 return plaintext(buf, os, op);
170 void InsetHyperlink::validate(LaTeXFeatures & features) const
172 features.require("hyperref");