]> git.lyx.org Git - features.git/blob - src/insets/InsetHyperlink.cpp
cosmetics. mainly replace #include "dostring.h" by #include "strfwd.h"
[features.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 const * const chars_url[2] = {"%", "#"};
37
38 static char const * 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         string url = to_utf8(getParam("target"));
81
82         string backslash = "\\";
83         string braces = "{}";
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         string name = to_utf8(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                 for (size_t i = 0, pos;
107                         (pos = name.find("\\", i)) != string::npos;
108                         i = pos + 2) {
109                         if      (name[pos+1] != '\\')
110                                 name.replace(pos,1,"\\textbackslash{}");
111                 }
112                 for (int k = 0; k < 6; k++) {
113                         for (size_t i = 0, pos;
114                                 (pos = name.find(chars_name[k], i)) != string::npos;
115                                 i = pos + 2) {
116                                 name.replace(pos,1,backslash + chars_name[k] + braces);
117                         }
118                 }
119                 // replace the tilde by the \sim character as suggested in the LaTeX FAQ
120                 // for URLs
121                 for (int i = 0, pos;
122                         (pos = name.find("~", i)) != string::npos;
123                         i = pos + 1)
124                         name.replace(pos,1,"$\\sim$");
125
126         }  // end if (!name.empty())
127         
128         //for the case there is no name given, the target is set as name
129         string urlname = url;
130         // set the hyperlink type
131         url = to_utf8(getParam("type")) + url;
132
133         if (runparams.moving_arg)
134                 os << "\\protect";
135         //set the target for the name when no name is given
136         if (!name.empty())
137                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(name) << '}';
138         else
139                 os << "\\href{" << from_utf8(url) << "}{" << from_utf8(urlname) << '}';
140         return 0;
141 }
142
143
144 int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
145                         OutputParams const &) const
146 {
147         odocstringstream oss;
148
149         oss << '[' << getParam("target");
150         if (getParam("name").empty())
151                 oss << ']';
152         else
153                 oss << "||" << getParam("name") << ']';
154
155         docstring const str = oss.str();
156         os << str;
157         return str.size();
158 }
159
160
161 int InsetHyperlink::docbook(Buffer const &, odocstream & os,
162                       OutputParams const &) const
163 {
164         os << "<ulink url=\""
165            << subst(getParam("target"), from_ascii("&"), from_ascii("&amp;"))
166            << "\">"
167            << getParam("name")
168            << "</ulink>";
169         return 0;
170 }
171
172
173 int InsetHyperlink::textString(Buffer const & buf, odocstream & os,
174                        OutputParams const & op) const
175 {
176         return plaintext(buf, os, op);
177 }
178
179
180 void InsetHyperlink::validate(LaTeXFeatures & features) const
181 {
182         features.require("hyperref");
183 }
184
185
186 } // namespace lyx