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 "insets/Inset.h"
27 #include "insets/InsetBibitem.h"
28 #include "insets/InsetBibtex.h"
29 #include "insets/InsetInclude.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lassert.h"
36 #include "support/lstrings.h"
37 #include "support/textutils.h"
39 #include "boost/regex.hpp"
44 using namespace lyx::support;
51 // gets the "family name" from an author-type string
52 docstring familyName(docstring const & name)
57 // first we look for a comma, and take the last name to be everything
58 // preceding the right-most one, so that we also get the "jr" part.
59 docstring::size_type idx = name.rfind(',');
60 if (idx != docstring::npos)
61 return ltrim(name.substr(0, idx));
63 // OK, so now we want to look for the last name. We're going to
64 // include the "von" part. This isn't perfect.
65 // Split on spaces, to get various tokens.
66 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
67 // If we only get two, assume the last one is the last name
68 if (pieces.size() <= 2)
71 // Now we look for the first token that begins with a lower case letter.
72 vector<docstring>::const_iterator it = pieces.begin();
73 vector<docstring>::const_iterator en = pieces.end();
74 for (; it != en; ++it) {
75 if ((*it).size() == 0)
77 char_type const c = (*it)[0];
82 if (it == en) // we never found a "von"
85 // reconstruct what we need to return
88 for (; it != en; ++it) {
98 // converts a string containing LaTeX commands into unicode
100 docstring convertLaTeXCommands(docstring const & str)
105 bool scanning_cmd = false;
106 bool scanning_math = false;
107 bool escaped = false; // used to catch \$, etc.
109 char_type const ch = val[0];
111 // if we're scanning math, we output everything until we
112 // find an unescaped $, at which point we break out.
119 scanning_math = false;
125 // if we're scanning a command name, then we just
126 // discard characters until we hit something that
129 if (isAlphaASCII(ch)) {
134 // so we're done with this command.
135 // now we fall through and check this character.
136 scanning_cmd = false;
139 // was the last character a \? If so, then this is something like:
140 // \\ or \$, so we'll just output it. That's probably not always right...
142 // exception: output \, as THIN SPACE
144 ret.push_back(0x2009);
155 scanning_math = true;
159 // we just ignore braces
160 if (ch == '{' || ch == '}') {
165 // we're going to check things that look like commands, so if
166 // this doesn't, just output it.
173 // ok, could be a command of some sort
174 // let's see if it corresponds to some unicode
175 // unicodesymbols has things in the form: \"{u},
176 // whereas we may see things like: \"u. So we'll
177 // look for that and change it, if necessary.
178 static boost::regex const reg("^\\\\\\W\\w");
179 if (boost::regex_search(to_utf8(val), reg)) {
180 val.insert(3, from_ascii("}"));
181 val.insert(2, from_ascii("{"));
184 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem,
185 Encodings::TEXT_CMD);
186 if (!cnvtd.empty()) {
187 // it did, so we'll take that bit and proceed with what's left
192 // it's a command of some sort
203 //////////////////////////////////////////////////////////////////////
207 //////////////////////////////////////////////////////////////////////
209 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
210 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_(),
215 docstring const BibTeXInfo::getAbbreviatedAuthor() const
218 docstring const opt = label();
223 docstring const remainder = trim(split(opt, authors, '('));
224 if (remainder.empty())
225 // in this case, we didn't find a "(",
226 // so we don't have author (year)
231 docstring author = convertLaTeXCommands(operator[]("author"));
232 if (author.empty()) {
233 author = convertLaTeXCommands(operator[]("editor"));
238 // FIXME Move this to a separate routine that can
239 // be called from elsewhere.
241 // OK, we've got some names. Let's format them.
242 // Try to split the author list on " and "
243 vector<docstring> const authors =
244 getVectorFromString(author, from_ascii(" and "));
246 if (authors.size() == 2)
247 return bformat(_("%1$s and %2$s"),
248 familyName(authors[0]), familyName(authors[1]));
250 if (authors.size() > 2)
251 return bformat(_("%1$s et al."), familyName(authors[0]));
253 return familyName(authors[0]);
257 docstring const BibTeXInfo::getYear() const
260 return operator[]("year");
262 docstring const opt = label();
267 docstring tmp = split(opt, authors, '(');
269 // we don't have author (year)
272 tmp = split(tmp, year, ')');
277 docstring const BibTeXInfo::getXRef() const
281 return operator[]("crossref");
286 string parseOptions(string const & format, string & optkey,
287 string & ifpart, string & elsepart);
289 // Calls parseOptions to deal with an embedded option, such as:
290 // {%number%[[, no.~%number%]]}
291 // which must appear at the start of format. ifelsepart gets the
292 // whole of the option, and we return what's left after the option.
293 // we return format if there is an error.
294 string parseEmbeddedOption(string const & format, string & ifelsepart)
296 LASSERT(format[0] == '{' && format[1] == '%', return format);
300 string const rest = parseOptions(format, optkey, ifpart, elsepart);
301 if (format == rest) { // parse error
302 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
305 LASSERT(rest.size() <= format.size(), /* */);
306 ifelsepart = format.substr(0, format.size() - rest.size());
311 // Gets a "clause" from a format string, where the clause is
312 // delimited by '[[' and ']]'. Returns what is left after the
313 // clause is removed, and returns format if there is an error.
314 string getClause(string const & format, string & clause)
319 // we'll remove characters from the front of fmt as we
322 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
327 // check for an embedded option
328 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
330 string const rest = parseEmbeddedOption(fmt, part);
332 LYXERR0("ERROR! Couldn't parse embedded option in `" << format <<"'.");
337 } else { // it's just a normal character
346 // parse an options string, which must appear at the start of the
347 // format parameter. puts the parsed bits in optkey, ifpart, and
348 // elsepart and returns what's left after the option is removed.
349 // if there's an error, it returns format itself.
350 string parseOptions(string const & format, string & optkey,
351 string & ifpart, string & elsepart)
353 LASSERT(format[0] == '{' && format[1] == '%', return format);
355 string fmt = format.substr(2);
356 size_t pos = fmt.find('%'); // end of key
357 if (pos == string::npos) {
358 LYXERR0("Error parsing `" << format <<"'. Can't find end of key.");
361 optkey = fmt.substr(0,pos);
362 fmt = fmt.substr(pos + 1);
363 // [[format]] should be next
364 if (fmt[0] != '[' || fmt[1] != '[') {
365 LYXERR0("Error parsing `" << format <<"'. Can't find '[[' after key.");
370 fmt = getClause(curfmt, ifpart);
372 LYXERR0("Error parsing `" << format <<"'. Couldn't get if clause.");
376 if (fmt[0] == '}') // we're done, no else clause
377 return fmt.substr(1);
379 // else part should follow
380 if (fmt[0] != '[' || fmt[1] != '[') {
381 LYXERR0("Error parsing `" << format <<"'. Can't find else clause.");
386 fmt = getClause(curfmt, elsepart);
388 if (fmt == curfmt || fmt[0] != '}') {
389 LYXERR0("Error parsing `" << format <<"'. Can't find end of option.");
392 return fmt.substr(1);
398 docstring BibTeXInfo::expandFormat(string const & format,
399 BibTeXInfo const * const xref, int & counter, Buffer const & buf,
402 // incorrect use of macros could put us in an infinite loop
403 static int max_passes = 5000;
404 docstring ret; // return value
406 bool scanning_key = false;
407 bool scanning_rich = false;
410 // we'll remove characters from the front of fmt as we
413 if (counter++ > max_passes) {
414 LYXERR0("Recursion limit reached while parsing `"
419 char_type thischar = fmt[0];
420 if (thischar == '%') {
421 // beginning or end of key
424 scanning_key = false;
425 // so we replace the key with its value, which may be empty
429 buf.params().documentClass().getCiteMacro(key);
430 fmt = val + fmt.substr(1);
432 } else if (key[0] == '_') {
433 // a translatable bit
435 buf.params().documentClass().getCiteMacro(key);
436 docstring const trans =
437 translateIfPossible(from_utf8(val), buf.params().language->code());
440 docstring const val = getValueForKey(key, xref);
449 else if (thischar == '{') {
450 // beginning of option?
452 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
455 if (fmt.size() > 1) {
457 // it is the beginning of an optional format
461 string const newfmt =
462 parseOptions(fmt, optkey, ifpart, elsepart);
463 if (newfmt == fmt) // parse error
466 docstring const val = getValueForKey(optkey, xref);
468 ret += expandFormat(ifpart, xref, counter, buf, richtext);
469 else if (!elsepart.empty())
470 ret += expandFormat(elsepart, xref, counter, buf, richtext);
471 // fmt will have been shortened for us already
475 // beginning of rich text
476 scanning_rich = true;
481 // we are here if '{' was not followed by % or !.
482 // So it's just a character.
485 else if (scanning_rich && thischar == '!'
486 && fmt.size() > 1 && fmt[1] == '}') {
488 scanning_rich = false;
492 else if (scanning_key)
493 key += char(thischar);
494 else if (richtext || !scanning_rich)
496 // else the character is discarded, which will happen only if
497 // richtext == false and we are scanning rich text
501 LYXERR0("Never found end of key in `" << format << "'!");
505 LYXERR0("Never found end of rich text in `" << format << "'!");
512 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
513 Buffer const & buf, bool richtext) const
519 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
524 DocumentClass const & dc = buf.params().documentClass();
525 string const & format = dc.getCiteFormat(to_utf8(entry_type_));
527 info_ = expandFormat(format, xref, counter, buf, richtext);
530 info_ = convertLaTeXCommands(info_);
535 docstring const & BibTeXInfo::operator[](docstring const & field) const
537 BibTeXInfo::const_iterator it = find(field);
540 static docstring const empty_value = docstring();
545 docstring const & BibTeXInfo::operator[](string const & field) const
547 return operator[](from_ascii(field));
551 docstring BibTeXInfo::getValueForKey(string const & key,
552 BibTeXInfo const * const xref) const
554 docstring const ret = operator[](key);
555 if (!ret.empty() || !xref)
561 //////////////////////////////////////////////////////////////////////
565 //////////////////////////////////////////////////////////////////////
568 // A functor for use with sort, leading to case insensitive sorting
569 class compareNoCase: public binary_function<docstring, docstring, bool>
572 bool operator()(docstring const & s1, docstring const & s2) const {
573 return compare_no_case(s1, s2) < 0;
579 vector<docstring> const BiblioInfo::getKeys() const
581 vector<docstring> bibkeys;
582 BiblioInfo::const_iterator it = begin();
583 for (; it != end(); ++it)
584 bibkeys.push_back(it->first);
585 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
590 vector<docstring> const BiblioInfo::getFields() const
592 vector<docstring> bibfields;
593 set<docstring>::const_iterator it = field_names_.begin();
594 set<docstring>::const_iterator end = field_names_.end();
595 for (; it != end; ++it)
596 bibfields.push_back(*it);
597 sort(bibfields.begin(), bibfields.end());
602 vector<docstring> const BiblioInfo::getEntries() const
604 vector<docstring> bibentries;
605 set<docstring>::const_iterator it = entry_types_.begin();
606 set<docstring>::const_iterator end = entry_types_.end();
607 for (; it != end; ++it)
608 bibentries.push_back(*it);
609 sort(bibentries.begin(), bibentries.end());
614 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
616 BiblioInfo::const_iterator it = find(key);
619 BibTeXInfo const & data = it->second;
620 return data.getAbbreviatedAuthor();
624 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
626 BiblioInfo::const_iterator it = find(key);
629 BibTeXInfo const & data = it->second;
630 return data.citeNumber();
634 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
636 BiblioInfo::const_iterator it = find(key);
639 BibTeXInfo const & data = it->second;
640 docstring year = data.getYear();
642 // let's try the crossref
643 docstring const xref = data.getXRef();
645 return _("No year"); // no luck
646 BiblioInfo::const_iterator const xrefit = find(xref);
648 return _("No year"); // no luck again
649 BibTeXInfo const & xref_data = xrefit->second;
650 year = xref_data.getYear();
652 if (use_modifier && data.modifier() != 0)
653 year += data.modifier();
658 docstring const BiblioInfo::getInfo(docstring const & key,
659 Buffer const & buf, bool richtext) const
661 BiblioInfo::const_iterator it = find(key);
664 BibTeXInfo const & data = it->second;
665 BibTeXInfo const * xrefptr = 0;
666 docstring const xref = data.getXRef();
668 BiblioInfo::const_iterator const xrefit = find(xref);
670 xrefptr = &(xrefit->second);
672 return data.getInfo(xrefptr, buf, richtext);
676 bool BiblioInfo::isBibtex(docstring const & key) const
678 BiblioInfo::const_iterator it = find(key);
681 return it->second.isBibTeX();
686 vector<docstring> const BiblioInfo::getCiteStrings(
687 docstring const & key, Buffer const & buf) const
689 CiteEngine const engine = buf.params().citeEngine();
690 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
691 return getNumericalStrings(key, buf);
693 return getAuthorYearStrings(key, buf);
697 vector<docstring> const BiblioInfo::getNumericalStrings(
698 docstring const & key, Buffer const & buf) const
701 return vector<docstring>();
703 docstring const author = getAbbreviatedAuthor(key);
704 docstring const year = getYear(key);
705 if (author.empty() || year.empty())
706 return vector<docstring>();
708 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
710 vector<docstring> vec(styles.size());
711 for (size_t i = 0; i != vec.size(); ++i) {
717 str = from_ascii("[#ID]");
721 str = _("Add to bibliography only.");
725 str = author + " [#ID]";
729 str = author + " #ID";
733 str = from_ascii("#ID");
745 str = '(' + year + ')';
756 vector<docstring> const BiblioInfo::getAuthorYearStrings(
757 docstring const & key, Buffer const & buf) const
760 return vector<docstring>();
762 docstring const author = getAbbreviatedAuthor(key);
763 docstring const year = getYear(key);
764 if (author.empty() || year.empty())
765 return vector<docstring>();
767 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
769 vector<docstring> vec(styles.size());
770 for (size_t i = 0; i != vec.size(); ++i) {
775 // jurabib only: Author/Annotator
776 // (i.e. the "before" field, 2nd opt arg)
777 str = author + "/<" + _("before") + '>';
781 str = _("Add to bibliography only.");
785 str = author + " (" + year + ')';
789 str = '(' + author + ", " + year + ')';
793 str = author + ' ' + year ;
797 str = author + ", " + year ;
809 str = '(' + year + ')';
818 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
820 bimap_.insert(info.begin(), info.end());
825 // used in xhtml to sort a list of BibTeXInfo objects
826 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
828 docstring const lauth = lhs->getAbbreviatedAuthor();
829 docstring const rauth = rhs->getAbbreviatedAuthor();
830 docstring const lyear = lhs->getYear();
831 docstring const ryear = rhs->getYear();
832 docstring const ltitl = lhs->operator[]("title");
833 docstring const rtitl = rhs->operator[]("title");
834 return (lauth < rauth)
835 || (lauth == rauth && lyear < ryear)
836 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
841 void BiblioInfo::collectCitedEntries(Buffer const & buf)
843 cited_entries_.clear();
844 // We are going to collect all the citation keys used in the document,
845 // getting them from the TOC.
846 // FIXME We may want to collect these differently, in the first case,
847 // so that we might have them in order of appearance.
848 set<docstring> citekeys;
849 Toc const & toc = buf.tocBackend().toc("citation");
850 Toc::const_iterator it = toc.begin();
851 Toc::const_iterator const en = toc.end();
852 for (; it != en; ++it) {
853 if (it->str().empty())
855 vector<docstring> const keys = getVectorFromString(it->str());
856 citekeys.insert(keys.begin(), keys.end());
858 if (citekeys.empty())
861 // We have a set of the keys used in this document.
862 // We will now convert it to a list of the BibTeXInfo objects used in
864 vector<BibTeXInfo const *> bi;
865 set<docstring>::const_iterator cit = citekeys.begin();
866 set<docstring>::const_iterator const cen = citekeys.end();
867 for (; cit != cen; ++cit) {
868 BiblioInfo::const_iterator const bt = find(*cit);
869 if (bt == end() || !bt->second.isBibTeX())
871 bi.push_back(&(bt->second));
874 sort(bi.begin(), bi.end(), lSorter);
876 // Now we can write the sorted keys
877 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
878 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
879 for (; bit != ben; ++bit)
880 cited_entries_.push_back((*bit)->key());
884 void BiblioInfo::makeCitationLabels(Buffer const & buf)
886 collectCitedEntries(buf);
887 CiteEngine const engine = buf.params().citeEngine();
889 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
893 // used to remember the last one we saw
894 // we'll be comparing entries to see if we need to add
895 // modifiers, like "1984a"
896 map<docstring, BibTeXInfo>::iterator last;
898 vector<docstring>::const_iterator it = cited_entries_.begin();
899 vector<docstring>::const_iterator const en = cited_entries_.end();
900 for (; it != en; ++it) {
901 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
902 // this shouldn't happen, but...
903 if (biit == bimap_.end())
904 // ...fail gracefully, anyway.
906 BibTeXInfo & entry = biit->second;
908 docstring const num = convert<docstring>(++keynumber);
909 entry.setCiteNumber(num);
911 if (it != cited_entries_.begin()
912 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
913 // we access the year via getYear() so as to get it from the xref,
914 // if we need to do so
915 && getYear(entry.key()) == getYear(last->second.key())) {
917 // so the last one should have been 'a'
918 last->second.setModifier('a');
920 } else if (modifier == 'z')
927 entry.setModifier(modifier);
928 // remember the last one
935 //////////////////////////////////////////////////////////////////////
939 //////////////////////////////////////////////////////////////////////
944 char const * const citeCommands[] = {
945 "cite", "citet", "citep", "citealt", "citealp",
946 "citeauthor", "citeyear", "citeyearpar", "nocite" };
948 unsigned int const nCiteCommands =
949 sizeof(citeCommands) / sizeof(char *);
951 CiteStyle const citeStylesArray[] = {
952 CITE, CITET, CITEP, CITEALT, CITEALP,
953 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
955 unsigned int const nCiteStyles =
956 sizeof(citeStylesArray) / sizeof(CiteStyle);
958 CiteStyle const citeStylesFull[] = {
959 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
961 unsigned int const nCiteStylesFull =
962 sizeof(citeStylesFull) / sizeof(CiteStyle);
964 CiteStyle const citeStylesUCase[] = {
965 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
967 unsigned int const nCiteStylesUCase =
968 sizeof(citeStylesUCase) / sizeof(CiteStyle);
973 CitationStyle citationStyleFromString(string const & command)
979 string cmd = command;
981 s.forceUpperCase = true;
985 size_t const n = cmd.size() - 1;
986 if (cmd != "cite" && cmd[n] == '*') {
988 cmd = cmd.substr(0, n);
991 char const * const * const last = citeCommands + nCiteCommands;
992 char const * const * const ptr = find(citeCommands, last, cmd);
995 size_t idx = ptr - citeCommands;
996 s.style = citeStylesArray[idx];
1002 string citationStyleToString(const CitationStyle & s)
1004 string cite = citeCommands[s.style];
1006 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
1007 if (std::find(citeStylesFull, last, s.style) != last)
1011 if (s.forceUpperCase) {
1012 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
1013 if (std::find(citeStylesUCase, last, s.style) != last)
1020 vector<CiteStyle> citeStyles(CiteEngine engine)
1022 unsigned int nStyles = 0;
1023 unsigned int start = 0;
1030 case ENGINE_NATBIB_AUTHORYEAR:
1031 case ENGINE_NATBIB_NUMERICAL:
1032 nStyles = nCiteStyles - 1;
1035 case ENGINE_JURABIB:
1036 nStyles = nCiteStyles;
1041 vector<CiteStyle> styles(nStyles);
1044 for (; i != styles.size(); ++i, ++j)
1045 styles[i] = citeStylesArray[j];