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"
23 #include "insets/Inset.h"
24 #include "insets/InsetBibitem.h"
25 #include "insets/InsetBibtex.h"
26 #include "insets/InsetInclude.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
31 #include "support/lstrings.h"
32 #include "support/textutils.h"
34 #include "boost/regex.hpp"
37 using namespace lyx::support;
42 //////////////////////////////////////////////////////////////////////
46 //////////////////////////////////////////////////////////////////////
48 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
49 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_()
53 bool BibTeXInfo::hasField(docstring const & field) const
55 return count(field) == 1;
59 docstring const & BibTeXInfo::operator[](docstring const & field) const
61 BibTeXInfo::const_iterator it = find(field);
64 static docstring const empty_value = docstring();
69 docstring const & BibTeXInfo::operator[](string const & field) const
71 return operator[](from_ascii(field));
75 docstring BibTeXInfo::getValueForKey(string const & key,
76 BibTeXInfo const * const xref) const
78 docstring const ret = operator[](key);
79 if (!ret.empty() || !xref)
85 docstring familyName(docstring const & name)
90 // first we look for a comma, and take the last name to be everything
91 // preceding the right-most one, so that we also get the "jr" part.
92 docstring::size_type idx = name.rfind(',');
93 if (idx != docstring::npos)
94 return ltrim(name.substr(0, idx));
96 // OK, so now we want to look for the last name. We're going to
97 // include the "von" part. This isn't perfect.
98 // Split on spaces, to get various tokens.
99 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
100 // If we only get two, assume the last one is the last name
101 if (pieces.size() <= 2)
102 return pieces.back();
104 // Now we look for the first token that begins with a lower case letter.
105 vector<docstring>::const_iterator it = pieces.begin();
106 vector<docstring>::const_iterator en = pieces.end();
107 for (; it != en; ++it) {
108 if ((*it).size() == 0)
110 char_type const c = (*it)[0];
115 if (it == en) // we never found a "von"
116 return pieces.back();
118 // reconstruct what we need to return
121 for (; it != en; ++it) {
131 docstring const BibTeXInfo::getAbbreviatedAuthor() const
134 docstring const opt = trim(operator[]("label"));
139 split(opt, authors, '(');
143 docstring author = operator[]("author");
144 if (author.empty()) {
145 author = operator[]("editor");
150 // OK, we've got some names. Let's format them.
151 // Try to split the author list on " and "
152 vector<docstring> const authors =
153 getVectorFromString(author, from_ascii(" and "));
155 if (authors.size() == 2)
156 return bformat(_("%1$s and %2$s"),
157 familyName(authors[0]), familyName(authors[1]));
159 if (authors.size() > 2)
160 return bformat(_("%1$s et al."), familyName(authors[0]));
162 return familyName(authors[0]);
166 docstring const BibTeXInfo::getYear() const
169 return operator[]("year");
171 docstring const opt = trim(operator[]("label"));
176 docstring const tmp = split(opt, authors, '(');
178 split(tmp, year, ')');
183 docstring const BibTeXInfo::getXRef() const
187 return operator[]("crossref");
193 docstring convertLaTeXCommands(docstring const & str)
198 bool scanning_cmd = false;
199 bool scanning_math = false;
200 bool escaped = false; // used to catch \$, etc.
202 char_type const ch = val[0];
204 // if we're scanning math, we output everything until we
205 // find an unescaped $, at which point we break out.
212 scanning_math = false;
218 // if we're scanning a command name, then we just
219 // discard characters until we hit something that
222 if (isAlphaASCII(ch)) {
227 // so we're done with this command.
228 // now we fall through and check this character.
229 scanning_cmd = false;
232 // was the last character a \? If so, then this is something like: \\,
233 // or \$, so we'll just output it. That's probably not always right...
244 scanning_math = true;
248 // we just ignore braces
249 if (ch == '{' || ch == '}') {
254 // we're going to check things that look like commands, so if
255 // this doesn't, just output it.
262 // ok, could be a command of some sort
263 // let's see if it corresponds to some unicode
264 // unicodesymbols has things in the form: \"{u},
265 // whereas we may see things like: \"u. So we'll
266 // look for that and change it, if necessary.
267 static boost::regex const reg("^\\\\\\W\\w");
268 if (boost::regex_search(to_utf8(val), reg)) {
269 val.insert(3, from_ascii("}"));
270 val.insert(2, from_ascii("{"));
273 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem);
274 if (!cnvtd.empty()) {
275 // it did, so we'll take that bit and proceed with what's left
280 // it's a command of some sort
291 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref) const
297 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
303 // This could be made a lot better using the entry_type_
304 // field to customize the output based upon entry type.
306 // Search for all possible "required" fields
307 docstring author = getValueForKey("author", xref);
309 author = getValueForKey("editor", xref);
311 docstring year = getValueForKey("year", xref);
312 docstring title = getValueForKey("title", xref);
313 docstring docLoc = getValueForKey("pages", xref);
314 if (docLoc.empty()) {
315 docLoc = getValueForKey("chapter", xref);
317 docLoc = _("Ch. ") + docLoc;
319 docLoc = _("pp. ") + docLoc;
322 docstring media = getValueForKey("journal", xref);
324 media = getValueForKey("publisher", xref);
326 media = getValueForKey("school", xref);
328 media = getValueForKey("institution");
331 docstring volume = getValueForKey("volume", xref);
333 odocstringstream result;
335 result << author << ", ";
339 result << ", " << media;
341 result << " (" << year << ")";
343 result << ", " << docLoc;
345 docstring const result_str = rtrim(result.str());
346 if (!result_str.empty()) {
347 info_ = convertLaTeXCommands(result_str);
351 // This should never happen (or at least be very unusual!)
352 static docstring e = docstring();
357 //////////////////////////////////////////////////////////////////////
361 //////////////////////////////////////////////////////////////////////
364 // A functor for use with sort, leading to case insensitive sorting
365 class compareNoCase: public binary_function<docstring, docstring, bool>
368 bool operator()(docstring const & s1, docstring const & s2) const {
369 return compare_no_case(s1, s2) < 0;
375 vector<docstring> const BiblioInfo::getKeys() const
377 vector<docstring> bibkeys;
378 BiblioInfo::const_iterator it = begin();
379 for (; it != end(); ++it)
380 bibkeys.push_back(it->first);
381 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
386 vector<docstring> const BiblioInfo::getFields() const
388 vector<docstring> bibfields;
389 set<docstring>::const_iterator it = field_names_.begin();
390 set<docstring>::const_iterator end = field_names_.end();
391 for (; it != end; ++it)
392 bibfields.push_back(*it);
393 sort(bibfields.begin(), bibfields.end());
398 vector<docstring> const BiblioInfo::getEntries() const
400 vector<docstring> bibentries;
401 set<docstring>::const_iterator it = entry_types_.begin();
402 set<docstring>::const_iterator end = entry_types_.end();
403 for (; it != end; ++it)
404 bibentries.push_back(*it);
405 sort(bibentries.begin(), bibentries.end());
410 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
412 BiblioInfo::const_iterator it = find(key);
415 BibTeXInfo const & data = it->second;
416 return data.getAbbreviatedAuthor();
420 docstring const BiblioInfo::getYear(docstring const & key) const
422 BiblioInfo::const_iterator it = find(key);
425 BibTeXInfo const & data = it->second;
426 docstring year = data.getYear();
429 // let's try the crossref
430 docstring const xref = data.getXRef();
432 return _("No year"); // no luck
433 BiblioInfo::const_iterator const xrefit = find(xref);
435 return _("No year"); // no luck again
436 BibTeXInfo const & xref_data = xrefit->second;
437 return xref_data.getYear();
438 return data.getYear();
442 docstring const BiblioInfo::getInfo(docstring const & key) const
444 BiblioInfo::const_iterator it = find(key);
447 BibTeXInfo const & data = it->second;
448 BibTeXInfo const * xrefptr = 0;
449 docstring const xref = data.getXRef();
451 BiblioInfo::const_iterator const xrefit = find(xref);
453 xrefptr = &(xrefit->second);
455 return data.getInfo(xrefptr);
459 vector<docstring> const BiblioInfo::getCiteStrings(
460 docstring const & key, Buffer const & buf) const
462 CiteEngine const engine = buf.params().citeEngine();
463 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
464 return getNumericalStrings(key, buf);
466 return getAuthorYearStrings(key, buf);
470 vector<docstring> const BiblioInfo::getNumericalStrings(
471 docstring const & key, Buffer const & buf) const
474 return vector<docstring>();
476 docstring const author = getAbbreviatedAuthor(key);
477 docstring const year = getYear(key);
478 if (author.empty() || year.empty())
479 return vector<docstring>();
481 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
483 vector<docstring> vec(styles.size());
484 for (size_t i = 0; i != vec.size(); ++i) {
490 str = from_ascii("[#ID]");
494 str = _("Add to bibliography only.");
498 str = author + " [#ID]";
502 str = author + " #ID";
506 str = from_ascii("#ID");
518 str = '(' + year + ')';
529 vector<docstring> const BiblioInfo::getAuthorYearStrings(
530 docstring const & key, Buffer const & buf) const
533 return vector<docstring>();
535 docstring const author = getAbbreviatedAuthor(key);
536 docstring const year = getYear(key);
537 if (author.empty() || year.empty())
538 return vector<docstring>();
540 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
542 vector<docstring> vec(styles.size());
543 for (size_t i = 0; i != vec.size(); ++i) {
548 // jurabib only: Author/Annotator
549 // (i.e. the "before" field, 2nd opt arg)
550 str = author + "/<" + _("before") + '>';
554 str = _("Add to bibliography only.");
558 str = author + " (" + year + ')';
562 str = '(' + author + ", " + year + ')';
566 str = author + ' ' + year ;
570 str = author + ", " + year ;
582 str = '(' + year + ')';
591 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
593 bimap_.insert(info.begin(), info.end());
597 //////////////////////////////////////////////////////////////////////
601 //////////////////////////////////////////////////////////////////////
606 char const * const citeCommands[] = {
607 "cite", "citet", "citep", "citealt", "citealp",
608 "citeauthor", "citeyear", "citeyearpar", "nocite" };
610 unsigned int const nCiteCommands =
611 sizeof(citeCommands) / sizeof(char *);
613 CiteStyle const citeStylesArray[] = {
614 CITE, CITET, CITEP, CITEALT, CITEALP,
615 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
617 unsigned int const nCiteStyles =
618 sizeof(citeStylesArray) / sizeof(CiteStyle);
620 CiteStyle const citeStylesFull[] = {
621 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
623 unsigned int const nCiteStylesFull =
624 sizeof(citeStylesFull) / sizeof(CiteStyle);
626 CiteStyle const citeStylesUCase[] = {
627 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
629 unsigned int const nCiteStylesUCase =
630 sizeof(citeStylesUCase) / sizeof(CiteStyle);
635 CitationStyle citationStyleFromString(string const & command)
641 string cmd = command;
643 s.forceUpperCase = true;
647 size_t const n = cmd.size() - 1;
648 if (cmd != "cite" && cmd[n] == '*') {
650 cmd = cmd.substr(0, n);
653 char const * const * const last = citeCommands + nCiteCommands;
654 char const * const * const ptr = find(citeCommands, last, cmd);
657 size_t idx = ptr - citeCommands;
658 s.style = citeStylesArray[idx];
664 string citationStyleToString(const CitationStyle & s)
666 string cite = citeCommands[s.style];
668 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
669 if (find(citeStylesFull, last, s.style) != last)
673 if (s.forceUpperCase) {
674 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
675 if (find(citeStylesUCase, last, s.style) != last)
682 vector<CiteStyle> citeStyles(CiteEngine engine)
684 unsigned int nStyles = 0;
685 unsigned int start = 0;
692 case ENGINE_NATBIB_AUTHORYEAR:
693 case ENGINE_NATBIB_NUMERICAL:
694 nStyles = nCiteStyles - 1;
698 nStyles = nCiteStyles;
703 vector<CiteStyle> styles(nStyles);
706 for (; i != styles.size(); ++i, ++j)
707 styles[i] = citeStylesArray[j];