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>
20 #pragma implementation
25 #include "gettext.h" // for _()
26 #include "helper_funcs.h"
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
29 #include "support/LRegex.h"
41 using namespace biblio;
43 char const * const citeCommands[] = {
44 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
45 "citeyear", "citeyearpar" };
47 unsigned int const nCiteCommands =
48 sizeof(citeCommands) / sizeof(char *);
50 CiteStyle const citeStyles[] = {
51 CITE, CITET, CITEP, CITEALT, CITEALP,
52 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
54 unsigned int const nCiteStyles =
55 sizeof(citeStyles) / sizeof(CiteStyle);
57 CiteStyle const citeStylesFull[] = {
58 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
60 unsigned int const nCiteStylesFull =
61 sizeof(citeStylesFull) / sizeof(CiteStyle);
63 CiteStyle const citeStylesUCase[] = {
64 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
66 unsigned int const nCiteStylesUCase =
67 sizeof(citeStylesUCase) / sizeof(CiteStyle);
70 // The functions doing the dirty work for the search.
71 vector<string>::const_iterator
72 simpleSearch(InfoMap const & theMap,
73 vector<string> const & keys,
75 vector<string>::const_iterator start,
83 vector<string> searchwords = getVectorFromString(tmp, " ");
85 // Loop over all keys from start...
86 for (vector<string>::const_iterator it = start;
87 // End condition is direction-dependent.
88 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
89 // increment is direction-dependent.
90 (dir == FORWARD) ? (++it) : (--it)) {
93 InfoMap::const_iterator info = theMap.find(*it);
94 if (info != theMap.end())
95 data += " " + info->second;
97 data = lowercase(data);
101 // Loop over all search words...
102 for (vector<string>::const_iterator sit = searchwords.begin();
103 sit != searchwords.end(); ++sit) {
104 if (data.find(*sit) == string::npos) {
110 if (found) return it;
117 vector<string>::const_iterator
118 regexSearch(InfoMap const & theMap,
119 vector<string> const & keys,
121 vector<string>::const_iterator start,
126 for (vector<string>::const_iterator it = start;
127 // End condition is direction-dependent.
128 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
129 // increment is direction-dependent.
130 (dir == FORWARD) ? (++it) : (--it)) {
133 InfoMap::const_iterator info = theMap.find(*it);
134 if (info != theMap.end())
135 data += " " + info->second;
137 if (reg.exec(data).size() > 0)
144 string const familyName(string const & name)
146 // Very simple parser
149 string::size_type idx = fname.rfind(".");
150 if (idx != string::npos)
151 fname = frontStrip(fname.substr(idx+1));
157 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
159 lyx::Assert(!map.empty());
161 InfoMap::const_iterator it = map.find(key);
164 if (it != map.end()) {
165 author = parseBibTeX(it->second, "author");
167 author = parseBibTeX(it->second, "editor");
169 vector<string> authors = getVectorFromString(author, "and");
171 if (!authors.empty()) {
174 for (vector<string>::iterator it = authors.begin();
175 it != authors.end(); ++it) {
176 *it = familyName(strip(*it));
180 if (authors.size() == 2)
181 author += _(" and ") + authors[1];
182 else if (authors.size() > 2)
183 author += _(" et al.");
188 author = _("Caesar et al.");
194 string const getYear(InfoMap const & map, string const & key)
196 lyx::Assert(!map.empty());
198 InfoMap::const_iterator it = map.find(key);
203 year = parseBibTeX(it->second, "year");
219 // A functor for use with std::sort, leading to case insensitive sorting
220 struct compareNoCase: public std::binary_function<string, string, bool>
222 bool operator()(string const & s1, string const & s2) const {
223 return compare_no_case(s1, s2) < 0;
227 vector<string> const getKeys(InfoMap const & map)
229 vector<string> bibkeys;
231 for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
232 bibkeys.push_back(it->first);
235 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
240 string const getInfo(InfoMap const & map, string const & key)
242 lyx::Assert(!map.empty());
244 InfoMap::const_iterator it = map.find(key);
245 if (it == map.end()) return string();
247 // Search for all possible "required" keys
248 string author = parseBibTeX(it->second, "author");
250 author = parseBibTeX(it->second, "editor");
252 string year = parseBibTeX(it->second, "year");
253 string title = parseBibTeX(it->second, "title");
254 string booktitle = parseBibTeX(it->second, "booktitle");
255 string chapter = parseBibTeX(it->second, "chapter");
256 string pages = parseBibTeX(it->second, "pages");
258 string media = parseBibTeX(it->second, "journal");
260 media = parseBibTeX(it->second, "publisher");
262 media = parseBibTeX(it->second, "school");
264 media = parseBibTeX(it->second, "institution");
266 ostringstream result;
269 result << ", " << year;
271 result << ", " << title;
272 if (!booktitle.empty())
273 result << ", in " << booktitle;
274 if (!chapter.empty())
275 result << ", Ch. " << chapter;
277 result << ", " << media;
279 result << ", pp. " << pages;
281 if (result.str().empty()) // not a BibTeX record
282 result << it->second;
284 return result.str().c_str();
288 vector<string>::const_iterator
289 searchKeys(InfoMap const & theMap,
290 vector<string> const & keys,
292 vector<string>::const_iterator start,
297 // Preliminary checks
298 if(start < keys.begin() || start >= keys.end())
301 string search_expr = frontStrip(strip(expr));
302 if (search_expr.empty())
306 return simpleSearch(theMap, keys, search_expr, start, dir,
309 return regexSearch(theMap, keys, search_expr, start, dir);
313 string const parseBibTeX(string data, string const & findkey)
317 for (string::iterator it=data.begin(); it<data.end(); ++it) {
318 if ((*it) == '\n' || (*it) == '\t')
322 data = frontStrip(data);
323 while (!data.empty() && data[0] != '=' &&
324 (data.find(' ') != string::npos || data.find('=') != string::npos)) {
326 string::size_type keypos = min(data.find(' '), data.find('='));
327 string key = lowercase(data.substr(0, keypos));
329 data = data.substr(keypos, data.length()-1);
330 data = frontStrip(strip(data));
331 if (data.length() > 1 && data[0]=='=') {
332 data = frontStrip(data.substr(1, data.length()-1));
340 } else if (data[0]=='"') {
348 data.find(enclosing)!=string::npos &&
350 string tmp = data.substr(keypos,
352 while (tmp.find('{') != string::npos &&
353 tmp.find('}') != string::npos &&
354 tmp.find('{') < tmp.find('}') &&
355 tmp.find('{') < tmp.find(enclosing)) {
357 keypos += tmp.find('{')+1;
358 tmp = data.substr(keypos,
360 keypos += tmp.find('}')+1;
361 tmp = data.substr(keypos,
365 if (tmp.find(enclosing)==string::npos)
368 keypos += tmp.find(enclosing);
369 tmp = data.substr(keypos,
373 value = data.substr(1, keypos-1);
375 if (keypos+1<data.length()-1)
376 data = frontStrip(data.substr(keypos+1, data.length()-1));
380 } else if (!keypos &&
383 keypos = data.length()-1;
384 if (data.find(' ') != string::npos)
385 keypos = data.find(' ');
386 if (data.find(',') != string::npos &&
387 keypos > data.find(','))
388 keypos = data.find(',');
390 value = data.substr(0, keypos);
392 if (keypos+1<data.length()-1)
393 data = frontStrip(data.substr(keypos+1, data.length()-1));
400 if (findkey == key) {
405 data = frontStrip(frontStrip(data,','));
408 else return keyvalue;
414 CitationStyle const getCitationStyle(string const & command)
416 if (command.empty()) return CitationStyle();
419 string cmd = command;
422 cs.forceUCase = true;
426 size_t n = cmd.size()-1;
429 cmd = cmd.substr(0,n);
432 char const * const * const last = citeCommands + nCiteCommands;
433 char const * const * const ptr = std::find(citeCommands, last, cmd);
436 size_t idx = ptr - citeCommands;
437 cs.style = citeStyles[idx];
444 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
446 string cite = citeCommands[command];
448 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
449 if (std::find(citeStylesFull, last, command) != last)
454 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
455 if (std::find(citeStylesUCase, last, command) != last)
463 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
465 unsigned int nStyles = 1;
466 unsigned int start = 0;
468 nStyles = nCiteStyles - 1;
472 vector<CiteStyle> styles(nStyles);
474 vector<CiteStyle>::size_type i = 0;
476 for (; i != styles.size(); ++i, ++j) {
477 styles[i] = citeStyles[j];
485 getNumericalStrings(string const & key,
486 InfoMap const & map, vector<CiteStyle> const & styles)
489 vector<string> vec(1);
490 vec[0] = _("No database");
494 vector<string> vec(styles.size());
496 string const author = getAbbreviatedAuthor(map, key);
497 string const year = getYear(map, key);
499 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
509 str = author + " [#ID]";
513 str = author + " #ID";
529 str = "(" + year + ")";
541 getAuthorYearStrings(string const & key,
542 InfoMap const & map, vector<CiteStyle> const & styles)
545 vector<string> vec(1);
546 vec[0] = _("No database");
550 vector<string> vec(styles.size());
552 string const author = getAbbreviatedAuthor(map, key);
553 string const year = getYear(map, key);
555 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
560 str = author + " (" + year + ")";
565 str = "(" + author + ", " + year + ")";
569 str = author + " " + year ;
573 str = author + ", " + year ;
585 str = "(" + year + ")";
595 } // namespace biblio