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>
33 string const familyName(string const & name)
38 // possible authorname combinations are:
39 // "Surname, FirstName"
41 // "FirstName Surname"
43 string::size_type idx = fname.find(",");
44 if (idx != string::npos)
45 return ltrim(fname.substr(0, idx));
46 idx = fname.rfind(".");
47 if (idx != string::npos)
48 fname = ltrim(fname.substr(idx + 1));
49 // test if we have a LaTeX Space in front
51 return fname.substr(2);
57 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
59 lyx::Assert(!map.empty());
61 InfoMap::const_iterator it = map.find(key);
64 string const & data = it->second;
66 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
67 string::size_type const pos = data.find("TheBibliographyRef");
68 if (pos != string::npos) {
73 string const opt = trim(data.substr(0, pos - 1));
78 split(opt, authors, '(');
82 string author = parseBibTeX(data, "author");
84 author = parseBibTeX(data, "editor");
87 author = parseBibTeX(data, "key");
93 vector<string> const authors = getVectorFromString(author, " and ");
97 author = familyName(authors[0]);
98 if (authors.size() == 2)
99 author += _(" and ") + familyName(authors[1]);
100 else if (authors.size() > 2)
101 author += _(" et al.");
107 string const getYear(InfoMap const & map, string const & key)
109 lyx::Assert(!map.empty());
111 InfoMap::const_iterator it = map.find(key);
114 string const & data = it->second;
116 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
117 string::size_type const pos = data.find("TheBibliographyRef");
118 if (pos != string::npos) {
124 trim(data.substr(0, pos - 1));
129 string const tmp = split(opt, authors, '(');
131 split(tmp, year, ')');
136 string year = parseBibTeX(data, "year");
146 // A functor for use with std::sort, leading to case insensitive sorting
147 struct compareNoCase: public std::binary_function<string, string, bool>
149 bool operator()(string const & s1, string const & s2) const {
150 return compare_ascii_no_case(s1, s2) < 0;
157 vector<string> const getKeys(InfoMap const & map)
159 vector<string> bibkeys;
160 InfoMap::const_iterator it = map.begin();
161 InfoMap::const_iterator end = map.end();
162 for (; it != end; ++it) {
163 bibkeys.push_back(it->first);
166 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
171 string const getInfo(InfoMap const & map, string const & key)
173 lyx::Assert(!map.empty());
175 InfoMap::const_iterator it = map.find(key);
178 string const & data = it->second;
180 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
181 string const separator("TheBibliographyRef");
182 string::size_type const pos = data.find(separator);
183 if (pos != string::npos) {
184 string::size_type const pos2 = pos + separator.size();
185 string const info = trim(data.substr(pos2));
189 // Search for all possible "required" keys
190 string author = parseBibTeX(data, "author");
192 author = parseBibTeX(data, "editor");
194 string year = parseBibTeX(data, "year");
195 string title = parseBibTeX(data, "title");
196 string booktitle = parseBibTeX(data, "booktitle");
197 string chapter = parseBibTeX(data, "chapter");
198 string number = parseBibTeX(data, "number");
199 string volume = parseBibTeX(data, "volume");
200 string pages = parseBibTeX(data, "pages");
202 string media = parseBibTeX(data, "journal");
204 media = parseBibTeX(data, "publisher");
206 media = parseBibTeX(data, "school");
208 media = parseBibTeX(data, "institution");
210 ostringstream result;
212 result << author << ", ";
215 if (!booktitle.empty())
216 result << ", in " << booktitle;
217 if (!chapter.empty())
218 result << ", Ch. " << chapter;
220 result << ", " << media;
222 result << ", vol. " << volume;
224 result << ", no. " << number;
226 result << ", pp. " << pages;
228 result << ", " << year;
230 string const result_str = rtrim(STRCONV(result.str()));
231 if (!result_str.empty())
234 // This should never happen (or at least be very unusual!)
241 // The functions doing the dirty work for the search.
242 vector<string>::const_iterator
243 simpleSearch(InfoMap const & theMap,
244 vector<string> const & keys,
246 vector<string>::const_iterator start,
252 tmp = lowercase(tmp);
254 vector<string> searchwords = getVectorFromString(tmp, " ");
256 // Loop over all keys from start...
257 for (vector<string>::const_iterator it = start;
258 // End condition is direction-dependent.
259 (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
260 // increment is direction-dependent.
261 (dir == FORWARD) ? (++it) : (--it)) {
264 InfoMap::const_iterator info = theMap.find(*it);
265 if (info != theMap.end())
266 data += " " + info->second;
268 data = lowercase(data);
272 // Loop over all search words...
273 for (vector<string>::const_iterator sit = searchwords.begin();
274 sit != searchwords.end(); ++sit) {
275 if (data.find(*sit) == string::npos) {
281 if (found) return it;
288 vector<string>::const_iterator
289 regexSearch(InfoMap const & theMap,
290 vector<string> const & keys,
292 vector<string>::const_iterator start,
295 boost::regex reg(STRCONV(expr));
297 for (vector<string>::const_iterator it = start;
298 // End condition is direction-dependent.
299 (dir == FORWARD) ? (it < keys.end()) : (it >= keys.begin());
300 // increment is direction-dependent.
301 (dir == FORWARD) ? (++it) : (--it)) {
304 InfoMap::const_iterator info = theMap.find(*it);
305 if (info != theMap.end())
306 data += " " + info->second;
308 if (boost::regex_match(STRCONV(data), reg)) {
320 vector<string>::const_iterator
321 searchKeys(InfoMap const & theMap,
322 vector<string> const & keys,
324 vector<string>::const_iterator start,
329 // Preliminary checks
330 if (start < keys.begin() || start >= keys.end())
333 string search_expr = trim(expr);
334 if (search_expr.empty())
338 return simpleSearch(theMap, keys, search_expr, start, dir,
341 return regexSearch(theMap, keys, search_expr, start, dir);
345 string const parseBibTeX(string data, string const & findkey)
348 // at first we delete all characters right of '%' and
349 // replace tabs through a space and remove leading spaces
350 // we read the data line by line so that the \n are
354 string dummy = token(data,'\n', Entries);
355 while (!dummy.empty()) {
356 dummy = subst(dummy, '\t', ' '); // no tabs
357 dummy = ltrim(dummy); // no leading spaces
358 // ignore lines with a beginning '%' or ignore all right of %
359 string::size_type const idx =
360 dummy.empty() ? string::npos : dummy.find('%');
361 if (idx != string::npos)
362 dummy.erase(idx, string::npos);
363 // do we have a new token or a new line of
364 // the same one? In the first case we ignore
365 // the \n and in the second we replace it
367 if (!dummy.empty()) {
368 if (!contains(dummy, "="))
369 data_ += (' ' + dummy);
373 dummy = token(data, '\n', ++Entries);
376 // replace double commas with "" for easy scanning
377 data = subst(data_, ",,", "\"\"");
383 // now get only the important line of the bibtex entry.
384 // all entries are devided by ',' except the last one.
385 data += ','; // now we have same behaviour for all entries
386 // because the last one is "blah ... }"
389 // parsing of title and booktitle is different from the
390 // others, because booktitle contains title
392 dummy = token(data, ',', Entries++);
393 if (!dummy.empty()) {
394 found = contains(ascii_lowercase(dummy), findkey);
395 if (findkey == "title" &&
396 contains(ascii_lowercase(dummy), "booktitle"))
399 } while (!found && !dummy.empty());
404 // we are not sure, if we get all, because "key= "blah, blah" is
406 // Therefore we read all until the next "=" character, which follows a
409 dummy = token(data, ',', Entries++);
410 while (!contains(dummy, '=') && !dummy.empty()) {
411 keyvalue += (',' + dummy);
412 dummy = token(data, ',', Entries++);
415 // replace double "" with originals ,, (two commas)
416 // leaving us with the all-important line
417 data = subst(keyvalue, "\"\"", ",,");
422 // 2. if there is no opening '{' then a closing '{' is probably cruft.
423 if (!contains(data, '{'))
424 data = rtrim(data, "}");
425 // happens, when last keyword
426 string::size_type const idx =
427 !data.empty() ? data.find('=') : string::npos;
429 if (idx == string::npos)
432 data = trim(data.substr(idx));
434 if (data.length() < 2 || data[0] != '=') { // a valid entry?
437 // delete '=' and the following spaces
438 data = ltrim(data, " =");
439 if (data.length() < 2) {
440 return data; // not long enough to find delimiters
442 string::size_type keypos = 1;
444 if (data[0] == '{') {
446 } else if (data[0] == '"') {
449 // no {} and no "", pure data but with a
450 // possible ',' at the end
451 return rtrim(data, ",");
453 string tmp = data.substr(keypos);
454 while (tmp.find('{') != string::npos &&
455 tmp.find('}') != string::npos &&
456 tmp.find('{') < tmp.find('}') &&
457 tmp.find('{') < tmp.find(enclosing)) {
459 keypos += tmp.find('{') + 1;
460 tmp = data.substr(keypos);
461 keypos += tmp.find('}') + 1;
462 tmp = data.substr(keypos);
464 if (tmp.find(enclosing) == string::npos)
467 keypos += tmp.find(enclosing);
468 return data.substr(1, keypos - 1);
477 using namespace biblio;
479 char const * const citeCommands[] = {
480 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
481 "citeyear", "citeyearpar" };
483 unsigned int const nCiteCommands =
484 sizeof(citeCommands) / sizeof(char *);
486 CiteStyle const citeStyles[] = {
487 CITE, CITET, CITEP, CITEALT, CITEALP,
488 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
490 unsigned int const nCiteStyles =
491 sizeof(citeStyles) / sizeof(CiteStyle);
493 CiteStyle const citeStylesFull[] = {
494 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
496 unsigned int const nCiteStylesFull =
497 sizeof(citeStylesFull) / sizeof(CiteStyle);
499 CiteStyle const citeStylesUCase[] = {
500 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
502 unsigned int const nCiteStylesUCase =
503 sizeof(citeStylesUCase) / sizeof(CiteStyle);
508 CitationStyle const getCitationStyle(string const & command)
510 if (command.empty()) return CitationStyle();
513 string cmd = command;
516 cs.forceUCase = true;
520 size_t n = cmd.size() - 1;
523 cmd = cmd.substr(0,n);
526 char const * const * const last = citeCommands + nCiteCommands;
527 char const * const * const ptr = std::find(citeCommands, last, cmd);
530 size_t idx = ptr - citeCommands;
531 cs.style = citeStyles[idx];
538 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
540 string cite = citeCommands[command];
542 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
543 if (std::find(citeStylesFull, last, command) != last)
548 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
549 if (std::find(citeStylesUCase, last, command) != last)
557 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
559 unsigned int nStyles = 1;
560 unsigned int start = 0;
562 nStyles = nCiteStyles - 1;
566 vector<CiteStyle> styles(nStyles);
568 vector<CiteStyle>::size_type i = 0;
570 for (; i != styles.size(); ++i, ++j) {
571 styles[i] = citeStyles[j];
579 getNumericalStrings(string const & key,
580 InfoMap const & map, vector<CiteStyle> const & styles)
583 return vector<string>();
586 string const author = getAbbreviatedAuthor(map, key);
587 string const year = getYear(map, key);
588 if (author.empty() || year.empty())
589 return vector<string>();
591 vector<string> vec(styles.size());
592 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
602 str = author + " [#ID]";
606 str = author + " #ID";
622 str = "(" + year + ")";
634 getAuthorYearStrings(string const & key,
635 InfoMap const & map, vector<CiteStyle> const & styles)
638 return vector<string>();
641 string const author = getAbbreviatedAuthor(map, key);
642 string const year = getYear(map, key);
643 if (author.empty() || year.empty())
644 return vector<string>();
646 vector<string> vec(styles.size());
647 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
653 str = author + " (" + year + ")";
657 str = "(" + author + ", " + year + ")";
661 str = author + " " + year ;
665 str = author + ", " + year ;
677 str = "(" + year + ")";
687 } // namespace biblio