1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
11 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 * \author Herbert Voss <voss@perce.de>
21 #pragma implementation
26 #include "gettext.h" // for _()
27 #include "helper_funcs.h"
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
30 #include "support/LRegex.h"
42 using namespace biblio;
44 char const * const citeCommands[] = {
45 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
46 "citeyear", "citeyearpar" };
48 unsigned int const nCiteCommands =
49 sizeof(citeCommands) / sizeof(char *);
51 CiteStyle const citeStyles[] = {
52 CITE, CITET, CITEP, CITEALT, CITEALP,
53 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
55 unsigned int const nCiteStyles =
56 sizeof(citeStyles) / sizeof(CiteStyle);
58 CiteStyle const citeStylesFull[] = {
59 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
61 unsigned int const nCiteStylesFull =
62 sizeof(citeStylesFull) / sizeof(CiteStyle);
64 CiteStyle const citeStylesUCase[] = {
65 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
67 unsigned int const nCiteStylesUCase =
68 sizeof(citeStylesUCase) / sizeof(CiteStyle);
71 // The functions doing the dirty work for the search.
72 vector<string>::const_iterator
73 simpleSearch(InfoMap const & theMap,
74 vector<string> const & keys,
76 vector<string>::const_iterator start,
84 vector<string> searchwords = getVectorFromString(tmp, " ");
86 // Loop over all keys from start...
87 for (vector<string>::const_iterator it = start;
88 // End condition is direction-dependent.
89 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
90 // increment is direction-dependent.
91 (dir == FORWARD) ? (++it) : (--it)) {
94 InfoMap::const_iterator info = theMap.find(*it);
95 if (info != theMap.end())
96 data += " " + info->second;
98 data = lowercase(data);
102 // Loop over all search words...
103 for (vector<string>::const_iterator sit = searchwords.begin();
104 sit != searchwords.end(); ++sit) {
105 if (data.find(*sit) == string::npos) {
111 if (found) return it;
118 vector<string>::const_iterator
119 regexSearch(InfoMap const & theMap,
120 vector<string> const & keys,
122 vector<string>::const_iterator start,
127 for (vector<string>::const_iterator it = start;
128 // End condition is direction-dependent.
129 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
130 // increment is direction-dependent.
131 (dir == FORWARD) ? (++it) : (--it)) {
134 InfoMap::const_iterator info = theMap.find(*it);
135 if (info != theMap.end())
136 data += " " + info->second;
138 if (reg.exec(data).size() > 0)
149 string const familyName(string const & name)
151 // Very simple parser
154 // possible authorname combinations are:
155 // "Surname, FirstName"
157 // "FirstName Surname"
159 string::size_type idx = fname.find(",");
160 if (idx != string::npos)
161 return frontStrip(fname.substr(0,idx));
162 idx = fname.rfind(".");
163 if (idx != string::npos)
164 fname = frontStrip(fname.substr(idx+1));
165 // test if we have a LaTeX Space in front
166 if (fname[0] == '\\')
167 return fname.substr(2);
173 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
175 lyx::Assert(!map.empty());
177 InfoMap::const_iterator it = map.find(key);
180 if (it != map.end()) {
181 author = parseBibTeX(it->second, "author");
183 author = parseBibTeX(it->second, "editor");
185 vector<string> authors = getVectorFromString(author, "and");
187 if (!authors.empty()) {
190 for (vector<string>::iterator it = authors.begin();
191 it != authors.end(); ++it) {
192 *it = familyName(strip(*it));
196 if (authors.size() == 2)
197 author += _(" and ") + authors[1];
198 else if (authors.size() > 2)
199 author += _(" et al.");
204 author = _("Caesar et al.");
210 string const getYear(InfoMap const & map, string const & key)
212 lyx::Assert(!map.empty());
214 InfoMap::const_iterator it = map.find(key);
219 year = parseBibTeX(it->second, "year");
228 // A functor for use with std::sort, leading to case insensitive sorting
229 struct compareNoCase: public std::binary_function<string, string, bool>
231 bool operator()(string const & s1, string const & s2) const {
232 return compare_no_case(s1, s2) < 0;
236 vector<string> const getKeys(InfoMap const & map)
238 vector<string> bibkeys;
240 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
241 bibkeys.push_back(it->first);
244 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
249 string const getInfo(InfoMap const & map, string const & key)
251 lyx::Assert(!map.empty());
253 InfoMap::const_iterator it = map.find(key);
256 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
257 if (!contains(it->second,'='))
258 return it->second.c_str();
260 // Search for all possible "required" keys
261 string author = parseBibTeX(it->second, "author");
263 author = parseBibTeX(it->second, "editor");
265 string year = parseBibTeX(it->second, "year");
266 string title = parseBibTeX(it->second, "title");
267 string booktitle = parseBibTeX(it->second, "booktitle");
268 string chapter = parseBibTeX(it->second, "chapter");
269 string number = parseBibTeX(it->second, "number");
270 string volume = parseBibTeX(it->second, "volume");
271 string pages = parseBibTeX(it->second, "pages");
273 string media = parseBibTeX(it->second, "journal");
275 media = parseBibTeX(it->second, "publisher");
277 media = parseBibTeX(it->second, "school");
279 media = parseBibTeX(it->second, "institution");
281 ostringstream result;
283 result << author << ", ";
286 if (!booktitle.empty())
287 result << ", in " << booktitle;
288 if (!chapter.empty())
289 result << ", Ch. " << chapter;
291 result << ", " << media;
293 result << ", vol. " << volume;
295 result << ", no. " << number;
297 result << ", pp. " << pages;
299 result << ", " << year;
301 string const result_str = strip(result.str().c_str());
302 if (!result_str.empty())
305 // This should never happen (or at least be very unusual!)
310 vector<string>::const_iterator
311 searchKeys(InfoMap const & theMap,
312 vector<string> const & keys,
314 vector<string>::const_iterator start,
319 // Preliminary checks
320 if (start < keys.begin() || start >= keys.end())
323 string search_expr = frontStrip(strip(expr));
324 if (search_expr.empty())
328 return simpleSearch(theMap, keys, search_expr, start, dir,
331 return regexSearch(theMap, keys, search_expr, start, dir);
335 string const parseBibTeX(string data, string const & findkey)
338 // at first we delete all characters right of '%' and
339 // replace tabs through a space and remove leading spaces
342 string dummy = token(data,'\n', Entries);
343 while (!dummy.empty()) {
344 dummy = subst(dummy, '\t', ' '); // no tabs
345 dummy = frontStrip(dummy); // no leading spaces
346 string::size_type const idx =
347 dummy.empty() ? string::npos : dummy.find('%');
348 if (idx != string::npos) {
349 // ignore lines with a beginning '%'
351 data_ += dummy.substr(0,data.find('%'));
356 dummy = token(data, '\n', ++Entries);
364 // now get only the important line of the bibtex entry.
365 // all entries are devided by ',' except the last one.
366 data += ','; // now we have same behaviour for all entries
367 // because the last one is "blah ... }"
369 dummy = token(data, ',', Entries);
370 while (!contains(lowercase(dummy), findkey) && !dummy.empty())
371 dummy = token(data, ',', ++Entries);
373 return string(); // no such keyword
374 // we are not sure, if we get all, because "key= "blah, blah" is allowed.
375 // therefore we read all until the next "=" character, which follows a
378 dummy = token(data, ',', ++Entries);
379 while (!contains(dummy, '=') && !dummy.empty()) {
380 keyvalue += (',' + dummy);
381 dummy = token(data, ',', ++Entries);
383 data = keyvalue; // now we have the important line
384 data = strip(data, ' '); // all spaces
385 if (!contains(data, '{')) // no opening '{'
386 data = strip(data, '}');// maybe there is a main closing '}'
387 // happens, when last keyword
388 string::size_type const idx =
389 !data.empty() ? data.find('=') : string::npos;
391 if (idx == string::npos)
394 data = data.substr(idx);
395 data = frontStrip(strip(data));
397 if (data.length() < 2 || data[0] != '=') { // a valid entry?
400 // delete '=' and the following spaces
401 data = frontStrip(frontStrip(data,'='));
402 if (data.length() < 2) {
403 return data; // not long enough to find delimiters
405 string::size_type keypos = 1;
407 if (data[0] == '{') {
409 } else if (data[0] == '"') {
412 // no {} and no "", pure data but with a
413 // possible ',' at the end
414 return strip(data,',');
416 string tmp = data.substr(keypos);
417 while (tmp.find('{') != string::npos &&
418 tmp.find('}') != string::npos &&
419 tmp.find('{') < tmp.find('}') &&
420 tmp.find('{') < tmp.find(enclosing)) {
422 keypos += tmp.find('{') + 1;
423 tmp = data.substr(keypos);
424 keypos += tmp.find('}') + 1;
425 tmp = data.substr(keypos);
427 if (tmp.find(enclosing) == string::npos)
430 keypos += tmp.find(enclosing);
431 return data.substr(1, keypos - 1);
438 CitationStyle const getCitationStyle(string const & command)
440 if (command.empty()) return CitationStyle();
443 string cmd = command;
446 cs.forceUCase = true;
450 size_t n = cmd.size()-1;
453 cmd = cmd.substr(0,n);
456 char const * const * const last = citeCommands + nCiteCommands;
457 char const * const * const ptr = std::find(citeCommands, last, cmd);
460 size_t idx = ptr - citeCommands;
461 cs.style = citeStyles[idx];
468 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
470 string cite = citeCommands[command];
472 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
473 if (std::find(citeStylesFull, last, command) != last)
478 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
479 if (std::find(citeStylesUCase, last, command) != last)
487 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
489 unsigned int nStyles = 1;
490 unsigned int start = 0;
492 nStyles = nCiteStyles - 1;
496 vector<CiteStyle> styles(nStyles);
498 vector<CiteStyle>::size_type i = 0;
500 for (; i != styles.size(); ++i, ++j) {
501 styles[i] = citeStyles[j];
509 getNumericalStrings(string const & key,
510 InfoMap const & map, vector<CiteStyle> const & styles)
513 vector<string> vec(1);
514 vec[0] = _("No database");
518 vector<string> vec(styles.size());
520 string const author = getAbbreviatedAuthor(map, key);
521 string const year = getYear(map, key);
523 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
533 str = author + " [#ID]";
537 str = author + " #ID";
553 str = "(" + year + ")";
565 getAuthorYearStrings(string const & key,
566 InfoMap const & map, vector<CiteStyle> const & styles)
569 vector<string> vec(1);
570 vec[0] = _("No database");
574 vector<string> vec(styles.size());
576 string const author = getAbbreviatedAuthor(map, key);
577 string const year = getYear(map, key);
579 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
584 str = author + " (" + year + ")";
589 str = "(" + author + ", " + year + ")";
593 str = author + " " + year ;
597 str = author + ", " + year ;
609 str = "(" + year + ")";
619 } // namespace biblio