2002-09-25 14:26:13 +00:00
|
|
|
|
/**
|
2007-10-13 19:06:09 +00:00
|
|
|
|
* \file InsetHyperlink.cpp
|
2002-09-25 14:26:13 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
2007-10-14 10:41:10 +00:00
|
|
|
|
* \author Jos<EFBFBD> Matos
|
|
|
|
|
* \author Uwe St<EFBFBD>hr
|
2002-09-25 14:26:13 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 14:26:13 +00:00
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-05-16 07:44:00 +00:00
|
|
|
|
#include <config.h>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
#include "InsetHyperlink.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "DispatchResult.h"
|
|
|
|
|
#include "FuncRequest.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
#include "LaTeXFeatures.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
|
#include "support/gettext.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "OutputParams.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-09-05 09:01:27 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
|
#include "support/docstream.h"
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2000-07-15 23:51:46 +00:00
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
InsetHyperlink::InsetHyperlink(InsetCommandParams const & p)
|
|
|
|
|
: InsetCommand(p, "href")
|
2000-07-27 08:55:59 +00:00
|
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2007-11-02 08:18:02 +00:00
|
|
|
|
CommandInfo const * InsetHyperlink::findInfo(string const & /* cmdName */)
|
2007-10-25 04:13:56 +00:00
|
|
|
|
{
|
|
|
|
|
static const char * const paramnames[] =
|
2007-11-01 17:37:43 +00:00
|
|
|
|
{"name", "target", "type", ""};
|
2007-10-25 04:13:56 +00:00
|
|
|
|
static const bool isoptional[] = {true, false};
|
2007-11-01 17:37:43 +00:00
|
|
|
|
static const CommandInfo info = {3, paramnames, isoptional};
|
2007-10-25 04:13:56 +00:00
|
|
|
|
return &info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
docstring const InsetHyperlink::getScreenLabel(Buffer const &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2007-10-13 19:06:09 +00:00
|
|
|
|
docstring const temp = from_ascii("Hyperlink: ");
|
2000-07-27 08:55:59 +00:00
|
|
|
|
|
2006-10-20 16:12:49 +00:00
|
|
|
|
docstring url;
|
2002-11-27 10:30:28 +00:00
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
url += getParam("name");
|
|
|
|
|
if (url.empty())
|
2006-10-20 16:12:49 +00:00
|
|
|
|
url += getParam("target");
|
2002-08-25 01:15:01 +00:00
|
|
|
|
|
|
|
|
|
// elide if long
|
|
|
|
|
if (url.length() > 30) {
|
|
|
|
|
url = url.substr(0, 10) + "..."
|
|
|
|
|
+ url.substr(url.length() - 17, url.length());
|
|
|
|
|
}
|
2006-10-20 16:12:49 +00:00
|
|
|
|
return temp + url;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
int InsetHyperlink::latex(Buffer const &, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const & runparams) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2007-11-02 08:09:43 +00:00
|
|
|
|
docstring url = getParam("target");
|
2007-11-02 08:32:20 +00:00
|
|
|
|
static docstring const backslash = from_ascii("\\");
|
|
|
|
|
static docstring const braces = from_ascii("{}");
|
2007-11-02 17:13:39 +00:00
|
|
|
|
static char_type const chars_url[2] = {'%', '#'};
|
2007-11-02 08:32:20 +00:00
|
|
|
|
static char_type const chars_name[6] = {
|
|
|
|
|
'&', '_', '$', '%', '#', '^'};
|
2007-10-14 03:10:51 +00:00
|
|
|
|
|
|
|
|
|
// The characters in chars_url[] need to be changed to a command when
|
|
|
|
|
// they are in the url field.
|
2007-11-02 17:13:39 +00:00
|
|
|
|
if (!url.empty()) {
|
|
|
|
|
// the chars_url[] characters must be handled for both, url and href
|
|
|
|
|
for (int k = 0; k < 2; k++) {
|
|
|
|
|
for (size_t i = 0, pos;
|
|
|
|
|
(pos = url.find(chars_url[k], i)) != string::npos;
|
|
|
|
|
i = pos + 2) {
|
|
|
|
|
url.replace(pos, 1, backslash + chars_url[k]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // end if (!url.empty())
|
2007-10-14 03:10:51 +00:00
|
|
|
|
|
2007-11-02 08:09:43 +00:00
|
|
|
|
docstring name = getParam("name");
|
2007-10-14 03:10:51 +00:00
|
|
|
|
|
|
|
|
|
// The characters in chars_name[] need to be changed to a command when
|
|
|
|
|
// they are in the name field.
|
|
|
|
|
if (!name.empty()) {
|
|
|
|
|
|
|
|
|
|
// handle the "\" character, but only when the following character
|
|
|
|
|
// is not also a "\", because "\\" is valid code
|
2007-11-02 08:32:20 +00:00
|
|
|
|
docstring const textbackslash = from_ascii("\\textbackslash{}");
|
2007-11-01 22:17:22 +00:00
|
|
|
|
for (size_t i = 0, pos;
|
2007-11-02 08:09:43 +00:00
|
|
|
|
(pos = name.find('\\', i)) != string::npos;
|
2007-10-14 03:10:51 +00:00
|
|
|
|
i = pos + 2) {
|
2007-11-02 23:42:27 +00:00
|
|
|
|
if (name[pos + 1] != '\\')
|
2007-11-02 08:09:43 +00:00
|
|
|
|
name.replace(pos, 1, textbackslash);
|
2007-10-14 03:10:51 +00:00
|
|
|
|
}
|
|
|
|
|
for (int k = 0; k < 6; k++) {
|
2007-11-01 22:17:22 +00:00
|
|
|
|
for (size_t i = 0, pos;
|
2007-10-14 03:10:51 +00:00
|
|
|
|
(pos = name.find(chars_name[k], i)) != string::npos;
|
|
|
|
|
i = pos + 2) {
|
2007-11-02 08:09:43 +00:00
|
|
|
|
name.replace(pos, 1, backslash + chars_name[k] + braces);
|
2007-10-14 03:10:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// replace the tilde by the \sim character as suggested in the LaTeX FAQ
|
|
|
|
|
// for URLs
|
2007-11-02 08:32:20 +00:00
|
|
|
|
docstring const sim = from_ascii("$\\sim$");
|
2007-11-02 23:42:27 +00:00
|
|
|
|
for (size_t i = 0, pos;
|
2007-11-02 08:09:43 +00:00
|
|
|
|
(pos = name.find('~', i)) != string::npos;
|
2007-10-14 03:10:51 +00:00
|
|
|
|
i = pos + 1)
|
2007-11-02 08:09:43 +00:00
|
|
|
|
name.replace(pos, 1, sim);
|
2007-10-14 03:10:51 +00:00
|
|
|
|
|
|
|
|
|
} // end if (!name.empty())
|
|
|
|
|
|
2003-05-23 09:23:03 +00:00
|
|
|
|
if (runparams.moving_arg)
|
2000-03-02 02:19:43 +00:00
|
|
|
|
os << "\\protect";
|
2007-11-02 13:56:46 +00:00
|
|
|
|
|
|
|
|
|
//for the case there is no name given, the target is set as name
|
|
|
|
|
os << "\\href{" << getParam("type") << url << "}{"
|
|
|
|
|
<< (name.empty()? url : name) << '}';
|
|
|
|
|
|
2000-03-02 02:19:43 +00:00
|
|
|
|
return 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-03-06 02:42:40 +00:00
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
int InsetHyperlink::plaintext(Buffer const &, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const &) const
|
2000-04-24 20:58:23 +00:00
|
|
|
|
{
|
2007-02-16 09:28:25 +00:00
|
|
|
|
odocstringstream oss;
|
|
|
|
|
|
|
|
|
|
oss << '[' << getParam("target");
|
2006-10-20 16:12:49 +00:00
|
|
|
|
if (getParam("name").empty())
|
2007-02-16 09:28:25 +00:00
|
|
|
|
oss << ']';
|
2000-04-24 20:58:23 +00:00
|
|
|
|
else
|
2007-02-16 09:28:25 +00:00
|
|
|
|
oss << "||" << getParam("name") << ']';
|
|
|
|
|
|
2007-02-24 14:35:38 +00:00
|
|
|
|
docstring const str = oss.str();
|
2007-02-16 09:28:25 +00:00
|
|
|
|
os << str;
|
|
|
|
|
return str.size();
|
2000-04-24 20:58:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
int InsetHyperlink::docbook(Buffer const &, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const &) const
|
2000-03-06 02:42:40 +00:00
|
|
|
|
{
|
2007-05-28 22:27:45 +00:00
|
|
|
|
os << "<ulink url=\""
|
2006-10-21 00:16:43 +00:00
|
|
|
|
<< subst(getParam("target"), from_ascii("&"), from_ascii("&"))
|
2007-05-28 22:27:45 +00:00
|
|
|
|
<< "\">"
|
2006-10-20 16:12:49 +00:00
|
|
|
|
<< getParam("name")
|
|
|
|
|
<< "</ulink>";
|
2000-03-06 02:42:40 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2008-01-10 16:09:01 +00:00
|
|
|
|
void InsetHyperlink::textString(Buffer const & buf, odocstream & os) const
|
2005-11-25 14:40:34 +00:00
|
|
|
|
{
|
2008-01-10 16:09:01 +00:00
|
|
|
|
plaintext(buf, os, OutputParams(0));
|
2005-11-25 14:40:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-13 19:06:09 +00:00
|
|
|
|
void InsetHyperlink::validate(LaTeXFeatures & features) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2007-10-13 19:06:09 +00:00
|
|
|
|
features.require("hyperref");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|