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 static void replaceAny(docstring & s, docstring const & items,
69 docstring const & replacement)
75 while (i < s.size()) {
76 if ((i = s.find_first_of(items, i)) == string::npos)
78 s.insert(i, replacement);
84 int InsetHyperlink::latex(Buffer const &, odocstream & os,
85 OutputParams const & runparams) const
87 docstring url = getParam("target");
88 static docstring const backslash = from_ascii("\\");
89 static docstring const braces = from_ascii("{}");
90 static char_type const chars_name[6] = {
91 '&', '_', '$', '%', '#', '^'};
93 // The characters in chars_url[] need to be changed to a command when
94 // they are in the url field.
95 // the chars_url[] characters must be handled for both, url and href
96 static docstring const chars_url = from_ascii("%#");
97 replaceAny(url, chars_url, backslash);
99 docstring name = getParam("name");
101 // The characters in chars_name[] need to be changed to a command when
102 // they are in the name field.
105 // handle the "\" character, but only when the following character
106 // is not also a "\", because "\\" is valid code
107 docstring const textbackslash = from_ascii("\\textbackslash{}");
108 for (size_t i = 0, pos;
109 (pos = name.find('\\', i)) != string::npos;
111 if (name[pos + 1] != '\\')
112 name.replace(pos, 1, textbackslash);
114 for (int k = 0; k < 6; k++) {
115 for (size_t i = 0, pos;
116 (pos = name.find(chars_name[k], i)) != string::npos;
118 name.replace(pos, 1, backslash + chars_name[k] + braces);
121 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
123 docstring const sim = from_ascii("$\\sim$");
125 (pos = name.find('~', i)) != string::npos;
127 name.replace(pos, 1, sim);
129 } // end if (!name.empty())
131 if (runparams.moving_arg)
134 //for the case there is no name given, the target is set as name
135 os << "\\href{" << getParam("type") << url << "}{"
136 << (name.empty()? url : name) << '}';
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");