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...
135 // exception: output \, as THIN SPACE
137 ret.push_back(0x2009);
148 scanning_math = true;
152 // we just ignore braces
153 if (ch == '{' || ch == '}') {
158 // we're going to check things that look like commands, so if
159 // this doesn't, just output it.
166 // ok, could be a command of some sort
167 // let's see if it corresponds to some unicode
168 // unicodesymbols has things in the form: \"{u},
169 // whereas we may see things like: \"u. So we'll
170 // look for that and change it, if necessary.
171 static boost::regex const reg("^\\\\\\W\\w");
172 if (boost::regex_search(to_utf8(val), reg)) {
173 val.insert(3, from_ascii("}"));
174 val.insert(2, from_ascii("{"));
177 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem,
178 Encodings::TEXT_CMD);
179 if (!cnvtd.empty()) {
180 // it did, so we'll take that bit and proceed with what's left
185 // it's a command of some sort
196 //////////////////////////////////////////////////////////////////////
200 //////////////////////////////////////////////////////////////////////
202 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
203 : is_bibtex_(true), bib_key_(key), entry_type_(type), info_()
207 bool BibTeXInfo::hasField(docstring const & field) const
209 return count(field) == 1;
213 docstring const BibTeXInfo::getAbbreviatedAuthor() const
216 docstring const opt = label();
221 split(opt, authors, '(');
225 docstring author = convertLaTeXCommands(operator[]("author"));
226 if (author.empty()) {
227 author = convertLaTeXCommands(operator[]("editor"));
232 // OK, we've got some names. Let's format them.
233 // Try to split the author list on " and "
234 vector<docstring> const authors =
235 getVectorFromString(author, from_ascii(" and "));
237 if (authors.size() == 2)
238 return bformat(_("%1$s and %2$s"),
239 familyName(authors[0]), familyName(authors[1]));
241 if (authors.size() > 2)
242 return bformat(_("%1$s et al."), familyName(authors[0]));
244 return familyName(authors[0]);
248 docstring const BibTeXInfo::getYear() const
251 return operator[]("year");
253 docstring const opt = label();
258 docstring const tmp = split(opt, authors, '(');
260 split(tmp, year, ')');
265 docstring const BibTeXInfo::getXRef() const
269 return operator[]("crossref");
273 docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref) const
279 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
285 // This could be made a lot better using the entry_type_
286 // field to customize the output based upon entry type.
288 // Search for all possible "required" fields
289 docstring author = getValueForKey("author", xref);
291 author = getValueForKey("editor", xref);
293 docstring year = getValueForKey("year", xref);
294 docstring title = getValueForKey("title", xref);
295 docstring docLoc = getValueForKey("pages", xref);
296 if (docLoc.empty()) {
297 docLoc = getValueForKey("chapter", xref);
299 docLoc = _("Ch. ") + docLoc;
301 docLoc = _("pp. ") + docLoc;
304 docstring media = getValueForKey("journal", xref);
306 media = getValueForKey("publisher", xref);
308 media = getValueForKey("school", xref);
310 media = getValueForKey("institution");
313 docstring volume = getValueForKey("volume", xref);
315 odocstringstream result;
317 result << author << ", ";
321 result << ", " << media;
323 result << " (" << year << ")";
325 result << ", " << docLoc;
327 docstring const result_str = rtrim(result.str());
328 if (!result_str.empty()) {
329 info_ = convertLaTeXCommands(result_str);
333 // This should never happen (or at least be very unusual!)
334 static docstring e = docstring();
339 docstring const & BibTeXInfo::operator[](docstring const & field) const
341 BibTeXInfo::const_iterator it = find(field);
344 static docstring const empty_value = docstring();
349 docstring const & BibTeXInfo::operator[](string const & field) const
351 return operator[](from_ascii(field));
355 docstring BibTeXInfo::getValueForKey(string const & key,
356 BibTeXInfo const * const xref) const
358 docstring const ret = operator[](key);
359 if (!ret.empty() || !xref)
365 //////////////////////////////////////////////////////////////////////
369 //////////////////////////////////////////////////////////////////////
372 // A functor for use with sort, leading to case insensitive sorting
373 class compareNoCase: public binary_function<docstring, docstring, bool>
376 bool operator()(docstring const & s1, docstring const & s2) const {
377 return compare_no_case(s1, s2) < 0;
383 vector<docstring> const BiblioInfo::getKeys() const
385 vector<docstring> bibkeys;
386 BiblioInfo::const_iterator it = begin();
387 for (; it != end(); ++it)
388 bibkeys.push_back(it->first);
389 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
394 vector<docstring> const BiblioInfo::getFields() const
396 vector<docstring> bibfields;
397 set<docstring>::const_iterator it = field_names_.begin();
398 set<docstring>::const_iterator end = field_names_.end();
399 for (; it != end; ++it)
400 bibfields.push_back(*it);
401 sort(bibfields.begin(), bibfields.end());
406 vector<docstring> const BiblioInfo::getEntries() const
408 vector<docstring> bibentries;
409 set<docstring>::const_iterator it = entry_types_.begin();
410 set<docstring>::const_iterator end = entry_types_.end();
411 for (; it != end; ++it)
412 bibentries.push_back(*it);
413 sort(bibentries.begin(), bibentries.end());
418 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
420 BiblioInfo::const_iterator it = find(key);
423 BibTeXInfo const & data = it->second;
424 return data.getAbbreviatedAuthor();
428 docstring const BiblioInfo::getYear(docstring const & key) const
430 BiblioInfo::const_iterator it = find(key);
433 BibTeXInfo const & data = it->second;
434 docstring year = data.getYear();
437 // let's try the crossref
438 docstring const xref = data.getXRef();
440 return _("No year"); // no luck
441 BiblioInfo::const_iterator const xrefit = find(xref);
443 return _("No year"); // no luck again
444 BibTeXInfo const & xref_data = xrefit->second;
445 return xref_data.getYear();
446 return data.getYear();
450 docstring const BiblioInfo::getInfo(docstring const & key) const
452 BiblioInfo::const_iterator it = find(key);
455 BibTeXInfo const & data = it->second;
456 BibTeXInfo const * xrefptr = 0;
457 docstring const xref = data.getXRef();
459 BiblioInfo::const_iterator const xrefit = find(xref);
461 xrefptr = &(xrefit->second);
463 return data.getInfo(xrefptr);
467 vector<docstring> const BiblioInfo::getCiteStrings(
468 docstring const & key, Buffer const & buf) const
470 CiteEngine const engine = buf.params().citeEngine();
471 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
472 return getNumericalStrings(key, buf);
474 return getAuthorYearStrings(key, buf);
478 vector<docstring> const BiblioInfo::getNumericalStrings(
479 docstring const & key, Buffer const & buf) const
482 return vector<docstring>();
484 docstring const author = getAbbreviatedAuthor(key);
485 docstring const year = getYear(key);
486 if (author.empty() || year.empty())
487 return vector<docstring>();
489 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
491 vector<docstring> vec(styles.size());
492 for (size_t i = 0; i != vec.size(); ++i) {
498 str = from_ascii("[#ID]");
502 str = _("Add to bibliography only.");
506 str = author + " [#ID]";
510 str = author + " #ID";
514 str = from_ascii("#ID");
526 str = '(' + year + ')';
537 vector<docstring> const BiblioInfo::getAuthorYearStrings(
538 docstring const & key, Buffer const & buf) const
541 return vector<docstring>();
543 docstring const author = getAbbreviatedAuthor(key);
544 docstring const year = getYear(key);
545 if (author.empty() || year.empty())
546 return vector<docstring>();
548 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
550 vector<docstring> vec(styles.size());
551 for (size_t i = 0; i != vec.size(); ++i) {
556 // jurabib only: Author/Annotator
557 // (i.e. the "before" field, 2nd opt arg)
558 str = author + "/<" + _("before") + '>';
562 str = _("Add to bibliography only.");
566 str = author + " (" + year + ')';
570 str = '(' + author + ", " + year + ')';
574 str = author + ' ' + year ;
578 str = author + ", " + year ;
590 str = '(' + year + ')';
599 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
601 bimap_.insert(info.begin(), info.end());
605 //////////////////////////////////////////////////////////////////////
609 //////////////////////////////////////////////////////////////////////
614 char const * const citeCommands[] = {
615 "cite", "citet", "citep", "citealt", "citealp",
616 "citeauthor", "citeyear", "citeyearpar", "nocite" };
618 unsigned int const nCiteCommands =
619 sizeof(citeCommands) / sizeof(char *);
621 CiteStyle const citeStylesArray[] = {
622 CITE, CITET, CITEP, CITEALT, CITEALP,
623 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
625 unsigned int const nCiteStyles =
626 sizeof(citeStylesArray) / sizeof(CiteStyle);
628 CiteStyle const citeStylesFull[] = {
629 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
631 unsigned int const nCiteStylesFull =
632 sizeof(citeStylesFull) / sizeof(CiteStyle);
634 CiteStyle const citeStylesUCase[] = {
635 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
637 unsigned int const nCiteStylesUCase =
638 sizeof(citeStylesUCase) / sizeof(CiteStyle);
643 CitationStyle citationStyleFromString(string const & command)
649 string cmd = command;
651 s.forceUpperCase = true;
655 size_t const n = cmd.size() - 1;
656 if (cmd != "cite" && cmd[n] == '*') {
658 cmd = cmd.substr(0, n);
661 char const * const * const last = citeCommands + nCiteCommands;
662 char const * const * const ptr = find(citeCommands, last, cmd);
665 size_t idx = ptr - citeCommands;
666 s.style = citeStylesArray[idx];
672 string citationStyleToString(const CitationStyle & s)
674 string cite = citeCommands[s.style];
676 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
677 if (std::find(citeStylesFull, last, s.style) != last)
681 if (s.forceUpperCase) {
682 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
683 if (std::find(citeStylesUCase, last, s.style) != last)
690 vector<CiteStyle> citeStyles(CiteEngine engine)
692 unsigned int nStyles = 0;
693 unsigned int start = 0;
700 case ENGINE_NATBIB_AUTHORYEAR:
701 case ENGINE_NATBIB_NUMERICAL:
702 nStyles = nCiteStyles - 1;
706 nStyles = nCiteStyles;
711 vector<CiteStyle> styles(nStyles);
714 for (; i != styles.size(); ++i, ++j)
715 styles[i] = citeStylesArray[j];