]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.h
Routines for calculating numerical labels for BibTeX citations.
[lyx.git] / src / BiblioInfo.h
1 // -*- C++ -*-
2 /**
3  * \file BiblioInfo.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  * \author Herbert Voß
9  * \author Richard Heck
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef BIBLIOINFO_H
15 #define BIBLIOINFO_H
16
17 #include "support/docstring.h"
18
19 #include "Citation.h"
20
21 #include <vector>
22 #include <map>
23 #include <set>
24
25
26 namespace lyx {
27         
28 class Buffer;
29
30 /// FIXME: To Citation.cpp?
31 /// Returns a vector of available Citation styles.
32 std::vector<CiteStyle> citeStyles(CiteEngine);
33 /// \param latex_str a LaTeX command, "cite", "Citep*", etc
34 CitationStyle citationStyleFromString(std::string const & latex_str);
35 /// the other way round
36 std::string citationStyleToString(CitationStyle const &);
37
38
39 /// Class to represent information about a BibTeX or
40 /// bibliography entry.
41 /// This class basically wraps a std::map, and many of its
42 /// methods simply delegate to the corresponding methods of
43 /// std::map.
44 class BibTeXInfo {
45 public:
46         /// The keys are BibTeX fields (e.g., author, title, etc), 
47         /// and the values are the associated field values.
48         typedef std::map<docstring, docstring>::const_iterator const_iterator;
49         ///
50         BibTeXInfo() : is_bibtex_(true) {}
51         /// argument sets isBibTeX_, so should be false only if it's coming
52         /// from a bibliography environment
53         BibTeXInfo(bool ib) : is_bibtex_(ib) {}
54         /// constructor that sets the entryType
55         BibTeXInfo(docstring const & key, docstring const & type);
56         ///
57         bool hasField(docstring const & field) const;
58         /// \return the short form of an authorlist
59         docstring const getAbbreviatedAuthor() const;
60         /// 
61         docstring const getYear() const;
62         ///
63         docstring const getXRef() const;
64         /// \return formatted BibTeX data suitable for framing.
65         /// \param pointer to crossref information
66         docstring const & getInfo(BibTeXInfo const * const xref = 0) const;
67         ///
68         int count(docstring const & f) const { return bimap_.count(f); }
69         ///
70         const_iterator find(docstring const & f) const { return bimap_.find(f); }
71         ///
72         const_iterator end() const { return bimap_.end(); }
73         /// \return value for field f
74         /// note that this will create an empty field if it does not exist
75         docstring & operator[](docstring const & f) 
76                 { return bimap_[f]; }
77         /// \return value for field f
78         /// this one, since it is const, will simply return docstring() if
79         /// we don't have the field and will NOT create an empty field
80         docstring const & operator[](docstring const & field) const;
81         ///
82         docstring const & operator[](std::string const & field) const;
83         ///
84         docstring const & allData() const { return all_data_; }
85         ///
86         void setAllData(docstring const & d) { all_data_ = d; }
87         ///
88         void label(docstring const & d) { label_= d; }
89         ///
90         docstring const & label() const { return label_; }
91         ///
92         docstring const & key() const { return bib_key_; }
93         ///
94         docstring citeKey() const { return cite_key_; }
95         ///
96         void setCiteKey(docstring const & k) { cite_key_ = k; }
97         ///
98         docstring entryType() const { return entry_type_; }
99         /// 
100         bool isBibTeX() const { return is_bibtex_; }
101 private:
102         /// like operator[], except, if the field is empty, it will attempt
103         /// to get the data from xref BibTeXInfo object, which would normally
104         /// be the one referenced in the crossref field.
105         docstring getValueForKey(std::string const & key, 
106                         BibTeXInfo const * const xref = 0) const;
107         /// true if from BibTeX; false if from bibliography environment
108         bool is_bibtex_;
109         /// the BibTeX key for this entry
110         docstring bib_key_;
111         /// the label that will appear in citations
112         /// this is easily set from bibliography environments, but has
113         /// to be calculated for entries we get from BibTeX
114         docstring label_;
115         /// a single string containing all BibTeX data associated with this key
116         docstring all_data_;
117         /// the BibTeX entry type (article, book, incollection, ...)
118         docstring entry_type_;
119         /// a cache for getInfo()
120         mutable docstring info_;
121         /// key to use when citing this entry 
122         /// currently used only by XHTML output routines
123         docstring cite_key_;
124         /// our map: <field, value>
125         std::map <docstring, docstring> bimap_;
126 };
127
128
129 /// Class to represent a collection of bibliographical data, whether
130 /// from BibTeX or from bibliography environments.
131 class BiblioInfo {
132 public:
133         /// bibliography key --> data for that key
134         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
135         /// \return a sorted vector of bibliography keys
136         std::vector<docstring> const getKeys() const;
137         /// \return a sorted vector of present BibTeX fields
138         std::vector<docstring> const getFields() const;
139         /// \return a sorted vector of BibTeX entry types in use
140         std::vector<docstring> const getEntries() const;
141         /// \return the short form of an authorlist
142         docstring const getAbbreviatedAuthor(docstring const & key) const;
143         /// \return the year from the bibtex data record
144         /// Note that this will get the year from the crossref if it's
145         /// not present in the record itself
146         docstring const getYear(docstring const & key) const;
147         /// \return formatted BibTeX data associated with a given key.
148         /// Empty if no info exists. 
149         /// Note that this will retrieve data from the crossref as needed.
150         docstring const getInfo(docstring const & key) const;
151         /**
152           * "Translates" the available Citation Styles into strings for a given key,
153           * either numerical or author-year depending upon the active engine. (See
154           * below for those methods.)
155           */
156         std::vector<docstring> const
157                         getCiteStrings(docstring const & key, Buffer const & buf) const;
158         /**
159                 * "Translates" the available Citation Styles into strings for a given key.
160                 * The returned string is displayed by the GUI.
161                 * [XX] is used in place of the actual reference
162                 * Eg, the vector will contain: [XX], Jones et al. [XX], ...
163                 * User supplies :
164                 *  the key,
165                 *  the buffer
166                 */
167         std::vector<docstring> const
168                         getNumericalStrings(docstring const & key, Buffer const & buf) const;
169         /**
170                 * "Translates" the available Citation Styles into strings for a given key.
171                 * The returned string is displayed by the GUI.
172                 * Eg, the vector will contain:
173                 *  Jones et al. (1990), (Jones et al. 1990), Jones et al. 1990, ...
174                 * User supplies :
175                 *  the key,
176                 *  the buffer
177                 */
178         std::vector<docstring> const
179                         getAuthorYearStrings(docstring const & key, Buffer const & buf) const;
180         /// Collects the cited entries from buf.
181         void collectCitedEntries(Buffer const & buf);
182         /// A list of BibTeX keys cited in the current document, sorted by
183         /// the last name of the author.
184         std::vector<docstring> const & citedEntries() const { return cited_entries_; }
185         ///
186         void makeCitationLabels(Buffer const & buf);
187         ///
188         const_iterator begin() const { return bimap_.begin(); }
189         ///
190         void clear() { bimap_.clear(); }
191         ///
192         bool empty() const { return bimap_.empty(); }
193         ///
194         const_iterator end() const { return bimap_.end(); }
195         ///
196         const_iterator find(docstring const & f) const { return bimap_.find(f); }
197         ///
198         void mergeBiblioInfo(BiblioInfo const & info);
199         ///
200         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
201         ///
202         void addFieldName(docstring const & f) { field_names_.insert(f); }
203         ///
204         void addEntryType(docstring const & f) { entry_types_.insert(f); }
205 private:
206         ///
207         std::set<docstring> field_names_;
208         ///
209         std::set<docstring> entry_types_;
210         /// our map: keys --> BibTeXInfo
211         std::map<docstring, BibTeXInfo> bimap_;
212         /// a possibly sorted list of entries cited in our Buffer.
213         /// do not try to make this a vector<BibTeXInfo *> or anything of
214         /// the sort, because reloads will invalidate those pointers. 
215         std::vector<docstring> cited_entries_;
216 };
217
218 } // namespace lyx
219
220 #endif // BIBLIOINFO_H