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