]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
942a91ea244991f2d49bf8ad09c91761dc925693
[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 docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
48 {
49         docstring const temp = from_ascii("Hyperlink: ");
50
51         docstring url;
52
53         url += getParam("name");
54         if (url.empty())
55                 url += getParam("target");
56
57         // elide if long
58         if (url.length() > 30) {
59                 url = url.substr(0, 10) + "..."
60                         + url.substr(url.length() - 17, url.length());
61         }
62         return temp + url;
63 }
64
65
66 int InsetHyperlink::latex(Buffer const &, odocstream & os,
67                     OutputParams const & runparams) const
68 {
69         string url = to_utf8(getParam("target"));
70
71         string backslash = "\\";
72         string braces = "{}";
73
74         // The characters in chars_url[] need to be changed to a command when
75         // they are in the url field.
76         if (!url.empty()) {
77                 // the chars_url[] characters must be handled for both, url and href
78                 for (int k = 0; k < 2; k++) {
79                         for (int i = 0, pos;
80                                 (pos = url.find(chars_url[k], i)) != string::npos;
81                                 i = pos + 2) {
82                                 url.replace(pos,1,backslash + chars_url[k]);
83                         }
84                 }
85         } // end if (!url.empty())
86
87         string name = to_utf8(getParam("name"));
88
89         // The characters in chars_name[] need to be changed to a command when
90         // they are in the name field.
91         if (!name.empty()) {
92
93                 // handle the "\" character, but only when the following character
94                 // is not also a "\", because "\\" is valid code
95                 for (int i = 0, pos;
96                         (pos = name.find("\\", i)) != string::npos;
97                         i = pos + 2) {
98                         if      (name[pos+1] != '\\')
99                                 name.replace(pos,1,"\\textbackslash{}");
100                 }
101                 for (int k = 0; k < 6; k++) {
102                         for (int i = 0, pos;
103                                 (pos = name.find(chars_name[k], i)) != string::npos;
104                                 i = pos + 2) {
105                                 name.replace(pos,1,backslash + chars_name[k] + braces);
106                         }
107                 }
108                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
109                 // for URLs
110                 for (int i = 0, pos;
111                         (pos = name.find("~", i)) != string::npos;
112                         i = pos + 1)
113                         name.replace(pos,1,"$\\sim$");
114
115         }  // end if (!name.empty())
116         
117         if (runparams.moving_arg)
118                 os << "\\protect";
119         //set the target for the name when no name is given
120         if (!name.empty())
121                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
122         else
123                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(url) << '}';
124         return 0;
125 }
126
127
128 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
129                         OutputParams const &) const
130 {
131         odocstringstream oss;
132
133         oss << '[' << getParam("target");
134         if (getParam("name").empty())
135                 oss << ']';
136         else
137                 oss << "||" << getParam("name") << ']';
138
139         docstring const str = oss.str();
140         os << str;
141         return str.size();
142 }
143
144
145 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
146                       OutputParams const &) const
147 {
148         os << "<ulink url=\""
149            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
150            << "\">"
151            << getParam("name")
152            << "</ulink>";
153         return 0;
154 }
155
156
157 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
158                        OutputParams const & op) const
159 {
160         return plaintext(buf, os, op);
161 }
162
163
164 void InsetHyperlink::validate(LaTeXFeatures & features) const
165 {
166         features.require("hyperref");
167 }
168
169
170 } // namespace lyx