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