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"
19 #include "support/gettext.h"
20 #include "OutputParams.h"
22 #include "support/lstrings.h"
23 #include "support/docstream.h"
26 using namespace lyx::support;
31 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
32 : InsetCommand(p, "href")
36 CommandInfo const * InsetHyperlink::findInfo(string const & /* cmdName */)
38 static const char * const paramnames[] =
39 {"name", "target", "type", ""};
40 static const bool isoptional[] = {true, false};
41 static const CommandInfo info = {3, paramnames, isoptional};
46 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
48 docstring const temp = from_ascii("Hyperlink: ");
52 url += getParam("name");
54 url += getParam("target");
57 if (url.length() > 30) {
58 url = url.substr(0, 10) + "..."
59 + url.substr(url.length() - 17, url.length());
65 int InsetHyperlink::latex(Buffer const &, odocstream & os,
66 OutputParams const & runparams) const
68 docstring url = getParam("target");
69 static docstring const backslash = from_ascii("\\");
70 static docstring const braces = from_ascii("{}");
71 static char_type const chars_url[2] = {'%', '#'};
72 static char_type const chars_name[6] = {
73 '&', '_', '$', '%', '#', '^'};
75 // The characters in chars_url[] need to be changed to a command when
76 // they are in the url field.
78 // the chars_url[] characters must be handled for both, url and href
79 for (int k = 0; k < 2; k++) {
80 for (size_t i = 0, pos;
81 (pos = url.find(chars_url[k], i)) != string::npos;
83 url.replace(pos, 1, backslash + chars_url[k]);
86 } // end if (!url.empty())
88 docstring name = getParam("name");
90 // The characters in chars_name[] need to be changed to a command when
91 // they are in the name field.
94 // handle the "\" character, but only when the following character
95 // is not also a "\", because "\\" is valid code
96 docstring const textbackslash = from_ascii("\\textbackslash{}");
97 for (size_t i = 0, pos;
98 (pos = name.find('\\', i)) != string::npos;
100 if (name[pos + 1] != '\\')
101 name.replace(pos, 1, textbackslash);
103 for (int k = 0; k < 6; k++) {
104 for (size_t i = 0, pos;
105 (pos = name.find(chars_name[k], i)) != string::npos;
107 name.replace(pos, 1, backslash + chars_name[k] + braces);
110 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
112 docstring const sim = from_ascii("$\\sim$");
113 for (size_t i = 0, pos;
114 (pos = name.find('~', i)) != string::npos;
116 name.replace(pos, 1, sim);
118 } // end if (!name.empty())
120 if (runparams.moving_arg)
123 //for the case there is no name given, the target is set as name
124 os << "\\href{" << getParam("type") << url << "}{"
125 << (name.empty()? url : name) << '}';
131 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
132 OutputParams const &) const
134 odocstringstream oss;
136 oss << '[' << getParam("target");
137 if (getParam("name").empty())
140 oss << "||" << getParam("name") << ']';
142 docstring const str = oss.str();
148 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
149 OutputParams const &) const
151 os << "<ulink url=\""
152 << subst(getParam("target"), from_ascii("&"), from_ascii("&"))
160 void InsetHyperlink::textString(Buffer const & buf, odocstream & os) const
162 plaintext(buf, os, OutputParams(0));
166 void InsetHyperlink::validate(LaTeXFeatures & features) const
168 features.require("hyperref");