3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
16 #include "BiblioInfo.h"
18 #include "BufferParams.h"
19 #include "buffer_funcs.h"
21 #include "InsetIterator.h"
23 #include "Paragraph.h"
24 #include "TextClass.h"
25 #include "TocBackend.h"
27 #include "support/convert.h"
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/lassert.h"
32 #include "support/lstrings.h"
33 #include "support/regex.h"
34 #include "support/textutils.h"
39 using namespace lyx::support;
46 // gets the "family name" from an author-type string
47 docstring familyName(docstring const & name)
52 // first we look for a comma, and take the last name to be everything
53 // preceding the right-most one, so that we also get the "jr" part.
54 docstring::size_type idx = name.rfind(',');
55 if (idx != docstring::npos)
56 return ltrim(name.substr(0, idx));
58 // OK, so now we want to look for the last name. We're going to
59 // include the "von" part. This isn't perfect.
60 // Split on spaces, to get various tokens.
61 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
62 // If we only get two, assume the last one is the last name
63 if (pieces.size() <= 2)
66 // Now we look for the first token that begins with a lower case letter.
67 vector<docstring>::const_iterator it = pieces.begin();
68 vector<docstring>::const_iterator en = pieces.end();
69 for (; it != en; ++it) {
72 char_type const c = (*it)[0];
77 if (it == en) // we never found a "von"
80 // reconstruct what we need to return
83 for (; it != en; ++it) {
94 // converts a string containing LaTeX commands into unicode
96 docstring convertLaTeXCommands(docstring const & str)
101 bool scanning_cmd = false;
102 bool scanning_math = false;
103 bool escaped = false; // used to catch \$, etc.
104 while (!val.empty()) {
105 char_type const ch = val[0];
107 // if we're scanning math, we output everything until we
108 // find an unescaped $, at which point we break out.
115 scanning_math = false;
121 // if we're scanning a command name, then we just
122 // discard characters until we hit something that
125 if (isAlphaASCII(ch)) {
130 // so we're done with this command.
131 // now we fall through and check this character.
132 scanning_cmd = false;
135 // was the last character a \? If so, then this is something like:
136 // \\ or \$, so we'll just output it. That's probably not always right...
138 // exception: output \, as THIN SPACE
140 ret.push_back(0x2009);
151 scanning_math = true;
155 // we just ignore braces
156 if (ch == '{' || ch == '}') {
161 // we're going to check things that look like commands, so if
162 // this doesn't, just output it.
169 // ok, could be a command of some sort
170 // let's see if it corresponds to some unicode
171 // unicodesymbols has things in the form: \"{u},
172 // whereas we may see things like: \"u. So we'll
173 // look for that and change it, if necessary.
174 // FIXME: This is a sort of mini-tex2lyx.
175 // Use the real tex2lyx instead!
176 static lyx::regex const reg("^\\\\\\W\\w");
177 if (lyx::regex_search(to_utf8(val), reg)) {
178 val.insert(3, from_ascii("}"));
179 val.insert(2, from_ascii("{"));
183 docstring const cnvtd = Encodings::fromLaTeXCommand(val,
184 Encodings::TEXT_CMD, termination, rem);
185 if (!cnvtd.empty()) {
186 // it did, so we'll take that bit and proceed with what's left
191 // it's a command of some sort
200 // Escape '<' and '>' and remove richtext markers (e.g. {!this is richtext!}) from a string.
201 docstring processRichtext(docstring const & str, bool richtext)
206 bool scanning_rich = false;
207 while (!val.empty()) {
208 char_type const ch = val[0];
209 if (ch == '{' && val.size() > 1 && val[1] == '!') {
210 // beginning of rich text
211 scanning_rich = true;
215 if (scanning_rich && ch == '!' && val.size() > 1 && val[1] == '}') {
217 scanning_rich = false;
225 // we need to escape '<' and '>'
233 } else if (!scanning_rich /* && !richtext */)
235 // else the character is discarded, which will happen only if
236 // richtext == false and we are scanning rich text
245 //////////////////////////////////////////////////////////////////////
249 //////////////////////////////////////////////////////////////////////
251 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
252 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_(),
257 docstring const BibTeXInfo::getAbbreviatedAuthor(bool jurabib_style) const
260 docstring const opt = label();
265 docstring const remainder = trim(split(opt, authors, '('));
266 if (remainder.empty())
267 // in this case, we didn't find a "(",
268 // so we don't have author (year)
273 docstring author = convertLaTeXCommands(operator[]("author"));
274 if (author.empty()) {
275 author = convertLaTeXCommands(operator[]("editor"));
280 // FIXME Move this to a separate routine that can
281 // be called from elsewhere.
283 // OK, we've got some names. Let's format them.
284 // Try to split the author list on " and "
285 vector<docstring> const authors =
286 getVectorFromString(author, from_ascii(" and "));
288 if (jurabib_style && (authors.size() == 2 || authors.size() == 3)) {
289 docstring shortauthor = familyName(authors[0])
290 + "/" + familyName(authors[1]);
291 if (authors.size() == 3)
292 shortauthor += "/" + familyName(authors[2]);
296 if (authors.size() == 2 && authors[1] != "others")
297 return bformat(from_ascii("%1$s and %2$s"),
298 familyName(authors[0]), familyName(authors[1]));
300 if (authors.size() >= 2)
301 return bformat(from_ascii("%1$s et al."),
302 familyName(authors[0]));
304 return familyName(authors[0]);
308 docstring const BibTeXInfo::getAbbreviatedAuthor(Buffer const & buf, bool jurabib_style) const
310 docstring const author = getAbbreviatedAuthor(jurabib_style);
313 vector<docstring> const authors = getVectorFromString(author, from_ascii(" and "));
314 if (authors.size() == 2)
315 return bformat(buf.B_("%1$s and %2$s"), authors[0], authors[1]);
316 docstring::size_type const idx = author.rfind(from_ascii(" et al."));
317 if (idx != docstring::npos)
318 return bformat(buf.B_("%1$s et al."), author.substr(0, idx));
323 docstring const BibTeXInfo::getYear() const
326 return operator[]("year");
328 docstring const opt = label();
333 docstring tmp = split(opt, authors, '(');
335 // we don't have author (year)
338 tmp = split(tmp, year, ')');
343 docstring const BibTeXInfo::getXRef() const
347 return operator[]("crossref");
353 string parseOptions(string const & format, string & optkey,
354 string & ifpart, string & elsepart);
356 // Calls parseOptions to deal with an embedded option, such as:
357 // {%number%[[, no.~%number%]]}
358 // which must appear at the start of format. ifelsepart gets the
359 // whole of the option, and we return what's left after the option.
360 // we return format if there is an error.
361 string parseEmbeddedOption(string const & format, string & ifelsepart)
363 LASSERT(format[0] == '{' && format[1] == '%', return format);
367 string const rest = parseOptions(format, optkey, ifpart, elsepart);
368 if (format == rest) { // parse error
369 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
372 LASSERT(rest.size() <= format.size(), /* */);
373 ifelsepart = format.substr(0, format.size() - rest.size());
378 // Gets a "clause" from a format string, where the clause is
379 // delimited by '[[' and ']]'. Returns what is left after the
380 // clause is removed, and returns format if there is an error.
381 string getClause(string const & format, string & clause)
386 // we'll remove characters from the front of fmt as we
388 while (!fmt.empty()) {
389 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
394 // check for an embedded option
395 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
397 string const rest = parseEmbeddedOption(fmt, part);
399 LYXERR0("ERROR! Couldn't parse embedded option in `" << format <<"'.");
404 } else { // it's just a normal character
413 // parse an options string, which must appear at the start of the
414 // format parameter. puts the parsed bits in optkey, ifpart, and
415 // elsepart and returns what's left after the option is removed.
416 // if there's an error, it returns format itself.
417 string parseOptions(string const & format, string & optkey,
418 string & ifpart, string & elsepart)
420 LASSERT(format[0] == '{' && format[1] == '%', return format);
422 string fmt = format.substr(2);
423 size_t pos = fmt.find('%'); // end of key
424 if (pos == string::npos) {
425 LYXERR0("Error parsing `" << format <<"'. Can't find end of key.");
428 optkey = fmt.substr(0,pos);
429 fmt = fmt.substr(pos + 1);
430 // [[format]] should be next
431 if (fmt[0] != '[' || fmt[1] != '[') {
432 LYXERR0("Error parsing `" << format <<"'. Can't find '[[' after key.");
437 fmt = getClause(curfmt, ifpart);
439 LYXERR0("Error parsing `" << format <<"'. Couldn't get if clause.");
443 if (fmt[0] == '}') // we're done, no else clause
444 return fmt.substr(1);
446 // else part should follow
447 if (fmt[0] != '[' || fmt[1] != '[') {
448 LYXERR0("Error parsing `" << format <<"'. Can't find else clause.");
453 fmt = getClause(curfmt, elsepart);
455 if (fmt == curfmt || fmt[0] != '}') {
456 LYXERR0("Error parsing `" << format <<"'. Can't find end of option.");
459 return fmt.substr(1);
466 docstring BibTeXInfo::expandFormat(string const & format,
467 BibTeXInfo const * const xref, int & counter, Buffer const & buf,
468 docstring before, docstring after, docstring dialog, bool next) const
470 // incorrect use of macros could put us in an infinite loop
471 static int max_passes = 5000;
472 docstring ret; // return value
474 bool scanning_key = false;
475 bool scanning_rich = false;
477 CiteEngineType const engine_type = buf.params().citeEngineType();
479 // we'll remove characters from the front of fmt as we
481 while (!fmt.empty()) {
482 if (counter++ > max_passes) {
483 LYXERR0("Recursion limit reached while parsing `"
488 char_type thischar = fmt[0];
489 if (thischar == '%') {
490 // beginning or end of key
493 scanning_key = false;
494 // so we replace the key with its value, which may be empty
498 buf.params().documentClass().getCiteMacro(engine_type, key);
499 fmt = val + fmt.substr(1);
501 } else if (key[0] == '_') {
502 // a translatable bit
504 buf.params().documentClass().getCiteMacro(engine_type, key);
505 docstring const trans =
506 translateIfPossible(from_utf8(val), buf.params().language->code());
509 docstring const val =
510 getValueForKey(key, buf, before, after, dialog, xref);
512 ret += from_ascii("{!<span class=\"bib-" + key + "\">!}");
515 ret += from_ascii("{!</span>!}");
523 else if (thischar == '{') {
524 // beginning of option?
526 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
529 if (fmt.size() > 1) {
531 // it is the beginning of an optional format
535 string const newfmt =
536 parseOptions(fmt, optkey, ifpart, elsepart);
537 if (newfmt == fmt) // parse error
540 docstring const val =
541 getValueForKey(optkey, buf, before, after, dialog, xref);
542 if (optkey == "next" && next)
543 ret += from_utf8(ifpart); // without expansion
544 else if (!val.empty())
545 ret += expandFormat(ifpart, xref, counter, buf,
546 before, after, dialog, next);
547 else if (!elsepart.empty())
548 ret += expandFormat(elsepart, xref, counter, buf,
549 before, after, dialog, next);
550 // fmt will have been shortened for us already
554 // beginning of rich text
555 scanning_rich = true;
557 ret += from_ascii("{!");
561 // we are here if '{' was not followed by % or !.
562 // So it's just a character.
565 else if (scanning_rich && thischar == '!'
566 && fmt.size() > 1 && fmt[1] == '}') {
568 scanning_rich = false;
570 ret += from_ascii("!}");
573 else if (scanning_key)
574 key += char(thischar);
580 LYXERR0("Never found end of key in `" << format << "'!");
584 LYXERR0("Never found end of rich text in `" << format << "'!");
591 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
592 Buffer const & buf, bool richtext) const
594 if (!richtext && !info_.empty())
596 if (richtext && !info_richtext_.empty())
597 return info_richtext_;
600 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
605 CiteEngineType const engine_type = buf.params().citeEngineType();
606 DocumentClass const & dc = buf.params().documentClass();
607 string const & format = dc.getCiteFormat(engine_type, to_utf8(entry_type_));
609 info_ = expandFormat(format, xref, counter, buf,
610 docstring(), docstring(), docstring(), false);
612 if (!info_.empty()) {
613 info_richtext_ = convertLaTeXCommands(processRichtext(info_, true));
614 info_ = convertLaTeXCommands(processRichtext(info_, false));
616 return info_richtext_;
622 docstring const BibTeXInfo::getLabel(BibTeXInfo const * const xref,
623 Buffer const & buf, string const & format, bool richtext,
624 docstring before, docstring after, docstring dialog, bool next) const
629 loclabel = expandFormat(format, xref, counter, buf,
630 before, after, dialog, next);
632 if (!loclabel.empty() && !next) {
633 loclabel = processRichtext(loclabel, richtext);
634 loclabel = convertLaTeXCommands(loclabel);
640 docstring const & BibTeXInfo::operator[](docstring const & field) const
642 BibTeXInfo::const_iterator it = find(field);
645 static docstring const empty_value = docstring();
650 docstring const & BibTeXInfo::operator[](string const & field) const
652 return operator[](from_ascii(field));
656 docstring BibTeXInfo::getValueForKey(string const & key, Buffer const & buf,
657 docstring const & before, docstring const & after, docstring const & dialog,
658 BibTeXInfo const * const xref) const
660 docstring ret = operator[](key);
661 if (ret.empty() && xref)
666 // FIXME: dialog, textbefore and textafter have nothing to do with this
669 else if (key == "entrytype")
671 else if (key == "key")
673 else if (key == "label")
675 else if (key == "abbrvauthor")
676 // Special key to provide abbreviated author names.
677 return getAbbreviatedAuthor(buf, false);
678 else if (key == "shortauthor")
679 // When shortauthor is not defined, jurabib automatically
680 // provides jurabib-style abbreviated author names. We do
682 return getAbbreviatedAuthor(buf, true);
683 else if (key == "shorttitle") {
684 // When shorttitle is not defined, jurabib uses for `article'
685 // and `periodical' entries the form `journal volume [year]'
686 // and for other types of entries it uses the `title' field.
687 if (entry_type_ == "article" || entry_type_ == "periodical")
688 return operator[]("journal") + " " + operator[]("volume")
689 + " [" + operator[]("year") + "]";
691 return operator[]("title");
692 } else if (key == "textbefore")
694 else if (key == "textafter")
696 else if (key == "year")
702 //////////////////////////////////////////////////////////////////////
706 //////////////////////////////////////////////////////////////////////
710 // A functor for use with sort, leading to case insensitive sorting
711 class compareNoCase: public binary_function<docstring, docstring, bool>
714 bool operator()(docstring const & s1, docstring const & s2) const {
715 return compare_no_case(s1, s2) < 0;
722 vector<docstring> const BiblioInfo::getKeys() const
724 vector<docstring> bibkeys;
725 BiblioInfo::const_iterator it = begin();
726 for (; it != end(); ++it)
727 bibkeys.push_back(it->first);
728 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
733 vector<docstring> const BiblioInfo::getFields() const
735 vector<docstring> bibfields;
736 set<docstring>::const_iterator it = field_names_.begin();
737 set<docstring>::const_iterator end = field_names_.end();
738 for (; it != end; ++it)
739 bibfields.push_back(*it);
740 sort(bibfields.begin(), bibfields.end());
745 vector<docstring> const BiblioInfo::getEntries() const
747 vector<docstring> bibentries;
748 set<docstring>::const_iterator it = entry_types_.begin();
749 set<docstring>::const_iterator end = entry_types_.end();
750 for (; it != end; ++it)
751 bibentries.push_back(*it);
752 sort(bibentries.begin(), bibentries.end());
757 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key, Buffer const & buf) const
759 BiblioInfo::const_iterator it = find(key);
762 BibTeXInfo const & data = it->second;
763 return data.getAbbreviatedAuthor(buf, false);
767 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
769 BiblioInfo::const_iterator it = find(key);
772 BibTeXInfo const & data = it->second;
773 return data.citeNumber();
777 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
779 BiblioInfo::const_iterator it = find(key);
782 BibTeXInfo const & data = it->second;
783 docstring year = data.getYear();
785 // let's try the crossref
786 docstring const xref = data.getXRef();
790 BiblioInfo::const_iterator const xrefit = find(xref);
794 BibTeXInfo const & xref_data = xrefit->second;
795 year = xref_data.getYear();
797 if (use_modifier && data.modifier() != 0)
798 year += data.modifier();
803 docstring const BiblioInfo::getYear(docstring const & key, Buffer const & buf, bool use_modifier) const
805 docstring const year = getYear(key, use_modifier);
807 return buf.B_("No year");
812 docstring const BiblioInfo::getInfo(docstring const & key,
813 Buffer const & buf, bool richtext) const
815 BiblioInfo::const_iterator it = find(key);
817 return docstring(_("Bibliography entry not found!"));
818 BibTeXInfo const & data = it->second;
819 BibTeXInfo const * xrefptr = 0;
820 docstring const xref = data.getXRef();
822 BiblioInfo::const_iterator const xrefit = find(xref);
824 xrefptr = &(xrefit->second);
826 return data.getInfo(xrefptr, buf, richtext);
830 docstring const BiblioInfo::getLabel(vector<docstring> const & keys,
831 Buffer const & buf, string const & style, bool richtext,
832 docstring const & before, docstring const & after, docstring const & dialog) const
834 CiteEngineType const engine_type = buf.params().citeEngineType();
835 DocumentClass const & dc = buf.params().documentClass();
836 string const & format = dc.getCiteFormat(engine_type, style, "cite");
837 docstring ret = from_utf8(format);
838 vector<docstring>::const_iterator key = keys.begin();
839 vector<docstring>::const_iterator ken = keys.end();
840 for (; key != ken; ++key) {
841 BiblioInfo::const_iterator it = find(*key);
842 BibTeXInfo empty_data;
843 empty_data.key(*key);
844 BibTeXInfo & data = empty_data;
845 BibTeXInfo const * xrefptr = 0;
848 docstring const xref = data.getXRef();
850 BiblioInfo::const_iterator const xrefit = find(xref);
852 xrefptr = &(xrefit->second);
855 ret = data.getLabel(xrefptr, buf, to_utf8(ret), richtext,
856 before, after, dialog, key+1 != ken);
862 bool BiblioInfo::isBibtex(docstring const & key) const
864 BiblioInfo::const_iterator it = find(key);
867 return it->second.isBibTeX();
871 vector<docstring> const BiblioInfo::getCiteStrings(
872 vector<docstring> const & keys, vector<CitationStyle> const & styles,
873 Buffer const & buf, bool richtext, docstring const & before,
874 docstring const & after, docstring const & dialog) const
877 return vector<docstring>();
880 vector<docstring> vec(styles.size());
881 for (size_t i = 0; i != vec.size(); ++i) {
882 style = styles[i].cmd;
883 vec[i] = getLabel(keys, buf, style, richtext, before, after, dialog);
890 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
892 bimap_.insert(info.begin(), info.end());
893 field_names_.insert(info.field_names_.begin(), info.field_names_.end());
894 entry_types_.insert(info.entry_types_.begin(), info.entry_types_.end());
900 // used in xhtml to sort a list of BibTeXInfo objects
901 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
903 docstring const lauth = lhs->getAbbreviatedAuthor();
904 docstring const rauth = rhs->getAbbreviatedAuthor();
905 docstring const lyear = lhs->getYear();
906 docstring const ryear = rhs->getYear();
907 docstring const ltitl = lhs->operator[]("title");
908 docstring const rtitl = rhs->operator[]("title");
909 return (lauth < rauth)
910 || (lauth == rauth && lyear < ryear)
911 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
917 void BiblioInfo::collectCitedEntries(Buffer const & buf)
919 cited_entries_.clear();
920 // We are going to collect all the citation keys used in the document,
921 // getting them from the TOC.
922 // FIXME We may want to collect these differently, in the first case,
923 // so that we might have them in order of appearance.
924 set<docstring> citekeys;
925 Toc const & toc = buf.tocBackend().toc("citation");
926 Toc::const_iterator it = toc.begin();
927 Toc::const_iterator const en = toc.end();
928 for (; it != en; ++it) {
929 if (it->str().empty())
931 vector<docstring> const keys = getVectorFromString(it->str());
932 citekeys.insert(keys.begin(), keys.end());
934 if (citekeys.empty())
937 // We have a set of the keys used in this document.
938 // We will now convert it to a list of the BibTeXInfo objects used in
940 vector<BibTeXInfo const *> bi;
941 set<docstring>::const_iterator cit = citekeys.begin();
942 set<docstring>::const_iterator const cen = citekeys.end();
943 for (; cit != cen; ++cit) {
944 BiblioInfo::const_iterator const bt = find(*cit);
945 if (bt == end() || !bt->second.isBibTeX())
947 bi.push_back(&(bt->second));
950 sort(bi.begin(), bi.end(), lSorter);
952 // Now we can write the sorted keys
953 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
954 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
955 for (; bit != ben; ++bit)
956 cited_entries_.push_back((*bit)->key());
960 void BiblioInfo::makeCitationLabels(Buffer const & buf)
962 collectCitedEntries(buf);
963 CiteEngineType const engine_type = buf.params().citeEngineType();
964 bool const numbers = (engine_type == ENGINE_TYPE_NUMERICAL);
968 // used to remember the last one we saw
969 // we'll be comparing entries to see if we need to add
970 // modifiers, like "1984a"
971 map<docstring, BibTeXInfo>::iterator last;
973 vector<docstring>::const_iterator it = cited_entries_.begin();
974 vector<docstring>::const_iterator const en = cited_entries_.end();
975 for (; it != en; ++it) {
976 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
977 // this shouldn't happen, but...
978 if (biit == bimap_.end())
979 // ...fail gracefully, anyway.
981 BibTeXInfo & entry = biit->second;
983 docstring const num = convert<docstring>(++keynumber);
984 entry.setCiteNumber(num);
986 if (it != cited_entries_.begin()
987 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
988 // we access the year via getYear() so as to get it from the xref,
989 // if we need to do so
990 && getYear(entry.key()) == getYear(last->second.key())) {
992 // so the last one should have been 'a'
993 last->second.setModifier('a');
995 } else if (modifier == 'z')
1002 entry.setModifier(modifier);
1003 // remember the last one
1010 //////////////////////////////////////////////////////////////////////
1014 //////////////////////////////////////////////////////////////////////
1017 CitationStyle citationStyleFromString(string const & command)
1020 if (command.empty())
1023 string cmd = command;
1024 if (cmd[0] == 'C') {
1025 cs.forceUpperCase = true;
1029 size_t const n = cmd.size() - 1;
1030 if (cmd[n] == '*') {
1031 cs.fullAuthorList = true;
1032 cmd = cmd.substr(0, n);
1040 string citationStyleToString(const CitationStyle & cs)
1042 string cmd = cs.cmd;
1043 if (cs.forceUpperCase)
1045 if (cs.fullAuthorList)