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 docstring parseOptions(docstring const & format, docstring & optkey,
285 docstring & ifpart, docstring & 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 docstring parseEmbeddedOption(docstring const & format,
293 docstring & ifelsepart)
295 LASSERT(format[0] == '{' && format[1] == '%', return format);
299 docstring const rest = parseOptions(format, optkey, ifpart, elsepart);
300 if (format == rest) { // parse error
301 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
304 LASSERT(rest.size() <= format.size(), /* */);
305 ifelsepart = format.substr(0, format.size() - rest.size());
310 // Gets a "clause" from a format string, where the clause is
311 // delimited by '[[' and ']]'. Returns what is left after the
312 // clause is removed, and returns format if there is an error.
313 docstring getClause(docstring const & format, docstring & clause)
315 docstring fmt = format;
318 // we'll remove characters from the front of fmt as we
321 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
326 // check for an embedded option
327 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
329 docstring const rest = parseEmbeddedOption(fmt, part);
331 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
336 } else { // it's just a normal character
345 /// parse an options string, which must appear at the start of the
346 /// format parameter. puts the parsed bits in optkey, ifpart, and
347 /// elsepart and returns what's left after the option is removed.
348 /// if there's an error, it returns format itself.
349 docstring parseOptions(docstring const & format, docstring & optkey,
350 docstring & ifpart, docstring & elsepart)
352 LASSERT(format[0] == '{' && format[1] == '%', return format);
354 docstring fmt = format.substr(2);
355 size_t pos = fmt.find('%'); // end of key
356 if (pos == string::npos) {
357 LYXERR0("Error parsing `" << format <<"'. Can't find end of key.");
360 optkey = fmt.substr(0,pos);
361 fmt = fmt.substr(pos + 1);
362 // [[format]] should be next
363 if (fmt[0] != '[' || fmt[1] != '[') {
364 LYXERR0("Error parsing `" << format <<"'. Can't find '[[' after key.");
368 docstring curfmt = fmt;
369 fmt = getClause(curfmt, ifpart);
371 LYXERR0("Error parsing `" << format <<"'. Couldn't get if clause.");
375 if (fmt[0] == '}') // we're done, no else clause
376 return fmt.substr(1);
378 // else part should follow
379 if (fmt[0] != '[' || fmt[1] != '[') {
380 LYXERR0("Error parsing `" << format <<"'. Can't find else clause.");
385 fmt = getClause(curfmt, elsepart);
387 if (fmt == curfmt || fmt[0] != '}') {
388 LYXERR0("Error parsing `" << format <<"'. Can't find end of option.");
391 return fmt.substr(1);
397 docstring BibTeXInfo::expandFormat(docstring const & format,
398 BibTeXInfo const * const xref, bool richtext) const
403 bool scanning_key = false;
404 bool scanning_rich = false;
406 docstring fmt = format;
407 // we'll remove characters from the front of fmt as we
410 char_type thischar = fmt[0];
411 if (thischar == '%') {
412 // beginning or end of key
415 scanning_key = false;
416 // so we replace the key with its value, which may be empty
417 docstring const val = getValueForKey(to_utf8(key), xref);
425 else if (thischar == '{') {
426 // beginning of option?
428 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
431 if (fmt.size() > 1) {
433 // it is the beginning of an optional format
437 docstring const newfmt =
438 parseOptions(fmt, optkey, ifpart, elsepart);
439 if (newfmt == fmt) // parse error
442 docstring const val = getValueForKey(to_utf8(optkey), xref);
444 ret += expandFormat(ifpart, xref, richtext);
445 else if (!elsepart.empty())
446 ret += expandFormat(elsepart, xref, richtext);
447 // fmt will have been shortened for us already
451 // beginning of rich text
452 scanning_rich = true;
457 // we are here if the '{' was at the end of the format. hmm.
460 else if (scanning_rich && thischar == '!'
461 && fmt.size() > 1 && fmt[1] == '}') {
463 scanning_rich = false;
467 else if (scanning_key)
469 else if (richtext || !scanning_rich)
471 // else the character is discarded, which will happen only if
472 // richtext == false and we are scanning rich text
476 LYXERR0("Never found end of key in `" << format << "'!");
480 LYXERR0("Never found end of rich text in `" << format << "'!");
489 // FIXME These would be better read from a file, so that they
490 // could be customized.
492 static docstring articleFormat = from_ascii("%author%, \"%title%\", {!<i>!}%journal%{!</i>!} {%volume%[[ %volume%{%number%[[, %number%]]}]]} (%year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}");
494 static docstring bookFormat = from_ascii("{%author%[[%author%]][[%editor%, ed.]]}, {!<i>!}%title%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%).{%note%[[ %note%]]}");
496 static docstring inSomething = from_ascii("%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%]]}");
498 static docstring thesis = from_ascii("%author%, %title% ({%address%[[%address%: ]]}%school%, %year%).{%note%[[ %note%]]}");
500 static docstring defaultFormat = from_ascii("{%author%[[%author%, ]][[{%editor%[[%editor%, ed., ]]}]]}\"%title%\"{%journal%[[, {!<i>!}%journal%{!</i>!}]][[{%publisher%[[, %publisher%]][[{%institution%[[, %institution%]]}]]}]]}{%year%[[ (%year%)]]}{%pages%[[, %pages%]]}.");
504 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
511 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
516 if (entry_type_ == "article")
517 info_ = expandFormat(articleFormat, xref, richtext);
518 else if (entry_type_ == "book")
519 info_ = expandFormat(bookFormat, xref, richtext);
520 else if (entry_type_.substr(0,2) == "in")
521 info_ = expandFormat(inSomething, xref, richtext);
522 else if (entry_type_ == "phdthesis" || entry_type_ == "mastersthesis")
523 info_ = expandFormat(thesis, xref, richtext);
525 info_ = expandFormat(defaultFormat, xref, richtext);
528 info_ = convertLaTeXCommands(info_);
533 docstring const & BibTeXInfo::operator[](docstring const & field) const
535 BibTeXInfo::const_iterator it = find(field);
538 static docstring const empty_value = docstring();
543 docstring const & BibTeXInfo::operator[](string const & field) const
545 return operator[](from_ascii(field));
549 docstring BibTeXInfo::getValueForKey(string const & key,
550 BibTeXInfo const * const xref) const
552 docstring const ret = operator[](key);
553 if (!ret.empty() || !xref)
559 //////////////////////////////////////////////////////////////////////
563 //////////////////////////////////////////////////////////////////////
566 // A functor for use with sort, leading to case insensitive sorting
567 class compareNoCase: public binary_function<docstring, docstring, bool>
570 bool operator()(docstring const & s1, docstring const & s2) const {
571 return compare_no_case(s1, s2) < 0;
577 vector<docstring> const BiblioInfo::getKeys() const
579 vector<docstring> bibkeys;
580 BiblioInfo::const_iterator it = begin();
581 for (; it != end(); ++it)
582 bibkeys.push_back(it->first);
583 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
588 vector<docstring> const BiblioInfo::getFields() const
590 vector<docstring> bibfields;
591 set<docstring>::const_iterator it = field_names_.begin();
592 set<docstring>::const_iterator end = field_names_.end();
593 for (; it != end; ++it)
594 bibfields.push_back(*it);
595 sort(bibfields.begin(), bibfields.end());
600 vector<docstring> const BiblioInfo::getEntries() const
602 vector<docstring> bibentries;
603 set<docstring>::const_iterator it = entry_types_.begin();
604 set<docstring>::const_iterator end = entry_types_.end();
605 for (; it != end; ++it)
606 bibentries.push_back(*it);
607 sort(bibentries.begin(), bibentries.end());
612 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
614 BiblioInfo::const_iterator it = find(key);
617 BibTeXInfo const & data = it->second;
618 return data.getAbbreviatedAuthor();
622 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
624 BiblioInfo::const_iterator it = find(key);
627 BibTeXInfo const & data = it->second;
628 return data.citeNumber();
632 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
634 BiblioInfo::const_iterator it = find(key);
637 BibTeXInfo const & data = it->second;
638 docstring year = data.getYear();
640 // let's try the crossref
641 docstring const xref = data.getXRef();
643 return _("No year"); // no luck
644 BiblioInfo::const_iterator const xrefit = find(xref);
646 return _("No year"); // no luck again
647 BibTeXInfo const & xref_data = xrefit->second;
648 year = xref_data.getYear();
650 if (use_modifier && data.modifier() != 0)
651 year += data.modifier();
656 docstring const BiblioInfo::getInfo(docstring const & key, bool richtext) const
658 BiblioInfo::const_iterator it = find(key);
661 BibTeXInfo const & data = it->second;
662 BibTeXInfo const * xrefptr = 0;
663 docstring const xref = data.getXRef();
665 BiblioInfo::const_iterator const xrefit = find(xref);
667 xrefptr = &(xrefit->second);
669 return data.getInfo(xrefptr, richtext);
673 bool BiblioInfo::isBibtex(docstring const & key) const
675 BiblioInfo::const_iterator it = find(key);
678 return it->second.isBibTeX();
683 vector<docstring> const BiblioInfo::getCiteStrings(
684 docstring const & key, Buffer const & buf) const
686 CiteEngine const engine = buf.params().citeEngine();
687 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
688 return getNumericalStrings(key, buf);
690 return getAuthorYearStrings(key, buf);
694 vector<docstring> const BiblioInfo::getNumericalStrings(
695 docstring const & key, Buffer const & buf) const
698 return vector<docstring>();
700 docstring const author = getAbbreviatedAuthor(key);
701 docstring const year = getYear(key);
702 if (author.empty() || year.empty())
703 return vector<docstring>();
705 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
707 vector<docstring> vec(styles.size());
708 for (size_t i = 0; i != vec.size(); ++i) {
714 str = from_ascii("[#ID]");
718 str = _("Add to bibliography only.");
722 str = author + " [#ID]";
726 str = author + " #ID";
730 str = from_ascii("#ID");
742 str = '(' + year + ')';
753 vector<docstring> const BiblioInfo::getAuthorYearStrings(
754 docstring const & key, Buffer const & buf) const
757 return vector<docstring>();
759 docstring const author = getAbbreviatedAuthor(key);
760 docstring const year = getYear(key);
761 if (author.empty() || year.empty())
762 return vector<docstring>();
764 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
766 vector<docstring> vec(styles.size());
767 for (size_t i = 0; i != vec.size(); ++i) {
772 // jurabib only: Author/Annotator
773 // (i.e. the "before" field, 2nd opt arg)
774 str = author + "/<" + _("before") + '>';
778 str = _("Add to bibliography only.");
782 str = author + " (" + year + ')';
786 str = '(' + author + ", " + year + ')';
790 str = author + ' ' + year ;
794 str = author + ", " + year ;
806 str = '(' + year + ')';
815 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
817 bimap_.insert(info.begin(), info.end());
822 // used in xhtml to sort a list of BibTeXInfo objects
823 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
825 docstring const lauth = lhs->getAbbreviatedAuthor();
826 docstring const rauth = rhs->getAbbreviatedAuthor();
827 docstring const lyear = lhs->getYear();
828 docstring const ryear = rhs->getYear();
829 docstring const ltitl = lhs->operator[]("title");
830 docstring const rtitl = rhs->operator[]("title");
831 return (lauth < rauth)
832 || (lauth == rauth && lyear < ryear)
833 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
838 void BiblioInfo::collectCitedEntries(Buffer const & buf)
840 cited_entries_.clear();
841 // We are going to collect all the citation keys used in the document,
842 // getting them from the TOC.
843 // FIXME We may want to collect these differently, in the first case,
844 // so that we might have them in order of appearance.
845 set<docstring> citekeys;
846 Toc const & toc = buf.tocBackend().toc("citation");
847 Toc::const_iterator it = toc.begin();
848 Toc::const_iterator const en = toc.end();
849 for (; it != en; ++it) {
850 if (it->str().empty())
852 vector<docstring> const keys = getVectorFromString(it->str());
853 citekeys.insert(keys.begin(), keys.end());
855 if (citekeys.empty())
858 // We have a set of the keys used in this document.
859 // We will now convert it to a list of the BibTeXInfo objects used in
861 vector<BibTeXInfo const *> bi;
862 set<docstring>::const_iterator cit = citekeys.begin();
863 set<docstring>::const_iterator const cen = citekeys.end();
864 for (; cit != cen; ++cit) {
865 BiblioInfo::const_iterator const bt = find(*cit);
866 if (bt == end() || !bt->second.isBibTeX())
868 bi.push_back(&(bt->second));
871 sort(bi.begin(), bi.end(), lSorter);
873 // Now we can write the sorted keys
874 vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
875 vector<BibTeXInfo const *>::const_iterator ben = bi.end();
876 for (; bit != ben; ++bit)
877 cited_entries_.push_back((*bit)->key());
881 void BiblioInfo::makeCitationLabels(Buffer const & buf)
883 collectCitedEntries(buf);
884 CiteEngine const engine = buf.params().citeEngine();
886 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
890 // used to remember the last one we saw
891 // we'll be comparing entries to see if we need to add
892 // modifiers, like "1984a"
893 map<docstring, BibTeXInfo>::iterator last;
895 vector<docstring>::const_iterator it = cited_entries_.begin();
896 vector<docstring>::const_iterator const en = cited_entries_.end();
897 for (; it != en; ++it) {
898 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
899 // this shouldn't happen, but...
900 if (biit == bimap_.end())
901 // ...fail gracefully, anyway.
903 BibTeXInfo & entry = biit->second;
905 docstring const num = convert<docstring>(++keynumber);
906 entry.setCiteNumber(num);
908 if (it != cited_entries_.begin()
909 && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
910 // we access the year via getYear() so as to get it from the xref,
911 // if we need to do so
912 && getYear(entry.key()) == getYear(last->second.key())) {
914 // so the last one should have been 'a'
915 last->second.setModifier('a');
917 } else if (modifier == 'z')
924 entry.setModifier(modifier);
925 // remember the last one
932 //////////////////////////////////////////////////////////////////////
936 //////////////////////////////////////////////////////////////////////
941 char const * const citeCommands[] = {
942 "cite", "citet", "citep", "citealt", "citealp",
943 "citeauthor", "citeyear", "citeyearpar", "nocite" };
945 unsigned int const nCiteCommands =
946 sizeof(citeCommands) / sizeof(char *);
948 CiteStyle const citeStylesArray[] = {
949 CITE, CITET, CITEP, CITEALT, CITEALP,
950 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
952 unsigned int const nCiteStyles =
953 sizeof(citeStylesArray) / sizeof(CiteStyle);
955 CiteStyle const citeStylesFull[] = {
956 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
958 unsigned int const nCiteStylesFull =
959 sizeof(citeStylesFull) / sizeof(CiteStyle);
961 CiteStyle const citeStylesUCase[] = {
962 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
964 unsigned int const nCiteStylesUCase =
965 sizeof(citeStylesUCase) / sizeof(CiteStyle);
970 CitationStyle citationStyleFromString(string const & command)
976 string cmd = command;
978 s.forceUpperCase = true;
982 size_t const n = cmd.size() - 1;
983 if (cmd != "cite" && cmd[n] == '*') {
985 cmd = cmd.substr(0, n);
988 char const * const * const last = citeCommands + nCiteCommands;
989 char const * const * const ptr = find(citeCommands, last, cmd);
992 size_t idx = ptr - citeCommands;
993 s.style = citeStylesArray[idx];
999 string citationStyleToString(const CitationStyle & s)
1001 string cite = citeCommands[s.style];
1003 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
1004 if (std::find(citeStylesFull, last, s.style) != last)
1008 if (s.forceUpperCase) {
1009 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
1010 if (std::find(citeStylesUCase, last, s.style) != last)
1017 vector<CiteStyle> citeStyles(CiteEngine engine)
1019 unsigned int nStyles = 0;
1020 unsigned int start = 0;
1027 case ENGINE_NATBIB_AUTHORYEAR:
1028 case ENGINE_NATBIB_NUMERICAL:
1029 nStyles = nCiteStyles - 1;
1032 case ENGINE_JURABIB:
1033 nStyles = nCiteStyles;
1038 vector<CiteStyle> styles(nStyles);
1041 for (; i != styles.size(); ++i, ++j)
1042 styles[i] = citeStylesArray[j];