]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
213e7b59ad08ce1eabd0d73a073a1794d93f092d
[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", ""};
51         static const bool isoptional[] = {true, false};
52         static const CommandInfo info = {2, 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         if (runparams.moving_arg)
128                 os << "\\protect";
129         //set the target for the name when no name is given
130         if (!name.empty())
131                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
132         else
133                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(url) << '}';
134         return 0;
135 }
136
137
138 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
139                         OutputParams const &) const
140 {
141         odocstringstream oss;
142
143         oss << '[' << getParam("target");
144         if (getParam("name").empty())
145                 oss << ']';
146         else
147                 oss << "||" << getParam("name") << ']';
148
149         docstring const str = oss.str();
150         os << str;
151         return str.size();
152 }
153
154
155 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
156                       OutputParams const &) const
157 {
158         os << "<ulink url=\""
159            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
160            << "\">"
161            << getParam("name")
162            << "</ulink>";
163         return 0;
164 }
165
166
167 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
168                        OutputParams const & op) const
169 {
170         return plaintext(buf, os, op);
171 }
172
173
174 void InsetHyperlink::validate(LaTeXFeatures & features) const
175 {
176         features.require("hyperref");
177 }
178
179
180 } // namespace lyx