]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
488b68a42ca42895dd7f7d9b172c4951038f0ab1
[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 "gettext.h"
20 #include "OutputParams.h"
21
22 #include "support/lstrings.h"
23
24 #include "support/std_ostream.h"
25
26
27 namespace lyx {
28
29 using support::subst;
30
31 using std::string;
32 using std::ostream;
33 using std::find;
34 using std::replace;
35
36 static char const * const chars_url[2] = {"%", "#"};
37
38 static char const * const chars_name[6] = {
39         "&", "_", "$", "%", "#", "^"};
40
41
42 InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
43         : InsetCommand(p, "href")
44 {}
45
46
47 CommandInfo const * InsetHyperlink::findInfo(std::string const & /* cmdName */)
48 {
49         static const char * const paramnames[] =
50                 {"name", "target", "type", ""};
51         static const bool isoptional[] = {true, false};
52         static const CommandInfo info = {3, paramnames, isoptional};
53         return &info;
54 }
55
56
57 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
58 {
59         docstring const temp = from_ascii("Hyperlink: ");
60
61         docstring url;
62
63         url += getParam("name");
64         if (url.empty())
65                 url += getParam("target");
66
67         // elide if long
68         if (url.length() > 30) {
69                 url = url.substr(0, 10) + "..."
70                         + url.substr(url.length() - 17, url.length());
71         }
72         return temp + url;
73 }
74
75
76 int InsetHyperlink::latex(Buffer const &, odocstream & os,
77                     OutputParams const & runparams) const
78 {
79         string url = to_utf8(getParam("target"));
80
81         string backslash = "\\";
82         string braces = "{}";
83
84         // The characters in chars_url[] need to be changed to a command when
85         // they are in the url field.
86         if (!url.empty()) {
87                 // the chars_url[] characters must be handled for both, url and href
88                 for (int k = 0; k < 2; k++) {
89                         for (int i = 0, pos;
90                                 (pos = url.find(chars_url[k], i)) != string::npos;
91                                 i = pos + 2) {
92                                 url.replace(pos,1,backslash + chars_url[k]);
93                         }
94                 }
95         } // end if (!url.empty())
96
97         string name = to_utf8(getParam("name"));
98
99         // The characters in chars_name[] need to be changed to a command when
100         // they are in the name field.
101         if (!name.empty()) {
102
103                 // handle the "\" character, but only when the following character
104                 // is not also a "\", because "\\" is valid code
105                 for (int i = 0, pos;
106                         (pos = name.find("\\", i)) != string::npos;
107                         i = pos + 2) {
108                         if      (name[pos+1] != '\\')
109                                 name.replace(pos,1,"\\textbackslash{}");
110                 }
111                 for (int k = 0; k < 6; k++) {
112                         for (int i = 0, pos;
113                                 (pos = name.find(chars_name[k], i)) != string::npos;
114                                 i = pos + 2) {
115                                 name.replace(pos,1,backslash + chars_name[k] + braces);
116                         }
117                 }
118                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
119                 // for URLs
120                 for (int i = 0, pos;
121                         (pos = name.find("~", i)) != string::npos;
122                         i = pos + 1)
123                         name.replace(pos,1,"$\\sim$");
124
125         }  // end if (!name.empty())
126         
127         //for the case there is no name given, the target is set as name
128         string urlname = url;
129         // set the hyperlink type
130         url = to_utf8(getParam("type")) + url;
131
132         if (runparams.moving_arg)
133                 os << "\\protect";
134         //set the target for the name when no name is given
135         if (!name.empty())
136                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
137         else
138                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(urlname) << '}';
139         return 0;
140 }
141
142
143 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
144                         OutputParams const &) const
145 {
146         odocstringstream oss;
147
148         oss << '[' << getParam("target");
149         if (getParam("name").empty())
150                 oss << ']';
151         else
152                 oss << "||" << getParam("name") << ']';
153
154         docstring const str = oss.str();
155         os << str;
156         return str.size();
157 }
158
159
160 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
161                       OutputParams const &) const
162 {
163         os << "<ulink url=\""
164            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
165            << "\">"
166            << getParam("name")
167            << "</ulink>";
168         return 0;
169 }
170
171
172 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
173                        OutputParams const & op) const
174 {
175         return plaintext(buf, os, op);
176 }
177
178
179 void InsetHyperlink::validate(LaTeXFeatures & features) const
180 {
181         features.require("hyperref");
182 }
183
184
185 } // namespace lyx