3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * \author Richard Heck (re-write of BibTeX representation)
10 * Full author contact details are available in file CREDITS.
16 #include "InsetIterator.h"
17 #include "Paragraph.h"
19 #include "insets/Inset.h"
20 #include "insets/InsetBibitem.h"
21 #include "insets/InsetBibtex.h"
22 #include "insets/InsetInclude.h"
24 #include "support/lstrings.h"
32 using support::ascii_lowercase;
33 using support::bformat;
34 using support::compare_ascii_no_case;
35 using support::contains;
36 using support::getVectorFromString;
38 using support::prefixIs;
49 vector<string> const init_possible_cite_commands()
51 char const * const pos[] = {
52 "cite", "citet", "citep", "citealt", "citealp",
53 "citeauthor", "citeyear", "citeyearpar",
54 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
55 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
56 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
58 "footcite", "footcitet", "footcitep", "footcitealt",
59 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
60 "citefield", "citetitle", "cite*"
62 size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
64 return vector<string>(pos, pos + size_pos);
68 vector<string> const & possible_cite_commands()
70 static vector<string> const pos = init_possible_cite_commands();
75 bool is_possible_cite_command(string const & input)
77 vector<string> const & possibles = possible_cite_commands();
78 vector<string>::const_iterator const end = possibles.end();
79 return std::find(possibles.begin(), end, input) != end;
83 string const default_cite_command(CiteEngine engine)
90 case ENGINE_NATBIB_AUTHORYEAR:
93 case ENGINE_NATBIB_NUMERICAL:
107 const docstring TheBibliographyRef(from_ascii("TheBibliographyRef"));
109 string const asValidLatexCommand(string const & input,
110 CiteEngine const engine)
112 string const default_str = default_cite_command(engine);
113 if (!is_possible_cite_command(input))
119 output = default_str;
122 case ENGINE_NATBIB_AUTHORYEAR:
123 case ENGINE_NATBIB_NUMERICAL:
124 if (input == "cite" || input == "citefield" ||
125 input == "citetitle" || input == "cite*")
126 output = default_str;
127 else if (prefixIs(input, "foot"))
128 output = input.substr(4);
133 case ENGINE_JURABIB: {
134 // Jurabib does not support the 'uppercase' natbib style.
136 output = string(1, 'c') + input.substr(1);
140 // Jurabib does not support the 'full' natbib style.
141 string::size_type const n = output.size() - 1;
142 if (output != "cite*" && output[n] == '*')
143 output = output.substr(0, n);
153 docstring const familyName(docstring const & name)
158 // Very simple parser
159 docstring fname = name;
161 // possible authorname combinations are:
162 // "Surname, FirstName"
164 // "FirstName Surname"
166 docstring::size_type idx = fname.find(',');
167 if (idx != docstring::npos)
168 return ltrim(fname.substr(0, idx));
169 idx = fname.rfind('.');
170 if (idx != docstring::npos && idx + 1 < fname.size())
171 fname = ltrim(fname.substr(idx + 1));
172 // test if we have a LaTeX Space in front
173 if (fname[0] == '\\')
174 return fname.substr(2);
180 docstring const getAbbreviatedAuthor(InfoMap const & map, string const & key)
182 BOOST_ASSERT(!map.empty());
184 InfoMap::const_iterator it = map.find(key);
187 docstring const & data = it->second;
189 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
190 docstring::size_type const pos = data.find(TheBibliographyRef);
191 if (pos != docstring::npos) {
196 docstring const opt = trim(data.substr(0, pos - 1));
201 split(opt, authors, '(');
205 docstring author = parseBibTeX(data, "author");
208 author = parseBibTeX(data, "editor");
210 if (author.empty()) {
211 author = parseBibTeX(data, "key");
214 return from_utf8(key);
218 vector<docstring> const authors = getVectorFromString(author, from_ascii(" and "));
222 if (authors.size() == 2)
223 return bformat(_("%1$s and %2$s"),
224 familyName(authors[0]), familyName(authors[1]));
226 if (authors.size() > 2)
227 return bformat(_("%1$s et al."), familyName(authors[0]));
229 return familyName(authors[0]);
233 docstring const getYear(InfoMap const & map, string const & key)
235 BOOST_ASSERT(!map.empty());
237 InfoMap::const_iterator it = map.find(key);
240 docstring const & data = it->second;
242 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
243 docstring::size_type const pos = data.find(TheBibliographyRef);
244 if (pos != docstring::npos) {
249 docstring const opt =
250 trim(data.substr(0, pos - 1));
255 docstring const tmp = split(opt, authors, '(');
257 split(tmp, year, ')');
262 docstring year = parseBibTeX(data, "year");
271 // A functor for use with std::sort, leading to case insensitive sorting
272 class compareNoCase: public std::binary_function<string, string, bool>
275 bool operator()(string const & s1, string const & s2) const {
276 return compare_ascii_no_case(s1, s2) < 0;
282 vector<string> const getKeys(InfoMap const & map)
284 vector<string> bibkeys;
285 InfoMap::const_iterator it = map.begin();
286 InfoMap::const_iterator end = map.end();
287 for (; it != end; ++it) {
288 bibkeys.push_back(it->first);
291 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
296 docstring const getInfo(InfoMap const & map, string const & key)
298 BOOST_ASSERT(!map.empty());
300 InfoMap::const_iterator it = map.find(key);
303 docstring const & data = it->second;
305 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
306 docstring::size_type const pos = data.find(TheBibliographyRef);
307 if (pos != docstring::npos) {
308 docstring::size_type const pos2 = pos + TheBibliographyRef.size();
309 docstring const info = trim(data.substr(pos2));
313 // Search for all possible "required" keys
314 docstring author = parseBibTeX(data, "author");
316 author = parseBibTeX(data, "editor");
318 docstring year = parseBibTeX(data, "year");
319 docstring title = parseBibTeX(data, "title");
320 docstring booktitle = parseBibTeX(data, "booktitle");
321 docstring chapter = parseBibTeX(data, "chapter");
322 docstring number = parseBibTeX(data, "number");
323 docstring volume = parseBibTeX(data, "volume");
324 docstring pages = parseBibTeX(data, "pages");
325 docstring annote = parseBibTeX(data, "annote");
326 docstring media = parseBibTeX(data, "journal");
328 media = parseBibTeX(data, "publisher");
330 media = parseBibTeX(data, "school");
332 media = parseBibTeX(data, "institution");
334 odocstringstream result;
336 result << author << ", ";
339 if (!booktitle.empty())
340 result << ", in " << booktitle;
341 if (!chapter.empty())
342 result << ", Ch. " << chapter;
344 result << ", " << media;
346 result << ", vol. " << volume;
348 result << ", no. " << number;
350 result << ", pp. " << pages;
352 result << ", " << year;
354 result << "\n\n" << annote;
356 docstring const result_str = rtrim(result.str());
357 if (!result_str.empty())
360 // This should never happen (or at least be very unusual!)
367 // Escape special chars.
368 // All characters are literals except: '.|*?+(){}[]^$\'
369 // These characters are literals when preceded by a "\", which is done here
370 // @todo: This function should be moved to support, and then the test in tests
371 // should be moved there as well.
372 string const escape_special_chars(string const & expr)
374 // Search for all chars '.|*?+(){}[^$]\'
375 // Note that '[' and '\' must be escaped.
376 // This is a limitation of boost::regex, but all other chars in BREs
377 // are assumed literal.
378 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
380 // $& is a perl-like expression that expands to all
381 // of the current match
382 // The '$' must be prefixed with the escape character '\' for
383 // boost to treat it as a literal.
384 // Thus, to prefix a matched expression with '\', we use:
385 return boost::regex_replace(expr, reg, "\\\\$&");
389 // A functor for use with std::find_if, used to ascertain whether a
390 // data entry matches the required regex_
391 // @throws: boost::regex_error if the supplied regex pattern is not valid
392 // @todo: This function should be moved to support.
393 class RegexMatch : public std::unary_function<string, bool>
396 // re and icase are used to construct an instance of boost::RegEx.
397 // if icase is true, then matching is insensitive to case
398 RegexMatch(InfoMap const & m, string const & re, bool icase)
399 : map_(m), regex_(re, icase) {}
401 bool operator()(string const & key) const {
402 // the data searched is the key + its associated BibTeX/biblio
405 InfoMap::const_iterator info = map_.find(key);
406 if (info != map_.end())
408 data += ' ' + to_utf8(info->second);
410 // Attempts to find a match for the current RE
411 // somewhere in data.
412 return boost::regex_search(data, regex_);
416 mutable boost::regex regex_;
422 vector<string>::const_iterator searchKeys(InfoMap const & theMap,
423 vector<string> const & keys,
424 string const & search_expr,
425 vector<string>::const_iterator start,
430 // Preliminary checks
431 if (start < keys.begin() || start >= keys.end())
434 string expr = trim(search_expr);
439 // We must escape special chars in the search_expr so that
440 // it is treated as a simple string by boost::regex.
441 expr = escape_special_chars(expr);
444 // Build the functor that will be passed to find_if.
445 RegexMatch const match(theMap, expr, !caseSensitive);
447 // Search the vector of 'keys' from 'start' for one
448 // that matches the predicate 'match'. Searching can
449 // be forward or backward from start.
451 return std::find_if(start, keys.end(), match);
453 vector<string>::const_reverse_iterator rit(start);
454 vector<string>::const_reverse_iterator rend = keys.rend();
455 rit = std::find_if(rit, rend, match);
459 // This is correct and always safe.
460 // (See Meyer's Effective STL, Item 28.)
461 return (++rit).base();
463 catch (boost::regex_error &) {
469 docstring const parseBibTeX(docstring data, string const & findkey)
471 // at first we delete all characters right of '%' and
472 // replace tabs through a space and remove leading spaces
473 // we read the data line by line so that the \n are
477 docstring dummy = token(data,'\n', Entries);
478 while (!dummy.empty()) {
480 dummy = subst(dummy, '\t', ' ');
482 dummy = ltrim(dummy);
483 // ignore lines with a beginning '%' or ignore all right of %
484 docstring::size_type const idx =
485 dummy.empty() ? docstring::npos : dummy.find('%');
486 if (idx != docstring::npos)
487 // Check if this is really a comment or just "\%"
488 if (idx == 0 || dummy[idx - 1] != '\\')
489 dummy.erase(idx, docstring::npos);
491 // This is "\%", so just erase the '\'
492 dummy.erase(idx - 1, 1);
493 // do we have a new token or a new line of
494 // the same one? In the first case we ignore
495 // the \n and in the second we replace it
497 if (!dummy.empty()) {
498 if (!contains(dummy, '='))
499 data_ += ' ' + dummy;
503 dummy = token(data, '\n', ++Entries);
506 // replace double commas with "" for easy scanning
507 data = subst(data_, from_ascii(",,"), from_ascii("\"\""));
513 // now get only the important line of the bibtex entry.
514 // all entries are devided by ',' except the last one.
516 // now we have same behaviour for all entries because the last one
520 // parsing of title and booktitle is different from the
521 // others, because booktitle contains title
523 dummy = token(data, ',', Entries++);
524 if (!dummy.empty()) {
525 found = contains(ascii_lowercase(dummy), from_ascii(findkey));
526 if (findkey == "title" &&
527 contains(ascii_lowercase(dummy), from_ascii("booktitle")))
530 } while (!found && !dummy.empty());
535 // we are not sure, if we get all, because "key= "blah, blah" is
537 // Therefore we read all until the next "=" character, which follows a
539 docstring keyvalue = dummy;
540 dummy = token(data, ',', Entries++);
541 while (!contains(dummy, '=') && !dummy.empty()) {
542 keyvalue += ',' + dummy;
543 dummy = token(data, ',', Entries++);
546 // replace double "" with originals ,, (two commas)
547 // leaving us with the all-important line
548 data = subst(keyvalue, from_ascii("\"\""), from_ascii(",,"));
553 // 2. if there is no opening '{' then a closing '{' is probably cruft.
554 if (!contains(data, '{'))
555 data = rtrim(data, "}");
556 // happens, when last keyword
557 docstring::size_type const idx =
558 !data.empty() ? data.find('=') : docstring::npos;
560 if (idx == docstring::npos)
563 data = trim(data.substr(idx));
566 if (data.length() < 2 || data[0] != '=')
569 // delete '=' and the following spaces
570 data = ltrim(data, " =");
571 if (data.length() < 2) {
572 // not long enough to find delimiters
575 docstring::size_type keypos = 1;
577 if (data[0] == '{') {
579 } else if (data[0] == '"') {
582 // no {} and no "", pure data but with a
583 // possible ',' at the end
584 return rtrim(data, ",");
586 docstring tmp = data.substr(keypos);
587 while (tmp.find('{') != docstring::npos &&
588 tmp.find('}') != docstring::npos &&
589 tmp.find('{') < tmp.find('}') &&
590 tmp.find('{') < tmp.find(enclosing)) {
591 keypos += tmp.find('{') + 1;
592 tmp = data.substr(keypos);
593 keypos += tmp.find('}') + 1;
594 tmp = data.substr(keypos);
596 if (tmp.find(enclosing) == docstring::npos)
599 keypos += tmp.find(enclosing);
600 return data.substr(1, keypos - 1);
610 char const * const citeCommands[] = {
611 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
612 "citeyear", "citeyearpar" };
614 unsigned int const nCiteCommands =
615 sizeof(citeCommands) / sizeof(char *);
617 CiteStyle const citeStyles[] = {
618 CITE, CITET, CITEP, CITEALT, CITEALP,
619 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
621 unsigned int const nCiteStyles =
622 sizeof(citeStyles) / sizeof(CiteStyle);
624 CiteStyle const citeStylesFull[] = {
625 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
627 unsigned int const nCiteStylesFull =
628 sizeof(citeStylesFull) / sizeof(CiteStyle);
630 CiteStyle const citeStylesUCase[] = {
631 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
633 unsigned int const nCiteStylesUCase =
634 sizeof(citeStylesUCase) / sizeof(CiteStyle);
639 CitationStyle::CitationStyle(string const & command)
640 : style(CITE), full(false), forceUCase(false)
645 string cmd = command;
651 string::size_type const n = cmd.size() - 1;
652 if (cmd != "cite" && cmd[n] == '*') {
654 cmd = cmd.substr(0,n);
657 char const * const * const last = citeCommands + nCiteCommands;
658 char const * const * const ptr = std::find(citeCommands, last, cmd);
661 size_t idx = ptr - citeCommands;
662 style = citeStyles[idx];
667 string const CitationStyle::asLatexStr() const
669 string cite = citeCommands[style];
671 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
672 if (std::find(citeStylesFull, last, style) != last)
677 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
678 if (std::find(citeStylesUCase, last, style) != last)
686 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
688 unsigned int nStyles = 0;
689 unsigned int start = 0;
696 case ENGINE_NATBIB_AUTHORYEAR:
697 case ENGINE_NATBIB_NUMERICAL:
698 nStyles = nCiteStyles - 1;
702 nStyles = nCiteStyles;
707 typedef vector<CiteStyle> cite_vec;
709 cite_vec styles(nStyles);
710 cite_vec::size_type i = 0;
712 for (; i != styles.size(); ++i, ++j)
713 styles[i] = citeStyles[j];
719 vector<docstring> const
720 getNumericalStrings(string const & key,
721 InfoMap const & map, vector<CiteStyle> const & styles)
724 return vector<docstring>();
726 docstring const author = getAbbreviatedAuthor(map, key);
727 docstring const year = getYear(map, key);
728 if (author.empty() || year.empty())
729 return vector<docstring>();
731 vector<docstring> vec(styles.size());
732 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
738 str = from_ascii("[#ID]");
742 str = author + " [#ID]";
746 str = author + " #ID";
750 str = from_ascii("#ID");
762 str = '(' + year + ')';
773 vector<docstring> const
774 getAuthorYearStrings(string const & key,
775 InfoMap const & map, vector<CiteStyle> const & styles)
778 return vector<docstring>();
780 docstring const author = getAbbreviatedAuthor(map, key);
781 docstring const year = getYear(map, key);
782 if (author.empty() || year.empty())
783 return vector<docstring>();
785 vector<docstring> vec(styles.size());
786 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
791 // jurabib only: Author/Annotator
792 // (i.e. the "before" field, 2nd opt arg)
793 str = author + "/<" + _("before") + '>';
797 str = author + " (" + year + ')';
801 str = '(' + author + ", " + year + ')';
805 str = author + ' ' + year ;
809 str = author + ", " + year ;
821 str = '(' + year + ')';
832 void fillWithBibKeys(Buffer const * const buf,
833 vector<pair<string, docstring> > & keys)
835 /// if this is a child document and the parent is already loaded
836 /// use the parent's list instead [ale990412]
837 Buffer const * const tmp = buf->getMasterBuffer();
840 fillWithBibKeys(tmp, keys);
844 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
845 it->fillWithBibKeys(*buf, keys, it);
847 } // namespace biblio