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
198 // these are used in the expandFormat() routine, etc.
200 static string const pp_text = N_("pp.");
201 static string const ed_text = N_("ed.");
202 static string const edby_text = N_("ed. by");
207 //////////////////////////////////////////////////////////////////////
211 //////////////////////////////////////////////////////////////////////
213 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
214 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_(),
219 docstring const BibTeXInfo::getAbbreviatedAuthor() const
222 docstring const opt = label();
227 docstring const remainder = trim(split(opt, authors, '('));
228 if (remainder.empty())
229 // in this case, we didn't find a "(",
230 // so we don't have author (year)
235 docstring author = convertLaTeXCommands(operator[]("author"));
236 if (author.empty()) {
237 author = convertLaTeXCommands(operator[]("editor"));
242 // FIXME Move this to a separate routine that can
243 // be called from elsewhere.
245 // OK, we've got some names. Let's format them.
246 // Try to split the author list on " and "
247 vector<docstring> const authors =
248 getVectorFromString(author, from_ascii(" and "));
250 if (authors.size() == 2)
251 return bformat(_("%1$s and %2$s"),
252 familyName(authors[0]), familyName(authors[1]));
254 if (authors.size() > 2)
255 return bformat(_("%1$s et al."), familyName(authors[0]));
257 return familyName(authors[0]);
261 docstring const BibTeXInfo::getYear() const
264 return operator[]("year");
266 docstring const opt = label();
271 docstring tmp = split(opt, authors, '(');
273 // we don't have author (year)
276 tmp = split(tmp, year, ')');
281 docstring const BibTeXInfo::getXRef() const
285 return operator[]("crossref");
290 string parseOptions(string const & format, string & optkey,
291 string & ifpart, string & elsepart);
293 // Calls parseOptions to deal with an embedded option, such as:
294 // {%number%[[, no.~%number%]]}
295 // which must appear at the start of format. ifelsepart gets the
296 // whole of the option, and we return what's left after the option.
297 // we return format if there is an error.
298 string parseEmbeddedOption(string const & format, string & ifelsepart)
300 LASSERT(format[0] == '{' && format[1] == '%', return format);
304 string const rest = parseOptions(format, optkey, ifpart, elsepart);
305 if (format == rest) { // parse error
306 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
309 LASSERT(rest.size() <= format.size(), /* */);
310 ifelsepart = format.substr(0, format.size() - rest.size());
315 // Gets a "clause" from a format string, where the clause is
316 // delimited by '[[' and ']]'. Returns what is left after the
317 // clause is removed, and returns format if there is an error.
318 string getClause(string const & format, string & clause)
323 // we'll remove characters from the front of fmt as we
326 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
331 // check for an embedded option
332 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
334 string const rest = parseEmbeddedOption(fmt, part);
336 LYXERR0("ERROR! Couldn't parse embedded option in `" << format <<"'.");
341 } else { // it's just a normal character
350 // parse an options string, which must appear at the start of the
351 // format parameter. puts the parsed bits in optkey, ifpart, and
352 // elsepart and returns what's left after the option is removed.
353 // if there's an error, it returns format itself.
354 string parseOptions(string const & format, string & optkey,
355 string & ifpart, string & elsepart)
357 LASSERT(format[0] == '{' && format[1] == '%', return format);
359 string fmt = format.substr(2);
360 size_t pos = fmt.find('%'); // end of key
361 if (pos == string::npos) {
362 LYXERR0("Error parsing `" << format <<"'. Can't find end of key.");
365 optkey = fmt.substr(0,pos);
366 fmt = fmt.substr(pos + 1);
367 // [[format]] should be next
368 if (fmt[0] != '[' || fmt[1] != '[') {
369 LYXERR0("Error parsing `" << format <<"'. Can't find '[[' after key.");
374 fmt = getClause(curfmt, ifpart);
376 LYXERR0("Error parsing `" << format <<"'. Couldn't get if clause.");
380 if (fmt[0] == '}') // we're done, no else clause
381 return fmt.substr(1);
383 // else part should follow
384 if (fmt[0] != '[' || fmt[1] != '[') {
385 LYXERR0("Error parsing `" << format <<"'. Can't find else clause.");
390 fmt = getClause(curfmt, elsepart);
392 if (fmt == curfmt || fmt[0] != '}') {
393 LYXERR0("Error parsing `" << format <<"'. Can't find end of option.");
396 return fmt.substr(1);
402 docstring BibTeXInfo::expandFormat(string const & format,
403 BibTeXInfo const * const xref, bool richtext) const
405 docstring ret; // return value
407 bool scanning_key = false;
408 bool scanning_rich = false;
411 // we'll remove characters from the front of fmt as we
414 char_type thischar = fmt[0];
415 if (thischar == '%') {
416 // beginning or end of key
419 scanning_key = false;
420 // so we replace the key with its value, which may be empty
421 if (key == "pp_text")
423 else if (key == "ed_text")
425 else if(key == "edby_text")
428 docstring const val = getValueForKey(key, xref);
437 else if (thischar == '{') {
438 // beginning of option?
440 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
443 if (fmt.size() > 1) {
445 // it is the beginning of an optional format
449 string const newfmt =
450 parseOptions(fmt, optkey, ifpart, elsepart);
451 if (newfmt == fmt) // parse error
454 docstring const val = getValueForKey(optkey, xref);
456 ret += expandFormat(ifpart, xref, richtext);
457 else if (!elsepart.empty())
458 ret += expandFormat(elsepart, xref, richtext);
459 // fmt will have been shortened for us already
463 // beginning of rich text
464 scanning_rich = true;
469 // we are here if the '{' was at the end of the format. hmm.
472 else if (scanning_rich && thischar == '!'
473 && fmt.size() > 1 && fmt[1] == '}') {
475 scanning_rich = false;
479 else if (scanning_key)
481 else if (richtext || !scanning_rich)
483 // else the character is discarded, which will happen only if
484 // richtext == false and we are scanning rich text
488 LYXERR0("Never found end of key in `" << format << "'!");
492 LYXERR0("Never found end of rich text in `" << format << "'!");
501 // FIXME These would be better read from a file, so that they
502 // could be customized.
504 static string articleFormat = "%author%, \"%title%\", {!<i>!}%journal%{!</i>!} {%volume%[[ %volume%{%number%[[, %number%]]}]]} (%year%){%pages%[[, %pp_text% %pages%]]}.{%note%[[ %note%]]}";
506 static string bookFormat = "{%author%[[%author%]][[%editor%, %ed_text%]]}, {!<i>!}%title%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%).{%note%[[ %note%]]}";
508 static string inSomething = "%author%, \"%title%\", in{%editor%[[ %editor%, %ed_text%,]]} {!<i>!}%booktitle%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%){%pages%[[, %pp_text% %pages%]]}.{%note%[[ %note%]]}";
510 static string thesis = "%author%, %title% ({%address%[[%address%: ]]}%school%, %year%).{%note%[[ %note%]]}";
512 static string defaultFormat = "{%author%[[%author%, ]][[{%editor%[[%editor%, %ed_text%, ]]}]]}\"%title%\"{%journal%[[, {!<i>!}%journal%{!</i>!}]][[{%publisher%[[, %publisher%]][[{%institution%[[, %institution%]]}]]}]]}{%year%[[ (%year%)]]}{%pages%[[, %pages%]]}.";
516 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
523 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
528 if (entry_type_ == "article")
529 info_ = expandFormat(articleFormat, xref, richtext);
530 else if (entry_type_ == "book")
531 info_ = expandFormat(bookFormat, xref, richtext);
532 else if (entry_type_.substr(0,2) == "in")
533 info_ = expandFormat(inSomething, xref, richtext);
534 else if (entry_type_ == "phdthesis" || entry_type_ == "mastersthesis")
535 info_ = expandFormat(thesis, xref, richtext);
537 info_ = expandFormat(defaultFormat, xref, richtext);
540 info_ = convertLaTeXCommands(info_);
545 docstring const & BibTeXInfo::operator[](docstring const & field) const
547 BibTeXInfo::const_iterator it = find(field);
550 static docstring const empty_value = docstring();
555 docstring const & BibTeXInfo::operator[](string const & field) const
557 return operator[](from_ascii(field));
561 docstring BibTeXInfo::getValueForKey(string const & key,
562 BibTeXInfo const * const xref) const
564 docstring const ret = operator[](key);
565 if (!ret.empty() || !xref)
571 //////////////////////////////////////////////////////////////////////
575 //////////////////////////////////////////////////////////////////////
578 // A functor for use with sort, leading to case insensitive sorting
579 class compareNoCase: public binary_function<docstring, docstring, bool>
582 bool operator()(docstring const & s1, docstring const & s2) const {
583 return compare_no_case(s1, s2) < 0;
589 vector<docstring> const BiblioInfo::getKeys() const
591 vector<docstring> bibkeys;
592 BiblioInfo::const_iterator it = begin();
593 for (; it != end(); ++it)
594 bibkeys.push_back(it->first);
595 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
600 vector<docstring> const BiblioInfo::getFields() const
602 vector<docstring> bibfields;
603 set<docstring>::const_iterator it = field_names_.begin();
604 set<docstring>::const_iterator end = field_names_.end();
605 for (; it != end; ++it)
606 bibfields.push_back(*it);
607 sort(bibfields.begin(), bibfields.end());
612 vector<docstring> const BiblioInfo::getEntries() const
614 vector<docstring> bibentries;
615 set<docstring>::const_iterator it = entry_types_.begin();
616 set<docstring>::const_iterator end = entry_types_.end();
617 for (; it != end; ++it)
618 bibentries.push_back(*it);
619 sort(bibentries.begin(), bibentries.end());
624 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
626 BiblioInfo::const_iterator it = find(key);
629 BibTeXInfo const & data = it->second;
630 return data.getAbbreviatedAuthor();
634 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
636 BiblioInfo::const_iterator it = find(key);
639 BibTeXInfo const & data = it->second;
640 return data.citeNumber();
644 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
646 BiblioInfo::const_iterator it = find(key);
649 BibTeXInfo const & data = it->second;
650 docstring year = data.getYear();
652 // let's try the crossref
653 docstring const xref = data.getXRef();
655 return _("No year"); // no luck
656 BiblioInfo::const_iterator const xrefit = find(xref);
658 return _("No year"); // no luck again
659 BibTeXInfo const & xref_data = xrefit->second;
660 year = xref_data.getYear();
662 if (use_modifier && data.modifier() != 0)
663 year += data.modifier();
668 docstring const BiblioInfo::getInfo(docstring const & key, bool richtext) const
670 BiblioInfo::const_iterator it = find(key);
673 BibTeXInfo const & data = it->second;
674 BibTeXInfo const * xrefptr = 0;
675 docstring const xref = data.getXRef();
677 BiblioInfo::const_iterator const xrefit = find(xref);
679 xrefptr = &(xrefit->second);
681 return data.getInfo(xrefptr, richtext);
685 bool BiblioInfo::isBibtex(docstring const & key) const
687 BiblioInfo::const_iterator it = find(key);
690 return it->second.isBibTeX();
695 vector<docstring> const BiblioInfo::getCiteStrings(
696 docstring const & key, Buffer const & buf) const
698 CiteEngine const engine = buf.params().citeEngine();
699 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
700 return getNumericalStrings(key, buf);
702 return getAuthorYearStrings(key, buf);
706 vector<docstring> const BiblioInfo::getNumericalStrings(
707 docstring const & key, Buffer const & buf) const
710 return vector<docstring>();
712 docstring const author = getAbbreviatedAuthor(key);
713 docstring const year = getYear(key);
714 if (author.empty() || year.empty())
715 return vector<docstring>();
717 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
719 vector<docstring> vec(styles.size());
720 for (size_t i = 0; i != vec.size(); ++i) {
726 str = from_ascii("[#ID]");
730 str = _("Add to bibliography only.");
734 str = author + " [#ID]";
738 str = author + " #ID";
742 str = from_ascii("#ID");
754 str = '(' + year + ')';
765 vector<docstring> const BiblioInfo::getAuthorYearStrings(
766 docstring const & key, Buffer const & buf) const
769 return vector<docstring>();
771 docstring const author = getAbbreviatedAuthor(key);
772 docstring const year = getYear(key);
773 if (author.empty() || year.empty())
774 return vector<docstring>();
776 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
778 vector<docstring> vec(styles.size());
779 for (size_t i = 0; i != vec.size(); ++i) {
784 // jurabib only: Author/Annotator
785 // (i.e. the "before" field, 2nd opt arg)
786 str = author + "/<" + _("before") + '>';
790 str = _("Add to bibliography only.");
794 str = author + " (" + year + ')';
798 str = '(' + author + ", " + year + ')';
802 str = author + ' ' + year ;
806 str = author + ", " + year ;
818 str = '(' + year + ')';
827 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
829 bimap_.insert(info.begin(), info.end());
834 // used in xhtml to sort a list of BibTeXInfo objects
835 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
837 docstring const lauth = lhs->getAbbreviatedAuthor();
838 docstring const rauth = rhs->getAbbreviatedAuthor();
839 docstring const lyear = lhs->getYear();
840 docstring const ryear = rhs->getYear();
841 docstring const ltitl = lhs->operator[]("title");
842 docstring const rtitl = rhs->operator[]("title");
843 return (lauth < rauth)
844 || (lauth == rauth && lyear < ryear)
845 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
850 void BiblioInfo::collectCitedEntries(Buffer const & buf)
852 cited_entries_.clear();
853 // We are going to collect all the citation keys used in the document,
854 // getting them from the TOC.
855 // FIXME We may want to collect these differently, in the first case,
856 // so that we might have them in order of appearance.
857 set<docstring> citekeys;
858 Toc const & toc = buf.tocBackend().toc("citation");
859 Toc::const_iterator it = toc.begin();
860 Toc::const_iterator const en = toc.end();
861 for (; it != en; ++it) {
862 if (it->str().empty())
864 vector<docstring> const keys = getVectorFromString(it->str());
865 citekeys.insert(keys.begin(), keys.end());
867 if (citekeys.empty())
870 // We have a set of the keys used in this document.
871 // We will now convert it to a list of the BibTeXInfo objects used in
873 vector<BibTeXInfo const *> bi;
874 set<docstring>::const_iterator cit = citekeys.begin();
875 set<docstring>::const_iterator const cen = citekeys.end();
876 for (; cit != cen; ++cit) {
877 BiblioInfo::const_iterator const bt = find(*cit);
878 if (bt == end() || !bt->second.isBibTeX())
880 bi.push_back(&(bt->second));
883 sort(bi.begin(), bi.end(), lSorter);
885 // Now we can write the sorted keys
886 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
887 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
888 for (; bit != ben; ++bit)
889 cited_entries_.push_back((*bit)->key());
893 void BiblioInfo::makeCitationLabels(Buffer const & buf)
895 collectCitedEntries(buf);
896 CiteEngine const engine = buf.params().citeEngine();
898 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
902 // used to remember the last one we saw
903 // we'll be comparing entries to see if we need to add
904 // modifiers, like "1984a"
905 map<docstring, BibTeXInfo>::iterator last;
907 vector<docstring>::const_iterator it = cited_entries_.begin();
908 vector<docstring>::const_iterator const en = cited_entries_.end();
909 for (; it != en; ++it) {
910 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
911 // this shouldn't happen, but...
912 if (biit == bimap_.end())
913 // ...fail gracefully, anyway.
915 BibTeXInfo & entry = biit->second;
917 docstring const num = convert<docstring>(++keynumber);
918 entry.setCiteNumber(num);
920 if (it != cited_entries_.begin()
921 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
922 // we access the year via getYear() so as to get it from the xref,
923 // if we need to do so
924 && getYear(entry.key()) == getYear(last->second.key())) {
926 // so the last one should have been 'a'
927 last->second.setModifier('a');
929 } else if (modifier == 'z')
936 entry.setModifier(modifier);
937 // remember the last one
944 //////////////////////////////////////////////////////////////////////
948 //////////////////////////////////////////////////////////////////////
953 char const * const citeCommands[] = {
954 "cite", "citet", "citep", "citealt", "citealp",
955 "citeauthor", "citeyear", "citeyearpar", "nocite" };
957 unsigned int const nCiteCommands =
958 sizeof(citeCommands) / sizeof(char *);
960 CiteStyle const citeStylesArray[] = {
961 CITE, CITET, CITEP, CITEALT, CITEALP,
962 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
964 unsigned int const nCiteStyles =
965 sizeof(citeStylesArray) / sizeof(CiteStyle);
967 CiteStyle const citeStylesFull[] = {
968 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
970 unsigned int const nCiteStylesFull =
971 sizeof(citeStylesFull) / sizeof(CiteStyle);
973 CiteStyle const citeStylesUCase[] = {
974 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
976 unsigned int const nCiteStylesUCase =
977 sizeof(citeStylesUCase) / sizeof(CiteStyle);
982 CitationStyle citationStyleFromString(string const & command)
988 string cmd = command;
990 s.forceUpperCase = true;
994 size_t const n = cmd.size() - 1;
995 if (cmd != "cite" && cmd[n] == '*') {
997 cmd = cmd.substr(0, n);
1000 char const * const * const last = citeCommands + nCiteCommands;
1001 char const * const * const ptr = find(citeCommands, last, cmd);
1004 size_t idx = ptr - citeCommands;
1005 s.style = citeStylesArray[idx];
1011 string citationStyleToString(const CitationStyle & s)
1013 string cite = citeCommands[s.style];
1015 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
1016 if (std::find(citeStylesFull, last, s.style) != last)
1020 if (s.forceUpperCase) {
1021 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
1022 if (std::find(citeStylesUCase, last, s.style) != last)
1029 vector<CiteStyle> citeStyles(CiteEngine engine)
1031 unsigned int nStyles = 0;
1032 unsigned int start = 0;
1039 case ENGINE_NATBIB_AUTHORYEAR:
1040 case ENGINE_NATBIB_NUMERICAL:
1041 nStyles = nCiteStyles - 1;
1044 case ENGINE_JURABIB:
1045 nStyles = nCiteStyles;
1050 vector<CiteStyle> styles(nStyles);
1053 for (; i != styles.size(); ++i, ++j)
1054 styles[i] = citeStylesArray[j];