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.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author José Matos
|
|
|
|
* \author Uwe Stö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
|
|
|
|
2010-08-29 17:23:35 +00:00
|
|
|
#include "Buffer.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "DispatchResult.h"
|
2010-08-29 17:23:35 +00:00
|
|
|
#include "Format.h"
|
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "FuncStatus.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "OutputParams.h"
|
2009-11-25 22:19:59 +00:00
|
|
|
#include "output_xhtml.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
2010-08-29 17:23:35 +00:00
|
|
|
#include "support/FileName.h"
|
|
|
|
#include "support/filetools.h"
|
2008-06-18 18:54:31 +00:00
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.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
|
|
|
|
2009-11-08 16:10:34 +00:00
|
|
|
InsetHyperlink::InsetHyperlink(Buffer * buf, InsetCommandParams const & p)
|
2010-10-29 00:46:21 +00:00
|
|
|
: InsetCommand(buf, p)
|
2000-07-27 08:55:59 +00:00
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2008-02-23 22:01:02 +00:00
|
|
|
ParamInfo const & InsetHyperlink::findInfo(string const & /* cmdName */)
|
2007-10-25 04:13:56 +00:00
|
|
|
{
|
2008-02-23 22:01:02 +00:00
|
|
|
static ParamInfo param_info_;
|
|
|
|
if (param_info_.empty()) {
|
Per Abdel's suggestion that we focus on bug-fixing at this point, this will be the last patch in this series for a bit. But I wanted to get this done before I forget what it is I was doing, so here it is.
The idea behind this patch is to make real key-value support for InsetCommand parameters possible. This should be particularly useful for the listings version of InsetInclude, though we would need some kind of UI for it before it would really be helpful. (See below for some thoughts.) This doesn't substantially change anything else, though some things do get re-arranged a bit.
Basically, the idea is this. First, we introduce a whole range of parameter types: Normal LaTeX optional and required parameters; ones for LyX's internal use (like embed); and finally, in connection with keyval, ones that represent keys and ones that represent optional and required arguments where the keyval stuff will appear. (I'm assuming here that there will always be exactly one of those, and that it will accept only keyval-type material.) The parameters themselves are stored in a map, so it's really only the output routines that need to care about the different types of parameters.
Regarding the frontend, it seems to me that something like the following would work:
(i) scan the parameter list for LATEX_KEY type parameters
(ii) the dialog will have a series of lines, each of which has a combo box listing the acceptable keys and a QLineEdit for entering its value, as well as a "delete" button of some sort for removing this key and its value
(iii) there should be an "add line" button to add a new line, activated only when all other lines are filled with values
Probably not even too hard.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23235 a592a061-630c-0410-9148-cb99ea01b6c8
2008-02-25 22:13:45 +00:00
|
|
|
param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
|
|
|
|
param_info_.add("target", ParamInfo::LATEX_REQUIRED);
|
|
|
|
param_info_.add("type", ParamInfo::LATEX_REQUIRED);
|
2008-02-23 22:01:02 +00:00
|
|
|
}
|
|
|
|
return param_info_;
|
2007-10-25 04:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring InsetHyperlink::screenLabel() 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
|
|
|
}
|
|
|
|
|
2010-08-29 17:23:35 +00:00
|
|
|
void InsetHyperlink::doDispatch(Cursor & cur, FuncRequest & cmd)
|
|
|
|
{
|
|
|
|
switch (cmd.action()) {
|
|
|
|
|
|
|
|
case LFUN_INSET_EDIT:
|
|
|
|
viewTarget();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
InsetCommand::doDispatch(cur, cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
|
|
|
|
FuncStatus & flag) const
|
|
|
|
{
|
|
|
|
switch (cmd.action()) {
|
|
|
|
case LFUN_INSET_EDIT:
|
2010-08-29 23:04:36 +00:00
|
|
|
flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
|
2010-08-29 17:23:35 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return InsetCommand::getStatus(cur, cmd, flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetHyperlink::viewTarget() const
|
|
|
|
{
|
2010-12-09 17:55:11 +00:00
|
|
|
if (getParam("type") == "file:") {
|
2010-08-29 23:04:36 +00:00
|
|
|
FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
|
2010-10-29 17:14:00 +00:00
|
|
|
string const format = formats.getFormatFromFile(url);
|
2010-08-29 23:04:36 +00:00
|
|
|
formats.view(buffer(), url, format);
|
|
|
|
}
|
2010-08-29 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
Introduce a wrapper class for odocstream to help ensuring that no
blank lines may be inadvertently output. This is achieved by using two
special iomanip-like variables (breakln and safebreakln) in the lyx::
namespace. When they are inserted in the stream, a newline is output
only if not already at the beginning of a line. The difference between
breakln and safebreakln is that, if needed, the former outputs '\n'
and the latter "%\n".
In future, the new class will also be used for counting the number of
newlines issued. Even if the infractrure for doing that is already in
place, the counting is essentially still done the old way.
There are still places in the code where the functionality of the
class could be used, most probably. ATM, it is used for InsetTabular,
InsetListings, InsetFloat, and InsetText.
The Comment and GreyedOut insets required a special treatment and a
new InsetLayout parameter (Display) has been introduced. The default
for Display is "true", meaning that the corresponding latex
environment is of "display" type, i.e., it stands on its own, whereas
"false" means that the contents appear inline with the text. The
latter is the case for both Comment and GreyedOut insets.
Mostly, the only visible effects on latex exports should be the
disappearing of some redundant % chars and the appearing/disappearing
of null {} latex groups after a comment or lyxgreyedout environments
(they are related to the presence or absence of a space immediately
after those environments), as well as the fact that math environments
are now started on their own lines.
As a last thing, only the latex code between \begin{document} and
\end{document} goes through the new class, the preamble being directly
output through odocstream, as usual.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37360 a592a061-630c-0410-9148-cb99ea01b6c8
2011-01-29 02:41:13 +00:00
|
|
|
int InsetHyperlink::latex(otexstream & os,
|
2010-08-29 16:40:52 +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");
|
2009-01-15 02:53:08 +00:00
|
|
|
docstring name = getParam("name");
|
2007-11-02 08:32:20 +00:00
|
|
|
static docstring const backslash = from_ascii("\\");
|
|
|
|
static docstring const braces = from_ascii("{}");
|
2009-04-22 00:55:55 +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
|
|
|
|
2009-01-15 02:53:08 +00:00
|
|
|
// For the case there is no name given, the target is set as name.
|
|
|
|
// Do this before !url.empty() and !name.empty() to handle characters
|
|
|
|
// like the "%" correctly.
|
|
|
|
if (name.empty())
|
|
|
|
name = url;
|
|
|
|
|
2007-11-02 17:13:39 +00:00
|
|
|
if (!url.empty()) {
|
2009-04-22 00:55:55 +00:00
|
|
|
// Replace the "\" character by its ASCII code according to the
|
|
|
|
// URL specifications because "\" is not allowed in URLs and by
|
|
|
|
// \href. Only do this when the following character is not also
|
|
|
|
// a "\", because "\\" is valid code
|
2009-01-14 01:27:01 +00:00
|
|
|
for (size_t i = 0, pos;
|
|
|
|
(pos = url.find('\\', i)) != string::npos;
|
|
|
|
i = pos + 2) {
|
|
|
|
if (url[pos + 1] != '\\')
|
2009-01-15 02:53:08 +00:00
|
|
|
url.replace(pos, 1, from_ascii("%5C"));
|
2009-01-14 01:27:01 +00:00
|
|
|
}
|
2009-04-22 00:55:55 +00:00
|
|
|
|
|
|
|
// The characters in chars_url[] need to be escaped in the url
|
|
|
|
// field because otherwise LaTeX will fail when the hyperlink is
|
|
|
|
// within an argument of another command, e.g. in a \footnote. It
|
|
|
|
// is important that they are escaped as "\#" and not as "\#{}".
|
2010-08-29 16:40:52 +00:00
|
|
|
for (int k = 0; k < 2; k++)
|
2009-04-22 00:55:55 +00:00
|
|
|
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]);
|
2009-01-15 02:53:08 +00:00
|
|
|
|
2008-08-19 22:06:10 +00:00
|
|
|
// add "http://" when the type is web (type = empty)
|
2008-08-29 22:44:48 +00:00
|
|
|
// and no "://" or "run:" is given
|
2008-08-19 22:06:10 +00:00
|
|
|
docstring type = getParam("type");
|
2008-08-29 22:44:48 +00:00
|
|
|
if (url.find(from_ascii("://")) == string::npos
|
|
|
|
&& url.find(from_ascii("run:")) == string::npos
|
2008-08-30 14:52:56 +00:00
|
|
|
&& type.empty())
|
2008-08-19 22:06:10 +00:00
|
|
|
url = from_ascii("http://") + url;
|
|
|
|
|
2007-11-02 17:13:39 +00:00
|
|
|
} // end if (!url.empty())
|
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
|
|
|
}
|
2009-04-22 00:55:55 +00:00
|
|
|
// The characters in chars_name[] need to be changed to a command
|
|
|
|
// when they are in the name field.
|
2009-01-15 02:53:08 +00:00
|
|
|
// Therefore the treatment of "\" must be the first thing
|
2010-08-29 16:40:52 +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;
|
2009-04-22 00:55:55 +00:00
|
|
|
i = pos + 2)
|
2007-11-02 08:09:43 +00:00
|
|
|
name.replace(pos, 1, backslash + chars_name[k] + braces);
|
2009-04-22 00:55:55 +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
|
|
|
|
2009-01-15 02:53:08 +00:00
|
|
|
// output the ready \href command
|
|
|
|
os << "\\href{" << getParam("type") << url << "}{" << name << '}';
|
2007-11-02 13:56:46 +00:00
|
|
|
|
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
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
int InsetHyperlink::plaintext(odocstream & os, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
int InsetHyperlink::docbook(odocstream & os, 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
|
|
|
|
2009-11-25 22:19:59 +00:00
|
|
|
docstring InsetHyperlink::xhtml(XHTMLStream & xs, OutputParams const &) const
|
2009-06-05 17:48:14 +00:00
|
|
|
{
|
2009-11-28 21:32:46 +00:00
|
|
|
docstring const & target = getParam("target");
|
|
|
|
docstring const & name = getParam("name");
|
2010-01-19 22:08:04 +00:00
|
|
|
xs << html::StartTag("a", to_utf8("href=\"" + target + "\""));
|
2009-11-28 21:32:46 +00:00
|
|
|
xs << (name.empty() ? target : name);
|
2010-01-19 22:08:04 +00:00
|
|
|
xs << html::EndTag("a");
|
2009-06-12 17:23:17 +00:00
|
|
|
return docstring();
|
2009-06-05 17:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 01:34:04 +00:00
|
|
|
void InsetHyperlink::toString(odocstream & os) const
|
2005-11-25 14:40:34 +00:00
|
|
|
{
|
2008-02-27 20:43:16 +00:00
|
|
|
plaintext(os, OutputParams(0));
|
2005-11-25 14:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 21:55:09 +00:00
|
|
|
void InsetHyperlink::forToc(docstring & os, size_t) const
|
|
|
|
{
|
|
|
|
docstring const & n = getParam("name");
|
|
|
|
if (!n.empty()) {
|
|
|
|
os += n;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
os += getParam("target");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-29 16:40:52 +00:00
|
|
|
docstring InsetHyperlink::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
|
|
|
|
{
|
|
|
|
docstring url = getParam("target");
|
|
|
|
docstring type = getParam("type");
|
|
|
|
docstring guitype = _("www");
|
|
|
|
if (type == "mailto:")
|
|
|
|
guitype = _("email");
|
|
|
|
else if (type == "file:")
|
|
|
|
guitype = _("file");
|
|
|
|
return bformat(_("Hyperlink (%1$s) to %2$s"), guitype, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2010-11-29 21:27:25 +00:00
|
|
|
docstring InsetHyperlink::contextMenuName() const
|
2010-08-29 16:40:52 +00:00
|
|
|
{
|
|
|
|
return from_ascii("context-hyperlink");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|