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