]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
Move debug.{cpp,h}, Messages.{cpp,h} and gettext.{cpp,h} to support/.
[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 "support/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 int InsetHyperlink::latex(Buffer const &, odocstream & os,
69                     OutputParams const & runparams) const
70 {
71         docstring url = getParam("target");
72         static docstring const backslash = from_ascii("\\");
73         static docstring const braces = from_ascii("{}");
74         static char_type const chars_url[2] = {'%', '#'};
75         static char_type const chars_name[6] = {
76                 '&', '_', '$', '%', '#', '^'};
77
78         // The characters in chars_url[] need to be changed to a command when
79         // they are in the url field.
80         if (!url.empty()) {
81                 // the chars_url[] characters must be handled for both, url and href
82                 for (int k = 0; k < 2; k++) {
83                         for (size_t i = 0, pos;
84                                 (pos = url.find(chars_url[k], i)) != string::npos;
85                                 i = pos + 2) {
86                                 url.replace(pos, 1, backslash + chars_url[k]);
87                         }
88                 }
89         } // end if (!url.empty())
90
91         docstring name = getParam("name");
92
93         // The characters in chars_name[] need to be changed to a command when
94         // they are in the name field.
95         if (!name.empty()) {
96
97                 // handle the "\" character, but only when the following character
98                 // is not also a "\", because "\\" is valid code
99                 docstring const textbackslash = from_ascii("\\textbackslash{}");
100                 for (size_t i = 0, pos;
101                         (pos = name.find('\\', i)) != string::npos;
102                         i = pos + 2) {
103                         if (name[pos + 1] != '\\')
104                                 name.replace(pos, 1, textbackslash);
105                 }
106                 for (int k = 0; k < 6; k++) {
107                         for (size_t i = 0, pos;
108                                 (pos = name.find(chars_name[k], i)) != string::npos;
109                                 i = pos + 2) {
110                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
111                         }
112                 }
113                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
114                 // for URLs
115                 docstring const sim = from_ascii("$\\sim$");
116                 for (size_t i = 0, pos;
117                         (pos = name.find('~', i)) != string::npos;
118                         i = pos + 1)
119                         name.replace(pos, 1, sim);
120
121         }  // end if (!name.empty())
122         
123         if (runparams.moving_arg)
124                 os << "\\protect";
125
126         //for the case there is no name given, the target is set as name
127         os << "\\href{" << getParam("type") << url << "}{"
128                 << (name.empty()? url : name) << '}';
129
130         return 0;
131 }
132
133
134 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
135                         OutputParams const &) const
136 {
137         odocstringstream oss;
138
139         oss << '[' << getParam("target");
140         if (getParam("name").empty())
141                 oss << ']';
142         else
143                 oss << "||" << getParam("name") << ']';
144
145         docstring const str = oss.str();
146         os << str;
147         return str.size();
148 }
149
150
151 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
152                       OutputParams const &) const
153 {
154         os << "<ulink url=\""
155            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
156            << "\">"
157            << getParam("name")
158            << "</ulink>";
159         return 0;
160 }
161
162
163 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
164                        OutputParams const & op) const
165 {
166         return plaintext(buf, os, op);
167 }
168
169
170 void InsetHyperlink::validate(LaTeXFeatures & features) const
171 {
172         features.require("hyperref");
173 }
174
175
176 } // namespace lyx