3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "BiblioInfo.h"
17 #include "BufferParams.h"
18 #include "buffer_funcs.h"
20 #include "InsetIterator.h"
22 #include "Paragraph.h"
23 #include "TextClass.h"
24 #include "TocBackend.h"
26 #include "support/convert.h"
27 #include "support/debug.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
31 #include "support/lstrings.h"
32 #include "support/regex.h"
33 #include "support/textutils.h"
38 using namespace lyx::support;
45 // gets the "family name" from an author-type string
46 docstring familyName(docstring const & name)
51 // first we look for a comma, and take the last name to be everything
52 // preceding the right-most one, so that we also get the "jr" part.
53 docstring::size_type idx = name.rfind(',');
54 if (idx != docstring::npos)
55 return ltrim(name.substr(0, idx));
57 // OK, so now we want to look for the last name. We're going to
58 // include the "von" part. This isn't perfect.
59 // Split on spaces, to get various tokens.
60 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
61 // If we only get two, assume the last one is the last name
62 if (pieces.size() <= 2)
65 // Now we look for the first token that begins with a lower case letter.
66 vector<docstring>::const_iterator it = pieces.begin();
67 vector<docstring>::const_iterator en = pieces.end();
68 for (; it != en; ++it) {
69 if ((*it).size() == 0)
71 char_type const c = (*it)[0];
76 if (it == en) // we never found a "von"
79 // reconstruct what we need to return
82 for (; it != en; ++it) {
92 // converts a string containing LaTeX commands into unicode
94 docstring convertLaTeXCommands(docstring const & str)
99 bool scanning_cmd = false;
100 bool scanning_math = false;
101 bool escaped = false; // used to catch \$, etc.
103 char_type const ch = val[0];
105 // if we're scanning math, we output everything until we
106 // find an unescaped $, at which point we break out.
113 scanning_math = false;
119 // if we're scanning a command name, then we just
120 // discard characters until we hit something that
123 if (isAlphaASCII(ch)) {
128 // so we're done with this command.
129 // now we fall through and check this character.
130 scanning_cmd = false;
133 // was the last character a \? If so, then this is something like:
134 // \\ or \$, so we'll just output it. That's probably not always right...
136 // exception: output \, as THIN SPACE
138 ret.push_back(0x2009);
149 scanning_math = true;
153 // we just ignore braces
154 if (ch == '{' || ch == '}') {
159 // we're going to check things that look like commands, so if
160 // this doesn't, just output it.
167 // ok, could be a command of some sort
168 // let's see if it corresponds to some unicode
169 // unicodesymbols has things in the form: \"{u},
170 // whereas we may see things like: \"u. So we'll
171 // look for that and change it, if necessary.
172 static lyx::regex const reg("^\\\\\\W\\w");
173 if (lyx::regex_search(to_utf8(val), reg)) {
174 val.insert(3, from_ascii("}"));
175 val.insert(2, from_ascii("{"));
178 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem,
179 Encodings::TEXT_CMD);
180 if (!cnvtd.empty()) {
181 // it did, so we'll take that bit and proceed with what's left
186 // it's a command of some sort
197 //////////////////////////////////////////////////////////////////////
201 //////////////////////////////////////////////////////////////////////
203 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
204 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_(),
209 docstring const BibTeXInfo::getAbbreviatedAuthor() const
212 docstring const opt = label();
217 docstring const remainder = trim(split(opt, authors, '('));
218 if (remainder.empty())
219 // in this case, we didn't find a "(",
220 // so we don't have author (year)
225 docstring author = convertLaTeXCommands(operator[]("author"));
226 if (author.empty()) {
227 author = convertLaTeXCommands(operator[]("editor"));
232 // FIXME Move this to a separate routine that can
233 // be called from elsewhere.
235 // OK, we've got some names. Let's format them.
236 // Try to split the author list on " and "
237 vector<docstring> const authors =
238 getVectorFromString(author, from_ascii(" and "));
240 if (authors.size() == 2)
241 return bformat(_("%1$s and %2$s"),
242 familyName(authors[0]), familyName(authors[1]));
244 if (authors.size() > 2)
245 return bformat(_("%1$s et al."), familyName(authors[0]));
247 return familyName(authors[0]);
251 docstring const BibTeXInfo::getYear() const
254 return operator[]("year");
256 docstring const opt = label();
261 docstring tmp = split(opt, authors, '(');
263 // we don't have author (year)
266 tmp = split(tmp, year, ')');
271 docstring const BibTeXInfo::getXRef() const
275 return operator[]("crossref");
280 string parseOptions(string const & format, string & optkey,
281 string & ifpart, string & elsepart);
283 // Calls parseOptions to deal with an embedded option, such as:
284 // {%number%[[, no.~%number%]]}
285 // which must appear at the start of format. ifelsepart gets the
286 // whole of the option, and we return what's left after the option.
287 // we return format if there is an error.
288 string parseEmbeddedOption(string const & format, string & ifelsepart)
290 LASSERT(format[0] == '{' && format[1] == '%', return format);
294 string const rest = parseOptions(format, optkey, ifpart, elsepart);
295 if (format == rest) { // parse error
296 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
299 LASSERT(rest.size() <= format.size(), /* */);
300 ifelsepart = format.substr(0, format.size() - rest.size());
305 // Gets a "clause" from a format string, where the clause is
306 // delimited by '[[' and ']]'. Returns what is left after the
307 // clause is removed, and returns format if there is an error.
308 string getClause(string const & format, string & clause)
313 // we'll remove characters from the front of fmt as we
316 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
321 // check for an embedded option
322 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
324 string const rest = parseEmbeddedOption(fmt, part);
326 LYXERR0("ERROR! Couldn't parse embedded option in `" << format <<"'.");
331 } else { // it's just a normal character
340 // parse an options string, which must appear at the start of the
341 // format parameter. puts the parsed bits in optkey, ifpart, and
342 // elsepart and returns what's left after the option is removed.
343 // if there's an error, it returns format itself.
344 string parseOptions(string const & format, string & optkey,
345 string & ifpart, string & elsepart)
347 LASSERT(format[0] == '{' && format[1] == '%', return format);
349 string fmt = format.substr(2);
350 size_t pos = fmt.find('%'); // end of key
351 if (pos == string::npos) {
352 LYXERR0("Error parsing `" << format <<"'. Can't find end of key.");
355 optkey = fmt.substr(0,pos);
356 fmt = fmt.substr(pos + 1);
357 // [[format]] should be next
358 if (fmt[0] != '[' || fmt[1] != '[') {
359 LYXERR0("Error parsing `" << format <<"'. Can't find '[[' after key.");
364 fmt = getClause(curfmt, ifpart);
366 LYXERR0("Error parsing `" << format <<"'. Couldn't get if clause.");
370 if (fmt[0] == '}') // we're done, no else clause
371 return fmt.substr(1);
373 // else part should follow
374 if (fmt[0] != '[' || fmt[1] != '[') {
375 LYXERR0("Error parsing `" << format <<"'. Can't find else clause.");
380 fmt = getClause(curfmt, elsepart);
382 if (fmt == curfmt || fmt[0] != '}') {
383 LYXERR0("Error parsing `" << format <<"'. Can't find end of option.");
386 return fmt.substr(1);
392 docstring BibTeXInfo::expandFormat(string const & format,
393 BibTeXInfo const * const xref, int & counter, Buffer const & buf,
396 // incorrect use of macros could put us in an infinite loop
397 static int max_passes = 5000;
398 docstring ret; // return value
400 bool scanning_key = false;
401 bool scanning_rich = false;
404 // we'll remove characters from the front of fmt as we
407 if (counter++ > max_passes) {
408 LYXERR0("Recursion limit reached while parsing `"
413 char_type thischar = fmt[0];
414 if (thischar == '%') {
415 // beginning or end of key
418 scanning_key = false;
419 // so we replace the key with its value, which may be empty
423 buf.params().documentClass().getCiteMacro(key);
424 fmt = val + fmt.substr(1);
426 } else if (key[0] == '_') {
427 // a translatable bit
429 buf.params().documentClass().getCiteMacro(key);
430 docstring const trans =
431 translateIfPossible(from_utf8(val), buf.params().language->code());
434 docstring const val = getValueForKey(key, xref);
443 else if (thischar == '{') {
444 // beginning of option?
446 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
449 if (fmt.size() > 1) {
451 // it is the beginning of an optional format
455 string const newfmt =
456 parseOptions(fmt, optkey, ifpart, elsepart);
457 if (newfmt == fmt) // parse error
460 docstring const val = getValueForKey(optkey, xref);
462 ret += expandFormat(ifpart, xref, counter, buf, richtext);
463 else if (!elsepart.empty())
464 ret += expandFormat(elsepart, xref, counter, buf, richtext);
465 // fmt will have been shortened for us already
469 // beginning of rich text
470 scanning_rich = true;
475 // we are here if '{' was not followed by % or !.
476 // So it's just a character.
479 else if (scanning_rich && thischar == '!'
480 && fmt.size() > 1 && fmt[1] == '}') {
482 scanning_rich = false;
486 else if (scanning_key)
487 key += char(thischar);
492 // we need to escape '<' and '>'
495 else if (thischar == '>')
500 } else if (!scanning_rich /* && !richtext */)
502 // else the character is discarded, which will happen only if
503 // richtext == false and we are scanning rich text
507 LYXERR0("Never found end of key in `" << format << "'!");
511 LYXERR0("Never found end of rich text in `" << format << "'!");
518 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
519 Buffer const & buf, bool richtext) const
525 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
530 DocumentClass const & dc = buf.params().documentClass();
531 string const & format = dc.getCiteFormat(to_utf8(entry_type_));
533 info_ = expandFormat(format, xref, counter, buf, richtext);
536 info_ = convertLaTeXCommands(info_);
541 docstring const & BibTeXInfo::operator[](docstring const & field) const
543 BibTeXInfo::const_iterator it = find(field);
546 static docstring const empty_value = docstring();
551 docstring const & BibTeXInfo::operator[](string const & field) const
553 return operator[](from_ascii(field));
557 docstring BibTeXInfo::getValueForKey(string const & key,
558 BibTeXInfo const * const xref) const
560 docstring const ret = operator[](key);
561 if (!ret.empty() || !xref)
567 //////////////////////////////////////////////////////////////////////
571 //////////////////////////////////////////////////////////////////////
574 // A functor for use with sort, leading to case insensitive sorting
575 class compareNoCase: public binary_function<docstring, docstring, bool>
578 bool operator()(docstring const & s1, docstring const & s2) const {
579 return compare_no_case(s1, s2) < 0;
585 vector<docstring> const BiblioInfo::getKeys() const
587 vector<docstring> bibkeys;
588 BiblioInfo::const_iterator it = begin();
589 for (; it != end(); ++it)
590 bibkeys.push_back(it->first);
591 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
596 vector<docstring> const BiblioInfo::getFields() const
598 vector<docstring> bibfields;
599 set<docstring>::const_iterator it = field_names_.begin();
600 set<docstring>::const_iterator end = field_names_.end();
601 for (; it != end; ++it)
602 bibfields.push_back(*it);
603 sort(bibfields.begin(), bibfields.end());
608 vector<docstring> const BiblioInfo::getEntries() const
610 vector<docstring> bibentries;
611 set<docstring>::const_iterator it = entry_types_.begin();
612 set<docstring>::const_iterator end = entry_types_.end();
613 for (; it != end; ++it)
614 bibentries.push_back(*it);
615 sort(bibentries.begin(), bibentries.end());
620 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
622 BiblioInfo::const_iterator it = find(key);
625 BibTeXInfo const & data = it->second;
626 return data.getAbbreviatedAuthor();
630 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
632 BiblioInfo::const_iterator it = find(key);
635 BibTeXInfo const & data = it->second;
636 return data.citeNumber();
640 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
642 BiblioInfo::const_iterator it = find(key);
645 BibTeXInfo const & data = it->second;
646 docstring year = data.getYear();
648 // let's try the crossref
649 docstring const xref = data.getXRef();
651 return _("No year"); // no luck
652 BiblioInfo::const_iterator const xrefit = find(xref);
654 return _("No year"); // no luck again
655 BibTeXInfo const & xref_data = xrefit->second;
656 year = xref_data.getYear();
658 if (use_modifier && data.modifier() != 0)
659 year += data.modifier();
664 docstring const BiblioInfo::getInfo(docstring const & key,
665 Buffer const & buf, bool richtext) const
667 BiblioInfo::const_iterator it = find(key);
670 BibTeXInfo const & data = it->second;
671 BibTeXInfo const * xrefptr = 0;
672 docstring const xref = data.getXRef();
674 BiblioInfo::const_iterator const xrefit = find(xref);
676 xrefptr = &(xrefit->second);
678 return data.getInfo(xrefptr, buf, richtext);
682 bool BiblioInfo::isBibtex(docstring const & key) const
684 BiblioInfo::const_iterator it = find(key);
687 return it->second.isBibTeX();
692 vector<docstring> const BiblioInfo::getCiteStrings(
693 docstring const & key, Buffer const & buf) const
695 CiteEngine const engine = buf.params().citeEngine();
696 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
697 return getNumericalStrings(key, buf);
699 return getAuthorYearStrings(key, buf);
703 vector<docstring> const BiblioInfo::getNumericalStrings(
704 docstring const & key, Buffer const & buf) const
707 return vector<docstring>();
709 docstring const author = getAbbreviatedAuthor(key);
710 docstring const year = getYear(key);
711 if (author.empty() || year.empty())
712 return vector<docstring>();
714 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
716 vector<docstring> vec(styles.size());
717 for (size_t i = 0; i != vec.size(); ++i) {
723 str = from_ascii("[#ID]");
727 str = _("Add to bibliography only.");
731 str = author + " [#ID]";
735 str = author + " #ID";
739 str = from_ascii("#ID");
751 str = '(' + year + ')';
762 vector<docstring> const BiblioInfo::getAuthorYearStrings(
763 docstring const & key, Buffer const & buf) const
766 return vector<docstring>();
768 docstring const author = getAbbreviatedAuthor(key);
769 docstring const year = getYear(key);
770 if (author.empty() || year.empty())
771 return vector<docstring>();
773 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
775 vector<docstring> vec(styles.size());
776 for (size_t i = 0; i != vec.size(); ++i) {
781 // jurabib only: Author/Annotator
782 // (i.e. the "before" field, 2nd opt arg)
783 str = author + "/<" + _("before") + '>';
787 str = _("Add to bibliography only.");
791 str = author + " (" + year + ')';
795 str = '(' + author + ", " + year + ')';
799 str = author + ' ' + year ;
803 str = author + ", " + year ;
815 str = '(' + year + ')';
824 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
826 bimap_.insert(info.begin(), info.end());
827 field_names_.insert(info.field_names_.begin(), info.field_names_.end());
828 entry_types_.insert(info.entry_types_.begin(), info.entry_types_.end());
833 // used in xhtml to sort a list of BibTeXInfo objects
834 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
836 docstring const lauth = lhs->getAbbreviatedAuthor();
837 docstring const rauth = rhs->getAbbreviatedAuthor();
838 docstring const lyear = lhs->getYear();
839 docstring const ryear = rhs->getYear();
840 docstring const ltitl = lhs->operator[]("title");
841 docstring const rtitl = rhs->operator[]("title");
842 return (lauth < rauth)
843 || (lauth == rauth && lyear < ryear)
844 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
849 void BiblioInfo::collectCitedEntries(Buffer const & buf)
851 cited_entries_.clear();
852 // We are going to collect all the citation keys used in the document,
853 // getting them from the TOC.
854 // FIXME We may want to collect these differently, in the first case,
855 // so that we might have them in order of appearance.
856 set<docstring> citekeys;
857 Toc const & toc = buf.tocBackend().toc("citation");
858 Toc::const_iterator it = toc.begin();
859 Toc::const_iterator const en = toc.end();
860 for (; it != en; ++it) {
861 if (it->str().empty())
863 vector<docstring> const keys = getVectorFromString(it->str());
864 citekeys.insert(keys.begin(), keys.end());
866 if (citekeys.empty())
869 // We have a set of the keys used in this document.
870 // We will now convert it to a list of the BibTeXInfo objects used in
872 vector<BibTeXInfo const *> bi;
873 set<docstring>::const_iterator cit = citekeys.begin();
874 set<docstring>::const_iterator const cen = citekeys.end();
875 for (; cit != cen; ++cit) {
876 BiblioInfo::const_iterator const bt = find(*cit);
877 if (bt == end() || !bt->second.isBibTeX())
879 bi.push_back(&(bt->second));
882 sort(bi.begin(), bi.end(), lSorter);
884 // Now we can write the sorted keys
885 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
886 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
887 for (; bit != ben; ++bit)
888 cited_entries_.push_back((*bit)->key());
892 void BiblioInfo::makeCitationLabels(Buffer const & buf)
894 collectCitedEntries(buf);
895 CiteEngine const engine = buf.params().citeEngine();
897 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
901 // used to remember the last one we saw
902 // we'll be comparing entries to see if we need to add
903 // modifiers, like "1984a"
904 map<docstring, BibTeXInfo>::iterator last;
906 vector<docstring>::const_iterator it = cited_entries_.begin();
907 vector<docstring>::const_iterator const en = cited_entries_.end();
908 for (; it != en; ++it) {
909 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
910 // this shouldn't happen, but...
911 if (biit == bimap_.end())
912 // ...fail gracefully, anyway.
914 BibTeXInfo & entry = biit->second;
916 docstring const num = convert<docstring>(++keynumber);
917 entry.setCiteNumber(num);
919 if (it != cited_entries_.begin()
920 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
921 // we access the year via getYear() so as to get it from the xref,
922 // if we need to do so
923 && getYear(entry.key()) == getYear(last->second.key())) {
925 // so the last one should have been 'a'
926 last->second.setModifier('a');
928 } else if (modifier == 'z')
935 entry.setModifier(modifier);
936 // remember the last one
943 //////////////////////////////////////////////////////////////////////
947 //////////////////////////////////////////////////////////////////////
952 char const * const citeCommands[] = {
953 "cite", "citet", "citep", "citealt", "citealp",
954 "citeauthor", "citeyear", "citeyearpar", "nocite" };
956 unsigned int const nCiteCommands =
957 sizeof(citeCommands) / sizeof(char *);
959 CiteStyle const citeStylesArray[] = {
960 CITE, CITET, CITEP, CITEALT, CITEALP,
961 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
963 unsigned int const nCiteStyles =
964 sizeof(citeStylesArray) / sizeof(CiteStyle);
966 CiteStyle const citeStylesFull[] = {
967 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
969 unsigned int const nCiteStylesFull =
970 sizeof(citeStylesFull) / sizeof(CiteStyle);
972 CiteStyle const citeStylesUCase[] = {
973 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
975 unsigned int const nCiteStylesUCase =
976 sizeof(citeStylesUCase) / sizeof(CiteStyle);
981 CitationStyle citationStyleFromString(string const & command)
987 string cmd = command;
989 s.forceUpperCase = true;
993 size_t const n = cmd.size() - 1;
994 if (cmd != "cite" && cmd[n] == '*') {
996 cmd = cmd.substr(0, n);
999 char const * const * const last = citeCommands + nCiteCommands;
1000 char const * const * const ptr = find(citeCommands, last, cmd);
1003 size_t idx = ptr - citeCommands;
1004 s.style = citeStylesArray[idx];
1010 string citationStyleToString(const CitationStyle & s)
1012 string cite = citeCommands[s.style];
1014 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
1015 if (std::find(citeStylesFull, last, s.style) != last)
1019 if (s.forceUpperCase) {
1020 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
1021 if (std::find(citeStylesUCase, last, s.style) != last)
1028 vector<CiteStyle> citeStyles(CiteEngine engine)
1030 vector<CiteStyle> styles(0);
1034 styles.push_back(CITE);
1036 case ENGINE_JURABIB:
1037 styles.push_back(CITE);
1038 case ENGINE_NATBIB_AUTHORYEAR:
1039 styles.push_back(CITET);
1040 styles.push_back(CITEP);
1041 styles.push_back(CITEALT);
1042 styles.push_back(CITEALP);
1043 styles.push_back(CITEAUTHOR);
1044 styles.push_back(CITEYEAR);
1045 styles.push_back(CITEYEARPAR);
1047 case ENGINE_NATBIB_NUMERICAL:
1048 styles.push_back(CITET);
1049 styles.push_back(CITEALT);
1050 styles.push_back(CITEAUTHOR);
1051 styles.push_back(CITEP);
1052 styles.push_back(CITEALP);
1053 styles.push_back(CITEYEAR);
1054 styles.push_back(CITEYEARPAR);
1058 styles.push_back(NOCITE);