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"
21 #include "Paragraph.h"
22 #include "TocBackend.h"
24 #include "insets/Inset.h"
25 #include "insets/InsetBibitem.h"
26 #include "insets/InsetBibtex.h"
27 #include "insets/InsetInclude.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
34 #include "support/lstrings.h"
35 #include "support/textutils.h"
37 #include "boost/regex.hpp"
42 using namespace lyx::support;
49 // gets the "family name" from an author-type string
50 docstring familyName(docstring const & name)
55 // first we look for a comma, and take the last name to be everything
56 // preceding the right-most one, so that we also get the "jr" part.
57 docstring::size_type idx = name.rfind(',');
58 if (idx != docstring::npos)
59 return ltrim(name.substr(0, idx));
61 // OK, so now we want to look for the last name. We're going to
62 // include the "von" part. This isn't perfect.
63 // Split on spaces, to get various tokens.
64 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
65 // If we only get two, assume the last one is the last name
66 if (pieces.size() <= 2)
69 // Now we look for the first token that begins with a lower case letter.
70 vector<docstring>::const_iterator it = pieces.begin();
71 vector<docstring>::const_iterator en = pieces.end();
72 for (; it != en; ++it) {
73 if ((*it).size() == 0)
75 char_type const c = (*it)[0];
80 if (it == en) // we never found a "von"
83 // reconstruct what we need to return
86 for (; it != en; ++it) {
96 // converts a string containing LaTeX commands into unicode
98 docstring convertLaTeXCommands(docstring const & str)
103 bool scanning_cmd = false;
104 bool scanning_math = false;
105 bool escaped = false; // used to catch \$, etc.
107 char_type const ch = val[0];
109 // if we're scanning math, we output everything until we
110 // find an unescaped $, at which point we break out.
117 scanning_math = false;
123 // if we're scanning a command name, then we just
124 // discard characters until we hit something that
127 if (isAlphaASCII(ch)) {
132 // so we're done with this command.
133 // now we fall through and check this character.
134 scanning_cmd = false;
137 // was the last character a \? If so, then this is something like:
138 // \\ or \$, so we'll just output it. That's probably not always right...
140 // exception: output \, as THIN SPACE
142 ret.push_back(0x2009);
153 scanning_math = true;
157 // we just ignore braces
158 if (ch == '{' || ch == '}') {
163 // we're going to check things that look like commands, so if
164 // this doesn't, just output it.
171 // ok, could be a command of some sort
172 // let's see if it corresponds to some unicode
173 // unicodesymbols has things in the form: \"{u},
174 // whereas we may see things like: \"u. So we'll
175 // look for that and change it, if necessary.
176 static boost::regex const reg("^\\\\\\W\\w");
177 if (boost::regex_search(to_utf8(val), reg)) {
178 val.insert(3, from_ascii("}"));
179 val.insert(2, from_ascii("{"));
182 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem,
183 Encodings::TEXT_CMD);
184 if (!cnvtd.empty()) {
185 // it did, so we'll take that bit and proceed with what's left
190 // it's a command of some sort
201 //////////////////////////////////////////////////////////////////////
205 //////////////////////////////////////////////////////////////////////
207 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
208 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_(),
213 docstring const BibTeXInfo::getAbbreviatedAuthor() const
216 docstring const opt = label();
221 docstring const remainder = trim(split(opt, authors, '('));
222 if (remainder.empty())
223 // in this case, we didn't find a "(",
224 // so we don't have author (year)
229 docstring author = convertLaTeXCommands(operator[]("author"));
230 if (author.empty()) {
231 author = convertLaTeXCommands(operator[]("editor"));
236 // FIXME Move this to a separate routine that can
237 // be called from elsewhere.
239 // OK, we've got some names. Let's format them.
240 // Try to split the author list on " and "
241 vector<docstring> const authors =
242 getVectorFromString(author, from_ascii(" and "));
244 if (authors.size() == 2)
245 return bformat(_("%1$s and %2$s"),
246 familyName(authors[0]), familyName(authors[1]));
248 if (authors.size() > 2)
249 return bformat(_("%1$s et al."), familyName(authors[0]));
251 return familyName(authors[0]);
255 docstring const BibTeXInfo::getYear() const
258 return operator[]("year");
260 docstring const opt = label();
265 docstring tmp = split(opt, authors, '(');
267 // we don't have author (year)
270 tmp = split(tmp, year, ')');
275 docstring const BibTeXInfo::getXRef() const
279 return operator[]("crossref");
284 string parseOptions(string const & format, string & optkey,
285 string & ifpart, string & elsepart);
287 /// Calls parseOptions to deal with an embedded option, such as:
288 /// {%number%[[, no.~%number%]]}
289 /// which must appear at the start of format. ifelsepart gets the
290 /// whole of the option, and we return what's left after the option.
291 /// we return format if there is an error.
292 string parseEmbeddedOption(string const & format, string & ifelsepart)
294 LASSERT(format[0] == '{' && format[1] == '%', return format);
298 string const rest = parseOptions(format, optkey, ifpart, elsepart);
299 if (format == rest) { // parse error
300 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
303 LASSERT(rest.size() <= format.size(), /* */);
304 ifelsepart = format.substr(0, format.size() - rest.size());
309 // Gets a "clause" from a format string, where the clause is
310 // delimited by '[[' and ']]'. Returns what is left after the
311 // clause is removed, and returns format if there is an error.
312 string getClause(string const & format, string & clause)
317 // we'll remove characters from the front of fmt as we
320 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
325 // check for an embedded option
326 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
328 string const rest = parseEmbeddedOption(fmt, part);
330 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
335 } else { // it's just a normal character
344 /// parse an options string, which must appear at the start of the
345 /// format parameter. puts the parsed bits in optkey, ifpart, and
346 /// elsepart and returns what's left after the option is removed.
347 /// if there's an error, it returns format itself.
348 string parseOptions(string const & format, string & optkey,
349 string & ifpart, string & elsepart)
351 LASSERT(format[0] == '{' && format[1] == '%', return format);
353 string fmt = format.substr(2);
354 size_t pos = fmt.find('%'); // end of key
355 if (pos == string::npos) {
356 LYXERR0("Error parsing `" << format <<"'. Can't find end of key.");
359 optkey = fmt.substr(0,pos);
360 fmt = fmt.substr(pos + 1);
361 // [[format]] should be next
362 if (fmt[0] != '[' || fmt[1] != '[') {
363 LYXERR0("Error parsing `" << format <<"'. Can't find '[[' after key.");
368 fmt = getClause(curfmt, ifpart);
370 LYXERR0("Error parsing `" << format <<"'. Couldn't get if clause.");
374 if (fmt[0] == '}') // we're done, no else clause
375 return fmt.substr(1);
377 // else part should follow
378 if (fmt[0] != '[' || fmt[1] != '[') {
379 LYXERR0("Error parsing `" << format <<"'. Can't find else clause.");
384 fmt = getClause(curfmt, elsepart);
386 if (fmt == curfmt || fmt[0] != '}') {
387 LYXERR0("Error parsing `" << format <<"'. Can't find end of option.");
390 return fmt.substr(1);
396 docstring BibTeXInfo::expandFormat(string const & format,
397 BibTeXInfo const * const xref, bool richtext) const
402 bool scanning_key = false;
403 bool scanning_rich = false;
406 // we'll remove characters from the front of fmt as we
409 char_type thischar = fmt[0];
410 if (thischar == '%') {
411 // beginning or end of key
414 scanning_key = false;
415 // so we replace the key with its value, which may be empty
416 docstring const val = getValueForKey(key, xref);
424 else if (thischar == '{') {
425 // beginning of option?
427 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
430 if (fmt.size() > 1) {
432 // it is the beginning of an optional format
436 string const newfmt =
437 parseOptions(fmt, optkey, ifpart, elsepart);
438 if (newfmt == fmt) // parse error
441 docstring const val = getValueForKey(optkey, xref);
443 ret += expandFormat(ifpart, xref, richtext);
444 else if (!elsepart.empty())
445 ret += expandFormat(elsepart, xref, richtext);
446 // fmt will have been shortened for us already
450 // beginning of rich text
451 scanning_rich = true;
456 // we are here if the '{' was at the end of the format. hmm.
459 else if (scanning_rich && thischar == '!'
460 && fmt.size() > 1 && fmt[1] == '}') {
462 scanning_rich = false;
466 else if (scanning_key)
468 else if (richtext || !scanning_rich)
470 // else the character is discarded, which will happen only if
471 // richtext == false and we are scanning rich text
475 LYXERR0("Never found end of key in `" << format << "'!");
479 LYXERR0("Never found end of rich text in `" << format << "'!");
488 // FIXME These would be better read from a file, so that they
489 // could be customized.
491 static string articleFormat = "%author%, \"%title%\", {!<i>!}%journal%{!</i>!} {%volume%[[ %volume%{%number%[[, %number%]]}]]} (%year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}";
493 static string bookFormat = "{%author%[[%author%]][[%editor%, ed.]]}, {!<i>!}%title%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%).{%note%[[ %note%]]}";
495 static string inSomething = "%author%, \"%title%\", in{%editor%[[ %editor%, ed.,]]} {!<i>!}%booktitle%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}";
497 static string thesis = "%author%, %title% ({%address%[[%address%: ]]}%school%, %year%).{%note%[[ %note%]]}";
499 static string defaultFormat = "{%author%[[%author%, ]][[{%editor%[[%editor%, ed., ]]}]]}\"%title%\"{%journal%[[, {!<i>!}%journal%{!</i>!}]][[{%publisher%[[, %publisher%]][[{%institution%[[, %institution%]]}]]}]]}{%year%[[ (%year%)]]}{%pages%[[, %pages%]]}.";
503 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
510 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
515 if (entry_type_ == "article")
516 info_ = expandFormat(articleFormat, xref, richtext);
517 else if (entry_type_ == "book")
518 info_ = expandFormat(bookFormat, xref, richtext);
519 else if (entry_type_.substr(0,2) == "in")
520 info_ = expandFormat(inSomething, xref, richtext);
521 else if (entry_type_ == "phdthesis" || entry_type_ == "mastersthesis")
522 info_ = expandFormat(thesis, xref, richtext);
524 info_ = expandFormat(defaultFormat, xref, richtext);
527 info_ = convertLaTeXCommands(info_);
532 docstring const & BibTeXInfo::operator[](docstring const & field) const
534 BibTeXInfo::const_iterator it = find(field);
537 static docstring const empty_value = docstring();
542 docstring const & BibTeXInfo::operator[](string const & field) const
544 return operator[](from_ascii(field));
548 docstring BibTeXInfo::getValueForKey(string const & key,
549 BibTeXInfo const * const xref) const
551 docstring const ret = operator[](key);
552 if (!ret.empty() || !xref)
558 //////////////////////////////////////////////////////////////////////
562 //////////////////////////////////////////////////////////////////////
565 // A functor for use with sort, leading to case insensitive sorting
566 class compareNoCase: public binary_function<docstring, docstring, bool>
569 bool operator()(docstring const & s1, docstring const & s2) const {
570 return compare_no_case(s1, s2) < 0;
576 vector<docstring> const BiblioInfo::getKeys() const
578 vector<docstring> bibkeys;
579 BiblioInfo::const_iterator it = begin();
580 for (; it != end(); ++it)
581 bibkeys.push_back(it->first);
582 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
587 vector<docstring> const BiblioInfo::getFields() const
589 vector<docstring> bibfields;
590 set<docstring>::const_iterator it = field_names_.begin();
591 set<docstring>::const_iterator end = field_names_.end();
592 for (; it != end; ++it)
593 bibfields.push_back(*it);
594 sort(bibfields.begin(), bibfields.end());
599 vector<docstring> const BiblioInfo::getEntries() const
601 vector<docstring> bibentries;
602 set<docstring>::const_iterator it = entry_types_.begin();
603 set<docstring>::const_iterator end = entry_types_.end();
604 for (; it != end; ++it)
605 bibentries.push_back(*it);
606 sort(bibentries.begin(), bibentries.end());
611 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
613 BiblioInfo::const_iterator it = find(key);
616 BibTeXInfo const & data = it->second;
617 return data.getAbbreviatedAuthor();
621 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
623 BiblioInfo::const_iterator it = find(key);
626 BibTeXInfo const & data = it->second;
627 return data.citeNumber();
631 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
633 BiblioInfo::const_iterator it = find(key);
636 BibTeXInfo const & data = it->second;
637 docstring year = data.getYear();
639 // let's try the crossref
640 docstring const xref = data.getXRef();
642 return _("No year"); // no luck
643 BiblioInfo::const_iterator const xrefit = find(xref);
645 return _("No year"); // no luck again
646 BibTeXInfo const & xref_data = xrefit->second;
647 year = xref_data.getYear();
649 if (use_modifier && data.modifier() != 0)
650 year += data.modifier();
655 docstring const BiblioInfo::getInfo(docstring const & key, bool richtext) const
657 BiblioInfo::const_iterator it = find(key);
660 BibTeXInfo const & data = it->second;
661 BibTeXInfo const * xrefptr = 0;
662 docstring const xref = data.getXRef();
664 BiblioInfo::const_iterator const xrefit = find(xref);
666 xrefptr = &(xrefit->second);
668 return data.getInfo(xrefptr, richtext);
672 bool BiblioInfo::isBibtex(docstring const & key) const
674 BiblioInfo::const_iterator it = find(key);
677 return it->second.isBibTeX();
682 vector<docstring> const BiblioInfo::getCiteStrings(
683 docstring const & key, Buffer const & buf) const
685 CiteEngine const engine = buf.params().citeEngine();
686 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
687 return getNumericalStrings(key, buf);
689 return getAuthorYearStrings(key, buf);
693 vector<docstring> const BiblioInfo::getNumericalStrings(
694 docstring const & key, Buffer const & buf) const
697 return vector<docstring>();
699 docstring const author = getAbbreviatedAuthor(key);
700 docstring const year = getYear(key);
701 if (author.empty() || year.empty())
702 return vector<docstring>();
704 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
706 vector<docstring> vec(styles.size());
707 for (size_t i = 0; i != vec.size(); ++i) {
713 str = from_ascii("[#ID]");
717 str = _("Add to bibliography only.");
721 str = author + " [#ID]";
725 str = author + " #ID";
729 str = from_ascii("#ID");
741 str = '(' + year + ')';
752 vector<docstring> const BiblioInfo::getAuthorYearStrings(
753 docstring const & key, Buffer const & buf) const
756 return vector<docstring>();
758 docstring const author = getAbbreviatedAuthor(key);
759 docstring const year = getYear(key);
760 if (author.empty() || year.empty())
761 return vector<docstring>();
763 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
765 vector<docstring> vec(styles.size());
766 for (size_t i = 0; i != vec.size(); ++i) {
771 // jurabib only: Author/Annotator
772 // (i.e. the "before" field, 2nd opt arg)
773 str = author + "/<" + _("before") + '>';
777 str = _("Add to bibliography only.");
781 str = author + " (" + year + ')';
785 str = '(' + author + ", " + year + ')';
789 str = author + ' ' + year ;
793 str = author + ", " + year ;
805 str = '(' + year + ')';
814 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
816 bimap_.insert(info.begin(), info.end());
821 // used in xhtml to sort a list of BibTeXInfo objects
822 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
824 docstring const lauth = lhs->getAbbreviatedAuthor();
825 docstring const rauth = rhs->getAbbreviatedAuthor();
826 docstring const lyear = lhs->getYear();
827 docstring const ryear = rhs->getYear();
828 docstring const ltitl = lhs->operator[]("title");
829 docstring const rtitl = rhs->operator[]("title");
830 return (lauth < rauth)
831 || (lauth == rauth && lyear < ryear)
832 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
837 void BiblioInfo::collectCitedEntries(Buffer const & buf)
839 cited_entries_.clear();
840 // We are going to collect all the citation keys used in the document,
841 // getting them from the TOC.
842 // FIXME We may want to collect these differently, in the first case,
843 // so that we might have them in order of appearance.
844 set<docstring> citekeys;
845 Toc const & toc = buf.tocBackend().toc("citation");
846 Toc::const_iterator it = toc.begin();
847 Toc::const_iterator const en = toc.end();
848 for (; it != en; ++it) {
849 if (it->str().empty())
851 vector<docstring> const keys = getVectorFromString(it->str());
852 citekeys.insert(keys.begin(), keys.end());
854 if (citekeys.empty())
857 // We have a set of the keys used in this document.
858 // We will now convert it to a list of the BibTeXInfo objects used in
860 vector<BibTeXInfo const *> bi;
861 set<docstring>::const_iterator cit = citekeys.begin();
862 set<docstring>::const_iterator const cen = citekeys.end();
863 for (; cit != cen; ++cit) {
864 BiblioInfo::const_iterator const bt = find(*cit);
865 if (bt == end() || !bt->second.isBibTeX())
867 bi.push_back(&(bt->second));
870 sort(bi.begin(), bi.end(), lSorter);
872 // Now we can write the sorted keys
873 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
874 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
875 for (; bit != ben; ++bit)
876 cited_entries_.push_back((*bit)->key());
880 void BiblioInfo::makeCitationLabels(Buffer const & buf)
882 collectCitedEntries(buf);
883 CiteEngine const engine = buf.params().citeEngine();
885 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
889 // used to remember the last one we saw
890 // we'll be comparing entries to see if we need to add
891 // modifiers, like "1984a"
892 map<docstring, BibTeXInfo>::iterator last;
894 vector<docstring>::const_iterator it = cited_entries_.begin();
895 vector<docstring>::const_iterator const en = cited_entries_.end();
896 for (; it != en; ++it) {
897 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
898 // this shouldn't happen, but...
899 if (biit == bimap_.end())
900 // ...fail gracefully, anyway.
902 BibTeXInfo & entry = biit->second;
904 docstring const num = convert<docstring>(++keynumber);
905 entry.setCiteNumber(num);
907 if (it != cited_entries_.begin()
908 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
909 // we access the year via getYear() so as to get it from the xref,
910 // if we need to do so
911 && getYear(entry.key()) == getYear(last->second.key())) {
913 // so the last one should have been 'a'
914 last->second.setModifier('a');
916 } else if (modifier == 'z')
923 entry.setModifier(modifier);
924 // remember the last one
931 //////////////////////////////////////////////////////////////////////
935 //////////////////////////////////////////////////////////////////////
940 char const * const citeCommands[] = {
941 "cite", "citet", "citep", "citealt", "citealp",
942 "citeauthor", "citeyear", "citeyearpar", "nocite" };
944 unsigned int const nCiteCommands =
945 sizeof(citeCommands) / sizeof(char *);
947 CiteStyle const citeStylesArray[] = {
948 CITE, CITET, CITEP, CITEALT, CITEALP,
949 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
951 unsigned int const nCiteStyles =
952 sizeof(citeStylesArray) / sizeof(CiteStyle);
954 CiteStyle const citeStylesFull[] = {
955 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
957 unsigned int const nCiteStylesFull =
958 sizeof(citeStylesFull) / sizeof(CiteStyle);
960 CiteStyle const citeStylesUCase[] = {
961 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
963 unsigned int const nCiteStylesUCase =
964 sizeof(citeStylesUCase) / sizeof(CiteStyle);
969 CitationStyle citationStyleFromString(string const & command)
975 string cmd = command;
977 s.forceUpperCase = true;
981 size_t const n = cmd.size() - 1;
982 if (cmd != "cite" && cmd[n] == '*') {
984 cmd = cmd.substr(0, n);
987 char const * const * const last = citeCommands + nCiteCommands;
988 char const * const * const ptr = find(citeCommands, last, cmd);
991 size_t idx = ptr - citeCommands;
992 s.style = citeStylesArray[idx];
998 string citationStyleToString(const CitationStyle & s)
1000 string cite = citeCommands[s.style];
1002 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
1003 if (std::find(citeStylesFull, last, s.style) != last)
1007 if (s.forceUpperCase) {
1008 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
1009 if (std::find(citeStylesUCase, last, s.style) != last)
1016 vector<CiteStyle> citeStyles(CiteEngine engine)
1018 unsigned int nStyles = 0;
1019 unsigned int start = 0;
1026 case ENGINE_NATBIB_AUTHORYEAR:
1027 case ENGINE_NATBIB_NUMERICAL:
1028 nStyles = nCiteStyles - 1;
1031 case ENGINE_JURABIB:
1032 nStyles = nCiteStyles;
1037 vector<CiteStyle> styles(nStyles);
1040 for (; i != styles.size(); ++i, ++j)
1041 styles[i] = citeStylesArray[j];