lyx_mirror/src/CiteEnginesList.cpp

314 lines
7.3 KiB
C++
Raw Normal View History

// -*- C++ -*-
/**
* \file CiteEnginesList.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
2020-12-05 22:17:02 +00:00
* \author Richard Kimberly Heck
* \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "CiteEnginesList.h"
#include "LaTeXFeatures.h"
#include "Lexer.h"
#include "support/debug.h"
#include "support/FileName.h"
#include "support/gettext.h"
#include "support/filetools.h"
#include "support/lstrings.h"
#include "support/Translator.h"
#include <algorithm>
using namespace std;
using namespace lyx::support;
namespace lyx {
//global variable: cite engine list
CiteEnginesList theCiteEnginesList;
LyXCiteEngine::LyXCiteEngine(string const & n, string const & i,
vector<string> const & cet, string const & cfm,
vector<string> const & dbs,
string const & d, vector<string> const & p):
name_(n), id_(i), engine_types_(cet), cite_framework_(cfm), default_biblios_(dbs),
description_(d), package_list_(p), checked_(false), available_(false)
{
filename_ = id_ + ".citeengine";
}
vector<string> LyXCiteEngine::prerequisites() const
{
if (!checked_)
isAvailable();
return prerequisites_;
}
bool LyXCiteEngine::isAvailable() const
{
if (package_list_.empty())
return true;
if (checked_)
return available_;
checked_ = true;
available_ = true;
//check whether all of the required packages are available
vector<string>::const_iterator it = package_list_.begin();
vector<string>::const_iterator end = package_list_.end();
for (; it != end; ++it) {
if (!LaTeXFeatures::isAvailable(*it)) {
available_ = false;
prerequisites_.push_back(*it);
}
}
return available_;
}
bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
{
return std::find(engine_types_.begin(), engine_types_.end(),
theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
}
string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
{
string res;
string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
//check whether all of the required packages are available
2016-12-31 16:38:26 +00:00
for (string const &s: default_biblios_) {
if (prefixIs(s, etp))
res = split(s, ':');
else if (!contains(s, ':') && res.empty())
res = s;
}
return res;
}
bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
{
2016-12-31 15:01:11 +00:00
string const bfs = ":" + bf;
2020-02-28 06:25:30 +00:00
for (string const & s: default_biblios_)
2016-12-31 15:01:11 +00:00
if (suffixIs(s, bfs) || bf == s)
return true;
2016-12-31 16:38:26 +00:00
2016-12-31 15:01:11 +00:00
return false;
}
bool LyXCiteEngine::required(string const & p) const
{
return find(package_list_.begin(), package_list_.end(), p) != package_list_.end();
}
// used when sorting the cite engine list.
class EngineSorter {
public:
int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
{
return _(ce1.getName()) < _(ce2.getName());
}
};
// Local translators
namespace {
typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
CiteEngineTypeTranslator const init_citeenginetypetranslator()
{
CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
translator.addPair("default", ENGINE_TYPE_DEFAULT);
return translator;
}
CiteEngineTypeTranslator const & citeenginetypetranslator()
{
static CiteEngineTypeTranslator const translator =
init_citeenginetypetranslator();
return translator;
}
Bulk cleanup/fix incorrect annotation at the end of namespaces. This commit does a bulk fix of incorrect annotations (comments) at the end of namespaces. The commit was generated by initially running clang-format, and then from the diff of the result extracting the hunks corresponding to fixes of namespace comments. The changes being applied and all the results have been manually reviewed. The source code successfully builds on macOS. Further details on the steps below, in case they're of interest to someone else in the future. 1. Checkout a fresh and up to date version of src/ git pull && git checkout -- src && git status src 2. Ensure there's a suitable .clang-format in place, i.e. with options to fix the comment at the end of namespaces, including: FixNamespaceComments: true SpacesBeforeTrailingComments: 1 and that clang-format is >= 5.0.0, by doing e.g.: clang-format -dump-config | grep Comments: clang-format --version 3. Apply clang-format to the source: clang-format -i $(find src -name "*.cpp" -or -name "*.h") 4. Create and filter out hunks related to fixing the namespace git diff -U0 src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace.patch 5. Filter out hunks corresponding to simple fixes into to a separate patch: pcregrep -M -e '^diff[^\n]+\nindex[^\n]+\n--- [^\n]+\n\+\+\+ [^\n]+\n' \ -e '^@@ -[0-9]+ \+[0-9]+ @@[^\n]*\n-\}[^\n]*\n\+\}[^\n]*\n' \ fix_namespace.patch > fix_namespace_simple.patch 6. Manually review the simple patch and then apply it, after first restoring the source. git checkout -- src patch -p1 < fix_namespace_simple.path 7. Manually review the (simple) changes and then stage the changes git diff src git add src 8. Again apply clang-format and filter out hunks related to any remaining fixes to the namespace, this time filter with more context. There will be fewer hunks as all the simple cases have already been handled: clang-format -i $(find src -name "*.cpp" -or -name "*.h") git diff src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace2.patch 9. Manually review/edit the resulting patch file to remove hunks for files which need to be dealt with manually, noting the file names and line numbers. Then restore files to as before applying clang-format and apply the patch: git checkout src patch -p1 < fix_namespace2.patch 10. Manually fix the files noted in the previous step. Stage files, review changes and commit.
2017-07-23 11:11:54 +00:00
} // namespace
string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
{
return citeenginetypetranslator().find(et);
}
CiteEngineType CiteEnginesList::getType(string const & et) const
{
return citeenginetypetranslator().find(et);
}
// Much of this is borrowed from LayoutFileList::read()
bool CiteEnginesList::read()
{
FileName const real_file = libFileSearch("", "lyxciteengines.lst");
LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
if (real_file.empty()) {
LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
<< "No cite engines will be available.");
return false;
}
Lexer lex;
if (!lex.setFile(real_file)) {
LYXERR0("lyxlex was not able to set file: "
<< real_file << ".\nNo cite engines will be available.");
return false;
}
if (!lex.isOK()) {
LYXERR0("unable to open cite engines file `"
<< to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
<< "'\nNo cite engines will be available.");
return false;
}
bool finished = false;
// Parse cite engines files
LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
while (lex.isOK() && !finished) {
LYXERR(Debug::TCLASS, "\tline by line");
switch (lex.lex()) {
case Lexer::LEX_FEOF:
finished = true;
break;
default:
string const cename = lex.getString();
LYXERR(Debug::TCLASS, "Engine name: " << cename);
if (!lex.next())
break;
string const fname = lex.getString();
LYXERR(Debug::TCLASS, "Filename: " << fname);
if (!lex.next(true))
break;
string cet = lex.getString();
LYXERR(Debug::TCLASS, "Engine Type: " << cet);
vector<string> cets;
while (!cet.empty()) {
string p;
cet = split(cet, p, '|');
cets.push_back(p);
}
if (!lex.next(true))
break;
string const citeframework = lex.getString();
LYXERR(Debug::TCLASS, "CiteFramework: " << citeframework);
if (!lex.next(true))
break;
string db = lex.getString();
LYXERR(Debug::TCLASS, "Default Biblio: " << db);
vector<string> dbs;
while (!db.empty()) {
string p;
db = split(db, p, '|');
dbs.push_back(p);
}
if (!lex.next(true))
break;
string const desc = lex.getString();
LYXERR(Debug::TCLASS, "Description: " << desc);
//FIXME Add packages
if (!lex.next())
break;
string str = lex.getString();
LYXERR(Debug::TCLASS, "Packages: " << str);
vector<string> pkgs;
while (!str.empty()) {
string p;
str = split(str, p, ',');
pkgs.push_back(p);
}
// This code is run when we have
// cename, fname, cets, citeframework, dbs, desc, pkgs
addCiteEngine(cename, fname, cets, citeframework, dbs, desc, pkgs);
} // end switch
} //end while
LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
if (!theCiteEnginesList.empty())
sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
return true;
}
void CiteEnginesList::addCiteEngine(string const & cename,
string const & filename, vector<string> const & cets,
string const & citeframework, vector<string> const & dbs,
string const & description, vector<string> const & pkgs)
{
LyXCiteEngine ce(cename, filename, cets, citeframework, dbs, description, pkgs);
englist_.push_back(ce);
}
LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
{
return englist_.begin();
}
LyXCiteEnginesList::iterator CiteEnginesList::begin()
{
return englist_.begin();
}
LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
{
return englist_.end();
}
LyXCiteEnginesList::iterator CiteEnginesList::end()
{
return englist_.end();
}
LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
{
2020-02-28 06:25:30 +00:00
for (auto const & eng : englist_)
if (eng.getID() == str) {
return &eng;
}
2020-02-28 06:25:30 +00:00
return nullptr;
}
LyXCiteEngine * CiteEnginesList::operator[](string const & str)
{
2020-02-28 06:25:30 +00:00
for (auto & eng : englist_)
if (eng.getID() == str) {
return &eng;
}
2020-02-28 06:25:30 +00:00
return nullptr;
}
} // namespace lyx