]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
b60b2b104236d4bab9ec417768ac7cd80fce08c5
[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, OutputParams const & runparams) const
68 {
69         docstring url = getParam("target");
70         static docstring const backslash = from_ascii("\\");
71         static docstring const braces = from_ascii("{}");
72         static char_type const chars_url[2] = {'%', '#'};
73         static char_type const chars_name[6] = {
74                 '&', '_', '$', '%', '#', '^'};
75
76         // The characters in chars_url[] need to be changed to a command when
77         // they are in the url field.
78         if (!url.empty()) {
79                 // the chars_url[] characters must be handled for both, url and href
80                 for (int k = 0; k < 2; k++) {
81                         for (size_t i = 0, pos;
82                                 (pos = url.find(chars_url[k], i)) != string::npos;
83                                 i = pos + 2) {
84                                 url.replace(pos, 1, backslash + chars_url[k]);
85                         }
86                 }
87
88                 // add "http://" when the type is web (type = empty)
89                 // and no "://" or "run:" is given
90                 docstring type = getParam("type");
91                 if (url.find(from_ascii("://")) == string::npos
92                         && url.find(from_ascii("run:")) == string::npos
93                         && type == "")
94                         url = from_ascii("http://") + url;
95
96         } // end if (!url.empty())
97
98         docstring name = getParam("name");
99
100         // The characters in chars_name[] need to be changed to a command when
101         // they are in the name field.
102         if (!name.empty()) {
103
104                 // handle the "\" character, but only when the following character
105                 // is not also a "\", because "\\" is valid code
106                 docstring const textbackslash = from_ascii("\\textbackslash{}");
107                 for (size_t i = 0, pos;
108                         (pos = name.find('\\', i)) != string::npos;
109                         i = pos + 2) {
110                         if (name[pos + 1] != '\\')
111                                 name.replace(pos, 1, textbackslash);
112                 }
113                 for (int k = 0; k < 6; k++) {
114                         for (size_t i = 0, pos;
115                                 (pos = name.find(chars_name[k], i)) != string::npos;
116                                 i = pos + 2) {
117                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
118                         }
119                 }
120                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
121                 // for URLs
122                 docstring const sim = from_ascii("$\\sim$");
123                 for (size_t i = 0, pos;
124                         (pos = name.find('~', i)) != string::npos;
125                         i = pos + 1)
126                         name.replace(pos, 1, sim);
127
128         }  // end if (!name.empty())
129         
130         if (runparams.moving_arg)
131                 os << "\\protect";
132
133         //for the case there is no name given, the target is set as name
134         os << "\\href{" << getParam("type") << url << "}{"
135                 << (name.empty()? url : name) << '}';
136
137         return 0;
138 }
139
140
141 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
142 {
143         odocstringstream oss;
144
145         oss << '[' << getParam("target");
146         if (getParam("name").empty())
147                 oss << ']';
148         else
149                 oss << "||" << getParam("name") << ']';
150
151         docstring const str = oss.str();
152         os << str;
153         return str.size();
154 }
155
156
157 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
158 {
159         os << "<ulink url=\""
160            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
161            << "\">"
162            << getParam("name")
163            << "</ulink>";
164         return 0;
165 }
166
167
168 void InsetHyperlink::textString(odocstream & os) const
169 {
170         plaintext(os, OutputParams(0));
171 }
172
173
174 void InsetHyperlink::validate(LaTeXFeatures & features) const
175 {
176         features.require("hyperref");
177 }
178
179
180 } // namespace lyx