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 `" << 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
408 bool scanning_key = false;
409 bool scanning_rich = false;
412 // we'll remove characters from the front of fmt as we
415 char_type thischar = fmt[0];
416 if (thischar == '%') {
417 // beginning or end of key
420 scanning_key = false;
421 // so we replace the key with its value, which may be empty
422 if (key == "pp_text")
424 else if (key == "ed_text")
426 else if(key == "edby_text")
429 docstring const val = getValueForKey(key, xref);
438 else if (thischar == '{') {
439 // beginning of option?
441 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
444 if (fmt.size() > 1) {
446 // it is the beginning of an optional format
450 string const newfmt =
451 parseOptions(fmt, optkey, ifpart, elsepart);
452 if (newfmt == fmt) // parse error
455 docstring const val = getValueForKey(optkey, xref);
457 ret += expandFormat(ifpart, xref, richtext);
458 else if (!elsepart.empty())
459 ret += expandFormat(elsepart, xref, richtext);
460 // fmt will have been shortened for us already
464 // beginning of rich text
465 scanning_rich = true;
470 // we are here if the '{' was at the end of the format. hmm.
473 else if (scanning_rich && thischar == '!'
474 && fmt.size() > 1 && fmt[1] == '}') {
476 scanning_rich = false;
480 else if (scanning_key)
482 else if (richtext || !scanning_rich)
484 // else the character is discarded, which will happen only if
485 // richtext == false and we are scanning rich text
489 LYXERR0("Never found end of key in `" << format << "'!");
493 LYXERR0("Never found end of rich text in `" << format << "'!");
502 // FIXME These would be better read from a file, so that they
503 // could be customized.
505 static string articleFormat = "%author%, \"%title%\", {!<i>!}%journal%{!</i>!} {%volume%[[ %volume%{%number%[[, %number%]]}]]} (%year%){%pages%[[, %pp_text% %pages%]]}.{%note%[[ %note%]]}";
507 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%]]}";
509 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%]]}";
511 static string thesis = "%author%, %title% ({%address%[[%address%: ]]}%school%, %year%).{%note%[[ %note%]]}";
513 static string defaultFormat = "{%author%[[%author%, ]][[{%editor%[[%editor%, %ed_text%, ]]}]]}\"%title%\"{%journal%[[, {!<i>!}%journal%{!</i>!}]][[{%publisher%[[, %publisher%]][[{%institution%[[, %institution%]]}]]}]]}{%year%[[ (%year%)]]}{%pages%[[, %pages%]]}.";
517 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
524 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
529 if (entry_type_ == "article")
530 info_ = expandFormat(articleFormat, xref, richtext);
531 else if (entry_type_ == "book")
532 info_ = expandFormat(bookFormat, xref, richtext);
533 else if (entry_type_.substr(0,2) == "in")
534 info_ = expandFormat(inSomething, xref, richtext);
535 else if (entry_type_ == "phdthesis" || entry_type_ == "mastersthesis")
536 info_ = expandFormat(thesis, xref, richtext);
538 info_ = expandFormat(defaultFormat, xref, richtext);
541 info_ = convertLaTeXCommands(info_);
546 docstring const & BibTeXInfo::operator[](docstring const & field) const
548 BibTeXInfo::const_iterator it = find(field);
551 static docstring const empty_value = docstring();
556 docstring const & BibTeXInfo::operator[](string const & field) const
558 return operator[](from_ascii(field));
562 docstring BibTeXInfo::getValueForKey(string const & key,
563 BibTeXInfo const * const xref) const
565 docstring const ret = operator[](key);
566 if (!ret.empty() || !xref)
572 //////////////////////////////////////////////////////////////////////
576 //////////////////////////////////////////////////////////////////////
579 // A functor for use with sort, leading to case insensitive sorting
580 class compareNoCase: public binary_function<docstring, docstring, bool>
583 bool operator()(docstring const & s1, docstring const & s2) const {
584 return compare_no_case(s1, s2) < 0;
590 vector<docstring> const BiblioInfo::getKeys() const
592 vector<docstring> bibkeys;
593 BiblioInfo::const_iterator it = begin();
594 for (; it != end(); ++it)
595 bibkeys.push_back(it->first);
596 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
601 vector<docstring> const BiblioInfo::getFields() const
603 vector<docstring> bibfields;
604 set<docstring>::const_iterator it = field_names_.begin();
605 set<docstring>::const_iterator end = field_names_.end();
606 for (; it != end; ++it)
607 bibfields.push_back(*it);
608 sort(bibfields.begin(), bibfields.end());
613 vector<docstring> const BiblioInfo::getEntries() const
615 vector<docstring> bibentries;
616 set<docstring>::const_iterator it = entry_types_.begin();
617 set<docstring>::const_iterator end = entry_types_.end();
618 for (; it != end; ++it)
619 bibentries.push_back(*it);
620 sort(bibentries.begin(), bibentries.end());
625 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
627 BiblioInfo::const_iterator it = find(key);
630 BibTeXInfo const & data = it->second;
631 return data.getAbbreviatedAuthor();
635 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
637 BiblioInfo::const_iterator it = find(key);
640 BibTeXInfo const & data = it->second;
641 return data.citeNumber();
645 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
647 BiblioInfo::const_iterator it = find(key);
650 BibTeXInfo const & data = it->second;
651 docstring year = data.getYear();
653 // let's try the crossref
654 docstring const xref = data.getXRef();
656 return _("No year"); // no luck
657 BiblioInfo::const_iterator const xrefit = find(xref);
659 return _("No year"); // no luck again
660 BibTeXInfo const & xref_data = xrefit->second;
661 year = xref_data.getYear();
663 if (use_modifier && data.modifier() != 0)
664 year += data.modifier();
669 docstring const BiblioInfo::getInfo(docstring const & key, bool richtext) const
671 BiblioInfo::const_iterator it = find(key);
674 BibTeXInfo const & data = it->second;
675 BibTeXInfo const * xrefptr = 0;
676 docstring const xref = data.getXRef();
678 BiblioInfo::const_iterator const xrefit = find(xref);
680 xrefptr = &(xrefit->second);
682 return data.getInfo(xrefptr, richtext);
686 bool BiblioInfo::isBibtex(docstring const & key) const
688 BiblioInfo::const_iterator it = find(key);
691 return it->second.isBibTeX();
696 vector<docstring> const BiblioInfo::getCiteStrings(
697 docstring const & key, Buffer const & buf) const
699 CiteEngine const engine = buf.params().citeEngine();
700 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
701 return getNumericalStrings(key, buf);
703 return getAuthorYearStrings(key, buf);
707 vector<docstring> const BiblioInfo::getNumericalStrings(
708 docstring const & key, Buffer const & buf) const
711 return vector<docstring>();
713 docstring const author = getAbbreviatedAuthor(key);
714 docstring const year = getYear(key);
715 if (author.empty() || year.empty())
716 return vector<docstring>();
718 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
720 vector<docstring> vec(styles.size());
721 for (size_t i = 0; i != vec.size(); ++i) {
727 str = from_ascii("[#ID]");
731 str = _("Add to bibliography only.");
735 str = author + " [#ID]";
739 str = author + " #ID";
743 str = from_ascii("#ID");
755 str = '(' + year + ')';
766 vector<docstring> const BiblioInfo::getAuthorYearStrings(
767 docstring const & key, Buffer const & buf) const
770 return vector<docstring>();
772 docstring const author = getAbbreviatedAuthor(key);
773 docstring const year = getYear(key);
774 if (author.empty() || year.empty())
775 return vector<docstring>();
777 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
779 vector<docstring> vec(styles.size());
780 for (size_t i = 0; i != vec.size(); ++i) {
785 // jurabib only: Author/Annotator
786 // (i.e. the "before" field, 2nd opt arg)
787 str = author + "/<" + _("before") + '>';
791 str = _("Add to bibliography only.");
795 str = author + " (" + year + ')';
799 str = '(' + author + ", " + year + ')';
803 str = author + ' ' + year ;
807 str = author + ", " + year ;
819 str = '(' + year + ')';
828 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
830 bimap_.insert(info.begin(), info.end());
835 // used in xhtml to sort a list of BibTeXInfo objects
836 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
838 docstring const lauth = lhs->getAbbreviatedAuthor();
839 docstring const rauth = rhs->getAbbreviatedAuthor();
840 docstring const lyear = lhs->getYear();
841 docstring const ryear = rhs->getYear();
842 docstring const ltitl = lhs->operator[]("title");
843 docstring const rtitl = rhs->operator[]("title");
844 return (lauth < rauth)
845 || (lauth == rauth && lyear < ryear)
846 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
851 void BiblioInfo::collectCitedEntries(Buffer const & buf)
853 cited_entries_.clear();
854 // We are going to collect all the citation keys used in the document,
855 // getting them from the TOC.
856 // FIXME We may want to collect these differently, in the first case,
857 // so that we might have them in order of appearance.
858 set<docstring> citekeys;
859 Toc const & toc = buf.tocBackend().toc("citation");
860 Toc::const_iterator it = toc.begin();
861 Toc::const_iterator const en = toc.end();
862 for (; it != en; ++it) {
863 if (it->str().empty())
865 vector<docstring> const keys = getVectorFromString(it->str());
866 citekeys.insert(keys.begin(), keys.end());
868 if (citekeys.empty())
871 // We have a set of the keys used in this document.
872 // We will now convert it to a list of the BibTeXInfo objects used in
874 vector<BibTeXInfo const *> bi;
875 set<docstring>::const_iterator cit = citekeys.begin();
876 set<docstring>::const_iterator const cen = citekeys.end();
877 for (; cit != cen; ++cit) {
878 BiblioInfo::const_iterator const bt = find(*cit);
879 if (bt == end() || !bt->second.isBibTeX())
881 bi.push_back(&(bt->second));
884 sort(bi.begin(), bi.end(), lSorter);
886 // Now we can write the sorted keys
887 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
888 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
889 for (; bit != ben; ++bit)
890 cited_entries_.push_back((*bit)->key());
894 void BiblioInfo::makeCitationLabels(Buffer const & buf)
896 collectCitedEntries(buf);
897 CiteEngine const engine = buf.params().citeEngine();
899 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
903 // used to remember the last one we saw
904 // we'll be comparing entries to see if we need to add
905 // modifiers, like "1984a"
906 map<docstring, BibTeXInfo>::iterator last;
908 vector<docstring>::const_iterator it = cited_entries_.begin();
909 vector<docstring>::const_iterator const en = cited_entries_.end();
910 for (; it != en; ++it) {
911 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
912 // this shouldn't happen, but...
913 if (biit == bimap_.end())
914 // ...fail gracefully, anyway.
916 BibTeXInfo & entry = biit->second;
918 docstring const num = convert<docstring>(++keynumber);
919 entry.setCiteNumber(num);
921 if (it != cited_entries_.begin()
922 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
923 // we access the year via getYear() so as to get it from the xref,
924 // if we need to do so
925 && getYear(entry.key()) == getYear(last->second.key())) {
927 // so the last one should have been 'a'
928 last->second.setModifier('a');
930 } else if (modifier == 'z')
937 entry.setModifier(modifier);
938 // remember the last one
945 //////////////////////////////////////////////////////////////////////
949 //////////////////////////////////////////////////////////////////////
954 char const * const citeCommands[] = {
955 "cite", "citet", "citep", "citealt", "citealp",
956 "citeauthor", "citeyear", "citeyearpar", "nocite" };
958 unsigned int const nCiteCommands =
959 sizeof(citeCommands) / sizeof(char *);
961 CiteStyle const citeStylesArray[] = {
962 CITE, CITET, CITEP, CITEALT, CITEALP,
963 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
965 unsigned int const nCiteStyles =
966 sizeof(citeStylesArray) / sizeof(CiteStyle);
968 CiteStyle const citeStylesFull[] = {
969 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
971 unsigned int const nCiteStylesFull =
972 sizeof(citeStylesFull) / sizeof(CiteStyle);
974 CiteStyle const citeStylesUCase[] = {
975 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
977 unsigned int const nCiteStylesUCase =
978 sizeof(citeStylesUCase) / sizeof(CiteStyle);
983 CitationStyle citationStyleFromString(string const & command)
989 string cmd = command;
991 s.forceUpperCase = true;
995 size_t const n = cmd.size() - 1;
996 if (cmd != "cite" && cmd[n] == '*') {
998 cmd = cmd.substr(0, n);
1001 char const * const * const last = citeCommands + nCiteCommands;
1002 char const * const * const ptr = find(citeCommands, last, cmd);
1005 size_t idx = ptr - citeCommands;
1006 s.style = citeStylesArray[idx];
1012 string citationStyleToString(const CitationStyle & s)
1014 string cite = citeCommands[s.style];
1016 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
1017 if (std::find(citeStylesFull, last, s.style) != last)
1021 if (s.forceUpperCase) {
1022 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
1023 if (std::find(citeStylesUCase, last, s.style) != last)
1030 vector<CiteStyle> citeStyles(CiteEngine engine)
1032 unsigned int nStyles = 0;
1033 unsigned int start = 0;
1040 case ENGINE_NATBIB_AUTHORYEAR:
1041 case ENGINE_NATBIB_NUMERICAL:
1042 nStyles = nCiteStyles - 1;
1045 case ENGINE_JURABIB:
1046 nStyles = nCiteStyles;
1051 vector<CiteStyle> styles(nStyles);
1054 for (; i != styles.size(); ++i, ++j)
1055 styles[i] = citeStylesArray[j];