]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.h
77c3dab729119273e0efa3fda12e5b7db2c9e31c
[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         /// \return the short form of an authorlist
57         docstring const getAbbreviatedAuthor() const;
58         /// 
59         docstring const getYear() const;
60         ///
61         docstring const getXRef() const;
62         /// \return formatted BibTeX data suitable for framing.
63         /// \param pointer to crossref information
64         docstring const & getInfo(BibTeXInfo const * const xref = 0) const;
65         ///
66         const_iterator find(docstring const & f) const { return bimap_.find(f); }
67         ///
68         const_iterator end() const { return bimap_.end(); }
69         /// \return value for field f
70         /// note that this will create an empty field if it does not exist
71         docstring & operator[](docstring const & f) 
72                 { return bimap_[f]; }
73         /// \return value for field f
74         /// this one, since it is const, will simply return docstring() if
75         /// we don't have the field and will NOT create an empty field
76         docstring const & operator[](docstring const & field) const;
77         ///
78         docstring const & operator[](std::string const & field) const;
79         ///
80         docstring const & allData() const { return all_data_; }
81         ///
82         void setAllData(docstring const & d) { all_data_ = d; }
83         ///
84         void label(docstring const & d) { label_= d; }
85         ///
86         docstring const & label() const { return label_; }
87         ///
88         docstring const & key() const { return bib_key_; }
89         /// numerical key for citing this entry. currently used only
90         /// by XHTML output routines.
91         docstring citeNumber() const { return cite_number_; }
92         ///
93         void setCiteNumber(docstring const & num) { cite_number_ = num; }
94         /// a,b,c, etc, for author-year. currently used only by XHTML 
95         /// output routines.
96         char modifier() const { return modifier_; }
97         ///
98         void setModifier(char c) { modifier_ = c; }
99         ///
100         docstring entryType() const { return entry_type_; }
101         /// 
102         bool isBibTeX() const { return is_bibtex_; }
103 private:
104         /// like operator[], except, if the field is empty, it will attempt
105         /// to get the data from xref BibTeXInfo object, which would normally
106         /// be the one referenced in the crossref field.
107         docstring getValueForKey(std::string const & key, 
108                         BibTeXInfo const * const xref = 0) const;
109         /// true if from BibTeX; false if from bibliography environment
110         bool is_bibtex_;
111         /// the BibTeX key for this entry
112         docstring bib_key_;
113         /// the label that will appear in citations
114         /// this is easily set from bibliography environments, but has
115         /// to be calculated for entries we get from BibTeX
116         docstring label_;
117         /// a single string containing all BibTeX data associated with this key
118         docstring all_data_;
119         /// the BibTeX entry type (article, book, incollection, ...)
120         docstring entry_type_;
121         /// a cache for getInfo()
122         mutable docstring info_;
123         /// 
124         docstring cite_number_;
125         ///
126         char modifier_;
127         /// our map: <field, value>
128         std::map <docstring, docstring> bimap_;
129 };
130
131
132 /// Class to represent a collection of bibliographical data, whether
133 /// from BibTeX or from bibliography environments.
134 class BiblioInfo {
135 public:
136         /// bibliography key --> data for that key
137         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
138         /// \return a sorted vector of bibliography keys
139         std::vector<docstring> const getKeys() const;
140         /// \return a sorted vector of present BibTeX fields
141         std::vector<docstring> const getFields() const;
142         /// \return a sorted vector of BibTeX entry types in use
143         std::vector<docstring> const getEntries() const;
144         /// \return the short form of an authorlist
145         docstring const getAbbreviatedAuthor(docstring const & key) const;
146         /// \return the year from the bibtex data record for \param key
147         /// if \param use_modifier is true, then we will also append any
148         /// modifier for this entry (e.g., 1998b).
149         /// Note that this will get the year from the crossref if it's
150         /// not present in the record itself.   
151         docstring const getYear(docstring const & key,
152                         bool use_modifier = false) const;
153         ///
154         docstring const getCiteNumber(docstring const & key) const;
155         /// \return formatted BibTeX data associated with a given key.
156         /// Empty if no info exists. 
157         /// Note that this will retrieve data from the crossref as needed.
158         docstring const getInfo(docstring const & key) const;
159         /**
160           * "Translates" the available Citation Styles into strings for a given key,
161           * either numerical or author-year depending upon the active engine. (See
162           * below for those methods.)
163           */
164         std::vector<docstring> const
165                         getCiteStrings(docstring const & key, Buffer const & buf) const;
166         /**
167                 * "Translates" the available Citation Styles into strings for a given key.
168                 * The returned string is displayed by the GUI.
169                 * [XX] is used in place of the actual reference
170                 * Eg, the vector will contain: [XX], Jones et al. [XX], ...
171                 * User supplies :
172                 *  the key,
173                 *  the buffer
174                 */
175         std::vector<docstring> const
176                         getNumericalStrings(docstring const & key, Buffer const & buf) const;
177         /**
178                 * "Translates" the available Citation Styles into strings for a given key.
179                 * The returned string is displayed by the GUI.
180                 * Eg, the vector will contain:
181                 *  Jones et al. (1990), (Jones et al. 1990), Jones et al. 1990, ...
182                 * User supplies :
183                 *  the key,
184                 *  the buffer
185                 */
186         std::vector<docstring> const
187                         getAuthorYearStrings(docstring const & key, Buffer const & buf) const;
188         /// Collects the cited entries from buf.
189         void collectCitedEntries(Buffer const & buf);
190         /// A list of BibTeX keys cited in the current document, sorted by
191         /// the last name of the author.
192         /// Make sure you have called collectCitedEntries() before you try to 
193         /// use this. You should probably call it just before you use this.
194         std::vector<docstring> const & citedEntries() const 
195                 { return cited_entries_; }
196         ///
197         void makeCitationLabels(Buffer const & buf);
198         ///
199         const_iterator begin() const { return bimap_.begin(); }
200         ///
201         void clear() { bimap_.clear(); }
202         ///
203         bool empty() const { return bimap_.empty(); }
204         ///
205         const_iterator end() const { return bimap_.end(); }
206         ///
207         const_iterator find(docstring const & f) const { return bimap_.find(f); }
208         ///
209         void mergeBiblioInfo(BiblioInfo const & info);
210         ///
211         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
212         ///
213         void addFieldName(docstring const & f) { field_names_.insert(f); }
214         ///
215         void addEntryType(docstring const & f) { entry_types_.insert(f); }
216 private:
217         ///
218         std::set<docstring> field_names_;
219         ///
220         std::set<docstring> entry_types_;
221         /// our map: keys --> BibTeXInfo
222         std::map<docstring, BibTeXInfo> bimap_;
223         /// a possibly sorted list of entries cited in our Buffer.
224         /// do not try to make this a vector<BibTeXInfo *> or anything of
225         /// the sort, because reloads will invalidate those pointers. 
226         std::vector<docstring> cited_entries_;
227 };
228
229 } // namespace lyx
230
231 #endif // BIBLIOINFO_H