]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.cpp
Support for nocite, provided by Bernhard Reiter.
[lyx.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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "BiblioInfo.h"
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "buffer_funcs.h"
19 #include "InsetIterator.h"
20 #include "Paragraph.h"
21
22 #include "insets/Inset.h"
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetInclude.h"
26
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include "boost/regex.hpp"
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38
39 //////////////////////////////////////////////////////////////////////
40 //
41 // BibTeXInfo
42 //
43 //////////////////////////////////////////////////////////////////////
44
45 BibTeXInfo::BibTeXInfo()
46         : isBibTeX(true)
47 {}
48
49         
50 bool BibTeXInfo::hasField(docstring const & field) const
51 {
52         return count(field) == 1;
53 }
54
55
56 docstring const & BibTeXInfo::getValueForField(docstring const & field) const
57 {
58         BibTeXInfo::const_iterator it = find(field);
59         if (it != end())
60                 return it->second;
61         static docstring const empty_value = docstring();
62         return empty_value;
63 }
64         
65         
66 docstring const & BibTeXInfo::getValueForField(string const & field) const
67 {
68         return getValueForField(from_ascii(field));
69 }
70
71
72 static docstring familyName(docstring const & name)
73 {
74         if (name.empty())
75                 return docstring();
76
77         // Very simple parser
78         docstring fname = name;
79
80         // possible authorname combinations are:
81         // "Surname, FirstName"
82         // "Surname, F."
83         // "FirstName Surname"
84         // "F. Surname"
85         docstring::size_type idx = fname.find(',');
86         if (idx != docstring::npos)
87                 return ltrim(fname.substr(0, idx));
88         idx = fname.rfind('.');
89         if (idx != docstring::npos && idx + 1 < fname.size())
90                 fname = ltrim(fname.substr(idx + 1));
91         // test if we have a LaTeX Space in front
92         if (fname[0] == '\\')
93                 return fname.substr(2);
94         return rtrim(fname);
95 }
96
97
98 docstring const BibTeXInfo::getAbbreviatedAuthor() const
99 {
100         if (!isBibTeX) 
101                 return docstring();
102  
103         docstring author = getValueForField("author");
104  
105         if (author.empty()) {
106                 author = getValueForField("editor");
107                 if (author.empty())
108                         return bibKey;
109         }
110
111         // OK, we've got some names. Let's format them.
112         // Try to split the author list on " and "
113         vector<docstring> const authors =
114                 getVectorFromString(author, from_ascii(" and "));
115         
116         if (authors.size() == 2)
117                 return bformat(_("%1$s and %2$s"),
118                         familyName(authors[0]), familyName(authors[1]));
119
120         if (authors.size() > 2)
121                 return bformat(_("%1$s et al."), familyName(authors[0]));
122
123         return familyName(authors[0]);
124 }
125
126
127 docstring const BibTeXInfo::getYear() const
128 {
129         if (!isBibTeX) 
130                 return docstring();
131  
132         docstring year = getValueForField("year");
133         if (year.empty())
134                 year = _("No year");
135         return year;
136 }
137
138
139 docstring const BibTeXInfo::getInfo() const
140 {
141         if (!isBibTeX) {
142                 BibTeXInfo::const_iterator it = find(from_ascii("ref"));
143                 return it->second;
144         }
145  
146         // FIXME
147         // This could be made a lot better using the entryType
148         // field to customize the output based upon entry type.
149         
150         // Search for all possible "required" fields
151         docstring author = getValueForField("author");
152         if (author.empty())
153                 author = getValueForField("editor");
154  
155         docstring year      = getValueForField("year");
156         docstring title     = getValueForField("title");
157         docstring docLoc    = getValueForField("pages");
158         if (docLoc.empty()) {
159                 docLoc = getValueForField("chapter");
160                 if (!docLoc.empty())
161                         docLoc = from_ascii("Ch. ") + docLoc;
162         }       else {
163                 docLoc = from_ascii("pp. ") + docLoc;
164         }
165
166         docstring media = getValueForField("journal");
167         if (media.empty()) {
168                 media = getValueForField("publisher");
169                 if (media.empty()) {
170                         media = getValueForField("school");
171                         if (media.empty())
172                                 media = getValueForField("institution");
173                 }
174         }
175         docstring volume = getValueForField("volume");
176
177         odocstringstream result;
178         if (!author.empty())
179                 result << author << ", ";
180         if (!title.empty())
181                 result << title;
182         if (!media.empty())
183                 result << ", " << media;
184         if (!year.empty())
185                 result << ", " << year;
186         if (!docLoc.empty())
187                 result << ", " << docLoc;
188
189         docstring const result_str = rtrim(result.str());
190         if (!result_str.empty())
191                 return result_str;
192
193         // This should never happen (or at least be very unusual!)
194         return docstring();
195 }
196
197
198 //////////////////////////////////////////////////////////////////////
199 //
200 // BiblioInfo
201 //
202 //////////////////////////////////////////////////////////////////////
203
204 namespace {
205 // A functor for use with sort, leading to case insensitive sorting
206         class compareNoCase: public binary_function<docstring, docstring, bool>
207         {
208                 public:
209                         bool operator()(docstring const & s1, docstring const & s2) const {
210                                 return compare_no_case(s1, s2) < 0;
211                         }
212         };
213 } // namespace anon
214
215
216 vector<docstring> const BiblioInfo::getKeys() const
217 {
218         vector<docstring> bibkeys;
219         BiblioInfo::const_iterator it  = begin();
220         for (; it != end(); ++it)
221                 bibkeys.push_back(it->first);
222         sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
223         return bibkeys;
224 }
225
226
227 vector<docstring> const BiblioInfo::getFields() const
228 {
229         vector<docstring> bibfields;
230         set<docstring>::const_iterator it = fieldNames.begin();
231         set<docstring>::const_iterator end = fieldNames.end();
232         for (; it != end; ++it)
233                 bibfields.push_back(*it);
234         sort(bibfields.begin(), bibfields.end());
235         return bibfields;
236 }
237
238
239 vector<docstring> const BiblioInfo::getEntries() const
240 {
241         vector<docstring> bibentries;
242         set<docstring>::const_iterator it = entryTypes.begin();
243         set<docstring>::const_iterator end = entryTypes.end();
244         for (; it != end; ++it)
245                 bibentries.push_back(*it);
246         sort(bibentries.begin(), bibentries.end());
247         return bibentries;
248 }
249
250
251 docstring const BiblioInfo::getAbbreviatedAuthor(docstring const & key) const
252 {
253         BiblioInfo::const_iterator it = find(key);
254         if (it == end())
255                 return docstring();
256         BibTeXInfo const & data = it->second;
257         return data.getAbbreviatedAuthor();
258 }
259
260
261 docstring const BiblioInfo::getYear(docstring const & key) const
262 {
263         BiblioInfo::const_iterator it = find(key);
264         if (it == end())
265                 return docstring();
266         BibTeXInfo const & data = it->second;
267         return data.getYear();
268 }
269
270
271 docstring const BiblioInfo::getInfo(docstring const & key) const
272 {
273         BiblioInfo::const_iterator it = find(key);
274         if (it == end())
275                 return docstring();
276         BibTeXInfo const & data = it->second;
277         return data.getInfo();
278 }
279
280
281 vector<docstring> const BiblioInfo::getCiteStrings(
282         docstring const & key, Buffer const & buf) const
283 {
284         biblio::CiteEngine const engine = buf.params().getEngine();
285         if (engine == biblio::ENGINE_BASIC || 
286             engine == biblio::ENGINE_NATBIB_NUMERICAL)
287                 return getNumericalStrings(key, buf);
288         else
289                 return getAuthorYearStrings(key, buf);
290 }
291
292
293 vector<docstring> const BiblioInfo::getNumericalStrings(
294         docstring const & key, Buffer const & buf) const
295 {
296         if (empty())
297                 return vector<docstring>();
298
299         docstring const author = getAbbreviatedAuthor(key);
300         docstring const year   = getYear(key);
301         if (author.empty() || year.empty())
302                 return vector<docstring>();
303
304         vector<biblio::CiteStyle> const & styles = 
305                 biblio::getCiteStyles(buf.params().getEngine());
306         
307         vector<docstring> vec(styles.size());
308         for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
309                 docstring str;
310
311                 switch (styles[i]) {
312                         case biblio::CITE:
313                         case biblio::CITEP:
314                                 str = from_ascii("[#ID]");
315                                 break;
316
317                         case biblio::NOCITE:
318                                 str = _("Add to bibliography only.");
319                                 break;
320
321                         case biblio::CITET:
322                                 str = author + " [#ID]";
323                                 break;
324
325                         case biblio::CITEALT:
326                                 str = author + " #ID";
327                                 break;
328
329                         case biblio::CITEALP:
330                                 str = from_ascii("#ID");
331                                 break;
332
333                         case biblio::CITEAUTHOR:
334                                 str = author;
335                                 break;
336
337                         case biblio::CITEYEAR:
338                                 str = year;
339                                 break;
340
341                         case biblio::CITEYEARPAR:
342                                 str = '(' + year + ')';
343                                 break;
344                 }
345
346                 vec[i] = str;
347         }
348
349         return vec;
350 }
351
352
353 vector<docstring> const BiblioInfo::getAuthorYearStrings(
354         docstring const & key, Buffer const & buf) const
355 {
356         if (empty())
357                 return vector<docstring>();
358
359         docstring const author = getAbbreviatedAuthor(key);
360         docstring const year   = getYear(key);
361         if (author.empty() || year.empty())
362                 return vector<docstring>();
363
364         vector<biblio::CiteStyle> const & styles = 
365                 getCiteStyles(buf.params().getEngine());
366         
367         vector<docstring> vec(styles.size());
368         for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
369                 docstring str;
370
371                 switch (styles[i]) {
372                         case biblio::CITE:
373                 // jurabib only: Author/Annotator
374                 // (i.e. the "before" field, 2nd opt arg)
375                                 str = author + "/<" + _("before") + '>';
376                                 break;
377
378                         case biblio::NOCITE:
379                                 str = _("Add to bibliography only.");
380                                 break;
381
382                         case biblio::CITET:
383                                 str = author + " (" + year + ')';
384                                 break;
385
386                         case biblio::CITEP:
387                                 str = '(' + author + ", " + year + ')';
388                                 break;
389
390                         case biblio::CITEALT:
391                                 str = author + ' ' + year ;
392                                 break;
393
394                         case biblio::CITEALP:
395                                 str = author + ", " + year ;
396                                 break;
397
398                         case biblio::CITEAUTHOR:
399                                 str = author;
400                                 break;
401
402                         case biblio::CITEYEAR:
403                                 str = year;
404                                 break;
405
406                         case biblio::CITEYEARPAR:
407                                 str = '(' + year + ')';
408                                 break;
409                 }
410                 vec[i] = str;
411         }
412         return vec;
413 }
414
415
416 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
417 {       
418         /// if this is a child document and the parent is already loaded
419         /// use the parent's list instead  [ale990412]
420         Buffer const * const tmp = buf->masterBuffer();
421         BOOST_ASSERT(tmp);
422         if (tmp != buf) {
423                 this->fillWithBibKeys(tmp);
424                 return;
425         }
426
427         // Pre-load all child documents.
428         buf->loadChildDocuments();
429
430         for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
431                 it->fillWithBibKeys(*buf, *this, it);
432 }
433
434
435 namespace biblio {
436
437 //////////////////////////////////////////////////////////////////////
438 //
439 // CitationStyle
440 //
441 //////////////////////////////////////////////////////////////////////
442
443 namespace {
444
445
446 char const * const citeCommands[] = {
447         "cite", "nocite", "citet", "citep", "citealt", "citealp",
448         "citeauthor", "citeyear", "citeyearpar" };
449
450 unsigned int const nCiteCommands =
451                 sizeof(citeCommands) / sizeof(char *);
452
453 CiteStyle const citeStyles[] = {
454         CITE, NOCITE, CITET, CITEP, CITEALT,
455 CITEALP, CITEAUTHOR, CITEYEAR, CITEYEARPAR };
456
457 unsigned int const nCiteStyles =
458                 sizeof(citeStyles) / sizeof(CiteStyle);
459
460 CiteStyle const citeStylesFull[] = {
461         CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
462
463 unsigned int const nCiteStylesFull =
464                 sizeof(citeStylesFull) / sizeof(CiteStyle);
465
466 CiteStyle const citeStylesUCase[] = {
467         CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
468
469 unsigned int const nCiteStylesUCase =
470         sizeof(citeStylesUCase) / sizeof(CiteStyle);
471
472 } // namespace anon
473
474
475 CitationStyle::CitationStyle(string const & command)
476         : style(CITE), full(false), forceUCase(false)
477 {
478         if (command.empty())
479                 return;
480
481         string cmd = command;
482         if (cmd[0] == 'C') {
483                 forceUCase = true;
484                 cmd[0] = 'c';
485         }
486
487         string::size_type const n = cmd.size() - 1;
488         if (cmd != "cite" && cmd[n] == '*') {
489                 full = true;
490                 cmd = cmd.substr(0,n);
491         }
492
493         char const * const * const last = citeCommands + nCiteCommands;
494         char const * const * const ptr = find(citeCommands, last, cmd);
495
496         if (ptr != last) {
497                 size_t idx = ptr - citeCommands;
498                 style = citeStyles[idx];
499         }
500 }
501
502
503 string const CitationStyle::asLatexStr() const
504 {
505         string cite = citeCommands[style];
506         if (full) {
507                 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
508                 if (find(citeStylesFull, last, style) != last)
509                         cite += '*';
510         }
511
512         if (forceUCase) {
513                 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
514                 if (find(citeStylesUCase, last, style) != last)
515                         cite[0] = 'C';
516         }
517
518         return cite;
519 }
520
521
522 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
523 {
524         unsigned int nStyles = 0;
525         unsigned int start = 0;
526
527         switch (engine) {
528                 case ENGINE_BASIC:
529                         nStyles = 2;
530                         start = 0;
531                         break;
532                 case ENGINE_NATBIB_AUTHORYEAR:
533                 case ENGINE_NATBIB_NUMERICAL:
534                         nStyles = nCiteStyles - 1;
535                         start = 1;
536                         break;
537                 case ENGINE_JURABIB:
538                         nStyles = nCiteStyles;
539                         start = 0;
540                         break;
541         }
542
543         typedef vector<CiteStyle> cite_vec;
544
545         cite_vec styles(nStyles);
546         size_t i = 0;
547         int j = start;
548         for (; i != styles.size(); ++i, ++j)
549                 styles[i] = citeStyles[j];
550
551         return styles;
552 }
553
554 } // namespace biblio
555 } // namespace lyx
556