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 familyName(docstring const & name)
80 // first we look for a comma, and take the last name to be everything
81 // preceding the right-most one, so that we also get the "jr" part.
82 docstring::size_type idx = name.rfind(',');
83 if (idx != docstring::npos)
84 return ltrim(name.substr(0, idx));
86 // OK, so now we want to look for the last name. We're going to
87 // include the "von" part. This isn't perfect.
88 // Split on spaces, to get various tokens.
89 vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
90 // If we only get two, assume the last one is the last name
91 if (pieces.size() <= 2)
94 // Now we look for the first token that begins with a lower case letter.
95 vector<docstring>::const_iterator it = pieces.begin();
96 vector<docstring>::const_iterator en = pieces.end();
97 for (; it != en; ++it) {
98 if ((*it).size() == 0)
100 char_type const c = (*it)[0];
105 if (it == en) // we never found a "von"
106 return pieces.back();
108 // reconstruct what we need to return
111 for (; it != en; ++it) {
121 docstring const BibTeXInfo::getAbbreviatedAuthor() const
124 docstring const opt = trim(operator[]("label"));
129 split(opt, authors, '(');
133 docstring author = operator[]("author");
134 if (author.empty()) {
135 author = operator[]("editor");
140 // OK, we've got some names. Let's format them.
141 // Try to split the author list on " and "
142 vector<docstring> const authors =
143 getVectorFromString(author, from_ascii(" and "));
145 if (authors.size() == 2)
146 return bformat(_("%1$s and %2$s"),
147 familyName(authors[0]), familyName(authors[1]));
149 if (authors.size() > 2)
150 return bformat(_("%1$s et al."), familyName(authors[0]));
152 return familyName(authors[0]);
156 docstring const BibTeXInfo::getYear() const
159 docstring const opt = trim(operator[]("label"));
164 docstring const tmp = split(opt, authors, '(');
166 split(tmp, year, ')');
170 docstring year = operator[]("year");
179 docstring convertLaTeXCommands(docstring const & str)
184 bool scanning_cmd = false;
185 bool scanning_math = false;
186 bool escaped = false; // used to catch \$, etc.
188 char_type const ch = val[0];
190 // if we're scanning math, we output everything until we
191 // find an unescaped $, at which point we break out.
198 scanning_math = false;
204 // if we're scanning a command name, then we just
205 // discard characters until we hit something that
208 if (isAlphaASCII(ch)) {
213 // so we're done with this command.
214 // now we fall through and check this character.
215 scanning_cmd = false;
218 // was the last character a \? If so, then this is something like: \\,
219 // or \$, so we'll just output it. That's probably not always right...
230 scanning_math = true;
234 // we just ignore braces
235 if (ch == '{' || ch == '}') {
240 // we're going to check things that look like commands, so if
241 // this doesn't, just output it.
248 // ok, could be a command of some sort
249 // let's see if it corresponds to some unicode
250 // unicodesymbols has things in the form: \"{u},
251 // whereas we may see things like: \"u. So we'll
252 // look for that and change it, if necessary.
253 static boost::regex const reg("^\\\\\\W\\w");
254 if (boost::regex_search(to_utf8(val), reg)) {
255 val.insert(3, from_ascii("}"));
256 val.insert(2, from_ascii("{"));
259 docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem);
260 if (!cnvtd.empty()) {
261 // it did, so we'll take that bit and proceed with what's left
266 // it's a command of some sort
277 docstring const BibTeXInfo::getInfo() const
283 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
289 // This could be made a lot better using the entryType
290 // field to customize the output based upon entry type.
292 // Search for all possible "required" fields
293 docstring author = operator[]("author");
295 author = operator[]("editor");
297 docstring year = operator[]("year");
298 docstring title = operator[]("title");
299 docstring docLoc = operator[]("pages");
300 if (docLoc.empty()) {
301 docLoc = operator[]("chapter");
303 docLoc = from_ascii("Ch. ") + docLoc;
305 docLoc = from_ascii("pp. ") + docLoc;
308 docstring media = operator[]("journal");
310 media = operator[]("publisher");
312 media = operator[]("school");
314 media = operator[]("institution");
317 docstring volume = operator[]("volume");
319 odocstringstream result;
321 result << author << ", ";
325 result << ", " << media;
327 result << ", " << year;
329 result << ", " << docLoc;
331 docstring const result_str = rtrim(result.str());
332 if (!result_str.empty()) {
333 info_ = convertLaTeXCommands(result_str);
337 // This should never happen (or at least be very unusual!)
342 //////////////////////////////////////////////////////////////////////
346 //////////////////////////////////////////////////////////////////////
349 // A functor for use with sort, leading to case insensitive sorting
350 class compareNoCase: public binary_function<docstring, docstring, bool>
353 bool operator()(docstring const & s1, docstring const & s2) const {
354 return compare_no_case(s1, s2) < 0;
360 vector<docstring> const BiblioInfo::getKeys() const
362 vector<docstring> bibkeys;
363 BiblioInfo::const_iterator it = begin();
364 for (; it != end(); ++it)
365 bibkeys.push_back(it->first);
366 sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
371 vector<docstring> const BiblioInfo::getFields() const
373 vector<docstring> bibfields;
374 set<docstring>::const_iterator it = field_names_.begin();
375 set<docstring>::const_iterator end = field_names_.end();
376 for (; it != end; ++it)
377 bibfields.push_back(*it);
378 sort(bibfields.begin(), bibfields.end());
383 vector<docstring> const BiblioInfo::getEntries() const
385 vector<docstring> bibentries;
386 set<docstring>::const_iterator it = entry_types_.begin();
387 set<docstring>::const_iterator end = entry_types_.end();
388 for (; it != end; ++it)
389 bibentries.push_back(*it);
390 sort(bibentries.begin(), bibentries.end());
395 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
397 BiblioInfo::const_iterator it = find(key);
400 BibTeXInfo const & data = it->second;
401 return data.getAbbreviatedAuthor();
405 docstring const BiblioInfo::getYear(docstring const & key) const
407 BiblioInfo::const_iterator it = find(key);
410 BibTeXInfo const & data = it->second;
411 return data.getYear();
415 docstring const BiblioInfo::getInfo(docstring const & key) const
417 BiblioInfo::const_iterator it = find(key);
420 BibTeXInfo const & data = it->second;
421 return data.getInfo();
425 vector<docstring> const BiblioInfo::getCiteStrings(
426 docstring const & key, Buffer const & buf) const
428 CiteEngine const engine = buf.params().citeEngine();
429 if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
430 return getNumericalStrings(key, buf);
432 return getAuthorYearStrings(key, buf);
436 vector<docstring> const BiblioInfo::getNumericalStrings(
437 docstring const & key, Buffer const & buf) const
440 return vector<docstring>();
442 docstring const author = getAbbreviatedAuthor(key);
443 docstring const year = getYear(key);
444 if (author.empty() || year.empty())
445 return vector<docstring>();
447 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
449 vector<docstring> vec(styles.size());
450 for (size_t i = 0; i != vec.size(); ++i) {
456 str = from_ascii("[#ID]");
460 str = _("Add to bibliography only.");
464 str = author + " [#ID]";
468 str = author + " #ID";
472 str = from_ascii("#ID");
484 str = '(' + year + ')';
495 vector<docstring> const BiblioInfo::getAuthorYearStrings(
496 docstring const & key, Buffer const & buf) const
499 return vector<docstring>();
501 docstring const author = getAbbreviatedAuthor(key);
502 docstring const year = getYear(key);
503 if (author.empty() || year.empty())
504 return vector<docstring>();
506 vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
508 vector<docstring> vec(styles.size());
509 for (size_t i = 0; i != vec.size(); ++i) {
514 // jurabib only: Author/Annotator
515 // (i.e. the "before" field, 2nd opt arg)
516 str = author + "/<" + _("before") + '>';
520 str = _("Add to bibliography only.");
524 str = author + " (" + year + ')';
528 str = '(' + author + ", " + year + ')';
532 str = author + ' ' + year ;
536 str = author + ", " + year ;
548 str = '(' + year + ')';
557 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
559 bimap_.insert(info.begin(), info.end());
563 //////////////////////////////////////////////////////////////////////
567 //////////////////////////////////////////////////////////////////////
572 char const * const citeCommands[] = {
573 "cite", "citet", "citep", "citealt", "citealp",
574 "citeauthor", "citeyear", "citeyearpar", "nocite" };
576 unsigned int const nCiteCommands =
577 sizeof(citeCommands) / sizeof(char *);
579 CiteStyle const citeStylesArray[] = {
580 CITE, CITET, CITEP, CITEALT, CITEALP,
581 CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
583 unsigned int const nCiteStyles =
584 sizeof(citeStylesArray) / sizeof(CiteStyle);
586 CiteStyle const citeStylesFull[] = {
587 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
589 unsigned int const nCiteStylesFull =
590 sizeof(citeStylesFull) / sizeof(CiteStyle);
592 CiteStyle const citeStylesUCase[] = {
593 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
595 unsigned int const nCiteStylesUCase =
596 sizeof(citeStylesUCase) / sizeof(CiteStyle);
601 CitationStyle citationStyleFromString(string const & command)
607 string cmd = command;
609 s.forceUpperCase = true;
613 size_t const n = cmd.size() - 1;
614 if (cmd != "cite" && cmd[n] == '*') {
616 cmd = cmd.substr(0, n);
619 char const * const * const last = citeCommands + nCiteCommands;
620 char const * const * const ptr = find(citeCommands, last, cmd);
623 size_t idx = ptr - citeCommands;
624 s.style = citeStylesArray[idx];
630 string citationStyleToString(const CitationStyle & s)
632 string cite = citeCommands[s.style];
634 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
635 if (find(citeStylesFull, last, s.style) != last)
639 if (s.forceUpperCase) {
640 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
641 if (find(citeStylesUCase, last, s.style) != last)
648 vector<CiteStyle> citeStyles(CiteEngine engine)
650 unsigned int nStyles = 0;
651 unsigned int start = 0;
658 case ENGINE_NATBIB_AUTHORYEAR:
659 case ENGINE_NATBIB_NUMERICAL:
660 nStyles = nCiteStyles - 1;
664 nStyles = nCiteStyles;
669 vector<CiteStyle> styles(nStyles);
672 for (; i != styles.size(); ++i, ++j)
673 styles[i] = citeStylesArray[j];