]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.cpp
cosmetics
[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_NATBIB_NUMERICAL)
286                 return getNumericalStrings(key, buf);
287         else
288                 return getAuthorYearStrings(key, buf);
289 }
290
291
292 vector<docstring> const BiblioInfo::getNumericalStrings(
293         docstring const & key, Buffer const & buf) const
294 {
295         if (empty())
296                 return vector<docstring>();
297
298         docstring const author = getAbbreviatedAuthor(key);
299         docstring const year   = getYear(key);
300         if (author.empty() || year.empty())
301                 return vector<docstring>();
302
303         vector<biblio::CiteStyle> const & styles = 
304                 biblio::getCiteStyles(buf.params().getEngine());
305         
306         vector<docstring> vec(styles.size());
307         for (size_t i = 0; i != vec.size(); ++i) {
308                 docstring str;
309
310                 switch (styles[i]) {
311                         case biblio::CITE:
312                         case biblio::CITEP:
313                                 str = from_ascii("[#ID]");
314                                 break;
315
316                         case biblio::CITET:
317                                 str = author + " [#ID]";
318                                 break;
319
320                         case biblio::CITEALT:
321                                 str = author + " #ID";
322                                 break;
323
324                         case biblio::CITEALP:
325                                 str = from_ascii("#ID");
326                                 break;
327
328                         case biblio::CITEAUTHOR:
329                                 str = author;
330                                 break;
331
332                         case biblio::CITEYEAR:
333                                 str = year;
334                                 break;
335
336                         case biblio::CITEYEARPAR:
337                                 str = '(' + year + ')';
338                                 break;
339                 }
340
341                 vec[i] = str;
342         }
343
344         return vec;
345 }
346
347
348 vector<docstring> const BiblioInfo::getAuthorYearStrings(
349         docstring const & key, Buffer const & buf) const
350 {
351         if (empty())
352                 return vector<docstring>();
353
354         docstring const author = getAbbreviatedAuthor(key);
355         docstring const year   = getYear(key);
356         if (author.empty() || year.empty())
357                 return vector<docstring>();
358
359         vector<biblio::CiteStyle> const & styles = 
360                 getCiteStyles(buf.params().getEngine());
361         
362         vector<docstring> vec(styles.size());
363         for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
364                 docstring str;
365
366                 switch (styles[i]) {
367                         case biblio::CITE:
368                 // jurabib only: Author/Annotator
369                 // (i.e. the "before" field, 2nd opt arg)
370                                 str = author + "/<" + _("before") + '>';
371                                 break;
372
373                         case biblio::CITET:
374                                 str = author + " (" + year + ')';
375                                 break;
376
377                         case biblio::CITEP:
378                                 str = '(' + author + ", " + year + ')';
379                                 break;
380
381                         case biblio::CITEALT:
382                                 str = author + ' ' + year ;
383                                 break;
384
385                         case biblio::CITEALP:
386                                 str = author + ", " + year ;
387                                 break;
388
389                         case biblio::CITEAUTHOR:
390                                 str = author;
391                                 break;
392
393                         case biblio::CITEYEAR:
394                                 str = year;
395                                 break;
396
397                         case biblio::CITEYEARPAR:
398                                 str = '(' + year + ')';
399                                 break;
400                 }
401                 vec[i] = str;
402         }
403         return vec;
404 }
405
406
407 void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
408 {       
409         /// if this is a child document and the parent is already loaded
410         /// use the parent's list instead  [ale990412]
411         Buffer const * const tmp = buf->masterBuffer();
412         BOOST_ASSERT(tmp);
413         if (tmp != buf) {
414                 this->fillWithBibKeys(tmp);
415                 return;
416         }
417
418         // Pre-load all child documents.
419         buf->loadChildDocuments();
420
421         for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
422                 it->fillWithBibKeys(*buf, *this, it);
423 }
424
425
426 namespace biblio {
427
428 //////////////////////////////////////////////////////////////////////
429 //
430 // CitationStyle
431 //
432 //////////////////////////////////////////////////////////////////////
433
434 namespace {
435
436
437 char const * const citeCommands[] = {
438         "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
439         "citeyear", "citeyearpar" };
440
441 unsigned int const nCiteCommands =
442                 sizeof(citeCommands) / sizeof(char *);
443
444 CiteStyle const citeStyles[] = {
445         CITE, CITET, CITEP, CITEALT, CITEALP,
446 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
447
448 unsigned int const nCiteStyles =
449                 sizeof(citeStyles) / sizeof(CiteStyle);
450
451 CiteStyle const citeStylesFull[] = {
452         CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
453
454 unsigned int const nCiteStylesFull =
455                 sizeof(citeStylesFull) / sizeof(CiteStyle);
456
457 CiteStyle const citeStylesUCase[] = {
458         CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
459
460 unsigned int const nCiteStylesUCase =
461         sizeof(citeStylesUCase) / sizeof(CiteStyle);
462
463 } // namespace anon
464
465
466 CitationStyle::CitationStyle(string const & command)
467         : style(CITE), full(false), forceUCase(false)
468 {
469         if (command.empty())
470                 return;
471
472         string cmd = command;
473         if (cmd[0] == 'C') {
474                 forceUCase = true;
475                 cmd[0] = 'c';
476         }
477
478         string::size_type const n = cmd.size() - 1;
479         if (cmd != "cite" && cmd[n] == '*') {
480                 full = true;
481                 cmd = cmd.substr(0,n);
482         }
483
484         char const * const * const last = citeCommands + nCiteCommands;
485         char const * const * const ptr = find(citeCommands, last, cmd);
486
487         if (ptr != last) {
488                 size_t idx = ptr - citeCommands;
489                 style = citeStyles[idx];
490         }
491 }
492
493
494 string const CitationStyle::asLatexStr() const
495 {
496         string cite = citeCommands[style];
497         if (full) {
498                 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
499                 if (find(citeStylesFull, last, style) != last)
500                         cite += '*';
501         }
502
503         if (forceUCase) {
504                 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
505                 if (find(citeStylesUCase, last, style) != last)
506                         cite[0] = 'C';
507         }
508
509         return cite;
510 }
511
512
513 vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
514 {
515         unsigned int nStyles = 0;
516         unsigned int start = 0;
517
518         switch (engine) {
519                 case ENGINE_BASIC:
520                         nStyles = 1;
521                         start = 0;
522                         break;
523                 case ENGINE_NATBIB_AUTHORYEAR:
524                 case ENGINE_NATBIB_NUMERICAL:
525                         nStyles = nCiteStyles - 1;
526                         start = 1;
527                         break;
528                 case ENGINE_JURABIB:
529                         nStyles = nCiteStyles;
530                         start = 0;
531                         break;
532         }
533
534         typedef vector<CiteStyle> cite_vec;
535
536         cite_vec styles(nStyles);
537         size_t i = 0;
538         int j = start;
539         for (; i != styles.size(); ++i, ++j)
540                 styles[i] = citeStyles[j];
541
542         return styles;
543 }
544
545 } // namespace biblio
546 } // namespace lyx
547