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;
44 // gets the "family name" from an author-type string
45 docstring familyName(docstring const & name)
50 // first we look for a comma, and take the last name to be everything
51 // preceding the right-most one, so that we also get the "jr" part.
52 docstring::size_type idx = name.rfind(',');
53 if (idx != docstring::npos)
54 return ltrim(name.substr(0, idx));
56 // OK, so now we want to look for the last name. We're going to
57 // include the "von" part. This isn't perfect.
58 // Split on spaces, to get various tokens.
59 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
60 // If we only get two, assume the last one is the last name
61 if (pieces.size() <= 2)
64 // Now we look for the first token that begins with a lower case letter.
65 vector<docstring>::const_iterator it = pieces.begin();
66 vector<docstring>::const_iterator en = pieces.end();
67 for (; it != en; ++it) {
68 if ((*it).size() == 0)
70 char_type const c = (*it)[0];
75 if (it == en) // we never found a "von"
78 // reconstruct what we need to return
81 for (; it != en; ++it) {
91 // converts a string containing LaTeX commands into unicode
93 docstring convertLaTeXCommands(docstring const & str)
98 bool scanning_cmd = false;
99 bool scanning_math = false;
100 bool escaped = false; // used to catch \$, etc.
102 char_type const ch = val[0];
104 // if we're scanning math, we output everything until we
105 // find an unescaped $, at which point we break out.
112 scanning_math = false;
118 // if we're scanning a command name, then we just
119 // discard characters until we hit something that
122 if (isAlphaASCII(ch)) {
127 // so we're done with this command.
128 // now we fall through and check this character.
129 scanning_cmd = false;
132 // was the last character a \? If so, then this is something like: \\,
133 // or \$, so we'll just output it. That's probably not always right...
144 scanning_math = true;
148 // we just ignore braces
149 if (ch == '{' || ch == '}') {
154 // we're going to check things that look like commands, so if
155 // this doesn't, just output it.
162 // ok, could be a command of some sort
163 // let's see if it corresponds to some unicode
164 // unicodesymbols has things in the form: \"{u},
165 // whereas we may see things like: \"u. So we'll
166 // look for that and change it, if necessary.
167 static boost::regex const reg("^\\\\\\W\\w");
168 if (boost::regex_search(to_utf8(val), reg)) {
169 val.insert(3, from_ascii("}"));
170 val.insert(2, from_ascii("{"));
173 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem);
174 if (!cnvtd.empty()) {
175 // it did, so we'll take that bit and proceed with what's left
180 // it's a command of some sort
191 //////////////////////////////////////////////////////////////////////
195 //////////////////////////////////////////////////////////////////////
197 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
198 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_()
202 bool BibTeXInfo::hasField(docstring const & field) const
204 return count(field) == 1;
208 docstring const BibTeXInfo::getAbbreviatedAuthor() const
211 docstring const opt = trim(operator[]("label"));
216 split(opt, authors, '(');
220 docstring author = operator[]("author");
221 if (author.empty()) {
222 author = operator[]("editor");
227 // OK, we've got some names. Let's format them.
228 // Try to split the author list on " and "
229 vector<docstring> const authors =
230 getVectorFromString(author, from_ascii(" and "));
232 if (authors.size() == 2)
233 return bformat(_("%1$s and %2$s"),
234 familyName(authors[0]), familyName(authors[1]));
236 if (authors.size() > 2)
237 return bformat(_("%1$s et al."), familyName(authors[0]));
239 return familyName(authors[0]);
243 docstring const BibTeXInfo::getYear() const
246 return operator[]("year");
248 docstring const opt = trim(operator[]("label"));
253 docstring const tmp = split(opt, authors, '(');
255 split(tmp, year, ')');
260 docstring const BibTeXInfo::getXRef() const
264 return operator[]("crossref");
268 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref) const
274 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
280 // This could be made a lot better using the entry_type_
281 // field to customize the output based upon entry type.
283 // Search for all possible "required" fields
284 docstring author = getValueForKey("author", xref);
286 author = getValueForKey("editor", xref);
288 docstring year = getValueForKey("year", xref);
289 docstring title = getValueForKey("title", xref);
290 docstring docLoc = getValueForKey("pages", xref);
291 if (docLoc.empty()) {
292 docLoc = getValueForKey("chapter", xref);
294 docLoc = _("Ch. ") + docLoc;
296 docLoc = _("pp. ") + docLoc;
299 docstring media = getValueForKey("journal", xref);
301 media = getValueForKey("publisher", xref);
303 media = getValueForKey("school", xref);
305 media = getValueForKey("institution");
308 docstring volume = getValueForKey("volume", xref);
310 odocstringstream result;
312 result << author << ", ";
316 result << ", " << media;
318 result << " (" << year << ")";
320 result << ", " << docLoc;
322 docstring const result_str = rtrim(result.str());
323 if (!result_str.empty()) {
324 info_ = convertLaTeXCommands(result_str);
328 // This should never happen (or at least be very unusual!)
329 static docstring e = docstring();
334 docstring const & BibTeXInfo::operator[](docstring const & field) const
336 BibTeXInfo::const_iterator it = find(field);
339 static docstring const empty_value = docstring();
344 docstring const & BibTeXInfo::operator[](string const & field) const
346 return operator[](from_ascii(field));
350 docstring BibTeXInfo::getValueForKey(string const & key,
351 BibTeXInfo const * const xref) const
353 docstring const ret = operator[](key);
354 if (!ret.empty() || !xref)
360 //////////////////////////////////////////////////////////////////////
364 //////////////////////////////////////////////////////////////////////
367 // A functor for use with sort, leading to case insensitive sorting
368 class compareNoCase: public binary_function<docstring, docstring, bool>
371 bool operator()(docstring const & s1, docstring const & s2) const {
372 return compare_no_case(s1, s2) < 0;
378 vector<docstring> const BiblioInfo::getKeys() const
380 vector<docstring> bibkeys;
381 BiblioInfo::const_iterator it = begin();
382 for (; it != end(); ++it)
383 bibkeys.push_back(it->first);
384 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
389 vector<docstring> const BiblioInfo::getFields() const
391 vector<docstring> bibfields;
392 set<docstring>::const_iterator it = field_names_.begin();
393 set<docstring>::const_iterator end = field_names_.end();
394 for (; it != end; ++it)
395 bibfields.push_back(*it);
396 sort(bibfields.begin(), bibfields.end());
401 vector<docstring> const BiblioInfo::getEntries() const
403 vector<docstring> bibentries;
404 set<docstring>::const_iterator it = entry_types_.begin();
405 set<docstring>::const_iterator end = entry_types_.end();
406 for (; it != end; ++it)
407 bibentries.push_back(*it);
408 sort(bibentries.begin(), bibentries.end());
413 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
415 BiblioInfo::const_iterator it = find(key);
418 BibTeXInfo const & data = it->second;
419 return data.getAbbreviatedAuthor();
423 docstring const BiblioInfo::getYear(docstring const & key) const
425 BiblioInfo::const_iterator it = find(key);
428 BibTeXInfo const & data = it->second;
429 docstring year = data.getYear();
432 // let's try the crossref
433 docstring const xref = data.getXRef();
435 return _("No year"); // no luck
436 BiblioInfo::const_iterator const xrefit = find(xref);
438 return _("No year"); // no luck again
439 BibTeXInfo const & xref_data = xrefit->second;
440 return xref_data.getYear();
441 return data.getYear();
445 docstring const BiblioInfo::getInfo(docstring const & key) const
447 BiblioInfo::const_iterator it = find(key);
450 BibTeXInfo const & data = it->second;
451 BibTeXInfo const * xrefptr = 0;
452 docstring const xref = data.getXRef();
454 BiblioInfo::const_iterator const xrefit = find(xref);
456 xrefptr = &(xrefit->second);
458 return data.getInfo(xrefptr);
462 vector<docstring> const BiblioInfo::getCiteStrings(
463 docstring const & key, Buffer const & buf) const
465 CiteEngine const engine = buf.params().citeEngine();
466 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
467 return getNumericalStrings(key, buf);
469 return getAuthorYearStrings(key, buf);
473 vector<docstring> const BiblioInfo::getNumericalStrings(
474 docstring const & key, Buffer const & buf) const
477 return vector<docstring>();
479 docstring const author = getAbbreviatedAuthor(key);
480 docstring const year = getYear(key);
481 if (author.empty() || year.empty())
482 return vector<docstring>();
484 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
486 vector<docstring> vec(styles.size());
487 for (size_t i = 0; i != vec.size(); ++i) {
493 str = from_ascii("[#ID]");
497 str = _("Add to bibliography only.");
501 str = author + " [#ID]";
505 str = author + " #ID";
509 str = from_ascii("#ID");
521 str = '(' + year + ')';
532 vector<docstring> const BiblioInfo::getAuthorYearStrings(
533 docstring const & key, Buffer const & buf) const
536 return vector<docstring>();
538 docstring const author = getAbbreviatedAuthor(key);
539 docstring const year = getYear(key);
540 if (author.empty() || year.empty())
541 return vector<docstring>();
543 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
545 vector<docstring> vec(styles.size());
546 for (size_t i = 0; i != vec.size(); ++i) {
551 // jurabib only: Author/Annotator
552 // (i.e. the "before" field, 2nd opt arg)
553 str = author + "/<" + _("before") + '>';
557 str = _("Add to bibliography only.");
561 str = author + " (" + year + ')';
565 str = '(' + author + ", " + year + ')';
569 str = author + ' ' + year ;
573 str = author + ", " + year ;
585 str = '(' + year + ')';
594 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
596 bimap_.insert(info.begin(), info.end());
600 //////////////////////////////////////////////////////////////////////
604 //////////////////////////////////////////////////////////////////////
609 char const * const citeCommands[] = {
610 "cite", "citet", "citep", "citealt", "citealp",
611 "citeauthor", "citeyear", "citeyearpar", "nocite" };
613 unsigned int const nCiteCommands =
614 sizeof(citeCommands) / sizeof(char *);
616 CiteStyle const citeStylesArray[] = {
617 CITE, CITET, CITEP, CITEALT, CITEALP,
618 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
620 unsigned int const nCiteStyles =
621 sizeof(citeStylesArray) / sizeof(CiteStyle);
623 CiteStyle const citeStylesFull[] = {
624 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
626 unsigned int const nCiteStylesFull =
627 sizeof(citeStylesFull) / sizeof(CiteStyle);
629 CiteStyle const citeStylesUCase[] = {
630 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
632 unsigned int const nCiteStylesUCase =
633 sizeof(citeStylesUCase) / sizeof(CiteStyle);
638 CitationStyle citationStyleFromString(string const & command)
644 string cmd = command;
646 s.forceUpperCase = true;
650 size_t const n = cmd.size() - 1;
651 if (cmd != "cite" && cmd[n] == '*') {
653 cmd = cmd.substr(0, n);
656 char const * const * const last = citeCommands + nCiteCommands;
657 char const * const * const ptr = find(citeCommands, last, cmd);
660 size_t idx = ptr - citeCommands;
661 s.style = citeStylesArray[idx];
667 string citationStyleToString(const CitationStyle & s)
669 string cite = citeCommands[s.style];
671 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
672 if (find(citeStylesFull, last, s.style) != last)
676 if (s.forceUpperCase) {
677 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
678 if (find(citeStylesUCase, last, s.style) != last)
685 vector<CiteStyle> citeStyles(CiteEngine engine)
687 unsigned int nStyles = 0;
688 unsigned int start = 0;
695 case ENGINE_NATBIB_AUTHORYEAR:
696 case ENGINE_NATBIB_NUMERICAL:
697 nStyles = nCiteStyles - 1;
701 nStyles = nCiteStyles;
706 vector<CiteStyle> styles(nStyles);
709 for (; i != styles.size(); ++i, ++j)
710 styles[i] = citeStylesArray[j];