]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
Clean up DeclareDocBookClass
[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 #include <output_docbook.h>
14
15 #include "InsetHyperlink.h"
16
17 #include "Buffer.h"
18 #include "DispatchResult.h"
19 #include "Encoding.h"
20 #include "Format.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
26 #include "xml.h"
27 #include "texstream.h"
28
29 #include "support/docstream.h"
30 #include "support/FileName.h"
31 #include "support/filetools.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34
35 #include "frontends/alert.h"
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42
43 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
44         : InsetCommand(buf, p)
45 {}
46
47
48 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
49 {
50         static ParamInfo param_info_;
51         if (param_info_.empty()) {
52                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
53                                 ParamInfo::HANDLING_LATEXIFY);
54                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
55                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
56                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
57         }
58         return param_info_;
59 }
60
61
62 docstring InsetHyperlink::screenLabel() const
63 {
64         // TODO: replace with unicode hyperlink character = U+1F517
65         docstring const temp = _("Hyperlink: ");
66
67         docstring url;
68
69         url += getParam("name");
70         if (url.empty()) {
71                 url += getParam("target");
72
73                 // elide if long and no name was provided
74                 if (url.length() > 30) {
75                         docstring end = url.substr(url.length() - 17, url.length());
76                         support::truncateWithEllipsis(url, 13);
77                         url += end;
78                 }
79         } else {
80                 // elide if long (approx number of chars in line of article class)
81                 if (url.length() > 80) {
82                         docstring end = url.substr(url.length() - 67, url.length());
83                         support::truncateWithEllipsis(url, 13);
84                         url += end;
85                 }
86         }
87         return temp + url;
88 }
89
90
91 void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
92 {
93         switch (cmd.action()) {
94
95         case LFUN_INSET_EDIT:
96                 viewTarget();
97                 break;
98
99         default:
100                 InsetCommand::doDispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
107                 FuncStatus & flag) const
108 {
109         switch (cmd.action()) {
110         case LFUN_INSET_EDIT:
111                 flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
112                 return true;
113
114         default:
115                 return InsetCommand::getStatus(cur, cmd, flag);
116         }
117 }
118
119
120 void InsetHyperlink::viewTarget() const
121 {
122         if (getParam("type") == "file:") {
123                 FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
124                 string const format = theFormats().getFormatFromFile(url);
125                 theFormats().view(buffer(), url, format);
126         }
127 }
128
129
130 void InsetHyperlink::latex(otexstream & os,
131                            OutputParams const & runparams) const
132 {
133         docstring url = getParam("target");
134         docstring name = getParam("name");
135         static char_type const chars_url[2] = {'%', '#'};
136
137         // For the case there is no name given, the target is set as name.
138         // Do this before !url.empty() and !name.empty() to handle characters
139         // such as % correctly.
140         if (name.empty())
141                 name = url;
142
143         if (!url.empty()) {
144                 // Use URI/URL-style percent-encoded string (hexadecimal).
145                 // We exclude some characters that must not be transformed
146                 // in hrefs: % # / : ? = & ! * ' ( ) ; @ + $ , [ ]
147                 // or that we need to treat manually: \.
148                 url = to_percent_encoding(url, from_ascii("%#\\/:?=&!*'();@+$,[]"));
149                 // We handle \ manually since \\ is valid
150                 for (size_t i = 0, pos;
151                         (pos = url.find('\\', i)) != string::npos;
152                         i = pos + 2) {
153                         if (url[pos + 1] != '\\')
154                                 url.replace(pos, 1, from_ascii("%5C"));
155                 }
156
157                 // The characters in chars_url[] need to be escaped in the url
158                 // field because otherwise LaTeX will fail when the hyperlink is
159                 // within an argument of another command, e.g. in a \footnote. It
160                 // is important that they are escaped as "\#" and not as "\#{}".
161                 // FIXME this is not necessary in outside of commands.
162                 for (int k = 0; k < 2; k++)
163                         for (size_t i = 0, pos;
164                                 (pos = url.find(chars_url[k], i)) != string::npos;
165                                 i = pos + 2)
166                                 url.replace(pos, 1, from_ascii("\\") + chars_url[k]);
167
168                 // add "http://" when the type is web (type = empty)
169                 // and no "://" or "run:" is given
170                 docstring type = getParam("type");
171                 if (url.find(from_ascii("://")) == string::npos
172                         && url.find(from_ascii("run:")) == string::npos
173                         && type.empty())
174                         url = from_ascii("http://") + url;
175
176         } // end if (!url.empty())
177
178         if (!name.empty()) {
179                 name = params().prepareCommand(runparams, name,
180                                         ParamInfo::HANDLING_LATEXIFY);
181                 // replace the tilde by the \sim character as suggested in the
182                 // LaTeX FAQ for URLs
183                 if (getParam("literal") != from_ascii("true")) {
184                         docstring const sim = from_ascii("$\\sim$");
185                         for (size_t i = 0, pos;
186                                 (pos = name.find('~', i)) != string::npos;
187                                 i = pos + 1)
188                                 name.replace(pos, 1, sim);
189                 }
190         }
191
192         if (runparams.moving_arg)
193                 os << "\\protect";
194
195         // output the ready \href command
196         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
197 }
198
199
200 int InsetHyperlink::plaintext(odocstringstream & os,
201         OutputParams const &, size_t) const
202 {
203         odocstringstream oss;
204
205         oss << '[' << getParam("target");
206         if (getParam("name").empty())
207                 oss << ']';
208         else
209                 oss << "||" << getParam("name") << ']';
210
211         docstring const str = oss.str();
212         os << str;
213         return str.size();
214 }
215
216
217 void InsetHyperlink::docbook(XMLStream & xs, OutputParams const &) const
218 {
219         xs << xml::StartTag("link", "xlink:href=\"" + subst(getParam("target"), from_ascii("&"), from_ascii("&amp;")) + "\"");
220         xs << xml::escapeString(getParam("name"));
221         xs << xml::EndTag("link");
222 }
223
224
225 docstring InsetHyperlink::xhtml(XMLStream & xs, OutputParams const &) const
226 {
227         docstring const & target =
228                 xml::xmlize(getParam("target"), XMLStream::ESCAPE_AND);
229         docstring const & name   = getParam("name");
230         xs << xml::StartTag("a", to_utf8("href=\"" + target + "\""));
231         xs << (name.empty() ? target : name);
232         xs << xml::EndTag("a");
233         return docstring();
234 }
235
236
237 void InsetHyperlink::toString(odocstream & os) const
238 {
239         odocstringstream ods;
240         plaintext(ods, OutputParams(0), INT_MAX);
241         os << ods.str();
242 }
243
244
245 void InsetHyperlink::forOutliner(docstring & os, size_t const, bool const) const
246 {
247         docstring const & n = getParam("name");
248         if (!n.empty()) {
249                 os += n;
250                 return;
251         }
252         os += getParam("target");
253 }
254
255
256 docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
257 {
258         docstring url = getParam("target");
259         docstring type = getParam("type");
260         docstring guitype = _("www");
261         if (type == "mailto:")
262                 guitype = _("email");
263         else if (type == "file:")
264                 guitype = _("file");
265         return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
266 }
267
268
269 void InsetHyperlink::validate(LaTeXFeatures & features) const
270 {
271         features.require("hyperref");
272         InsetCommand::validate(features);
273 }
274
275
276 string InsetHyperlink::contextMenuName() const
277 {
278         return "context-hyperlink";
279 }
280
281
282 } // namespace lyx