]> git.lyx.org Git - lyx.git/blob - src/insets/InsetHyperlink.cpp
Restore XHTML output for InsetListings.
[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 "OutputParams.h"
20 #include "output_xhtml.h"
21
22 #include "support/docstream.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
33         : InsetCommand(buf, p, "href")
34 {}
35
36
37 ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
38 {
39         static ParamInfo param_info_;
40         if (param_info_.empty()) {
41                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
42                 param_info_.add("target", ParamInfo::LATEX_REQUIRED);
43                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
44         }
45         return param_info_;
46 }
47
48
49 docstring InsetHyperlink::screenLabel() 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(odocstream & os,
69                                                   OutputParams const & runparams) const
70 {
71         docstring url = getParam("target");
72         docstring name = getParam("name");
73         static docstring const backslash = from_ascii("\\");
74         static docstring const braces = from_ascii("{}");
75         static char_type const chars_url[2] = {'%', '#'};
76         static char_type const chars_name[6] = {
77                 '&', '_', '$', '%', '#', '^'};
78
79         // For the case there is no name given, the target is set as name.
80         // Do this before !url.empty() and !name.empty() to handle characters
81         // like the "%" correctly.
82         if (name.empty())
83                 name = url;
84
85         if (!url.empty()) {
86                 // Replace the "\" character by its ASCII code according to the
87                 // URL specifications because "\" is not allowed in URLs and by
88                 // \href. Only do this when the following character is not also
89                 // a "\", because "\\" is valid code
90                 for (size_t i = 0, pos;
91                         (pos = url.find('\\', i)) != string::npos;
92                         i = pos + 2) {
93                         if (url[pos + 1] != '\\')
94                                 url.replace(pos, 1, from_ascii("%5C"));
95                 }
96
97                 // The characters in chars_url[] need to be escaped in the url
98                 // field because otherwise LaTeX will fail when the hyperlink is
99                 // within an argument of another command, e.g. in a \footnote. It
100                 // is important that they are escaped as "\#" and not as "\#{}".
101                 for (int k = 0; k < 2; k++)
102                         for (size_t i = 0, pos;
103                                 (pos = url.find(chars_url[k], i)) != string::npos;
104                                 i = pos + 2)
105                                 url.replace(pos, 1, backslash + chars_url[k]);
106                 
107                 // add "http://" when the type is web (type = empty)
108                 // and no "://" or "run:" is given
109                 docstring type = getParam("type");
110                 if (url.find(from_ascii("://")) == string::npos
111                         && url.find(from_ascii("run:")) == string::npos
112                         && type.empty())
113                         url = from_ascii("http://") + url;
114
115         } // end if (!url.empty())
116
117         // The characters in chars_name[] need to be changed to a command when
118         // they are in the name field.
119         if (!name.empty()) {
120                 // handle the "\" character, but only when the following character
121                 // is not also a "\", because "\\" is valid code
122                 docstring const textbackslash = from_ascii("\\textbackslash{}");
123                 for (size_t i = 0, pos;
124                         (pos = name.find('\\', i)) != string::npos;
125                         i = pos + 2) {
126                         if (name[pos + 1] != '\\')
127                                 name.replace(pos, 1, textbackslash);
128                 }
129                 // The characters in chars_name[] need to be changed to a command
130                 // when they are in the name field.
131                 // Therefore the treatment of "\" must be the first thing
132                 for (int k = 0; k < 6; k++)
133                         for (size_t i = 0, pos;
134                                 (pos = name.find(chars_name[k], i)) != string::npos;
135                                 i = pos + 2)
136                                 name.replace(pos, 1, backslash + chars_name[k] + braces);
137
138                 // replace the tilde by the \sim character as suggested in the
139                 // LaTeX FAQ for URLs
140                 docstring const sim = from_ascii("$\\sim$");
141                 for (size_t i = 0, pos;
142                         (pos = name.find('~', i)) != string::npos;
143                         i = pos + 1)
144                         name.replace(pos, 1, sim);
145
146         }  // end if (!name.empty())
147         
148         if (runparams.moving_arg)
149                 os << "\\protect";
150
151         // output the ready \href command
152         os << "\\href{" << getParam("type") << url << "}{" << name << '}';
153
154         return 0;
155 }
156
157
158 int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
159 {
160         odocstringstream oss;
161
162         oss << '[' << getParam("target");
163         if (getParam("name").empty())
164                 oss << ']';
165         else
166                 oss << "||" << getParam("name") << ']';
167
168         docstring const str = oss.str();
169         os << str;
170         return str.size();
171 }
172
173
174 int InsetHyperlink::docbook(odocstream & os, OutputParams const &) const
175 {
176         os << "<ulink url=\""
177            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
178            << "\">"
179            << getParam("name")
180            << "</ulink>";
181         return 0;
182 }
183
184
185 docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
186 {
187         docstring const & target = getParam("target");
188         docstring const & name   = getParam("name");
189         xs << StartTag("a", to_utf8("href=\"" + target + "\""));
190         xs << (name.empty() ? target : name);
191         xs << EndTag("a");
192         return docstring();
193 }
194
195
196 void InsetHyperlink::tocString(odocstream & os) const
197 {
198         plaintext(os, OutputParams(0));
199 }
200
201
202 void InsetHyperlink::validate(LaTeXFeatures & features) const
203 {
204         features.require("hyperref");
205 }
206
207
208 } // namespace lyx