]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[lyx.git] / src / insets / InsetHyperlink.cpp
1 /**
2  * \file InsetHyperlink.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetHyperlink.h"
15
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "LaTeXFeatures.h"
19 #include "OutputParams.h"
20
21 #include "support/docstream.h"
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29
30
31 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
32         : InsetCommand(p, "href")
33 {}
34
35
36 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
37 {
38         static ParamInfo param_info_;
39         if (param_info_.empty()) {
40                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
41                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
42                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
43         }
44         return param_info_;
45 }
46
47
48 docstring InsetHyperlink::screenLabel() const
49 {
50         docstring const temp = from_ascii("Hyperlink: ");
51
52         docstring url;
53
54         url += getParam("name");
55         if (url.empty())
56                 url += getParam("target");
57
58         // elide if long
59         if (url.length() > 30) {
60                 url = url.substr(0, 10) + "..."
61                         + url.substr(url.length() - 17, url.length());
62         }
63         return temp + url;
64 }
65
66
67 int InsetHyperlink::latex(odocstream & os,
68                                                   OutputParams const & runparams) const
69 {
70         docstring url = getParam("target");
71         docstring name = getParam("name");
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                 '&', '_', '$', '%', '#', '^'};
77
78         // For the case there is no name given, the target is set as name.
79         // Do this before !url.empty() and !name.empty() to handle characters
80         // like the "%" correctly.
81         if (name.empty())
82                 name = url;
83
84         if (!url.empty()) {
85                 // Replace the "\" character by its ASCII code according to the
86                 // URL specifications because "\" is not allowed in URLs and by
87                 // \href. Only do this when the following character is not also
88                 // a "\", because "\\" is valid code
89                 for (size_t i = 0, pos;
90                         (pos = url.find('\\', i)) != string::npos;
91                         i = pos + 2) {
92                         if (url[pos + 1] != '\\')
93                                 url.replace(pos, 1, from_ascii("%5C"));
94                 }
95
96                 // The characters in chars_url[] need to be escaped in the url
97                 // field because otherwise LaTeX will fail when the hyperlink is
98                 // within an argument of another command, e.g. in a \footnote. It
99                 // is important that they are escaped as "\#" and not as "\#{}".
100                 for (int k = 0; k < 2; k++)
101                         for (size_t i = 0, pos;
102                                 (pos = url.find(chars_url[k], i)) != string::npos;
103                                 i = pos + 2)
104                                 url.replace(pos, 1, backslash + chars_url[k]);
105                 
106                 // add "http://" when the type is web (type = empty)
107                 // and no "://" or "run:" is given
108                 docstring type = getParam("type");
109                 if (url.find(from_ascii("://")) == string::npos
110                         && url.find(from_ascii("run:")) == string::npos
111                         && type.empty())
112                         url = from_ascii("http://") + url;
113
114         } // end if (!url.empty())
115
116         // The characters in chars_name[] need to be changed to a command when
117         // they are in the name field.
118         if (!name.empty()) {
119                 // handle the "\" character, but only when the following character
120                 // is not also a "\", because "\\" is valid code
121                 docstring const textbackslash = from_ascii("\\textbackslash{}");
122                 for (size_t i = 0, pos;
123                         (pos = name.find('\\', i)) != string::npos;
124                         i = pos + 2) {
125                         if (name[pos + 1] != '\\')
126                                 name.replace(pos, 1, textbackslash);
127                 }
128                 // The characters in chars_name[] need to be changed to a command
129                 // when they are in the name field.
130                 // Therefore the treatment of "\" must be the first thing
131                 for (int k = 0; k < 6; k++)
132                         for (size_t i = 0, pos;
133                                 (pos = name.find(chars_name[k], i)) != string::npos;
134                                 i = pos + 2)
135                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
136
137                 // replace the tilde by the \sim character as suggested in the
138                 // LaTeX FAQ for URLs
139                 docstring const sim = from_ascii("$\\sim$");
140                 for (size_t i = 0, pos;
141                         (pos = name.find('~', i)) != string::npos;
142                         i = pos + 1)
143                         name.replace(pos, 1, sim);
144
145         }  // end if (!name.empty())
146         
147         if (runparams.moving_arg)
148                 os << "\\protect";
149
150         // output the ready \href command
151         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
152
153         return 0;
154 }
155
156
157 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
158 {
159         odocstringstream oss;
160
161         oss << '[' << getParam("target");
162         if (getParam("name").empty())
163                 oss << ']';
164         else
165                 oss << "||" << getParam("name") << ']';
166
167         docstring const str = oss.str();
168         os << str;
169         return str.size();
170 }
171
172
173 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
174 {
175         os << "<ulink url=\""
176            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
177            << "\">"
178            << getParam("name")
179            << "</ulink>";
180         return 0;
181 }
182
183
184 docstring InsetHyperlink::xhtml(odocstream & os, OutputParams const &) const
185 {
186         os << "<a href=\""
187                         // FIXME Do we need to do more escaping than this?
188            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
189            << "\">"
190            << getParam("name")
191            << "</a>";
192         return docstring();
193 }
194
195
196 void InsetHyperlink::tocString(odocstream & os) const
197 {
198         plaintext(os, OutputParams(0));
199 }
200
201
202 void InsetHyperlink::validate(LaTeXFeatures & features) const
203 {
204         features.require("hyperref");
205 }
206
207
208 } // namespace lyx