2002-09-25 14:26:13 +00:00
|
|
|
/**
|
2007-04-25 01:24:38 +00:00
|
|
|
* \file InsetBibtex.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.
|
|
|
|
*
|
|
|
|
* \author Alejandro Aguilar Sierra
|
2007-08-16 01:59:20 +00:00
|
|
|
* \author Richard Heck (BibTeX parser improvements)
|
2017-01-08 08:39:46 +00:00
|
|
|
* \author Jürgen Spitzmüller
|
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-06-02 10:03:27 +00:00
|
|
|
#include <config.h>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "InsetBibtex.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2010-10-31 00:31:57 +00:00
|
|
|
#include "BiblioInfo.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
2017-01-08 08:39:46 +00:00
|
|
|
#include "CiteEnginesList.h"
|
2010-10-31 01:04:03 +00:00
|
|
|
#include "Cursor.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "DispatchResult.h"
|
|
|
|
#include "Encoding.h"
|
2011-09-15 01:07:49 +00:00
|
|
|
#include "Exporter.h"
|
2008-04-28 15:14:18 +00:00
|
|
|
#include "Format.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "FuncRequest.h"
|
2008-04-28 15:14:18 +00:00
|
|
|
#include "FuncStatus.h"
|
2005-07-14 12:53:12 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2009-06-11 22:39:16 +00:00
|
|
|
#include "output_xhtml.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "OutputParams.h"
|
2010-10-11 01:05:20 +00:00
|
|
|
#include "PDFOptions.h"
|
2016-06-19 02:39:38 +00:00
|
|
|
#include "texstream.h"
|
2007-11-07 23:25:08 +00:00
|
|
|
#include "TextClass.h"
|
2018-09-25 13:24:46 +00:00
|
|
|
#include "TocBackend.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
2007-04-28 20:44:46 +00:00
|
|
|
#include "frontends/alert.h"
|
2005-07-14 12:53:12 +00:00
|
|
|
|
2008-04-28 15:14:18 +00:00
|
|
|
#include "support/convert.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
2018-09-02 04:10:01 +00:00
|
|
|
#include "support/docstring_list.h"
|
2008-03-13 17:19:36 +00:00
|
|
|
#include "support/ExceptionMessage.h"
|
2010-10-31 00:31:57 +00:00
|
|
|
#include "support/FileNameList.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/filetools.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/gettext.h"
|
2001-07-29 17:39:01 +00:00
|
|
|
#include "support/lstrings.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
#include "support/os.h"
|
2013-02-06 10:36:58 +00:00
|
|
|
#include "support/PathChanger.h"
|
2007-04-04 20:06:34 +00:00
|
|
|
#include "support/textutils.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
2008-02-07 17:04:06 +00:00
|
|
|
#include <limits>
|
2005-07-14 12:53:12 +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 {
|
|
|
|
|
|
|
|
namespace Alert = frontend::Alert;
|
|
|
|
namespace os = support::os;
|
2003-09-16 11:03:20 +00:00
|
|
|
|
2003-02-18 12:36:02 +00:00
|
|
|
|
2009-11-08 15:53:21 +00:00
|
|
|
InsetBibtex::InsetBibtex(Buffer * buf, InsetCommandParams const & p)
|
2010-10-29 00:46:21 +00:00
|
|
|
: InsetCommand(buf, p)
|
2018-09-02 01:48:48 +00:00
|
|
|
{
|
|
|
|
buffer().removeBiblioTempFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetBibtex::~InsetBibtex()
|
|
|
|
{
|
|
|
|
if (isBufferLoaded()) {
|
|
|
|
/* We do not use buffer() because Coverity believes that this
|
|
|
|
* may throw an exception. Actually this code path is not
|
|
|
|
* taken when buffer_ == 0 */
|
|
|
|
buffer_->removeBiblioTempFiles();
|
|
|
|
}
|
|
|
|
}
|
2000-07-15 23:51:46 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-02-23 22:01:02 +00:00
|
|
|
ParamInfo const & InsetBibtex::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("btprint", ParamInfo::LATEX_OPTIONAL);
|
|
|
|
param_info_.add("bibfiles", ParamInfo::LATEX_REQUIRED);
|
|
|
|
param_info_.add("options", ParamInfo::LYX_INTERNAL);
|
2017-01-08 08:39:46 +00:00
|
|
|
param_info_.add("biblatexopts", ParamInfo::LATEX_OPTIONAL);
|
2008-02-23 22:01:02 +00:00
|
|
|
}
|
|
|
|
return param_info_;
|
2007-10-25 04:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
|
2003-02-26 19:28:38 +00:00
|
|
|
{
|
2010-04-09 19:00:42 +00:00
|
|
|
switch (cmd.action()) {
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2008-04-28 15:14:18 +00:00
|
|
|
case LFUN_INSET_EDIT:
|
|
|
|
editDatabases();
|
|
|
|
break;
|
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
case LFUN_INSET_MODIFY: {
|
2007-10-19 17:22:55 +00:00
|
|
|
InsetCommandParams p(BIBTEX_CODE);
|
2007-10-16 18:53:10 +00:00
|
|
|
try {
|
2010-10-29 00:25:28 +00:00
|
|
|
if (!InsetCommand::string2params(to_utf8(cmd.argument()), p)) {
|
2010-07-08 20:04:35 +00:00
|
|
|
cur.noScreenUpdate();
|
2007-10-16 18:53:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-12-12 19:57:42 +00:00
|
|
|
} catch (ExceptionMessage const & message) {
|
|
|
|
if (message.type_ == WarningException) {
|
2007-10-16 18:53:10 +00:00
|
|
|
Alert::warning(message.title_, message.details_);
|
2010-07-08 20:04:35 +00:00
|
|
|
cur.noScreenUpdate();
|
2011-12-03 22:15:11 +00:00
|
|
|
} else
|
2016-06-06 19:52:52 +00:00
|
|
|
throw;
|
2007-10-16 18:53:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-12-08 09:24:04 +00:00
|
|
|
|
|
|
|
cur.recordUndo();
|
2008-03-29 22:13:30 +00:00
|
|
|
setParams(p);
|
2018-09-02 01:48:48 +00:00
|
|
|
buffer().removeBiblioTempFiles();
|
2010-08-09 17:01:51 +00:00
|
|
|
cur.forceBufferUpdate();
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-03-07 21:44:48 +00:00
|
|
|
}
|
2004-04-03 08:37:12 +00:00
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
default:
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCommand::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-02-26 19:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-03 19:52:47 +00:00
|
|
|
|
2008-04-28 15:14:18 +00:00
|
|
|
bool InsetBibtex::getStatus(Cursor & cur, FuncRequest const & cmd,
|
|
|
|
FuncStatus & flag) const
|
|
|
|
{
|
2010-04-09 19:00:42 +00:00
|
|
|
switch (cmd.action()) {
|
2008-04-28 15:14:18 +00:00
|
|
|
case LFUN_INSET_EDIT:
|
2008-05-29 15:14:00 +00:00
|
|
|
flag.setEnabled(true);
|
2008-04-28 15:14:18 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return InsetCommand::getStatus(cur, cmd, flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetBibtex::editDatabases() const
|
|
|
|
{
|
|
|
|
vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
|
|
|
|
|
|
|
|
if (bibfilelist.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int nr_databases = bibfilelist.size();
|
|
|
|
if (nr_databases > 1) {
|
2017-01-14 16:48:37 +00:00
|
|
|
docstring const engine = usingBiblatex() ? _("Biblatex") : _("BibTeX");
|
2017-01-08 08:39:46 +00:00
|
|
|
docstring message = bformat(_("The %1$s[[BibTeX/Biblatex]] inset includes %2$s databases.\n"
|
2008-04-28 15:14:18 +00:00
|
|
|
"If you proceed, all of them will be opened."),
|
2017-01-08 08:39:46 +00:00
|
|
|
engine, convert<docstring>(nr_databases));
|
2008-04-28 15:14:18 +00:00
|
|
|
int const ret = Alert::prompt(_("Open Databases?"),
|
|
|
|
message, 0, 1, _("&Cancel"), _("&Proceed"));
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<docstring>::const_iterator it = bibfilelist.begin();
|
|
|
|
vector<docstring>::const_iterator en = bibfilelist.end();
|
|
|
|
for (; it != en; ++it) {
|
2018-09-02 04:10:01 +00:00
|
|
|
FileName const bibfile = buffer().getBibfilePath(*it);
|
2017-03-14 03:33:40 +00:00
|
|
|
theFormats().edit(buffer(), bibfile,
|
|
|
|
theFormats().getFormatFromFile(bibfile));
|
2008-04-28 15:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-08 08:39:46 +00:00
|
|
|
bool InsetBibtex::usingBiblatex() const
|
|
|
|
{
|
2017-01-14 13:44:18 +00:00
|
|
|
return buffer().masterParams().useBiblatex();
|
2017-01-08 08:39:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring InsetBibtex::screenLabel() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2017-01-08 08:39:46 +00:00
|
|
|
return usingBiblatex() ? _("Biblatex Generated Bibliography")
|
|
|
|
: _("BibTeX Generated Bibliography");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-28 12:48:04 +00:00
|
|
|
docstring InsetBibtex::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
|
|
|
|
{
|
2016-06-11 11:57:18 +00:00
|
|
|
docstring tip = _("Databases:");
|
2008-04-28 12:48:04 +00:00
|
|
|
vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
|
|
|
|
|
2016-06-11 11:57:18 +00:00
|
|
|
tip += "<ul>";
|
|
|
|
if (bibfilelist.empty())
|
|
|
|
tip += "<li>" + _("none") + "</li>";
|
|
|
|
else
|
|
|
|
for (docstring const & bibfile : bibfilelist)
|
|
|
|
tip += "<li>" + bibfile + "</li>";
|
|
|
|
tip += "</ul>";
|
2008-04-28 12:48:04 +00:00
|
|
|
|
|
|
|
// Style-Options
|
2008-04-28 13:13:42 +00:00
|
|
|
bool toc = false;
|
2008-04-28 12:48:04 +00:00
|
|
|
docstring style = getParam("options"); // maybe empty! and with bibtotoc
|
|
|
|
docstring bibtotoc = from_ascii("bibtotoc");
|
|
|
|
if (prefixIs(style, bibtotoc)) {
|
2008-04-28 13:13:42 +00:00
|
|
|
toc = true;
|
2008-04-28 12:48:04 +00:00
|
|
|
if (contains(style, char_type(',')))
|
|
|
|
style = split(style, bibtotoc, char_type(','));
|
|
|
|
}
|
|
|
|
|
2017-02-04 11:02:00 +00:00
|
|
|
docstring const btprint = getParam("btprint");
|
2017-01-08 08:39:46 +00:00
|
|
|
if (!usingBiblatex()) {
|
|
|
|
tip += _("Style File:");
|
|
|
|
tip += "<ul><li>" + (style.empty() ? _("none") : style) + "</li></ul>";
|
2008-04-28 12:48:04 +00:00
|
|
|
|
2017-01-08 08:39:46 +00:00
|
|
|
tip += _("Lists:") + " ";
|
2008-04-28 13:13:42 +00:00
|
|
|
if (btprint == "btPrintAll")
|
|
|
|
tip += _("all references");
|
|
|
|
else if (btprint == "btPrintNotCited")
|
|
|
|
tip += _("all uncited references");
|
|
|
|
else
|
|
|
|
tip += _("all cited references");
|
2017-01-08 08:39:46 +00:00
|
|
|
if (toc) {
|
|
|
|
tip += ", ";
|
|
|
|
tip += _("included in TOC");
|
|
|
|
}
|
2017-02-04 18:23:46 +00:00
|
|
|
if (!buffer().parent()
|
|
|
|
&& buffer().params().multibib == "child") {
|
|
|
|
tip += "<br />";
|
|
|
|
tip += _("Note: This bibliography is not output, since bibliographies in the master file "
|
|
|
|
"are not allowed with the setting 'Multiple bibliographies per child document'");
|
|
|
|
}
|
2017-01-08 08:39:46 +00:00
|
|
|
} else {
|
2017-02-04 11:02:00 +00:00
|
|
|
tip += _("Lists:") + " ";
|
|
|
|
if (btprint == "bibbysection")
|
|
|
|
tip += _("all reference units");
|
|
|
|
else if (btprint == "btPrintAll")
|
|
|
|
tip += _("all references");
|
|
|
|
else
|
|
|
|
tip += _("all cited references");
|
|
|
|
if (toc) {
|
|
|
|
tip += ", ";
|
|
|
|
tip += _("included in TOC");
|
|
|
|
}
|
2017-01-08 08:39:46 +00:00
|
|
|
if (!getParam("biblatexopts").empty()) {
|
2018-08-17 08:32:57 +00:00
|
|
|
tip += "<br />";
|
2017-01-08 08:39:46 +00:00
|
|
|
tip += _("Options: ") + getParam("biblatexopts");
|
|
|
|
}
|
2008-04-28 13:13:42 +00:00
|
|
|
}
|
|
|
|
|
2008-04-28 12:48:04 +00:00
|
|
|
return tip;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-10 20:02:48 +00:00
|
|
|
void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2017-01-08 08:39:46 +00:00
|
|
|
// The sequence of the commands:
|
|
|
|
// With normal BibTeX:
|
2001-08-20 13:42:29 +00:00
|
|
|
// 1. \bibliographystyle{style}
|
2003-09-03 17:40:58 +00:00
|
|
|
// 2. \addcontentsline{...} - if option bibtotoc set
|
|
|
|
// 3. \bibliography{database}
|
2017-01-08 08:39:46 +00:00
|
|
|
// With bibtopic:
|
2004-03-29 13:17:25 +00:00
|
|
|
// 1. \bibliographystyle{style}
|
|
|
|
// 2. \begin{btSect}{database}
|
|
|
|
// 3. \btPrint{Cited|NotCited|All}
|
|
|
|
// 4. \end{btSect}
|
2017-01-08 08:39:46 +00:00
|
|
|
// With Biblatex:
|
|
|
|
// \printbibliography[biblatexopts]
|
2017-02-04 11:02:00 +00:00
|
|
|
// or
|
|
|
|
// \bibbysection[biblatexopts] - if btprint is "bibbysection"
|
2004-04-03 08:37:12 +00:00
|
|
|
|
2017-02-04 18:23:46 +00:00
|
|
|
// chapterbib does not allow bibliographies in the master
|
|
|
|
if (!usingBiblatex() && !runparams.is_child
|
|
|
|
&& buffer().params().multibib == "child")
|
|
|
|
return;
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
|
2003-09-03 17:40:58 +00:00
|
|
|
string bibtotoc;
|
|
|
|
if (prefixIs(style, "bibtotoc")) {
|
|
|
|
bibtotoc = "bibtotoc";
|
2007-10-20 10:03:45 +00:00
|
|
|
if (contains(style, ','))
|
2003-09-03 17:40:58 +00:00
|
|
|
style = split(style, bibtotoc, ',');
|
|
|
|
}
|
2004-04-03 08:37:12 +00:00
|
|
|
|
2017-01-08 08:39:46 +00:00
|
|
|
if (usingBiblatex()) {
|
|
|
|
// Options
|
|
|
|
string opts = to_utf8(getParam("biblatexopts"));
|
|
|
|
// bibtotoc-Option
|
|
|
|
if (!bibtotoc.empty())
|
|
|
|
opts = opts.empty() ? "heading=bibintoc" : "heading=bibintoc," + opts;
|
|
|
|
// The bibliography command
|
2007-12-28 16:56:57 +00:00
|
|
|
docstring btprint = getParam("btprint");
|
2017-01-08 08:39:46 +00:00
|
|
|
if (btprint == "btPrintAll")
|
2007-12-28 16:56:57 +00:00
|
|
|
os << "\\nocite{*}\n";
|
2017-02-04 11:02:00 +00:00
|
|
|
if (btprint == "bibbysection" && !buffer().masterParams().multibib.empty())
|
|
|
|
os << "\\bibbysection";
|
|
|
|
else
|
|
|
|
os << "\\printbibliography";
|
2017-01-08 08:39:46 +00:00
|
|
|
if (!opts.empty())
|
|
|
|
os << "[" << opts << "]";
|
|
|
|
os << "\n";
|
|
|
|
} else {// using BibTeX
|
|
|
|
// Database(s)
|
|
|
|
vector<docstring> const db_out =
|
|
|
|
buffer().prepareBibFilePaths(runparams, getBibFiles(), false);
|
|
|
|
// Style options
|
|
|
|
if (style == "default")
|
2017-01-14 13:44:18 +00:00
|
|
|
style = buffer().masterParams().defaultBiblioStyle();
|
2017-01-15 14:40:20 +00:00
|
|
|
if (!style.empty() && !buffer().masterParams().useBibtopic()) {
|
2017-01-14 13:44:18 +00:00
|
|
|
string base = buffer().masterBuffer()->prepareFileNameForLaTeX(style, ".bst", runparams.nice);
|
2017-01-08 08:39:46 +00:00
|
|
|
FileName const try_in_file =
|
|
|
|
makeAbsPath(base + ".bst", buffer().filePath());
|
|
|
|
bool const not_from_texmf = try_in_file.isReadableFile();
|
|
|
|
// If this style does not come from texmf and we are not
|
|
|
|
// exporting to .tex copy it to the tmp directory.
|
|
|
|
// This prevents problems with spaces and 8bit characters
|
|
|
|
// in the file name.
|
|
|
|
if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
|
|
|
|
not_from_texmf) {
|
|
|
|
// use new style name
|
|
|
|
DocFileName const in_file = DocFileName(try_in_file);
|
|
|
|
base = removeExtension(in_file.mangledFileName());
|
|
|
|
FileName const out_file = makeAbsPath(base + ".bst",
|
|
|
|
buffer().masterBuffer()->temppath());
|
|
|
|
bool const success = in_file.copyTo(out_file);
|
|
|
|
if (!success) {
|
|
|
|
LYXERR0("Failed to copy '" << in_file
|
|
|
|
<< "' to '" << out_file << "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME UNICODE
|
|
|
|
os << "\\bibliographystyle{"
|
|
|
|
<< from_utf8(latex_path(buffer().prepareFileNameForLaTeX(base, ".bst", runparams.nice)))
|
|
|
|
<< "}\n";
|
|
|
|
}
|
|
|
|
// Warn about spaces in bst path. Warn only once.
|
|
|
|
static bool warned_about_bst_spaces = false;
|
|
|
|
if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) {
|
|
|
|
warned_about_bst_spaces = true;
|
|
|
|
Alert::warning(_("Export Warning!"),
|
|
|
|
_("There are spaces in the path to your BibTeX style file.\n"
|
|
|
|
"BibTeX will be unable to find it."));
|
|
|
|
}
|
|
|
|
// Handle the bibtopic case
|
2017-01-15 14:40:20 +00:00
|
|
|
if (!db_out.empty() && buffer().masterParams().useBibtopic()) {
|
2017-01-08 08:39:46 +00:00
|
|
|
os << "\\begin{btSect}";
|
|
|
|
if (!style.empty())
|
|
|
|
os << "[" << style << "]";
|
|
|
|
os << "{" << getStringFromVector(db_out) << "}\n";
|
|
|
|
docstring btprint = getParam("btprint");
|
|
|
|
if (btprint.empty())
|
|
|
|
// default
|
|
|
|
btprint = from_ascii("btPrintCited");
|
|
|
|
os << "\\" << btprint << "\n"
|
|
|
|
<< "\\end{btSect}\n";
|
|
|
|
}
|
|
|
|
// bibtotoc option
|
2017-01-15 14:40:20 +00:00
|
|
|
if (!bibtotoc.empty() && !buffer().masterParams().useBibtopic()) {
|
2017-01-08 08:39:46 +00:00
|
|
|
// set label for hyperref, see http://www.lyx.org/trac/ticket/6470
|
2017-01-14 13:44:18 +00:00
|
|
|
if (buffer().masterParams().pdfoptions().use_hyperref)
|
2017-01-08 08:39:46 +00:00
|
|
|
os << "\\phantomsection";
|
2017-01-14 13:44:18 +00:00
|
|
|
if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
|
2017-01-08 08:39:46 +00:00
|
|
|
os << "\\addcontentsline{toc}{chapter}{\\bibname}";
|
2017-01-14 13:44:18 +00:00
|
|
|
else if (buffer().masterParams().documentClass().hasLaTeXLayout("section"))
|
2017-01-08 08:39:46 +00:00
|
|
|
os << "\\addcontentsline{toc}{section}{\\refname}";
|
|
|
|
}
|
|
|
|
// The bibliography command
|
2017-01-15 14:40:20 +00:00
|
|
|
if (!db_out.empty() && !buffer().masterParams().useBibtopic()) {
|
2017-01-08 08:39:46 +00:00
|
|
|
docstring btprint = getParam("btprint");
|
|
|
|
if (btprint == "btPrintAll") {
|
|
|
|
os << "\\nocite{*}\n";
|
|
|
|
}
|
|
|
|
os << "\\bibliography{" << getStringFromVector(db_out) << "}\n";
|
2007-12-28 16:56:57 +00:00
|
|
|
}
|
2000-03-02 02:19:43 +00:00
|
|
|
}
|
2003-09-03 17:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-02 04:10:01 +00:00
|
|
|
docstring_list InsetBibtex::getBibFiles() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2018-09-02 04:10:01 +00:00
|
|
|
return getVectorFromString(getParam("bibfiles"));
|
2001-08-20 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// methods for parsing bibtex files
|
|
|
|
|
|
|
|
typedef map<docstring, docstring> VarMap;
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
/// remove whitespace characters, optionally a single comma,
|
2007-04-04 20:06:34 +00:00
|
|
|
/// and further whitespace characters from the stream.
|
|
|
|
/// @return true if a comma was found, false otherwise
|
|
|
|
///
|
2008-11-16 12:21:29 +00:00
|
|
|
bool removeWSAndComma(ifdocstream & ifs) {
|
2007-04-04 20:06:34 +00:00
|
|
|
char_type ch;
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
if (!ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// skip whitespace
|
|
|
|
do {
|
|
|
|
ifs.get(ch);
|
|
|
|
} while (ifs && isSpace(ch));
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
if (!ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ch != ',') {
|
|
|
|
ifs.putback(ch);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip whitespace
|
|
|
|
do {
|
|
|
|
ifs.get(ch);
|
|
|
|
} while (ifs && isSpace(ch));
|
|
|
|
|
|
|
|
if (ifs) {
|
|
|
|
ifs.putback(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-23 15:18:01 +00:00
|
|
|
|
|
|
|
enum charCase {
|
|
|
|
makeLowerCase,
|
|
|
|
keepCase
|
|
|
|
};
|
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
/// remove whitespace characters, read characer sequence
|
|
|
|
/// not containing whitespace characters or characters in
|
|
|
|
/// delimChars, and remove further whitespace characters.
|
|
|
|
///
|
|
|
|
/// @return true if a string of length > 0 could be read.
|
2007-05-28 22:27:45 +00:00
|
|
|
///
|
2008-11-16 12:21:29 +00:00
|
|
|
bool readTypeOrKey(docstring & val, ifdocstream & ifs,
|
2011-12-03 22:15:11 +00:00
|
|
|
docstring const & delimChars, docstring const & illegalChars,
|
2007-07-02 18:01:27 +00:00
|
|
|
charCase chCase) {
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
char_type ch;
|
|
|
|
|
|
|
|
val.clear();
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
if (!ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// skip whitespace
|
|
|
|
do {
|
|
|
|
ifs.get(ch);
|
|
|
|
} while (ifs && isSpace(ch));
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
if (!ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
return false;
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
// read value
|
2007-07-03 18:58:02 +00:00
|
|
|
bool legalChar = true;
|
2011-12-03 22:15:11 +00:00
|
|
|
while (ifs && !isSpace(ch) &&
|
2007-08-16 01:59:20 +00:00
|
|
|
delimChars.find(ch) == docstring::npos &&
|
|
|
|
(legalChar = (illegalChars.find(ch) == docstring::npos))
|
2011-12-03 22:15:11 +00:00
|
|
|
)
|
2007-08-16 01:59:20 +00:00
|
|
|
{
|
|
|
|
if (chCase == makeLowerCase)
|
2007-04-23 15:18:01 +00:00
|
|
|
val += lowercase(ch);
|
2007-08-16 01:59:20 +00:00
|
|
|
else
|
2007-04-23 15:18:01 +00:00
|
|
|
val += ch;
|
2007-04-04 20:06:34 +00:00
|
|
|
ifs.get(ch);
|
|
|
|
}
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-07-02 18:01:27 +00:00
|
|
|
if (!legalChar) {
|
|
|
|
ifs.putback(ch);
|
|
|
|
return false;
|
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
// skip whitespace
|
|
|
|
while (ifs && isSpace(ch)) {
|
|
|
|
ifs.get(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifs) {
|
|
|
|
ifs.putback(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.length() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// read subsequent bibtex values that are delimited with a #-character.
|
2007-05-28 22:27:45 +00:00
|
|
|
/// Concatenate all parts and replace names with the associated string in
|
2007-04-04 20:06:34 +00:00
|
|
|
/// the variable strings.
|
|
|
|
/// @return true if reading was successfull (all single parts were delimited
|
|
|
|
/// correctly)
|
2009-01-16 23:23:14 +00:00
|
|
|
bool readValue(docstring & val, ifdocstream & ifs, const VarMap & strings) {
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
char_type ch;
|
|
|
|
|
2009-01-16 23:23:14 +00:00
|
|
|
val.clear();
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
if (!ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// skip whitespace
|
|
|
|
do {
|
|
|
|
ifs.get(ch);
|
|
|
|
} while (ifs && isSpace(ch));
|
|
|
|
|
|
|
|
if (!ifs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check for field type
|
2010-12-07 00:13:19 +00:00
|
|
|
if (isDigitASCII(ch)) {
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
// read integer value
|
|
|
|
do {
|
|
|
|
val += ch;
|
|
|
|
ifs.get(ch);
|
2010-12-07 00:13:19 +00:00
|
|
|
} while (ifs && isDigitASCII(ch));
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
if (!ifs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (ch == '"' || ch == '{') {
|
2007-08-16 01:59:20 +00:00
|
|
|
// set end delimiter
|
|
|
|
char_type delim = ch == '"' ? '"': '}';
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2008-04-30 15:57:53 +00:00
|
|
|
// Skip whitespace
|
2007-08-16 01:59:20 +00:00
|
|
|
do {
|
|
|
|
ifs.get(ch);
|
|
|
|
} while (ifs && isSpace(ch));
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-08-16 01:59:20 +00:00
|
|
|
if (!ifs)
|
|
|
|
return false;
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2008-04-30 15:57:53 +00:00
|
|
|
// We now have the first non-whitespace character
|
|
|
|
// We'll collapse adjacent whitespace.
|
2007-08-16 01:59:20 +00:00
|
|
|
bool lastWasWhiteSpace = false;
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-08-16 01:59:20 +00:00
|
|
|
// inside this delimited text braces must match.
|
|
|
|
// Thus we can have a closing delimiter only
|
|
|
|
// when nestLevel == 0
|
2007-04-04 20:06:34 +00:00
|
|
|
int nestLevel = 0;
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
while (ifs && (nestLevel > 0 || ch != delim)) {
|
2007-08-16 01:59:20 +00:00
|
|
|
if (isSpace(ch)) {
|
|
|
|
lastWasWhiteSpace = true;
|
|
|
|
ifs.get(ch);
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-03 22:15:11 +00:00
|
|
|
// We output the space only after we stop getting
|
2008-04-30 15:57:53 +00:00
|
|
|
// whitespace so as not to output any whitespace
|
|
|
|
// at the end of the value.
|
2007-08-16 01:59:20 +00:00
|
|
|
if (lastWasWhiteSpace) {
|
|
|
|
lastWasWhiteSpace = false;
|
|
|
|
val += ' ';
|
|
|
|
}
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
val += ch;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
// update nesting level
|
|
|
|
switch (ch) {
|
|
|
|
case '{':
|
|
|
|
++nestLevel;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
--nestLevel;
|
2011-12-03 22:15:11 +00:00
|
|
|
if (nestLevel < 0)
|
2010-08-02 15:40:33 +00:00
|
|
|
return false;
|
2007-04-04 20:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-02 15:40:33 +00:00
|
|
|
if (ifs)
|
|
|
|
ifs.get(ch);
|
2007-04-04 20:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ifs)
|
|
|
|
return false;
|
|
|
|
|
2010-08-05 20:25:29 +00:00
|
|
|
// FIXME Why is this here?
|
2007-04-04 20:06:34 +00:00
|
|
|
ifs.get(ch);
|
|
|
|
|
|
|
|
if (!ifs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// reading a string name
|
|
|
|
docstring strName;
|
|
|
|
|
|
|
|
while (ifs && !isSpace(ch) && ch != '#' && ch != ',' && ch != '}' && ch != ')') {
|
|
|
|
strName += lowercase(ch);
|
|
|
|
ifs.get(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ifs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// replace the string with its assigned value or
|
|
|
|
// discard it if it's not assigned
|
|
|
|
if (strName.length()) {
|
|
|
|
VarMap::const_iterator pos = strings.find(strName);
|
|
|
|
if (pos != strings.end()) {
|
|
|
|
val += pos->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip WS
|
|
|
|
while (ifs && isSpace(ch)) {
|
|
|
|
ifs.get(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ifs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// continue reading next value on concatenate with '#'
|
2007-05-28 22:27:45 +00:00
|
|
|
} while (ch == '#');
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
ifs.putback(ch);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2003-02-18 12:36:02 +00:00
|
|
|
|
2017-10-18 07:20:31 +00:00
|
|
|
void InsetBibtex::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
|
2010-12-03 19:29:46 +00:00
|
|
|
{
|
2017-10-18 07:20:31 +00:00
|
|
|
parseBibTeXFiles(checkedFiles);
|
2010-12-03 19:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 07:20:31 +00:00
|
|
|
void InsetBibtex::parseBibTeXFiles(FileNameList & checkedFiles) const
|
2001-08-20 13:42:29 +00:00
|
|
|
{
|
2008-03-29 14:38:38 +00:00
|
|
|
// This bibtex parser is a first step to parse bibtex files
|
|
|
|
// more precisely.
|
|
|
|
//
|
|
|
|
// - it reads the whole bibtex entry and does a syntax check
|
|
|
|
// (matching delimiters, missing commas,...
|
|
|
|
// - it recovers from errors starting with the next @-character
|
|
|
|
// - it reads @string definitions and replaces them in the
|
|
|
|
// field values.
|
|
|
|
// - it accepts more characters in keys or value names than
|
|
|
|
// bibtex does.
|
|
|
|
//
|
|
|
|
// Officially bibtex does only support ASCII, but in practice
|
|
|
|
// you can use the encoding of the main document as long as
|
|
|
|
// some elements like keys and names are pure ASCII. Therefore
|
|
|
|
// we convert the file from the buffer encoding.
|
|
|
|
// We don't restrict keys to ASCII in LyX, since our own
|
|
|
|
// InsetBibitem can generate non-ASCII keys, and nonstandard
|
|
|
|
// 8bit clean bibtex forks exist.
|
2010-12-03 20:08:04 +00:00
|
|
|
|
|
|
|
BiblioInfo keylist;
|
|
|
|
|
2018-09-02 04:10:01 +00:00
|
|
|
docstring_list const files = getBibFiles();
|
|
|
|
for (auto const & bf : files) {
|
|
|
|
FileName const bibfile = buffer().getBibfilePath(bf);
|
|
|
|
if (bibfile.empty()) {
|
|
|
|
LYXERR0("Unable to find path for " << bf << "!");
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-18 07:20:31 +00:00
|
|
|
if (find(checkedFiles.begin(), checkedFiles.end(), bibfile) != checkedFiles.end())
|
|
|
|
// already checked this one. Skip.
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
// record that we check this.
|
|
|
|
checkedFiles.push_back(bibfile);
|
|
|
|
ifdocstream ifs(bibfile.toFilesystemEncoding().c_str(),
|
2017-01-14 13:44:18 +00:00
|
|
|
ios_base::in, buffer().masterParams().encoding().iconvName());
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
char_type ch;
|
|
|
|
VarMap strings;
|
|
|
|
|
|
|
|
while (ifs) {
|
|
|
|
ifs.get(ch);
|
2008-04-30 16:02:20 +00:00
|
|
|
if (!ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
break;
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
if (ch != '@')
|
2007-04-04 20:06:34 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
docstring entryType;
|
|
|
|
|
2008-04-30 21:04:49 +00:00
|
|
|
if (!readTypeOrKey(entryType, ifs, from_ascii("{("), docstring(), makeLowerCase)) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Error reading entry type." << std::endl;
|
2006-12-17 10:52:04 +00:00
|
|
|
continue;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2008-04-30 21:04:49 +00:00
|
|
|
if (!ifs) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
|
2008-04-30 15:57:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2008-04-30 15:57:53 +00:00
|
|
|
if (entryType == from_ascii("comment")) {
|
2007-12-12 19:28:07 +00:00
|
|
|
ifs.ignore(numeric_limits<int>::max(), '\n');
|
2007-04-04 20:06:34 +00:00
|
|
|
continue;
|
2007-05-28 22:27:45 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
ifs.get(ch);
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!ifs) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
break;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2007-04-23 15:18:01 +00:00
|
|
|
if ((ch != '(') && (ch != '{')) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Invalid entry delimiter." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
ifs.putback(ch);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// process the entry
|
|
|
|
if (entryType == from_ascii("string")) {
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
// read string and add it to the strings map
|
2007-04-04 20:06:34 +00:00
|
|
|
// (or replace it's old value)
|
|
|
|
docstring name;
|
|
|
|
docstring value;
|
|
|
|
|
2008-04-30 21:04:49 +00:00
|
|
|
if (!readTypeOrKey(name, ifs, from_ascii("="), from_ascii("#{}(),"), makeLowerCase)) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Error reading string name." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
continue;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
|
|
|
|
2008-04-30 21:04:49 +00:00
|
|
|
if (!ifs) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
|
2008-04-30 15:57:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2007-07-02 18:01:27 +00:00
|
|
|
// next char must be an equal sign
|
2007-04-04 20:06:34 +00:00
|
|
|
ifs.get(ch);
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!ifs || ch != '=') {
|
2011-12-03 22:15:11 +00:00
|
|
|
lyxerr << "BibTeX Parser: No `=' after string name: " <<
|
2008-04-30 15:57:53 +00:00
|
|
|
name << "." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
continue;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!readValue(value, ifs, strings)) {
|
2011-12-03 22:15:11 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unable to read value for string: " <<
|
2008-04-30 15:57:53 +00:00
|
|
|
name << "." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
continue;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
strings[name] = value;
|
|
|
|
|
|
|
|
} else if (entryType == from_ascii("preamble")) {
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
// preamble definitions are discarded.
|
2007-04-04 20:06:34 +00:00
|
|
|
// can they be of any use in lyx?
|
|
|
|
docstring value;
|
|
|
|
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!readValue(value, ifs, strings)) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unable to read preamble value." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
continue;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2007-08-16 01:59:20 +00:00
|
|
|
// Citation entry. Try to read the key.
|
2007-04-04 20:06:34 +00:00
|
|
|
docstring key;
|
|
|
|
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!readTypeOrKey(key, ifs, from_ascii(","), from_ascii("}"), keepCase)) {
|
2013-02-04 16:51:59 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unable to read key for entry type:" <<
|
2008-04-30 15:57:53 +00:00
|
|
|
entryType << "." << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2008-04-30 21:04:49 +00:00
|
|
|
if (!ifs) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
|
2008-04-30 21:04:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-08-16 01:59:20 +00:00
|
|
|
/////////////////////////////////////////////
|
2011-12-03 22:15:11 +00:00
|
|
|
// now we have a key, so we will add an entry
|
2007-08-16 01:59:20 +00:00
|
|
|
// (even if it's empty, as bibtex does)
|
2007-05-28 22:27:45 +00:00
|
|
|
//
|
2007-08-16 01:59:20 +00:00
|
|
|
// we now read the field = value pairs.
|
|
|
|
// all items must be separated by a comma. If
|
|
|
|
// it is missing the scanning of this entry is
|
|
|
|
// stopped and the next is searched.
|
|
|
|
docstring name;
|
|
|
|
docstring value;
|
|
|
|
docstring data;
|
2008-02-14 05:00:54 +00:00
|
|
|
BibTeXInfo keyvalmap(key, entryType);
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
bool readNext = removeWSAndComma(ifs);
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2007-04-04 20:06:34 +00:00
|
|
|
while (ifs && readNext) {
|
|
|
|
|
|
|
|
// read field name
|
2011-12-03 22:15:11 +00:00
|
|
|
if (!readTypeOrKey(name, ifs, from_ascii("="),
|
2007-07-02 18:01:27 +00:00
|
|
|
from_ascii("{}(),"), makeLowerCase) || !ifs)
|
2007-04-04 20:06:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// next char must be an equal sign
|
2010-08-05 20:25:29 +00:00
|
|
|
// FIXME Whitespace??
|
2007-04-04 20:06:34 +00:00
|
|
|
ifs.get(ch);
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!ifs) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
break;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
if (ch != '=') {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Missing `=' after field name: " <<
|
2008-04-30 15:57:53 +00:00
|
|
|
name << ", for key: " << key << "." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
ifs.putback(ch);
|
|
|
|
break;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
// read field value
|
2008-04-30 15:57:53 +00:00
|
|
|
if (!readValue(value, ifs, strings)) {
|
2010-08-09 17:33:43 +00:00
|
|
|
lyxerr << "BibTeX Parser: Unable to read value for field: " <<
|
2008-04-30 15:57:53 +00:00
|
|
|
name << ", for key: " << key << "." << std::endl;
|
2007-04-04 20:06:34 +00:00
|
|
|
break;
|
2008-04-30 15:57:53 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
2007-08-16 01:59:20 +00:00
|
|
|
keyvalmap[name] = value;
|
|
|
|
data += "\n\n" + value;
|
2008-02-14 05:28:59 +00:00
|
|
|
keylist.addFieldName(name);
|
2007-04-04 20:06:34 +00:00
|
|
|
readNext = removeWSAndComma(ifs);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2007-04-04 20:06:34 +00:00
|
|
|
|
|
|
|
// add the new entry
|
2008-02-14 05:28:59 +00:00
|
|
|
keylist.addEntryType(entryType);
|
2008-02-14 07:10:12 +00:00
|
|
|
keyvalmap.setAllData(data);
|
2007-08-20 16:30:02 +00:00
|
|
|
keylist[key] = keyvalmap;
|
2008-04-30 15:57:53 +00:00
|
|
|
} //< else (citation entry)
|
2007-04-04 20:06:34 +00:00
|
|
|
} //< searching '@'
|
|
|
|
} //< for loop over files
|
2010-12-03 20:08:04 +00:00
|
|
|
|
|
|
|
buffer().addBiblioInfo(keylist);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
2008-03-29 15:32:36 +00:00
|
|
|
bool InsetBibtex::addDatabase(docstring const & db)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2008-04-19 15:52:35 +00:00
|
|
|
docstring bibfiles = getParam("bibfiles");
|
2008-04-22 23:09:20 +00:00
|
|
|
if (tokenPos(bibfiles, ',', db) != -1)
|
2008-04-22 21:55:33 +00:00
|
|
|
return false;
|
|
|
|
if (!bibfiles.empty())
|
2008-04-19 15:52:35 +00:00
|
|
|
bibfiles += ',';
|
2008-04-22 21:55:33 +00:00
|
|
|
setParam("bibfiles", bibfiles + db);
|
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-29 15:32:36 +00:00
|
|
|
bool InsetBibtex::delDatabase(docstring const & db)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2008-04-19 15:52:35 +00:00
|
|
|
docstring bibfiles = getParam("bibfiles");
|
|
|
|
if (contains(bibfiles, db)) {
|
|
|
|
int const n = tokenPos(bibfiles, ',', db);
|
|
|
|
docstring bd = db;
|
|
|
|
if (n > 0) {
|
|
|
|
// this is not the first database
|
|
|
|
docstring tmp = ',' + bd;
|
|
|
|
setParam("bibfiles", subst(bibfiles, tmp, docstring()));
|
|
|
|
} else if (n == 0)
|
|
|
|
// this is the first (or only) database
|
|
|
|
setParam("bibfiles", split(bibfiles, bd, ','));
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-29 15:32:36 +00:00
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2004-03-29 13:17:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
void InsetBibtex::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
2017-02-04 18:23:46 +00:00
|
|
|
BufferParams const & mparams = features.buffer().masterParams();
|
|
|
|
if (mparams.useBibtopic())
|
2004-03-29 13:17:25 +00:00
|
|
|
features.require("bibtopic");
|
2017-02-04 18:23:46 +00:00
|
|
|
else if (!mparams.useBiblatex() && mparams.multibib == "child")
|
|
|
|
features.require("chapterbib");
|
2009-12-14 16:04:33 +00:00
|
|
|
// FIXME XHTML
|
|
|
|
// It'd be better to be able to get this from an InsetLayout, but at present
|
|
|
|
// InsetLayouts do not seem really to work for things that aren't InsetTexts.
|
2009-12-03 21:06:41 +00:00
|
|
|
if (features.runparams().flavor == OutputParams::HTML)
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet("div.bibtexentry { margin-left: 2em; text-indent: -2em; }\n"
|
2009-12-03 21:06:41 +00:00
|
|
|
"span.bibtexlabel:before{ content: \"[\"; }\n"
|
2011-12-06 22:17:13 +00:00
|
|
|
"span.bibtexlabel:after{ content: \"] \"; }");
|
2004-03-29 13:17:25 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2018-09-25 13:24:46 +00:00
|
|
|
docstring InsetBibtex::getRefLabel() const
|
|
|
|
{
|
|
|
|
if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
|
|
|
|
return buffer().B_("Bibliography");
|
2018-10-08 09:32:30 +00:00
|
|
|
return buffer().B_("References");
|
2018-09-25 13:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetBibtex::addToToc(DocIterator const & cpit, bool output_active,
|
|
|
|
UpdateType, TocBackend & backend) const
|
|
|
|
{
|
|
|
|
if (!prefixIs(to_utf8(getParam("options")), "bibtotoc"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
docstring const str = getRefLabel();
|
|
|
|
TocBuilder & b = backend.builder("tableofcontents");
|
|
|
|
b.pushItem(cpit, str, output_active);
|
|
|
|
b.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-08 19:52:18 +00:00
|
|
|
int InsetBibtex::plaintext(odocstringstream & os,
|
2013-03-09 17:42:56 +00:00
|
|
|
OutputParams const & op, size_t max_length) const
|
2013-01-14 16:26:55 +00:00
|
|
|
{
|
2018-09-25 13:24:46 +00:00
|
|
|
docstring const reflabel = getRefLabel();
|
2013-03-09 17:42:56 +00:00
|
|
|
|
2013-03-12 16:45:15 +00:00
|
|
|
// We could output more information here, e.g., what databases are included
|
|
|
|
// and information about options. But I don't necessarily see any reason to
|
|
|
|
// do this right now.
|
|
|
|
if (op.for_tooltip || op.for_toc || op.for_search) {
|
|
|
|
os << '[' << reflabel << ']' << '\n';
|
|
|
|
return PLAINTEXT_NEWLINE;
|
2013-03-09 17:42:56 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 16:26:55 +00:00
|
|
|
BiblioInfo bibinfo = buffer().masterBibInfo();
|
|
|
|
bibinfo.makeCitationLabels(buffer());
|
|
|
|
vector<docstring> const & cites = bibinfo.citedEntries();
|
|
|
|
|
2013-03-08 19:52:18 +00:00
|
|
|
size_t start_size = os.str().size();
|
2013-01-14 16:26:55 +00:00
|
|
|
docstring refoutput;
|
|
|
|
refoutput += reflabel + "\n\n";
|
|
|
|
|
2017-01-07 16:12:08 +00:00
|
|
|
// Tell BiblioInfo our purpose
|
|
|
|
CiteItem ci;
|
|
|
|
ci.context = CiteItem::Export;
|
|
|
|
|
2013-01-14 16:26:55 +00:00
|
|
|
// Now we loop over the entries
|
|
|
|
vector<docstring>::const_iterator vit = cites.begin();
|
|
|
|
vector<docstring>::const_iterator const ven = cites.end();
|
|
|
|
for (; vit != ven; ++vit) {
|
2013-03-08 19:52:18 +00:00
|
|
|
if (start_size + refoutput.size() >= max_length)
|
|
|
|
break;
|
2013-01-14 16:26:55 +00:00
|
|
|
BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
|
|
|
|
if (biit == bibinfo.end())
|
|
|
|
continue;
|
|
|
|
BibTeXInfo const & entry = biit->second;
|
2013-02-01 15:18:37 +00:00
|
|
|
refoutput += "[" + entry.label() + "] ";
|
2013-01-14 16:26:55 +00:00
|
|
|
// FIXME Right now, we are calling BibInfo::getInfo on the key,
|
|
|
|
// which will give us all the cross-referenced info. But for every
|
|
|
|
// entry, so there's a lot of repitition. This should be fixed.
|
2017-01-07 16:12:08 +00:00
|
|
|
refoutput += bibinfo.getInfo(entry.key(), buffer(), ci) + "\n\n";
|
2013-01-14 16:26:55 +00:00
|
|
|
}
|
|
|
|
os << refoutput;
|
|
|
|
return refoutput.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-03 22:15:11 +00:00
|
|
|
// FIXME
|
2010-01-11 16:11:55 +00:00
|
|
|
// docstring InsetBibtex::entriesAsXHTML(vector<docstring> const & entries)
|
|
|
|
// And then here just: entriesAsXHTML(buffer().masterBibInfo().citedEntries())
|
2009-11-30 22:48:10 +00:00
|
|
|
docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
|
2009-06-11 21:23:18 +00:00
|
|
|
{
|
2010-01-08 16:40:41 +00:00
|
|
|
BiblioInfo const & bibinfo = buffer().masterBibInfo();
|
2013-05-30 14:00:02 +00:00
|
|
|
bool const all_entries = getParam("btprint") == "btPrintAll";
|
2017-07-03 17:53:14 +00:00
|
|
|
vector<docstring> const & cites =
|
2013-05-30 14:00:02 +00:00
|
|
|
all_entries ? bibinfo.getKeys() : bibinfo.citedEntries();
|
2010-01-11 16:11:55 +00:00
|
|
|
|
2013-01-14 16:25:28 +00:00
|
|
|
docstring const reflabel = buffer().B_("References");
|
2011-12-03 22:15:11 +00:00
|
|
|
|
2017-01-07 16:12:08 +00:00
|
|
|
// tell BiblioInfo our purpose
|
|
|
|
CiteItem ci;
|
|
|
|
ci.context = CiteItem::Export;
|
|
|
|
ci.richtext = true;
|
|
|
|
ci.max_key_size = UINT_MAX;
|
|
|
|
|
2010-01-19 22:08:04 +00:00
|
|
|
xs << html::StartTag("h2", "class='bibtex'")
|
2010-09-15 13:28:55 +00:00
|
|
|
<< reflabel
|
2010-01-19 22:08:04 +00:00
|
|
|
<< html::EndTag("h2")
|
|
|
|
<< html::StartTag("div", "class='bibtex'");
|
2009-11-30 22:48:10 +00:00
|
|
|
|
2009-06-11 22:39:16 +00:00
|
|
|
// Now we loop over the entries
|
2010-01-08 16:40:41 +00:00
|
|
|
vector<docstring>::const_iterator vit = cites.begin();
|
|
|
|
vector<docstring>::const_iterator const ven = cites.end();
|
2009-06-11 22:39:16 +00:00
|
|
|
for (; vit != ven; ++vit) {
|
2010-01-08 16:40:41 +00:00
|
|
|
BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
|
|
|
|
if (biit == bibinfo.end())
|
|
|
|
continue;
|
2013-05-30 14:00:02 +00:00
|
|
|
|
2010-01-08 16:40:41 +00:00
|
|
|
BibTeXInfo const & entry = biit->second;
|
2017-07-03 17:53:14 +00:00
|
|
|
string const attr = "class='bibtexentry' id='LyXCite-"
|
2013-05-30 14:00:02 +00:00
|
|
|
+ to_utf8(html::cleanAttr(entry.key())) + "'";
|
|
|
|
xs << html::StartTag("div", attr);
|
2017-07-03 17:53:14 +00:00
|
|
|
|
2013-05-30 14:00:02 +00:00
|
|
|
// don't print labels if we're outputting all entries
|
|
|
|
if (!all_entries) {
|
|
|
|
xs << html::StartTag("span", "class='bibtexlabel'")
|
|
|
|
<< entry.label()
|
|
|
|
<< html::EndTag("span");
|
|
|
|
}
|
2017-07-03 17:53:14 +00:00
|
|
|
|
2009-06-11 22:39:16 +00:00
|
|
|
// FIXME Right now, we are calling BibInfo::getInfo on the key,
|
|
|
|
// which will give us all the cross-referenced info. But for every
|
2009-11-30 22:49:37 +00:00
|
|
|
// entry, so there's a lot of repitition. This should be fixed.
|
2011-12-03 22:15:11 +00:00
|
|
|
xs << html::StartTag("span", "class='bibtexinfo'")
|
2011-04-01 19:18:25 +00:00
|
|
|
<< XHTMLStream::ESCAPE_AND
|
2017-01-07 16:12:08 +00:00
|
|
|
<< bibinfo.getInfo(entry.key(), buffer(), ci)
|
2011-04-01 19:18:25 +00:00
|
|
|
<< html::EndTag("span")
|
|
|
|
<< html::EndTag("div")
|
|
|
|
<< html::CR();
|
2009-06-11 22:39:16 +00:00
|
|
|
}
|
2010-01-19 22:08:04 +00:00
|
|
|
xs << html::EndTag("div");
|
2009-06-12 17:23:17 +00:00
|
|
|
return docstring();
|
2009-06-11 21:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-03 21:55:19 +00:00
|
|
|
void InsetBibtex::write(ostream & os) const
|
|
|
|
{
|
|
|
|
params().Write(os, &buffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-29 14:48:55 +00:00
|
|
|
string InsetBibtex::contextMenuName() const
|
2008-04-28 15:14:18 +00:00
|
|
|
{
|
2011-10-29 14:48:55 +00:00
|
|
|
return "context-bibtex";
|
2008-04-28 15:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|