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.
17 #include "buffer_funcs.h"
19 #include "InsetIterator.h"
20 #include "Paragraph.h"
22 #include "insets/Inset.h"
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetInclude.h"
27 #include "support/lstrings.h"
29 #include "boost/regex.hpp"
37 using support::ascii_lowercase;
38 using support::bformat;
39 using support::compare_ascii_no_case;
40 using support::contains;
41 using support::getVectorFromString;
43 using support::prefixIs;
54 vector<string> const init_possible_cite_commands()
56 char const * const pos[] = {
57 "cite", "citet", "citep", "citealt", "citealp",
58 "citeauthor", "citeyear", "citeyearpar",
59 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
60 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
61 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
63 "footcite", "footcitet", "footcitep", "footcitealt",
64 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
65 "citefield", "citetitle", "cite*"
67 size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
69 return vector<string>(pos, pos + size_pos);
73 vector<string> const & possible_cite_commands()
75 static vector<string> const pos = init_possible_cite_commands();
80 bool is_possible_cite_command(string const & input)
82 vector<string> const & possibles = possible_cite_commands();
83 vector<string>::const_iterator const end = possibles.end();
84 return std::find(possibles.begin(), end, input) != end;
88 string const default_cite_command(CiteEngine engine)
95 case ENGINE_NATBIB_AUTHORYEAR:
98 case ENGINE_NATBIB_NUMERICAL:
112 const docstring TheBibliographyRef(from_ascii("TheBibliographyRef"));
114 string const asValidLatexCommand(string const & input,
115 CiteEngine const engine)
117 string const default_str = default_cite_command(engine);
118 if (!is_possible_cite_command(input))
124 output = default_str;
127 case ENGINE_NATBIB_AUTHORYEAR:
128 case ENGINE_NATBIB_NUMERICAL:
129 if (input == "cite" || input == "citefield" ||
130 input == "citetitle" || input == "cite*")
131 output = default_str;
132 else if (prefixIs(input, "foot"))
133 output = input.substr(4);
138 case ENGINE_JURABIB: {
139 // Jurabib does not support the 'uppercase' natbib style.
141 output = string(1, 'c') + input.substr(1);
145 // Jurabib does not support the 'full' natbib style.
146 string::size_type const n = output.size() - 1;
147 if (output != "cite*" && output[n] == '*')
148 output = output.substr(0, n);
158 docstring const familyName(docstring const & name)
163 // Very simple parser
164 docstring fname = name;
166 // possible authorname combinations are:
167 // "Surname, FirstName"
169 // "FirstName Surname"
171 docstring::size_type idx = fname.find(',');
172 if (idx != docstring::npos)
173 return ltrim(fname.substr(0, idx));
174 idx = fname.rfind('.');
175 if (idx != docstring::npos && idx + 1 < fname.size())
176 fname = ltrim(fname.substr(idx + 1));
177 // test if we have a LaTeX Space in front
178 if (fname[0] == '\\')
179 return fname.substr(2);
185 docstring const getAbbreviatedAuthor(InfoMap const & map, string const & key)
187 BOOST_ASSERT(!map.empty());
189 InfoMap::const_iterator it = map.find(key);
192 docstring const & data = it->second;
194 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
195 docstring::size_type const pos = data.find(TheBibliographyRef);
196 if (pos != docstring::npos) {
201 docstring const opt = trim(data.substr(0, pos - 1));
206 split(opt, authors, '(');
210 docstring author = parseBibTeX(data, "author");
213 author = parseBibTeX(data, "editor");
215 if (author.empty()) {
216 author = parseBibTeX(data, "key");
219 return from_utf8(key);
223 vector<docstring> const authors = getVectorFromString(author, from_ascii(" and "));
227 if (authors.size() == 2)
228 return bformat(_("%1$s and %2$s"),
229 familyName(authors[0]), familyName(authors[1]));
231 if (authors.size() > 2)
232 return bformat(_("%1$s et al."), familyName(authors[0]));
234 return familyName(authors[0]);
238 docstring const getYear(InfoMap const & map, string const & key)
240 BOOST_ASSERT(!map.empty());
242 InfoMap::const_iterator it = map.find(key);
245 docstring const & data = it->second;
247 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
248 docstring::size_type const pos = data.find(TheBibliographyRef);
249 if (pos != docstring::npos) {
254 docstring const opt =
255 trim(data.substr(0, pos - 1));
260 docstring const tmp = split(opt, authors, '(');
262 split(tmp, year, ')');
267 docstring year = parseBibTeX(data, "year");
276 // A functor for use with std::sort, leading to case insensitive sorting
277 class compareNoCase: public std::binary_function<string, string, bool>
280 bool operator()(string const & s1, string const & s2) const {
281 return compare_ascii_no_case(s1, s2) < 0;
287 vector<string> const getKeys(InfoMap const & map)
289 vector<string> bibkeys;
290 InfoMap::const_iterator it = map.begin();
291 InfoMap::const_iterator end = map.end();
292 for (; it != end; ++it) {
293 bibkeys.push_back(it->first);
296 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
301 docstring const getInfo(InfoMap const & map, string const & key)
303 BOOST_ASSERT(!map.empty());
305 InfoMap::const_iterator it = map.find(key);
308 docstring const & data = it->second;
310 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
311 docstring::size_type const pos = data.find(TheBibliographyRef);
312 if (pos != docstring::npos) {
313 docstring::size_type const pos2 = pos + TheBibliographyRef.size();
314 docstring const info = trim(data.substr(pos2));
318 // Search for all possible "required" keys
319 docstring author = parseBibTeX(data, "author");
321 author = parseBibTeX(data, "editor");
323 docstring year = parseBibTeX(data, "year");
324 docstring title = parseBibTeX(data, "title");
325 docstring booktitle = parseBibTeX(data, "booktitle");
326 docstring chapter = parseBibTeX(data, "chapter");
327 docstring number = parseBibTeX(data, "number");
328 docstring volume = parseBibTeX(data, "volume");
329 docstring pages = parseBibTeX(data, "pages");
330 docstring annote = parseBibTeX(data, "annote");
331 docstring media = parseBibTeX(data, "journal");
333 media = parseBibTeX(data, "publisher");
335 media = parseBibTeX(data, "school");
337 media = parseBibTeX(data, "institution");
339 odocstringstream result;
341 result << author << ", ";
344 if (!booktitle.empty())
345 result << ", in " << booktitle;
346 if (!chapter.empty())
347 result << ", Ch. " << chapter;
349 result << ", " << media;
351 result << ", vol. " << volume;
353 result << ", no. " << number;
355 result << ", pp. " << pages;
357 result << ", " << year;
359 result << "\n\n" << annote;
361 docstring const result_str = rtrim(result.str());
362 if (!result_str.empty())
365 // This should never happen (or at least be very unusual!)
372 // Escape special chars.
373 // All characters are literals except: '.|*?+(){}[]^$\'
374 // These characters are literals when preceded by a "\", which is done here
375 // @todo: This function should be moved to support, and then the test in tests
376 // should be moved there as well.
377 string const escape_special_chars(string const & expr)
379 // Search for all chars '.|*?+(){}[^$]\'
380 // Note that '[' and '\' must be escaped.
381 // This is a limitation of boost::regex, but all other chars in BREs
382 // are assumed literal.
383 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
385 // $& is a perl-like expression that expands to all
386 // of the current match
387 // The '$' must be prefixed with the escape character '\' for
388 // boost to treat it as a literal.
389 // Thus, to prefix a matched expression with '\', we use:
390 return boost::regex_replace(expr, reg, "\\\\$&");
394 // A functor for use with std::find_if, used to ascertain whether a
395 // data entry matches the required regex_
396 // @throws: boost::regex_error if the supplied regex pattern is not valid
397 // @todo: This function should be moved to support.
398 class RegexMatch : public std::unary_function<string, bool>
401 // re and icase are used to construct an instance of boost::RegEx.
402 // if icase is true, then matching is insensitive to case
403 RegexMatch(InfoMap const & m, string const & re, bool icase)
404 : map_(m), regex_(re, icase) {}
406 bool operator()(string const & key) const {
407 // the data searched is the key + its associated BibTeX/biblio
410 InfoMap::const_iterator info = map_.find(key);
411 if (info != map_.end())
413 data += ' ' + to_utf8(info->second);
415 // Attempts to find a match for the current RE
416 // somewhere in data.
417 return boost::regex_search(data, regex_);
421 mutable boost::regex regex_;
427 vector<string>::const_iterator searchKeys(InfoMap const & theMap,
428 vector<string> const & keys,
429 string const & search_expr,
430 vector<string>::const_iterator start,
435 // Preliminary checks
436 if (start < keys.begin() || start >= keys.end())
439 string expr = trim(search_expr);
444 // We must escape special chars in the search_expr so that
445 // it is treated as a simple string by boost::regex.
446 expr = escape_special_chars(expr);
449 // Build the functor that will be passed to find_if.
450 RegexMatch const match(theMap, expr, !caseSensitive);
452 // Search the vector of 'keys' from 'start' for one
453 // that matches the predicate 'match'. Searching can
454 // be forward or backward from start.
456 return std::find_if(start, keys.end(), match);
458 vector<string>::const_reverse_iterator rit(start);
459 vector<string>::const_reverse_iterator rend = keys.rend();
460 rit = std::find_if(rit, rend, match);
464 // This is correct and always safe.
465 // (See Meyer's Effective STL, Item 28.)
466 return (++rit).base();
468 catch (boost::regex_error &) {
474 docstring const parseBibTeX(docstring data, string const & findkey)
476 // at first we delete all characters right of '%' and
477 // replace tabs through a space and remove leading spaces
478 // we read the data line by line so that the \n are
482 docstring dummy = token(data,'\n', Entries);
483 while (!dummy.empty()) {
485 dummy = subst(dummy, '\t', ' ');
487 dummy = ltrim(dummy);
488 // ignore lines with a beginning '%' or ignore all right of %
489 docstring::size_type const idx =
490 dummy.empty() ? docstring::npos : dummy.find('%');
491 if (idx != docstring::npos)
492 // Check if this is really a comment or just "\%"
493 if (idx == 0 || dummy[idx - 1] != '\\')
494 dummy.erase(idx, docstring::npos);
496 // This is "\%", so just erase the '\'
497 dummy.erase(idx - 1, 1);
498 // do we have a new token or a new line of
499 // the same one? In the first case we ignore
500 // the \n and in the second we replace it
502 if (!dummy.empty()) {
503 if (!contains(dummy, '='))
504 data_ += ' ' + dummy;
508 dummy = token(data, '\n', ++Entries);
511 // replace double commas with "" for easy scanning
512 data = subst(data_, from_ascii(",,"), from_ascii("\"\""));
518 // now get only the important line of the bibtex entry.
519 // all entries are devided by ',' except the last one.
521 // now we have same behaviour for all entries because the last one
525 // parsing of title and booktitle is different from the
526 // others, because booktitle contains title
528 dummy = token(data, ',', Entries++);
529 if (!dummy.empty()) {
530 found = contains(ascii_lowercase(dummy), from_ascii(findkey));
531 if (findkey == "title" &&
532 contains(ascii_lowercase(dummy), from_ascii("booktitle")))
535 } while (!found && !dummy.empty());
540 // we are not sure, if we get all, because "key= "blah, blah" is
542 // Therefore we read all until the next "=" character, which follows a
544 docstring keyvalue = dummy;
545 dummy = token(data, ',', Entries++);
546 while (!contains(dummy, '=') && !dummy.empty()) {
547 keyvalue += ',' + dummy;
548 dummy = token(data, ',', Entries++);
551 // replace double "" with originals ,, (two commas)
552 // leaving us with the all-important line
553 data = subst(keyvalue, from_ascii("\"\""), from_ascii(",,"));
558 // 2. if there is no opening '{' then a closing '{' is probably cruft.
559 if (!contains(data, '{'))
560 data = rtrim(data, "}");
561 // happens, when last keyword
562 docstring::size_type const idx =
563 !data.empty() ? data.find('=') : docstring::npos;
565 if (idx == docstring::npos)
568 data = trim(data.substr(idx));
571 if (data.length() < 2 || data[0] != '=')
574 // delete '=' and the following spaces
575 data = ltrim(data, " =");
576 if (data.length() < 2) {
577 // not long enough to find delimiters
580 docstring::size_type keypos = 1;
582 if (data[0] == '{') {
584 } else if (data[0] == '"') {
587 // no {} and no "", pure data but with a
588 // possible ',' at the end
589 return rtrim(data, ",");
591 docstring tmp = data.substr(keypos);
592 while (tmp.find('{') != docstring::npos &&
593 tmp.find('}') != docstring::npos &&
594 tmp.find('{') < tmp.find('}') &&
595 tmp.find('{') < tmp.find(enclosing)) {
596 keypos += tmp.find('{') + 1;
597 tmp = data.substr(keypos);
598 keypos += tmp.find('}') + 1;
599 tmp = data.substr(keypos);
601 if (tmp.find(enclosing) == docstring::npos)
604 keypos += tmp.find(enclosing);
605 return data.substr(1, keypos - 1);
615 char const * const citeCommands[] = {
616 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
617 "citeyear", "citeyearpar" };
619 unsigned int const nCiteCommands =
620 sizeof(citeCommands) / sizeof(char *);
622 CiteStyle const citeStyles[] = {
623 CITE, CITET, CITEP, CITEALT, CITEALP,
624 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
626 unsigned int const nCiteStyles =
627 sizeof(citeStyles) / sizeof(CiteStyle);
629 CiteStyle const citeStylesFull[] = {
630 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
632 unsigned int const nCiteStylesFull =
633 sizeof(citeStylesFull) / sizeof(CiteStyle);
635 CiteStyle const citeStylesUCase[] = {
636 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
638 unsigned int const nCiteStylesUCase =
639 sizeof(citeStylesUCase) / sizeof(CiteStyle);
644 CitationStyle::CitationStyle(string const & command)
645 : style(CITE), full(false), forceUCase(false)
650 string cmd = command;
656 string::size_type const n = cmd.size() - 1;
657 if (cmd != "cite" && cmd[n] == '*') {
659 cmd = cmd.substr(0,n);
662 char const * const * const last = citeCommands + nCiteCommands;
663 char const * const * const ptr = std::find(citeCommands, last, cmd);
666 size_t idx = ptr - citeCommands;
667 style = citeStyles[idx];
672 string const CitationStyle::asLatexStr() const
674 string cite = citeCommands[style];
676 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
677 if (std::find(citeStylesFull, last, style) != last)
682 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
683 if (std::find(citeStylesUCase, last, style) != last)
691 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
693 unsigned int nStyles = 0;
694 unsigned int start = 0;
701 case ENGINE_NATBIB_AUTHORYEAR:
702 case ENGINE_NATBIB_NUMERICAL:
703 nStyles = nCiteStyles - 1;
707 nStyles = nCiteStyles;
712 typedef vector<CiteStyle> cite_vec;
714 cite_vec styles(nStyles);
715 cite_vec::size_type i = 0;
717 for (; i != styles.size(); ++i, ++j)
718 styles[i] = citeStyles[j];
724 vector<docstring> const
725 getNumericalStrings(string const & key,
726 InfoMap const & map, vector<CiteStyle> const & styles)
729 return vector<docstring>();
731 docstring const author = getAbbreviatedAuthor(map, key);
732 docstring const year = getYear(map, key);
733 if (author.empty() || year.empty())
734 return vector<docstring>();
736 vector<docstring> vec(styles.size());
737 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
743 str = from_ascii("[#ID]");
747 str = author + " [#ID]";
751 str = author + " #ID";
755 str = from_ascii("#ID");
767 str = '(' + year + ')';
778 vector<docstring> const
779 getAuthorYearStrings(string const & key,
780 InfoMap const & map, vector<CiteStyle> const & styles)
783 return vector<docstring>();
785 docstring const author = getAbbreviatedAuthor(map, key);
786 docstring const year = getYear(map, key);
787 if (author.empty() || year.empty())
788 return vector<docstring>();
790 vector<docstring> vec(styles.size());
791 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
796 // jurabib only: Author/Annotator
797 // (i.e. the "before" field, 2nd opt arg)
798 str = author + "/<" + _("before") + '>';
802 str = author + " (" + year + ')';
806 str = '(' + author + ", " + year + ')';
810 str = author + ' ' + year ;
814 str = author + ", " + year ;
826 str = '(' + year + ')';
837 void fillWithBibKeys(Buffer const * const buf,
838 vector<pair<string, docstring> > & keys)
840 /// if this is a child document and the parent is already loaded
841 /// use the parent's list instead [ale990412]
842 Buffer const * const tmp = buf->getMasterBuffer();
845 fillWithBibKeys(tmp, keys);
849 // Pre-load all child documents.
850 loadChildDocuments(*buf);
852 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
853 it->fillWithBibKeys(*buf, keys, it);
855 } // namespace biblio