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.
18 #include "InsetIterator.h"
19 #include "Paragraph.h"
21 #include "insets/Inset.h"
22 #include "insets/InsetBibitem.h"
23 #include "insets/InsetBibtex.h"
24 #include "insets/InsetInclude.h"
26 #include "support/lstrings.h"
28 #include "boost/regex.hpp"
36 using support::ascii_lowercase;
37 using support::bformat;
38 using support::compare_ascii_no_case;
39 using support::contains;
40 using support::getVectorFromString;
42 using support::prefixIs;
53 vector<string> const init_possible_cite_commands()
55 char const * const pos[] = {
56 "cite", "citet", "citep", "citealt", "citealp",
57 "citeauthor", "citeyear", "citeyearpar",
58 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
59 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
60 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
62 "footcite", "footcitet", "footcitep", "footcitealt",
63 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
64 "citefield", "citetitle", "cite*"
66 size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
68 return vector<string>(pos, pos + size_pos);
72 vector<string> const & possible_cite_commands()
74 static vector<string> const pos = init_possible_cite_commands();
79 bool is_possible_cite_command(string const & input)
81 vector<string> const & possibles = possible_cite_commands();
82 vector<string>::const_iterator const end = possibles.end();
83 return std::find(possibles.begin(), end, input) != end;
87 string const default_cite_command(CiteEngine engine)
94 case ENGINE_NATBIB_AUTHORYEAR:
97 case ENGINE_NATBIB_NUMERICAL:
111 const docstring TheBibliographyRef(from_ascii("TheBibliographyRef"));
113 string const asValidLatexCommand(string const & input,
114 CiteEngine const engine)
116 string const default_str = default_cite_command(engine);
117 if (!is_possible_cite_command(input))
123 output = default_str;
126 case ENGINE_NATBIB_AUTHORYEAR:
127 case ENGINE_NATBIB_NUMERICAL:
128 if (input == "cite" || input == "citefield" ||
129 input == "citetitle" || input == "cite*")
130 output = default_str;
131 else if (prefixIs(input, "foot"))
132 output = input.substr(4);
137 case ENGINE_JURABIB: {
138 // Jurabib does not support the 'uppercase' natbib style.
140 output = string(1, 'c') + input.substr(1);
144 // Jurabib does not support the 'full' natbib style.
145 string::size_type const n = output.size() - 1;
146 if (output != "cite*" && output[n] == '*')
147 output = output.substr(0, n);
157 docstring const familyName(docstring const & name)
162 // Very simple parser
163 docstring fname = name;
165 // possible authorname combinations are:
166 // "Surname, FirstName"
168 // "FirstName Surname"
170 docstring::size_type idx = fname.find(',');
171 if (idx != docstring::npos)
172 return ltrim(fname.substr(0, idx));
173 idx = fname.rfind('.');
174 if (idx != docstring::npos && idx + 1 < fname.size())
175 fname = ltrim(fname.substr(idx + 1));
176 // test if we have a LaTeX Space in front
177 if (fname[0] == '\\')
178 return fname.substr(2);
184 docstring const getAbbreviatedAuthor(InfoMap const & map, string const & key)
186 BOOST_ASSERT(!map.empty());
188 InfoMap::const_iterator it = map.find(key);
191 docstring const & data = it->second;
193 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
194 docstring::size_type const pos = data.find(TheBibliographyRef);
195 if (pos != docstring::npos) {
200 docstring const opt = trim(data.substr(0, pos - 1));
205 split(opt, authors, '(');
209 docstring author = parseBibTeX(data, "author");
212 author = parseBibTeX(data, "editor");
214 if (author.empty()) {
215 author = parseBibTeX(data, "key");
218 return from_utf8(key);
222 vector<docstring> const authors = getVectorFromString(author, from_ascii(" and "));
226 if (authors.size() == 2)
227 return bformat(_("%1$s and %2$s"),
228 familyName(authors[0]), familyName(authors[1]));
230 if (authors.size() > 2)
231 return bformat(_("%1$s et al."), familyName(authors[0]));
233 return familyName(authors[0]);
237 docstring const getYear(InfoMap const & map, string const & key)
239 BOOST_ASSERT(!map.empty());
241 InfoMap::const_iterator it = map.find(key);
244 docstring const & data = it->second;
246 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
247 docstring::size_type const pos = data.find(TheBibliographyRef);
248 if (pos != docstring::npos) {
253 docstring const opt =
254 trim(data.substr(0, pos - 1));
259 docstring const tmp = split(opt, authors, '(');
261 split(tmp, year, ')');
266 docstring year = parseBibTeX(data, "year");
275 // A functor for use with std::sort, leading to case insensitive sorting
276 class compareNoCase: public std::binary_function<string, string, bool>
279 bool operator()(string const & s1, string const & s2) const {
280 return compare_ascii_no_case(s1, s2) < 0;
286 vector<string> const getKeys(InfoMap const & map)
288 vector<string> bibkeys;
289 InfoMap::const_iterator it = map.begin();
290 InfoMap::const_iterator end = map.end();
291 for (; it != end; ++it) {
292 bibkeys.push_back(it->first);
295 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
300 docstring const getInfo(InfoMap const & map, string const & key)
302 BOOST_ASSERT(!map.empty());
304 InfoMap::const_iterator it = map.find(key);
307 docstring const & data = it->second;
309 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
310 docstring::size_type const pos = data.find(TheBibliographyRef);
311 if (pos != docstring::npos) {
312 docstring::size_type const pos2 = pos + TheBibliographyRef.size();
313 docstring const info = trim(data.substr(pos2));
317 // Search for all possible "required" keys
318 docstring author = parseBibTeX(data, "author");
320 author = parseBibTeX(data, "editor");
322 docstring year = parseBibTeX(data, "year");
323 docstring title = parseBibTeX(data, "title");
324 docstring booktitle = parseBibTeX(data, "booktitle");
325 docstring chapter = parseBibTeX(data, "chapter");
326 docstring number = parseBibTeX(data, "number");
327 docstring volume = parseBibTeX(data, "volume");
328 docstring pages = parseBibTeX(data, "pages");
329 docstring annote = parseBibTeX(data, "annote");
330 docstring media = parseBibTeX(data, "journal");
332 media = parseBibTeX(data, "publisher");
334 media = parseBibTeX(data, "school");
336 media = parseBibTeX(data, "institution");
338 odocstringstream result;
340 result << author << ", ";
343 if (!booktitle.empty())
344 result << ", in " << booktitle;
345 if (!chapter.empty())
346 result << ", Ch. " << chapter;
348 result << ", " << media;
350 result << ", vol. " << volume;
352 result << ", no. " << number;
354 result << ", pp. " << pages;
356 result << ", " << year;
358 result << "\n\n" << annote;
360 docstring const result_str = rtrim(result.str());
361 if (!result_str.empty())
364 // This should never happen (or at least be very unusual!)
371 // Escape special chars.
372 // All characters are literals except: '.|*?+(){}[]^$\'
373 // These characters are literals when preceded by a "\", which is done here
374 // @todo: This function should be moved to support, and then the test in tests
375 // should be moved there as well.
376 string const escape_special_chars(string const & expr)
378 // Search for all chars '.|*?+(){}[^$]\'
379 // Note that '[' and '\' must be escaped.
380 // This is a limitation of boost::regex, but all other chars in BREs
381 // are assumed literal.
382 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
384 // $& is a perl-like expression that expands to all
385 // of the current match
386 // The '$' must be prefixed with the escape character '\' for
387 // boost to treat it as a literal.
388 // Thus, to prefix a matched expression with '\', we use:
389 return boost::regex_replace(expr, reg, "\\\\$&");
393 // A functor for use with std::find_if, used to ascertain whether a
394 // data entry matches the required regex_
395 // @throws: boost::regex_error if the supplied regex pattern is not valid
396 // @todo: This function should be moved to support.
397 class RegexMatch : public std::unary_function<string, bool>
400 // re and icase are used to construct an instance of boost::RegEx.
401 // if icase is true, then matching is insensitive to case
402 RegexMatch(InfoMap const & m, string const & re, bool icase)
403 : map_(m), regex_(re, icase) {}
405 bool operator()(string const & key) const {
406 // the data searched is the key + its associated BibTeX/biblio
409 InfoMap::const_iterator info = map_.find(key);
410 if (info != map_.end())
412 data += ' ' + to_utf8(info->second);
414 // Attempts to find a match for the current RE
415 // somewhere in data.
416 return boost::regex_search(data, regex_);
420 mutable boost::regex regex_;
426 vector<string>::const_iterator searchKeys(InfoMap const & theMap,
427 vector<string> const & keys,
428 string const & search_expr,
429 vector<string>::const_iterator start,
434 // Preliminary checks
435 if (start < keys.begin() || start >= keys.end())
438 string expr = trim(search_expr);
443 // We must escape special chars in the search_expr so that
444 // it is treated as a simple string by boost::regex.
445 expr = escape_special_chars(expr);
448 // Build the functor that will be passed to find_if.
449 RegexMatch const match(theMap, expr, !caseSensitive);
451 // Search the vector of 'keys' from 'start' for one
452 // that matches the predicate 'match'. Searching can
453 // be forward or backward from start.
455 return std::find_if(start, keys.end(), match);
457 vector<string>::const_reverse_iterator rit(start);
458 vector<string>::const_reverse_iterator rend = keys.rend();
459 rit = std::find_if(rit, rend, match);
463 // This is correct and always safe.
464 // (See Meyer's Effective STL, Item 28.)
465 return (++rit).base();
467 catch (boost::regex_error &) {
473 docstring const parseBibTeX(docstring data, string const & findkey)
475 // at first we delete all characters right of '%' and
476 // replace tabs through a space and remove leading spaces
477 // we read the data line by line so that the \n are
481 docstring dummy = token(data,'\n', Entries);
482 while (!dummy.empty()) {
484 dummy = subst(dummy, '\t', ' ');
486 dummy = ltrim(dummy);
487 // ignore lines with a beginning '%' or ignore all right of %
488 docstring::size_type const idx =
489 dummy.empty() ? docstring::npos : dummy.find('%');
490 if (idx != docstring::npos)
491 // Check if this is really a comment or just "\%"
492 if (idx == 0 || dummy[idx - 1] != '\\')
493 dummy.erase(idx, docstring::npos);
495 // This is "\%", so just erase the '\'
496 dummy.erase(idx - 1, 1);
497 // do we have a new token or a new line of
498 // the same one? In the first case we ignore
499 // the \n and in the second we replace it
501 if (!dummy.empty()) {
502 if (!contains(dummy, '='))
503 data_ += ' ' + dummy;
507 dummy = token(data, '\n', ++Entries);
510 // replace double commas with "" for easy scanning
511 data = subst(data_, from_ascii(",,"), from_ascii("\"\""));
517 // now get only the important line of the bibtex entry.
518 // all entries are devided by ',' except the last one.
520 // now we have same behaviour for all entries because the last one
524 // parsing of title and booktitle is different from the
525 // others, because booktitle contains title
527 dummy = token(data, ',', Entries++);
528 if (!dummy.empty()) {
529 found = contains(ascii_lowercase(dummy), from_ascii(findkey));
530 if (findkey == "title" &&
531 contains(ascii_lowercase(dummy), from_ascii("booktitle")))
534 } while (!found && !dummy.empty());
539 // we are not sure, if we get all, because "key= "blah, blah" is
541 // Therefore we read all until the next "=" character, which follows a
543 docstring keyvalue = dummy;
544 dummy = token(data, ',', Entries++);
545 while (!contains(dummy, '=') && !dummy.empty()) {
546 keyvalue += ',' + dummy;
547 dummy = token(data, ',', Entries++);
550 // replace double "" with originals ,, (two commas)
551 // leaving us with the all-important line
552 data = subst(keyvalue, from_ascii("\"\""), from_ascii(",,"));
557 // 2. if there is no opening '{' then a closing '{' is probably cruft.
558 if (!contains(data, '{'))
559 data = rtrim(data, "}");
560 // happens, when last keyword
561 docstring::size_type const idx =
562 !data.empty() ? data.find('=') : docstring::npos;
564 if (idx == docstring::npos)
567 data = trim(data.substr(idx));
570 if (data.length() < 2 || data[0] != '=')
573 // delete '=' and the following spaces
574 data = ltrim(data, " =");
575 if (data.length() < 2) {
576 // not long enough to find delimiters
579 docstring::size_type keypos = 1;
581 if (data[0] == '{') {
583 } else if (data[0] == '"') {
586 // no {} and no "", pure data but with a
587 // possible ',' at the end
588 return rtrim(data, ",");
590 docstring tmp = data.substr(keypos);
591 while (tmp.find('{') != docstring::npos &&
592 tmp.find('}') != docstring::npos &&
593 tmp.find('{') < tmp.find('}') &&
594 tmp.find('{') < tmp.find(enclosing)) {
595 keypos += tmp.find('{') + 1;
596 tmp = data.substr(keypos);
597 keypos += tmp.find('}') + 1;
598 tmp = data.substr(keypos);
600 if (tmp.find(enclosing) == docstring::npos)
603 keypos += tmp.find(enclosing);
604 return data.substr(1, keypos - 1);
614 char const * const citeCommands[] = {
615 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
616 "citeyear", "citeyearpar" };
618 unsigned int const nCiteCommands =
619 sizeof(citeCommands) / sizeof(char *);
621 CiteStyle const citeStyles[] = {
622 CITE, CITET, CITEP, CITEALT, CITEALP,
623 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
625 unsigned int const nCiteStyles =
626 sizeof(citeStyles) / sizeof(CiteStyle);
628 CiteStyle const citeStylesFull[] = {
629 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
631 unsigned int const nCiteStylesFull =
632 sizeof(citeStylesFull) / sizeof(CiteStyle);
634 CiteStyle const citeStylesUCase[] = {
635 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
637 unsigned int const nCiteStylesUCase =
638 sizeof(citeStylesUCase) / sizeof(CiteStyle);
643 CitationStyle::CitationStyle(string const & command)
644 : style(CITE), full(false), forceUCase(false)
649 string cmd = command;
655 string::size_type const n = cmd.size() - 1;
656 if (cmd != "cite" && cmd[n] == '*') {
658 cmd = cmd.substr(0,n);
661 char const * const * const last = citeCommands + nCiteCommands;
662 char const * const * const ptr = std::find(citeCommands, last, cmd);
665 size_t idx = ptr - citeCommands;
666 style = citeStyles[idx];
671 string const CitationStyle::asLatexStr() const
673 string cite = citeCommands[style];
675 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
676 if (std::find(citeStylesFull, last, style) != last)
681 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
682 if (std::find(citeStylesUCase, last, style) != last)
690 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
692 unsigned int nStyles = 0;
693 unsigned int start = 0;
700 case ENGINE_NATBIB_AUTHORYEAR:
701 case ENGINE_NATBIB_NUMERICAL:
702 nStyles = nCiteStyles - 1;
706 nStyles = nCiteStyles;
711 typedef vector<CiteStyle> cite_vec;
713 cite_vec styles(nStyles);
714 cite_vec::size_type i = 0;
716 for (; i != styles.size(); ++i, ++j)
717 styles[i] = citeStyles[j];
723 vector<docstring> const
724 getNumericalStrings(string const & key,
725 InfoMap const & map, vector<CiteStyle> const & styles)
728 return vector<docstring>();
730 docstring const author = getAbbreviatedAuthor(map, key);
731 docstring const year = getYear(map, key);
732 if (author.empty() || year.empty())
733 return vector<docstring>();
735 vector<docstring> vec(styles.size());
736 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
742 str = from_ascii("[#ID]");
746 str = author + " [#ID]";
750 str = author + " #ID";
754 str = from_ascii("#ID");
766 str = '(' + year + ')';
777 vector<docstring> const
778 getAuthorYearStrings(string const & key,
779 InfoMap const & map, vector<CiteStyle> const & styles)
782 return vector<docstring>();
784 docstring const author = getAbbreviatedAuthor(map, key);
785 docstring const year = getYear(map, key);
786 if (author.empty() || year.empty())
787 return vector<docstring>();
789 vector<docstring> vec(styles.size());
790 for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
795 // jurabib only: Author/Annotator
796 // (i.e. the "before" field, 2nd opt arg)
797 str = author + "/<" + _("before") + '>';
801 str = author + " (" + year + ')';
805 str = '(' + author + ", " + year + ')';
809 str = author + ' ' + year ;
813 str = author + ", " + year ;
825 str = '(' + year + ')';
836 void fillWithBibKeys(Buffer const * const buf,
837 vector<pair<string, docstring> > & keys)
839 /// if this is a child document and the parent is already loaded
840 /// use the parent's list instead [ale990412]
841 Buffer const * const tmp = buf->getMasterBuffer();
844 fillWithBibKeys(tmp, keys);
848 for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
849 it->fillWithBibKeys(*buf, keys, it);
851 } // namespace biblio