3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
20 #include "gettext.h" // for _()
21 #include "helper_funcs.h"
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
25 #include <boost/regex.hpp>
38 using namespace biblio;
40 char const * const citeCommands[] = {
41 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
42 "citeyear", "citeyearpar" };
44 unsigned int const nCiteCommands =
45 sizeof(citeCommands) / sizeof(char *);
47 CiteStyle const citeStyles[] = {
48 CITE, CITET, CITEP, CITEALT, CITEALP,
49 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
51 unsigned int const nCiteStyles =
52 sizeof(citeStyles) / sizeof(CiteStyle);
54 CiteStyle const citeStylesFull[] = {
55 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
57 unsigned int const nCiteStylesFull =
58 sizeof(citeStylesFull) / sizeof(CiteStyle);
60 CiteStyle const citeStylesUCase[] = {
61 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
63 unsigned int const nCiteStylesUCase =
64 sizeof(citeStylesUCase) / sizeof(CiteStyle);
67 // The functions doing the dirty work for the search.
68 vector<string>::const_iterator
69 simpleSearch(InfoMap const & theMap,
70 vector<string> const & keys,
72 vector<string>::const_iterator start,
80 vector<string> searchwords = getVectorFromString(tmp, " ");
82 // Loop over all keys from start...
83 for (vector<string>::const_iterator it = start;
84 // End condition is direction-dependent.
85 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
86 // increment is direction-dependent.
87 (dir == FORWARD) ? (++it) : (--it)) {
90 InfoMap::const_iterator info = theMap.find(*it);
91 if (info != theMap.end())
92 data += " " + info->second;
94 data = lowercase(data);
98 // Loop over all search words...
99 for (vector<string>::const_iterator sit = searchwords.begin();
100 sit != searchwords.end(); ++sit) {
101 if (data.find(*sit) == string::npos) {
107 if (found) return it;
114 vector<string>::const_iterator
115 regexSearch(InfoMap const & theMap,
116 vector<string> const & keys,
118 vector<string>::const_iterator start,
121 boost::regex reg(expr);
123 for (vector<string>::const_iterator it = start;
124 // End condition is direction-dependent.
125 (dir == FORWARD) ? (it < keys.end()) : (it >= keys.begin());
126 // increment is direction-dependent.
127 (dir == FORWARD) ? (++it) : (--it)) {
130 InfoMap::const_iterator info = theMap.find(*it);
131 if (info != theMap.end())
132 data += " " + info->second;
134 if (boost::regex_match(data, reg)) {
146 string const familyName(string const & name)
148 // Very simple parser
151 // possible authorname combinations are:
152 // "Surname, FirstName"
154 // "FirstName Surname"
156 string::size_type idx = fname.find(",");
157 if (idx != string::npos)
158 return ltrim(fname.substr(0, idx));
159 idx = fname.rfind(".");
160 if (idx != string::npos)
161 fname = ltrim(fname.substr(idx + 1));
162 // test if we have a LaTeX Space in front
163 if (fname[0] == '\\')
164 return fname.substr(2);
170 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
172 lyx::Assert(!map.empty());
174 InfoMap::const_iterator it = map.find(key);
178 string::size_type const pos = it->second.find("TheBibliographyRef");
179 if (pos != string::npos) {
184 string const opt = trim(it->second.substr(0, pos - 1));
189 split(opt, authors, '(');
193 string author = parseBibTeX(it->second, "author");
195 author = parseBibTeX(it->second, "editor");
197 if (author.empty()) {
198 author = parseBibTeX(it->second, "key");
204 vector<string> authors = getVectorFromString(author, " and");
206 if (!authors.empty()) {
209 for (vector<string>::iterator it = authors.begin();
210 it != authors.end(); ++it) {
211 *it = familyName(rtrim(*it));
215 if (authors.size() == 2)
216 author += _(" and ") + authors[1];
217 else if (authors.size() > 2)
218 author += _(" et al.");
225 string const getYear(InfoMap const & map, string const & key)
227 lyx::Assert(!map.empty());
229 InfoMap::const_iterator it = map.find(key);
233 string::size_type const pos = it->second.find("TheBibliographyRef");
234 if (pos != string::npos) {
240 trim(it->second.substr(0, pos - 1));
245 string const tmp = split(opt, authors, '(');
247 split(tmp, year, ')');
252 string year = parseBibTeX(it->second, "year");
260 // A functor for use with std::sort, leading to case insensitive sorting
261 struct compareNoCase: public std::binary_function<string, string, bool>
263 bool operator()(string const & s1, string const & s2) const {
264 return compare_ascii_no_case(s1, s2) < 0;
268 vector<string> const getKeys(InfoMap const & map)
270 vector<string> bibkeys;
272 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
273 bibkeys.push_back(it->first);
276 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
281 string const getInfo(InfoMap const & map, string const & key)
283 lyx::Assert(!map.empty());
285 InfoMap::const_iterator it = map.find(key);
289 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
290 string const separator("TheBibliographyRef");
291 string::size_type const pos = it->second.find(separator);
292 if (pos != string::npos) {
293 string::size_type const pos2 = pos + separator.size();
294 string const info = trim(it->second.substr(pos2));
298 // Search for all possible "required" keys
299 string author = parseBibTeX(it->second, "author");
301 author = parseBibTeX(it->second, "editor");
303 string year = parseBibTeX(it->second, "year");
304 string title = parseBibTeX(it->second, "title");
305 string booktitle = parseBibTeX(it->second, "booktitle");
306 string chapter = parseBibTeX(it->second, "chapter");
307 string number = parseBibTeX(it->second, "number");
308 string volume = parseBibTeX(it->second, "volume");
309 string pages = parseBibTeX(it->second, "pages");
311 string media = parseBibTeX(it->second, "journal");
313 media = parseBibTeX(it->second, "publisher");
315 media = parseBibTeX(it->second, "school");
317 media = parseBibTeX(it->second, "institution");
319 ostringstream result;
321 result << author << ", ";
324 if (!booktitle.empty())
325 result << ", in " << booktitle;
326 if (!chapter.empty())
327 result << ", Ch. " << chapter;
329 result << ", " << media;
331 result << ", vol. " << volume;
333 result << ", no. " << number;
335 result << ", pp. " << pages;
337 result << ", " << year;
339 string const result_str = rtrim(result.str().c_str());
340 if (!result_str.empty())
343 // This should never happen (or at least be very unusual!)
348 vector<string>::const_iterator
349 searchKeys(InfoMap const & theMap,
350 vector<string> const & keys,
352 vector<string>::const_iterator start,
357 // Preliminary checks
358 if (start < keys.begin() || start >= keys.end())
361 string search_expr = trim(expr);
362 if (search_expr.empty())
366 return simpleSearch(theMap, keys, search_expr, start, dir,
369 return regexSearch(theMap, keys, search_expr, start, dir);
373 string const parseBibTeX(string data, string const & findkey)
376 // at first we delete all characters right of '%' and
377 // replace tabs through a space and remove leading spaces
378 // we read the data line by line so that the \n are
382 string dummy = token(data,'\n', Entries);
383 while (!dummy.empty()) {
384 dummy = subst(dummy, '\t', ' '); // no tabs
385 dummy = ltrim(dummy); // no leading spaces
386 // ignore lines with a beginning '%' or ignore all right of %
387 string::size_type const idx =
388 dummy.empty() ? string::npos : dummy.find('%');
389 if (idx != string::npos)
390 dummy.erase(idx, string::npos);
391 // do we have a new token or a new line of
392 // the same one? In the first case we ignore
393 // the \n and in the second we replace it
395 if (!dummy.empty()) {
396 if (!contains(dummy, "="))
397 data_ += (' ' + dummy);
401 dummy = token(data, '\n', ++Entries);
404 // replace double commas with "" for easy scanning
405 data = subst(data_, ",,", "\"\"");
411 // now get only the important line of the bibtex entry.
412 // all entries are devided by ',' except the last one.
413 data += ','; // now we have same behaviour for all entries
414 // because the last one is "blah ... }"
417 // parsing of title and booktitle is different from the
418 // others, because booktitle contains title
420 dummy = token(data, ',', Entries++);
421 if (!dummy.empty()) {
422 found = contains(ascii_lowercase(dummy), findkey);
423 if (findkey == "title" &&
424 contains(ascii_lowercase(dummy), "booktitle"))
427 } while (!found && !dummy.empty());
432 // we are not sure, if we get all, because "key= "blah, blah" is
434 // Therefore we read all until the next "=" character, which follows a
437 dummy = token(data, ',', Entries++);
438 while (!contains(dummy, '=') && !dummy.empty()) {
439 keyvalue += (',' + dummy);
440 dummy = token(data, ',', Entries++);
443 // replace double "" with originals ,, (two commas)
444 // leaving us with the all-important line
445 data = subst(keyvalue, "\"\"", ",,");
450 // 2. if there is no opening '{' then a closing '{' is probably cruft.
451 if (!contains(data, '{'))
452 data = rtrim(data, "}");
453 // happens, when last keyword
454 string::size_type const idx =
455 !data.empty() ? data.find('=') : string::npos;
457 if (idx == string::npos)
460 data = trim(data.substr(idx));
462 if (data.length() < 2 || data[0] != '=') { // a valid entry?
465 // delete '=' and the following spaces
466 data = ltrim(data, " =");
467 if (data.length() < 2) {
468 return data; // not long enough to find delimiters
470 string::size_type keypos = 1;
472 if (data[0] == '{') {
474 } else if (data[0] == '"') {
477 // no {} and no "", pure data but with a
478 // possible ',' at the end
479 return rtrim(data, ",");
481 string tmp = data.substr(keypos);
482 while (tmp.find('{') != string::npos &&
483 tmp.find('}') != string::npos &&
484 tmp.find('{') < tmp.find('}') &&
485 tmp.find('{') < tmp.find(enclosing)) {
487 keypos += tmp.find('{') + 1;
488 tmp = data.substr(keypos);
489 keypos += tmp.find('}') + 1;
490 tmp = data.substr(keypos);
492 if (tmp.find(enclosing) == string::npos)
495 keypos += tmp.find(enclosing);
496 return data.substr(1, keypos - 1);
503 CitationStyle const getCitationStyle(string const & command)
505 if (command.empty()) return CitationStyle();
508 string cmd = command;
511 cs.forceUCase = true;
515 size_t n = cmd.size() - 1;
518 cmd = cmd.substr(0,n);
521 char const * const * const last = citeCommands + nCiteCommands;
522 char const * const * const ptr = std::find(citeCommands, last, cmd);
525 size_t idx = ptr - citeCommands;
526 cs.style = citeStyles[idx];
533 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
535 string cite = citeCommands[command];
537 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
538 if (std::find(citeStylesFull, last, command) != last)
543 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
544 if (std::find(citeStylesUCase, last, command) != last)
552 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
554 unsigned int nStyles = 1;
555 unsigned int start = 0;
557 nStyles = nCiteStyles - 1;
561 vector<CiteStyle> styles(nStyles);
563 vector<CiteStyle>::size_type i = 0;
565 for (; i != styles.size(); ++i, ++j) {
566 styles[i] = citeStyles[j];
574 getNumericalStrings(string const & key,
575 InfoMap const & map, vector<CiteStyle> const & styles)
578 return vector<string>();
581 string const author = getAbbreviatedAuthor(map, key);
582 string const year = getYear(map, key);
583 if (author.empty() || year.empty())
584 return vector<string>();
586 vector<string> vec(styles.size());
587 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
597 str = author + " [#ID]";
601 str = author + " #ID";
617 str = "(" + year + ")";
629 getAuthorYearStrings(string const & key,
630 InfoMap const & map, vector<CiteStyle> const & styles)
633 return vector<string>();
636 string const author = getAbbreviatedAuthor(map, key);
637 string const year = getYear(map, key);
638 if (author.empty() || year.empty())
639 return vector<string>();
641 vector<string> vec(styles.size());
642 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
648 str = author + " (" + year + ")";
652 str = "(" + author + ", " + year + ")";
656 str = author + " " + year ;
660 str = author + ", " + year ;
672 str = "(" + year + ")";
682 } // namespace biblio