]> git.lyx.org Git - features.git/blob - src/BiblioInfo.cpp
Improve info display for biblatex databases, part III
[features.git] / src / BiblioInfo.cpp
1 /**
2  * \file BiblioInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Herbert Voß
8  * \author Richard Heck
9  * \author Julien Rioux
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "BiblioInfo.h"
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "buffer_funcs.h"
20 #include "Encoding.h"
21 #include "InsetIterator.h"
22 #include "Language.h"
23 #include "output_xhtml.h"
24 #include "Paragraph.h"
25 #include "TextClass.h"
26 #include "TocBackend.h"
27
28 #include "support/convert.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
33 #include "support/lstrings.h"
34 #include "support/regex.h"
35 #include "support/textutils.h"
36
37 #include <set>
38
39 using namespace std;
40 using namespace lyx::support;
41
42
43 namespace lyx {
44
45 namespace {
46
47 // gets the "family name" from an author-type string
48 docstring familyName(docstring const & name)
49 {
50         if (name.empty())
51                 return docstring();
52
53         // first we look for a comma, and take the last name to be everything
54         // preceding the right-most one, so that we also get the "jr" part.
55         docstring::size_type idx = name.rfind(',');
56         if (idx != docstring::npos)
57                 return ltrim(name.substr(0, idx));
58
59         // OK, so now we want to look for the last name. We're going to
60         // include the "von" part. This isn't perfect.
61         // Split on spaces, to get various tokens.
62         vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
63         // If we only get two, assume the last one is the last name
64         if (pieces.size() <= 2)
65                 return pieces.back();
66
67         // Now we look for the first token that begins with a lower case letter.
68         vector<docstring>::const_iterator it = pieces.begin();
69         vector<docstring>::const_iterator en = pieces.end();
70         for (; it != en; ++it) {
71                 if ((*it).empty())
72                         continue;
73                 char_type const c = (*it)[0];
74                 if (isLower(c))
75                         break;
76         }
77
78         if (it == en) // we never found a "von"
79                 return pieces.back();
80
81         // reconstruct what we need to return
82         docstring retval;
83         bool first = true;
84         for (; it != en; ++it) {
85                 if (!first)
86                         retval += " ";
87                 else
88                         first = false;
89                 retval += *it;
90         }
91         return retval;
92 }
93
94
95 // converts a string containing LaTeX commands into unicode
96 // for display.
97 docstring convertLaTeXCommands(docstring const & str)
98 {
99         docstring val = str;
100         docstring ret;
101
102         bool scanning_cmd = false;
103         bool scanning_math = false;
104         bool escaped = false; // used to catch \$, etc.
105         while (!val.empty()) {
106                 char_type const ch = val[0];
107
108                 // if we're scanning math, we output everything until we
109                 // find an unescaped $, at which point we break out.
110                 if (scanning_math) {
111                         if (escaped)
112                                 escaped = false;
113                         else if (ch == '\\')
114                                 escaped = true;
115                         else if (ch == '$')
116                                 scanning_math = false;
117                         ret += ch;
118                         val = val.substr(1);
119                         continue;
120                 }
121
122                 // if we're scanning a command name, then we just
123                 // discard characters until we hit something that
124                 // isn't alpha.
125                 if (scanning_cmd) {
126                         if (isAlphaASCII(ch)) {
127                                 val = val.substr(1);
128                                 escaped = false;
129                                 continue;
130                         }
131                         // so we're done with this command.
132                         // now we fall through and check this character.
133                         scanning_cmd = false;
134                 }
135
136                 // was the last character a \? If so, then this is something like:
137                 // \\ or \$, so we'll just output it. That's probably not always right...
138                 if (escaped) {
139                         // exception: output \, as THIN SPACE
140                         if (ch == ',')
141                                 ret.push_back(0x2009);
142                         else
143                                 ret += ch;
144                         val = val.substr(1);
145                         escaped = false;
146                         continue;
147                 }
148
149                 if (ch == '$') {
150                         ret += ch;
151                         val = val.substr(1);
152                         scanning_math = true;
153                         continue;
154                 }
155
156                 // we just ignore braces
157                 if (ch == '{' || ch == '}') {
158                         val = val.substr(1);
159                         continue;
160                 }
161
162                 // we're going to check things that look like commands, so if
163                 // this doesn't, just output it.
164                 if (ch != '\\') {
165                         ret += ch;
166                         val = val.substr(1);
167                         continue;
168                 }
169
170                 // ok, could be a command of some sort
171                 // let's see if it corresponds to some unicode
172                 // unicodesymbols has things in the form: \"{u},
173                 // whereas we may see things like: \"u. So we'll
174                 // look for that and change it, if necessary.
175                 // FIXME: This is a sort of mini-tex2lyx.
176                 //        Use the real tex2lyx instead!
177                 static lyx::regex const reg("^\\\\\\W\\w");
178                 if (lyx::regex_search(to_utf8(val), reg)) {
179                         val.insert(3, from_ascii("}"));
180                         val.insert(2, from_ascii("{"));
181                 }
182                 bool termination;
183                 docstring rem;
184                 docstring const cnvtd = Encodings::fromLaTeXCommand(val,
185                                 Encodings::TEXT_CMD, termination, rem);
186                 if (!cnvtd.empty()) {
187                         // it did, so we'll take that bit and proceed with what's left
188                         ret += cnvtd;
189                         val = rem;
190                         continue;
191                 }
192                 // it's a command of some sort
193                 scanning_cmd = true;
194                 escaped = true;
195                 val = val.substr(1);
196         }
197         return ret;
198 }
199
200
201 // Escape '<' and '>' and remove richtext markers (e.g. {!this is richtext!}) from a string.
202 docstring processRichtext(docstring const & str, bool richtext)
203 {
204         docstring val = str;
205         docstring ret;
206
207         bool scanning_rich = false;
208         while (!val.empty()) {
209                 char_type const ch = val[0];
210                 if (ch == '{' && val.size() > 1 && val[1] == '!') {
211                         // beginning of rich text
212                         scanning_rich = true;
213                         val = val.substr(2);
214                         continue;
215                 }
216                 if (scanning_rich && ch == '!' && val.size() > 1 && val[1] == '}') {
217                         // end of rich text
218                         scanning_rich = false;
219                         val = val.substr(2);
220                         continue;
221                 }
222                 if (richtext) {
223                         if (scanning_rich)
224                                 ret += ch;
225                         else {
226                                 // we need to escape '<' and '>'
227                                 if (ch == '<')
228                                         ret += "&lt;";
229                                 else if (ch == '>')
230                                         ret += "&gt;";
231                                 else
232                                         ret += ch;
233                         }
234                 } else if (!scanning_rich /* && !richtext */)
235                         ret += ch;
236                 // else the character is discarded, which will happen only if
237                 // richtext == false and we are scanning rich text
238                 val = val.substr(1);
239         }
240         return ret;
241 }
242
243 } // anon namespace
244
245
246 //////////////////////////////////////////////////////////////////////
247 //
248 // BibTeXInfo
249 //
250 //////////////////////////////////////////////////////////////////////
251
252 BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
253         : is_bibtex_(true), bib_key_(key), entry_type_(type), info_(),
254           modifier_(0)
255 {}
256
257
258 docstring const BibTeXInfo::getAbbreviatedAuthor(
259     Buffer const * buf, bool jurabib_style) const
260 {
261         if (!is_bibtex_) {
262                 docstring const opt = label();
263                 if (opt.empty())
264                         return docstring();
265
266                 docstring authors;
267                 docstring const remainder = trim(split(opt, authors, '('));
268                 if (remainder.empty())
269                         // in this case, we didn't find a "(",
270                         // so we don't have author (year)
271                         return docstring();
272                 return authors;
273         }
274
275         docstring author = operator[]("author");
276         if (author.empty()) {
277                 author = operator[]("editor");
278                 if (author.empty())
279                         return author;
280         }
281
282         // FIXME Move this to a separate routine that can
283         // be called from elsewhere.
284         //
285         // OK, we've got some names. Let's format them.
286         // Try to split the author list on " and "
287         vector<docstring> const authors =
288                 getVectorFromString(author, from_ascii(" and "));
289
290         if (jurabib_style && (authors.size() == 2 || authors.size() == 3)) {
291                 docstring shortauthor = familyName(authors[0])
292                         + "/" + familyName(authors[1]);
293                 if (authors.size() == 3)
294                         shortauthor += "/" + familyName(authors[2]);
295                 return convertLaTeXCommands(shortauthor);
296         }
297
298         docstring retval = familyName(authors[0]);
299
300         if (authors.size() == 2 && authors[1] != "others") {
301                 docstring const dformat = buf ? 
302                         buf->B_("%1$s and %2$s") : from_ascii("%1$s and %2$s");
303                 retval = bformat(dformat, familyName(authors[0]), familyName(authors[1]));
304         } else if (authors.size() >= 2) {
305                 // we get here either if the author list is longer than two names
306                 // or if the second 'name' is "others". we do the same thing either
307                 // way.
308                 docstring const dformat = buf ? 
309                         buf->B_("%1$s et al.") : from_ascii("%1$s et al.");
310                 retval = bformat(dformat, familyName(authors[0]));
311         }
312
313         return convertLaTeXCommands(retval);
314 }
315
316
317 docstring const BibTeXInfo::getYear() const
318 {
319         if (is_bibtex_) {
320                 // first try legacy year field
321                 docstring year = operator[]("year");
322                 if (!year.empty())
323                         return year;
324                 // now try biblatex's date field
325                 year = operator[]("date");
326                 // Format is [-]YYYY-MM-DD*/[-]YYYY-MM-DD*
327                 // We only want the years.
328                 static regex const yreg("[-]?([\\d]{4}).*");
329                 static regex const ereg(".*/[-]?([\\d]{4}).*");
330                 smatch sm;
331                 string const date = to_utf8(year);
332                 regex_match(date, sm, yreg);
333                 year = from_ascii(sm[1]);
334                 // check for an endyear
335                 if (regex_match(date, sm, ereg))
336                         year += char_type(0x2013) + from_ascii(sm[1]);
337                 return year;
338         }
339
340         docstring const opt = label();
341         if (opt.empty())
342                 return docstring();
343
344         docstring authors;
345         docstring tmp = split(opt, authors, '(');
346         if (tmp.empty())
347                 // we don't have author (year)
348                 return docstring();
349         docstring year;
350         tmp = split(tmp, year, ')');
351         return year;
352 }
353
354
355 namespace {
356
357 docstring parseOptions(docstring const & format, string & optkey,
358                     docstring & ifpart, docstring & elsepart);
359
360 // Calls parseOptions to deal with an embedded option, such as:
361 //   {%number%[[, no.~%number%]]}
362 // which must appear at the start of format. ifelsepart gets the
363 // whole of the option, and we return what's left after the option.
364 // we return format if there is an error.
365 docstring parseEmbeddedOption(docstring const & format, docstring & ifelsepart)
366 {
367         LASSERT(format[0] == '{' && format[1] == '%', return format);
368         string optkey;
369         docstring ifpart;
370         docstring elsepart;
371         docstring const rest = parseOptions(format, optkey, ifpart, elsepart);
372         if (format == rest) { // parse error
373                 LYXERR0("ERROR! Couldn't parse `" << format <<"'.");
374                 return format;
375         }
376         LASSERT(rest.size() <= format.size(),
377                 { ifelsepart = docstring(); return format; });
378         ifelsepart = format.substr(0, format.size() - rest.size());
379         return rest;
380 }
381
382
383 // Gets a "clause" from a format string, where the clause is
384 // delimited by '[[' and ']]'. Returns what is left after the
385 // clause is removed, and returns format if there is an error.
386 docstring getClause(docstring const & format, docstring & clause)
387 {
388         docstring fmt = format;
389         // remove '[['
390         fmt = fmt.substr(2);
391         // we'll remove characters from the front of fmt as we
392         // deal with them
393         while (!fmt.empty()) {
394                 if (fmt[0] == ']' && fmt.size() > 1 && fmt[1] == ']') {
395                         // that's the end
396                         fmt = fmt.substr(2);
397                         break;
398                 }
399                 // check for an embedded option
400                 if (fmt[0] == '{' && fmt.size() > 1 && fmt[1] == '%') {
401                         docstring part;
402                         docstring const rest = parseEmbeddedOption(fmt, part);
403                         if (fmt == rest) {
404                                 LYXERR0("ERROR! Couldn't parse embedded option in `" << format <<"'.");
405                                 return format;
406                         }
407                         clause += part;
408                         fmt = rest;
409                 } else { // it's just a normal character
410                                 clause += fmt[0];
411                                 fmt = fmt.substr(1);
412                 }
413         }
414         return fmt;
415 }
416
417
418 // parse an options string, which must appear at the start of the
419 // format parameter. puts the parsed bits in optkey, ifpart, and
420 // elsepart and returns what's left after the option is removed.
421 // if there's an error, it returns format itself.
422 docstring parseOptions(docstring const & format, string & optkey,
423                     docstring & ifpart, docstring & elsepart)
424 {
425         LASSERT(format[0] == '{' && format[1] == '%', return format);
426         // strip '{%'
427         docstring fmt = format.substr(2);
428         size_t pos = fmt.find('%'); // end of key
429         if (pos == string::npos) {
430                 LYXERR0("Error parsing  `" << format <<"'. Can't find end of key.");
431                 return format;
432         }
433         optkey = to_utf8(fmt.substr(0, pos));
434         fmt = fmt.substr(pos + 1);
435         // [[format]] should be next
436         if (fmt[0] != '[' || fmt[1] != '[') {
437                 LYXERR0("Error parsing  `" << format <<"'. Can't find '[[' after key.");
438                 return format;
439         }
440
441         docstring curfmt = fmt;
442         fmt = getClause(curfmt, ifpart);
443         if (fmt == curfmt) {
444                 LYXERR0("Error parsing  `" << format <<"'. Couldn't get if clause.");
445                 return format;
446         }
447
448         if (fmt[0] == '}') // we're done, no else clause
449                 return fmt.substr(1);
450
451         // else part should follow
452         if (fmt[0] != '[' || fmt[1] != '[') {
453                 LYXERR0("Error parsing  `" << format <<"'. Can't find else clause.");
454                 return format;
455         }
456
457         curfmt = fmt;
458         fmt = getClause(curfmt, elsepart);
459         // we should be done
460         if (fmt == curfmt || fmt[0] != '}') {
461                 LYXERR0("Error parsing  `" << format <<"'. Can't find end of option.");
462                 return format;
463         }
464         return fmt.substr(1);
465 }
466
467
468 } // anon namespace
469
470 /* FIXME
471 Bug #9131 revealed an oddity in how we are generating citation information
472 when more than one key is given. We end up building a longer and longer format 
473 string as we go, which we then have to re-parse, over and over and over again,
474 rather than generating the information for the individual keys and then putting
475 all of that together. We do that to deal with the way separators work, from what
476 I can tell, but it still feels like a hack. Fixing this would require quite a
477 bit of work, however.
478 */
479 docstring BibTeXInfo::expandFormat(docstring const & format,
480                 BibTeXInfoList const xrefs, int & counter, Buffer const & buf,
481                 docstring before, docstring after, docstring dialog, bool next) const
482 {
483         // incorrect use of macros could put us in an infinite loop
484         static int const max_passes = 5000;
485         // the use of overly large keys can lead to performance problems, due
486         // to eventual attempts to convert LaTeX macros to unicode. See bug
487         // #8944. This is perhaps not the best solution, but it will have to
488         // do for now.
489         static size_t const max_keysize = 128;
490         odocstringstream ret; // return value
491         string key;
492         bool scanning_key = false;
493         bool scanning_rich = false;
494
495         CiteEngineType const engine_type = buf.params().citeEngineType();
496         docstring fmt = format;
497         // we'll remove characters from the front of fmt as we
498         // deal with them
499         while (!fmt.empty()) {
500                 if (counter > max_passes) {
501                         LYXERR0("Recursion limit reached while parsing `"
502                                 << format << "'.");
503                         return _("ERROR!");
504                 }
505
506                 char_type thischar = fmt[0];
507                 if (thischar == '%') {
508                         // beginning or end of key
509                         if (scanning_key) {
510                                 // end of key
511                                 scanning_key = false;
512                                 // so we replace the key with its value, which may be empty
513                                 if (key[0] == '!') {
514                                         // macro
515                                         string const val =
516                                                 buf.params().documentClass().getCiteMacro(engine_type, key);
517                                         fmt = from_utf8(val) + fmt.substr(1);
518                                         counter += 1;
519                                         continue;
520                                 } else if (key[0] == '_') {
521                                         // a translatable bit
522                                         string const val =
523                                                 buf.params().documentClass().getCiteMacro(engine_type, key);
524                                         docstring const trans =
525                                                 translateIfPossible(from_utf8(val), buf.params().language->code());
526                                         ret << trans;
527                                 } else {
528                                         docstring const val =
529                                                 getValueForKey(key, buf, before, after, dialog, xrefs, max_keysize);
530                                         if (!scanning_rich)
531                                                 ret << from_ascii("{!<span class=\"bib-" + key + "\">!}");
532                                         ret << val;
533                                         if (!scanning_rich)
534                                                 ret << from_ascii("{!</span>!}");
535                                 }
536                         } else {
537                                 // beginning of key
538                                 key.clear();
539                                 scanning_key = true;
540                         }
541                 }
542                 else if (thischar == '{') {
543                         // beginning of option?
544                         if (scanning_key) {
545                                 LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
546                                 return _("ERROR!");
547                         }
548                         if (fmt.size() > 1) {
549                                 if (fmt[1] == '%') {
550                                         // it is the beginning of an optional format
551                                         string optkey;
552                                         docstring ifpart;
553                                         docstring elsepart;
554                                         docstring const newfmt =
555                                                 parseOptions(fmt, optkey, ifpart, elsepart);
556                                         if (newfmt == fmt) // parse error
557                                                 return _("ERROR!");
558                                         fmt = newfmt;
559                                         docstring const val =
560                                                 getValueForKey(optkey, buf, before, after, dialog, xrefs);
561                                         if (optkey == "next" && next)
562                                                 ret << ifpart; // without expansion
563                                         else if (!val.empty()) {
564                                                 int newcounter = 0;
565                                                 ret << expandFormat(ifpart, xrefs, newcounter, buf,
566                                                         before, after, dialog, next);
567                                         } else if (!elsepart.empty()) {
568                                                 int newcounter = 0;
569                                                 ret << expandFormat(elsepart, xrefs, newcounter, buf,
570                                                         before, after, dialog, next);
571                                         }
572                                         // fmt will have been shortened for us already
573                                         continue;
574                                 }
575                                 if (fmt[1] == '!') {
576                                         // beginning of rich text
577                                         scanning_rich = true;
578                                         fmt = fmt.substr(2);
579                                         ret << from_ascii("{!");
580                                         continue;
581                                 }
582                         }
583                         // we are here if '{' was not followed by % or !.
584                         // So it's just a character.
585                         ret << thischar;
586                 }
587                 else if (scanning_rich && thischar == '!'
588                          && fmt.size() > 1 && fmt[1] == '}') {
589                         // end of rich text
590                         scanning_rich = false;
591                         fmt = fmt.substr(2);
592                         ret << from_ascii("!}");
593                         continue;
594                 }
595                 else if (scanning_key)
596                         key += char(thischar);
597                 else {
598                         try {
599                                 ret.put(thischar);
600                         } catch (EncodingException & /* e */) {
601                                 LYXERR0("Uncodable character '" << docstring(1, thischar) << " in citation label!");
602                         }
603                 }
604                 fmt = fmt.substr(1);
605         } // for loop
606         if (scanning_key) {
607                 LYXERR0("Never found end of key in `" << format << "'!");
608                 return _("ERROR!");
609         }
610         if (scanning_rich) {
611                 LYXERR0("Never found end of rich text in `" << format << "'!");
612                 return _("ERROR!");
613         }
614         return ret.str();
615 }
616
617
618 docstring const & BibTeXInfo::getInfo(BibTeXInfoList const xrefs,
619         Buffer const & buf, bool richtext) const
620 {
621         if (!richtext && !info_.empty())
622                 return info_;
623         if (richtext && !info_richtext_.empty())
624                 return info_richtext_;
625
626         if (!is_bibtex_) {
627                 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
628                 info_ = it->second;
629                 return info_;
630         }
631
632         CiteEngineType const engine_type = buf.params().citeEngineType();
633         DocumentClass const & dc = buf.params().documentClass();
634         docstring const & format =
635                 from_utf8(dc.getCiteFormat(engine_type, to_utf8(entry_type_)));
636         int counter = 0;
637         info_ = expandFormat(format, xrefs, counter, buf,
638                 docstring(), docstring(), docstring(), false);
639
640         if (info_.empty()) {
641                 // this probably shouldn't happen
642                 return info_;
643         }
644
645         if (richtext) {
646                 info_richtext_ = convertLaTeXCommands(processRichtext(info_, true));
647                 return info_richtext_;
648         }
649
650         info_ = convertLaTeXCommands(processRichtext(info_, false));
651         return info_;
652 }
653
654
655 docstring const BibTeXInfo::getLabel(BibTeXInfoList const xrefs,
656         Buffer const & buf, docstring const & format, bool richtext,
657         docstring const & before, docstring const & after, 
658         docstring const & dialog, bool next) const
659 {
660         docstring loclabel;
661
662         int counter = 0;
663         loclabel = expandFormat(format, xrefs, counter, buf,
664                 before, after, dialog, next);
665
666         if (!loclabel.empty() && !next) {
667                 loclabel = processRichtext(loclabel, richtext);
668                 loclabel = convertLaTeXCommands(loclabel);
669         }
670
671         return loclabel;
672 }
673
674
675 docstring const & BibTeXInfo::operator[](docstring const & field) const
676 {
677         BibTeXInfo::const_iterator it = find(field);
678         if (it != end())
679                 return it->second;
680         static docstring const empty_value = docstring();
681         return empty_value;
682 }
683
684
685 docstring const & BibTeXInfo::operator[](string const & field) const
686 {
687         return operator[](from_ascii(field));
688 }
689
690
691 docstring BibTeXInfo::getValueForKey(string const & oldkey, Buffer const & buf,
692         docstring const & before, docstring const & after, docstring const & dialog,
693         BibTeXInfoList const xrefs, size_t maxsize) const
694 {
695         // anything less is pointless
696         LASSERT(maxsize >= 16, maxsize = 16);
697         string key = oldkey;
698         bool cleanit = false;
699         if (prefixIs(oldkey, "clean:")) {
700                 key = oldkey.substr(6);
701                 cleanit = true;
702         }
703
704         docstring ret = operator[](key);
705         if (ret.empty() && !xrefs.empty()) {
706                 vector<BibTeXInfo const *>::const_iterator it = xrefs.begin();
707                 vector<BibTeXInfo const *>::const_iterator en = xrefs.end();
708                 for (; it != en; ++it) {
709                         if (*it && !(**it)[key].empty()) {
710                                 ret = (**it)[key];
711                                 break;
712                         }
713                 }
714         }
715         if (ret.empty()) {
716                 // some special keys
717                 // FIXME: dialog, textbefore and textafter have nothing to do with this
718                 if (key == "dialog")
719                         ret = dialog;
720                 else if (key == "entrytype")
721                         ret = entry_type_;
722                 else if (key == "key")
723                         ret = bib_key_;
724                 else if (key == "label")
725                         ret = label_;
726                 else if (key == "modifier" && modifier_ != 0)
727                         ret = modifier_;
728                 else if (key == "numericallabel")
729                         ret = cite_number_;
730                 else if (key == "abbrvauthor")
731                         // Special key to provide abbreviated author names.
732                         ret = getAbbreviatedAuthor(&buf, false);
733                 else if (key == "shortauthor")
734                         // When shortauthor is not defined, jurabib automatically
735                         // provides jurabib-style abbreviated author names. We do
736                         // this as well.
737                         ret = getAbbreviatedAuthor(&buf, true);
738                 else if (key == "shorttitle") {
739                         // When shorttitle is not defined, jurabib uses for `article'
740                         // and `periodical' entries the form `journal volume [year]'
741                         // and for other types of entries it uses the `title' field.
742                         if (entry_type_ == "article" || entry_type_ == "periodical")
743                                 ret = operator[]("journal") + " " + operator[]("volume")
744                                         + " [" + operator[]("year") + "]";
745                         else
746                                 ret = operator[]("title");
747                 } else if (key == "bibentry") {
748                         // Special key to provide the full bibliography entry: see getInfo()
749                         CiteEngineType const engine_type = buf.params().citeEngineType();
750                         DocumentClass const & dc = buf.params().documentClass();
751                         docstring const & format =
752                                 from_utf8(dc.getCiteFormat(engine_type, to_utf8(entry_type_)));
753                         int counter = 0;
754                         ret = expandFormat(format, xrefs, counter, buf,
755                                 docstring(), docstring(), docstring(), false);
756                 } else if (key == "textbefore")
757                         ret = before;
758                 else if (key == "textafter")
759                         ret = after;
760                 else if (key == "year")
761                         ret = getYear();
762         }
763
764         if (cleanit)
765                 ret = html::cleanAttr(ret);
766
767         // make sure it is not too big
768         support::truncateWithEllipsis(ret, maxsize);
769         return ret;
770 }
771
772
773 //////////////////////////////////////////////////////////////////////
774 //
775 // BiblioInfo
776 //
777 //////////////////////////////////////////////////////////////////////
778
779 namespace {
780
781 // A functor for use with sort, leading to case insensitive sorting
782 class compareNoCase: public binary_function<docstring, docstring, bool>
783 {
784 public:
785         bool operator()(docstring const & s1, docstring const & s2) const {
786                 return compare_no_case(s1, s2) < 0;
787         }
788 };
789
790 } // namespace anon
791
792
793 vector<docstring> const BiblioInfo::getXRefs(BibTeXInfo const & data, bool const nested) const
794 {
795         vector<docstring> result;
796         if (!data.isBibTeX())
797                 return result;
798         // Legacy crossref field. This is not nestable.
799         if (!nested && !data["crossref"].empty())
800                 result.push_back(data["crossref"]);
801         // Biblatex's xdata field. Infinitely nestable.
802         // XData field can consist of a comma-separated list of keys
803         vector<docstring> const xdatakeys = getVectorFromString(data["xdata"]);
804         if (!xdatakeys.empty()) {
805                 vector<docstring>::const_iterator xit = xdatakeys.begin();
806                 vector<docstring>::const_iterator xen = xdatakeys.end();
807                 for (; xit != xen; ++xit) {
808                         docstring const xdatakey = *xit;
809                         result.push_back(xdatakey);
810                         BiblioInfo::const_iterator it = find(xdatakey);
811                         if (it != end()) {
812                                 BibTeXInfo const & xdata = it->second;
813                                 vector<docstring> const nxdata = getXRefs(xdata, true);
814                                 if (!nxdata.empty())
815                                         result.insert(result.end(), nxdata.begin(), nxdata.end());
816                         }
817                 }
818         }
819         return result;
820 }
821
822
823 vector<docstring> const BiblioInfo::getKeys() const
824 {
825         vector<docstring> bibkeys;
826         BiblioInfo::const_iterator it  = begin();
827         for (; it != end(); ++it)
828                 bibkeys.push_back(it->first);
829         sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
830         return bibkeys;
831 }
832
833
834 vector<docstring> const BiblioInfo::getFields() const
835 {
836         vector<docstring> bibfields;
837         set<docstring>::const_iterator it = field_names_.begin();
838         set<docstring>::const_iterator end = field_names_.end();
839         for (; it != end; ++it)
840                 bibfields.push_back(*it);
841         sort(bibfields.begin(), bibfields.end());
842         return bibfields;
843 }
844
845
846 vector<docstring> const BiblioInfo::getEntries() const
847 {
848         vector<docstring> bibentries;
849         set<docstring>::const_iterator it = entry_types_.begin();
850         set<docstring>::const_iterator end = entry_types_.end();
851         for (; it != end; ++it)
852                 bibentries.push_back(*it);
853         sort(bibentries.begin(), bibentries.end());
854         return bibentries;
855 }
856
857
858 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key, Buffer const & buf) const
859 {
860         BiblioInfo::const_iterator it = find(key);
861         if (it == end())
862                 return docstring();
863         BibTeXInfo const & data = it->second;
864         return data.getAbbreviatedAuthor(&buf, false);
865 }
866
867
868 docstring const BiblioInfo::getCiteNumber(docstring const & key) const
869 {
870         BiblioInfo::const_iterator it = find(key);
871         if (it == end())
872                 return docstring();
873         BibTeXInfo const & data = it->second;
874         return data.citeNumber();
875 }
876
877
878 docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) const
879 {
880         BiblioInfo::const_iterator it = find(key);
881         if (it == end())
882                 return docstring();
883         BibTeXInfo const & data = it->second;
884         docstring year = data.getYear();
885         if (year.empty()) {
886                 // let's try the crossrefs
887                 vector<docstring> const xrefs = getXRefs(data);
888                 if (xrefs.empty())
889                         // no luck
890                         return docstring();
891                 vector<docstring>::const_iterator it = xrefs.begin();
892                 vector<docstring>::const_iterator en = xrefs.end();
893                 for (; it != en; ++it) {
894                         BiblioInfo::const_iterator const xrefit = find(*it);
895                         if (xrefit == end())
896                                 continue;
897                         BibTeXInfo const & xref_data = xrefit->second;
898                         year = xref_data.getYear();
899                         if (!year.empty())
900                                 // success!
901                                 break;
902                 }
903         }
904         if (use_modifier && data.modifier() != 0)
905                 year += data.modifier();
906         return year;
907 }
908
909
910 docstring const BiblioInfo::getYear(docstring const & key, Buffer const & buf, bool use_modifier) const
911 {
912         docstring const year = getYear(key, use_modifier);
913         if (year.empty())
914                 return buf.B_("No year");
915         return year;
916 }
917
918
919 docstring const BiblioInfo::getInfo(docstring const & key,
920         Buffer const & buf, bool richtext) const
921 {
922         BiblioInfo::const_iterator it = find(key);
923         if (it == end())
924                 return docstring(_("Bibliography entry not found!"));
925         BibTeXInfo const & data = it->second;
926         BibTeXInfoList xrefptrs;
927         vector<docstring> const xrefs = getXRefs(data);
928         if (!xrefs.empty()) {
929                 vector<docstring>::const_iterator it = xrefs.begin();
930                 vector<docstring>::const_iterator en = xrefs.end();
931                 for (; it != en; ++it) {
932                         BiblioInfo::const_iterator const xrefit = find(*it);
933                         if (xrefit != end())
934                                 xrefptrs.push_back(&(xrefit->second));
935                 }
936         }
937         return data.getInfo(xrefptrs, buf, richtext);
938 }
939
940
941 docstring const BiblioInfo::getLabel(vector<docstring> keys,
942         Buffer const & buf, string const & style, bool for_xhtml,
943         size_t max_size, docstring const & before, docstring const & after,
944         docstring const & dialog) const
945 {
946         // shorter makes no sense
947         LASSERT(max_size >= 16, max_size = 16);
948
949         // we can't display more than 10 of these, anyway
950         bool const too_many_keys = keys.size() > 10;
951         if (too_many_keys)
952                 keys.resize(10);
953
954         CiteEngineType const engine_type = buf.params().citeEngineType();
955         DocumentClass const & dc = buf.params().documentClass();
956         docstring const & format = from_utf8(dc.getCiteFormat(engine_type, style, "cite"));
957         docstring ret = format;
958         vector<docstring>::const_iterator key = keys.begin();
959         vector<docstring>::const_iterator ken = keys.end();
960         for (; key != ken; ++key) {
961                 BiblioInfo::const_iterator it = find(*key);
962                 BibTeXInfo empty_data;
963                 empty_data.key(*key);
964                 BibTeXInfo & data = empty_data;
965                 vector<BibTeXInfo const *> xrefptrs;
966                 if (it != end()) {
967                         data = it->second;
968                         vector<docstring> const xrefs = getXRefs(data);
969                         if (!xrefs.empty()) {
970                                 vector<docstring>::const_iterator it = xrefs.begin();
971                                 vector<docstring>::const_iterator en = xrefs.end();
972                                 for (; it != en; ++it) {
973                                         BiblioInfo::const_iterator const xrefit = find(*it);
974                                         if (xrefit != end())
975                                                 xrefptrs.push_back(&(xrefit->second));
976                                 }
977                         }
978                 }
979                 ret = data.getLabel(xrefptrs, buf, ret, for_xhtml,
980                         before, after, dialog, key + 1 != ken);
981         }
982
983         if (too_many_keys)
984                 ret.push_back(0x2026);//HORIZONTAL ELLIPSIS
985         support::truncateWithEllipsis(ret, max_size);
986         return ret;
987 }
988
989
990 bool BiblioInfo::isBibtex(docstring const & key) const
991 {
992         docstring key1;
993         split(key, key1, ',');
994         BiblioInfo::const_iterator it = find(key1);
995         if (it == end())
996                 return false;
997         return it->second.isBibTeX();
998 }
999
1000
1001 vector<docstring> const BiblioInfo::getCiteStrings(
1002         vector<docstring> const & keys, vector<CitationStyle> const & styles,
1003         Buffer const & buf, docstring const & before,
1004         docstring const & after, docstring const & dialog, size_t max_size) const
1005 {
1006         if (empty())
1007                 return vector<docstring>();
1008
1009         string style;
1010         vector<docstring> vec(styles.size());
1011         for (size_t i = 0; i != vec.size(); ++i) {
1012                 style = styles[i].cmd;
1013                 vec[i] = getLabel(keys, buf, style, false, max_size, before, after, dialog);
1014         }
1015
1016         return vec;
1017 }
1018
1019
1020 void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
1021 {
1022         bimap_.insert(info.begin(), info.end());
1023         field_names_.insert(info.field_names_.begin(), info.field_names_.end());
1024         entry_types_.insert(info.entry_types_.begin(), info.entry_types_.end());
1025 }
1026
1027
1028 namespace {
1029
1030 // used in xhtml to sort a list of BibTeXInfo objects
1031 bool lSorter(BibTeXInfo const * lhs, BibTeXInfo const * rhs)
1032 {
1033         docstring const lauth = lhs->getAbbreviatedAuthor();
1034         docstring const rauth = rhs->getAbbreviatedAuthor();
1035         docstring const lyear = lhs->getYear();
1036         docstring const ryear = rhs->getYear();
1037         docstring const ltitl = lhs->operator[]("title");
1038         docstring const rtitl = rhs->operator[]("title");
1039         return  (lauth < rauth)
1040                 || (lauth == rauth && lyear < ryear)
1041                 || (lauth == rauth && lyear == ryear && ltitl < rtitl);
1042 }
1043
1044 }
1045
1046
1047 void BiblioInfo::collectCitedEntries(Buffer const & buf)
1048 {
1049         cited_entries_.clear();
1050         // We are going to collect all the citation keys used in the document,
1051         // getting them from the TOC.
1052         // FIXME We may want to collect these differently, in the first case,
1053         // so that we might have them in order of appearance.
1054         set<docstring> citekeys;
1055         shared_ptr<Toc const> toc = buf.tocBackend().toc("citation");
1056         Toc::const_iterator it = toc->begin();
1057         Toc::const_iterator const en = toc->end();
1058         for (; it != en; ++it) {
1059                 if (it->str().empty())
1060                         continue;
1061                 vector<docstring> const keys = getVectorFromString(it->str());
1062                 citekeys.insert(keys.begin(), keys.end());
1063         }
1064         if (citekeys.empty())
1065                 return;
1066
1067         // We have a set of the keys used in this document.
1068         // We will now convert it to a list of the BibTeXInfo objects used in
1069         // this document...
1070         vector<BibTeXInfo const *> bi;
1071         set<docstring>::const_iterator cit = citekeys.begin();
1072         set<docstring>::const_iterator const cen = citekeys.end();
1073         for (; cit != cen; ++cit) {
1074                 BiblioInfo::const_iterator const bt = find(*cit);
1075                 if (bt == end() || !bt->second.isBibTeX())
1076                         continue;
1077                 bi.push_back(&(bt->second));
1078         }
1079         // ...and sort it.
1080         sort(bi.begin(), bi.end(), lSorter);
1081
1082         // Now we can write the sorted keys
1083         vector<BibTeXInfo const *>::const_iterator bit = bi.begin();
1084         vector<BibTeXInfo const *>::const_iterator ben = bi.end();
1085         for (; bit != ben; ++bit)
1086                 cited_entries_.push_back((*bit)->key());
1087 }
1088
1089
1090 void BiblioInfo::makeCitationLabels(Buffer const & buf)
1091 {
1092         collectCitedEntries(buf);
1093         CiteEngineType const engine_type = buf.params().citeEngineType();
1094         bool const numbers = (engine_type & ENGINE_TYPE_NUMERICAL);
1095
1096         int keynumber = 0;
1097         char modifier = 0;
1098         // used to remember the last one we saw
1099         // we'll be comparing entries to see if we need to add
1100         // modifiers, like "1984a"
1101         map<docstring, BibTeXInfo>::iterator last;
1102
1103         vector<docstring>::const_iterator it = cited_entries_.begin();
1104         vector<docstring>::const_iterator const en = cited_entries_.end();
1105         for (; it != en; ++it) {
1106                 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
1107                 // this shouldn't happen, but...
1108                 if (biit == bimap_.end())
1109                         // ...fail gracefully, anyway.
1110                         continue;
1111                 BibTeXInfo & entry = biit->second;
1112                 if (numbers) {
1113                         docstring const num = convert<docstring>(++keynumber);
1114                         entry.setCiteNumber(num);
1115                 } else {
1116                         // coverity complains about our derefercing the iterator last,
1117                         // which was not initialized above. but it does get initialized
1118                         // after the first time through the loop, which is the point of
1119                         // the first test.
1120                         // coverity[FORWARD_NULL]
1121                         if (it != cited_entries_.begin()
1122                             && entry.getAbbreviatedAuthor() == last->second.getAbbreviatedAuthor()
1123                             // we access the year via getYear() so as to get it from the xref,
1124                             // if we need to do so
1125                             && getYear(entry.key()) == getYear(last->second.key())) {
1126                                 if (modifier == 0) {
1127                                         // so the last one should have been 'a'
1128                                         last->second.setModifier('a');
1129                                         modifier = 'b';
1130                                 } else if (modifier == 'z')
1131                                         modifier = 'A';
1132                                 else
1133                                         modifier++;
1134                         } else {
1135                                 modifier = 0;
1136                         }
1137                         entry.setModifier(modifier);
1138                         // remember the last one
1139                         last = biit;
1140                 }
1141         }
1142         // Set the labels
1143         it = cited_entries_.begin();
1144         for (; it != en; ++it) {
1145                 map<docstring, BibTeXInfo>::iterator const biit = bimap_.find(*it);
1146                 // this shouldn't happen, but...
1147                 if (biit == bimap_.end())
1148                         // ...fail gracefully, anyway.
1149                         continue;
1150                 BibTeXInfo & entry = biit->second;
1151                 if (numbers) {
1152                         entry.label(entry.citeNumber());
1153                 } else {
1154                         docstring const auth = entry.getAbbreviatedAuthor(&buf, false);
1155                         // we do it this way so as to access the xref, if necessary
1156                         // note that this also gives us the modifier
1157                         docstring const year = getYear(*it, buf, true);
1158                         if (!auth.empty() && !year.empty())
1159                                 entry.label(auth + ' ' + year);
1160                         else
1161                                 entry.label(entry.key());
1162                 }
1163         }
1164 }
1165
1166
1167 //////////////////////////////////////////////////////////////////////
1168 //
1169 // CitationStyle
1170 //
1171 //////////////////////////////////////////////////////////////////////
1172
1173
1174 CitationStyle citationStyleFromString(string const & command)
1175 {
1176         CitationStyle cs;
1177         if (command.empty())
1178                 return cs;
1179
1180         string cmd = command;
1181         if (cmd[0] == 'C') {
1182                 cs.forceUpperCase = true;
1183                 cmd[0] = 'c';
1184         }
1185
1186         size_t const n = cmd.size() - 1;
1187         if (cmd[n] == '*') {
1188                 cs.fullAuthorList = true;
1189                 cmd = cmd.substr(0, n);
1190         }
1191
1192         cs.cmd = cmd;
1193         return cs;
1194 }
1195
1196
1197 string citationStyleToString(const CitationStyle & cs)
1198 {
1199         string cmd = cs.cmd;
1200         if (cs.forceUpperCase)
1201                 cmd[0] = uppercase(cmd[0]);
1202         if (cs.fullAuthorList)
1203                 cmd += '*';
1204         return cmd;
1205 }
1206
1207 } // namespace lyx