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