]> git.lyx.org Git - lyx.git/blobdiff - src/BiblioInfo.cpp
Improve info display for biblatex databases
[lyx.git] / src / BiblioInfo.cpp
index d306c87e5f149cfb254327fb3748f2a71aac1c89..c85eabd5e75ee5ddc605d41a65655d8222d3b38f 100644 (file)
@@ -255,7 +255,8 @@ BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
 {}
 
 
-docstring const BibTeXInfo::getAbbreviatedAuthor(bool jurabib_style) const
+docstring const BibTeXInfo::getAbbreviatedAuthor(
+    Buffer const * buf, bool jurabib_style) const
 {
        if (!is_bibtex_) {
                docstring const opt = label();
@@ -296,37 +297,45 @@ docstring const BibTeXInfo::getAbbreviatedAuthor(bool jurabib_style) const
 
        docstring retval = familyName(authors[0]);
 
-       if (authors.size() == 2 && authors[1] != "others")
-               retval = bformat(from_ascii("%1$s and %2$s"),
-                       familyName(authors[0]), familyName(authors[1]));
-
-       if (authors.size() >= 2)
-               retval = bformat(from_ascii("%1$s et al."),
-                       familyName(authors[0]));
+       if (authors.size() == 2 && authors[1] != "others") {
+               docstring const dformat = buf ? 
+                       buf->B_("%1$s and %2$s") : from_ascii("%1$s and %2$s");
+               retval = bformat(dformat, familyName(authors[0]), familyName(authors[1]));
+       } else if (authors.size() >= 2) {
+               // we get here either if the author list is longer than two names
+               // or if the second 'name' is "others". we do the same thing either
+               // way.
+               docstring const dformat = buf ? 
+                       buf->B_("%1$s et al.") : from_ascii("%1$s et al.");
+               retval = bformat(dformat, familyName(authors[0]));
+       }
 
        return convertLaTeXCommands(retval);
 }
 
 
-docstring const BibTeXInfo::getAbbreviatedAuthor(Buffer const & buf, bool jurabib_style) const
-{
-       docstring const author = getAbbreviatedAuthor(jurabib_style);
-       if (!is_bibtex_)
-               return author;
-       vector<docstring> const authors = getVectorFromString(author, from_ascii(" and "));
-       if (authors.size() == 2)
-               return bformat(buf.B_("%1$s and %2$s"), authors[0], authors[1]);
-       docstring::size_type const idx = author.rfind(from_ascii(" et al."));
-       if (idx != docstring::npos)
-               return bformat(buf.B_("%1$s et al."), author.substr(0, idx));
-       return author;
-}
-
-
 docstring const BibTeXInfo::getYear() const
 {
-       if (is_bibtex_)
-               return operator[]("year");
+       if (is_bibtex_) {
+               // first try legacy year field
+               docstring year = operator[]("year");
+               if (!year.empty())
+                       return year;
+               // now try biblatex's date field
+               year = operator[]("date");
+               // Format is [-]YYYY-MM-DD*/[-]YYYY-MM-DD*
+               // We only want the years.
+               static regex const yreg("[-]?([\\d]{4}).*");
+               static regex const ereg(".*/([\\d]{4}).*");
+               smatch sm;
+               string const date = to_utf8(year);
+               regex_match(date, sm, yreg);
+               year = from_ascii(sm[1]);
+               // check for an endyear
+               if (regex_match(date, sm, ereg))
+                       year += char_type(0x2013) + from_ascii(sm[1]);
+               return year;
+       }
 
        docstring const opt = label();
        if (opt.empty())
@@ -720,12 +729,12 @@ docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
                        ret = cite_number_;
                else if (key == "abbrvauthor")
                        // Special key to provide abbreviated author names.
-                       ret = getAbbreviatedAuthor(buf, false);
+                       ret = getAbbreviatedAuthor(&buf, false);
                else if (key == "shortauthor")
                        // When shortauthor is not defined, jurabib automatically
                        // provides jurabib-style abbreviated author names. We do
                        // this as well.
-                       ret = getAbbreviatedAuthor(buf, true);
+                       ret = getAbbreviatedAuthor(&buf, true);
                else if (key == "shorttitle") {
                        // When shorttitle is not defined, jurabib uses for `article'
                        // and `periodical' entries the form `journal volume [year]'
@@ -756,8 +765,7 @@ docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
                ret = html::cleanAttr(ret);
 
        // make sure it is not too big
-       if (ret.size() > maxsize)
-               ret = ret.substr(0, maxsize - 3) + from_ascii("...");
+       support::truncateWithEllipsis(ret, maxsize);
        return ret;
 }
 
@@ -823,7 +831,7 @@ docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key, Buffer c
        if (it == end())
                return docstring();
        BibTeXInfo const & data = it->second;
-       return data.getAbbreviatedAuthor(buf, false);
+       return data.getAbbreviatedAuthor(&buf, false);
 }
 
 
@@ -928,14 +936,9 @@ docstring const BiblioInfo::getLabel(vector<docstring> keys,
                        before, after, dialog, key + 1 != ken);
        }
 
-       if (ret.size() > max_size) {
-               ret.resize(max_size - 3);
-               ret += "...";
-       } else if (too_many_keys) {
-               if (ret.size() > max_size - 3)
-                       ret.resize(max_size - 3);
-               ret += "...";
-       }
+       if (too_many_keys)
+               ret.push_back(0x2026);//HORIZONTAL ELLIPSIS
+       support::truncateWithEllipsis(ret, max_size);
        return ret;
 }
 
@@ -1066,6 +1069,11 @@ void BiblioInfo::makeCitationLabels(Buffer const & buf)
                        docstring const num = convert<docstring>(++keynumber);
                        entry.setCiteNumber(num);
                } else {
+                       // coverity complains about our derefercing the iterator last,
+                       // which was not initialized above. but it does get initialized
+                       // after the first time through the loop, which is the point of
+                       // the first test.
+                       // coverity[FORWARD_NULL]
                        if (it != cited_entries_.begin()
                            && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
                            // we access the year via getYear() so as to get it from the xref,
@@ -1099,7 +1107,7 @@ void BiblioInfo::makeCitationLabels(Buffer const & buf)
                if (numbers) {
                        entry.label(entry.citeNumber());
                } else {
-                       docstring const auth = entry.getAbbreviatedAuthor(buf, false);
+                       docstring const auth = entry.getAbbreviatedAuthor(&buf, false);
                        // we do it this way so as to access the xref, if necessary
                        // note that this also gives us the modifier
                        docstring const year = getYear(*it, buf, true);
@@ -1146,7 +1154,7 @@ string citationStyleToString(const CitationStyle & cs)
 {
        string cmd = cs.cmd;
        if (cs.forceUpperCase)
-               cmd[0] = 'C';
+               cmd[0] = uppercase(cmd[0]);
        if (cs.fullAuthorList)
                cmd += '*';
        return cmd;