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