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