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