]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.h
150e77f0c494bf301b806c48a8e0cb8812395083
[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         ///
90         docstring citeKey() const { return cite_key_; }
91         ///
92         void setCiteKey(docstring const & k) { cite_key_ = k; }
93         ///
94         docstring entryType() const { return entry_type_; }
95         /// 
96         bool isBibTeX() const { return is_bibtex_; }
97 private:
98         /// like operator[], except, if the field is empty, it will attempt
99         /// to get the data from xref BibTeXInfo object, which would normally
100         /// be the one referenced in the crossref field.
101         docstring getValueForKey(std::string const & key, 
102                         BibTeXInfo const * const xref = 0) const;
103         /// true if from BibTeX; false if from bibliography environment
104         bool is_bibtex_;
105         /// the BibTeX key for this entry
106         docstring bib_key_;
107         /// the label that will appear in citations
108         /// this is easily set from bibliography environments, but has
109         /// to be calculated for entries we get from BibTeX
110         docstring label_;
111         /// a single string containing all BibTeX data associated with this key
112         docstring all_data_;
113         /// the BibTeX entry type (article, book, incollection, ...)
114         docstring entry_type_;
115         /// a cache for getInfo()
116         mutable docstring info_;
117         /// key to use when citing this entry 
118         /// currently used only by XHTML output routines
119         docstring cite_key_;
120         /// our map: <field, value>
121         std::map <docstring, docstring> bimap_;
122 };
123
124
125 /// Class to represent a collection of bibliographical data, whether
126 /// from BibTeX or from bibliography environments.
127 class BiblioInfo {
128 public:
129         /// bibliography key --> data for that key
130         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
131         /// \return a sorted vector of bibliography keys
132         std::vector<docstring> const getKeys() const;
133         /// \return a sorted vector of present BibTeX fields
134         std::vector<docstring> const getFields() const;
135         /// \return a sorted vector of BibTeX entry types in use
136         std::vector<docstring> const getEntries() const;
137         /// \return the short form of an authorlist
138         docstring const getAbbreviatedAuthor(docstring const & key) const;
139         /// \return the year from the bibtex data record
140         /// Note that this will get the year from the crossref if it's
141         /// not present in the record itself
142         docstring const getYear(docstring const & key) const;
143         /// \return formatted BibTeX data associated with a given key.
144         /// Empty if no info exists. 
145         /// Note that this will retrieve data from the crossref as needed.
146         docstring const getInfo(docstring const & key) const;
147         /**
148           * "Translates" the available Citation Styles into strings for a given key,
149           * either numerical or author-year depending upon the active engine. (See
150           * below for those methods.)
151           */
152         std::vector<docstring> const
153                         getCiteStrings(docstring const & key, Buffer const & buf) const;
154         /**
155                 * "Translates" the available Citation Styles into strings for a given key.
156                 * The returned string is displayed by the GUI.
157                 * [XX] is used in place of the actual reference
158                 * Eg, the vector will contain: [XX], Jones et al. [XX], ...
159                 * User supplies :
160                 *  the key,
161                 *  the buffer
162                 */
163         std::vector<docstring> const
164                         getNumericalStrings(docstring const & key, Buffer const & buf) const;
165         /**
166                 * "Translates" the available Citation Styles into strings for a given key.
167                 * The returned string is displayed by the GUI.
168                 * Eg, the vector will contain:
169                 *  Jones et al. (1990), (Jones et al. 1990), Jones et al. 1990, ...
170                 * User supplies :
171                 *  the key,
172                 *  the buffer
173                 */
174         std::vector<docstring> const
175                         getAuthorYearStrings(docstring const & key, Buffer const & buf) const;
176         /// Collects the cited entries from buf.
177         void collectCitedEntries(Buffer const & buf);
178         /// A list of BibTeX keys cited in the current document, sorted by
179         /// the last name of the author.
180         std::vector<docstring> const & citedEntries() const { return cited_entries_; }
181         ///
182         void makeCitationLabels(Buffer const & buf);
183         ///
184         const_iterator begin() const { return bimap_.begin(); }
185         ///
186         void clear() { bimap_.clear(); }
187         ///
188         bool empty() const { return bimap_.empty(); }
189         ///
190         const_iterator end() const { return bimap_.end(); }
191         ///
192         const_iterator find(docstring const & f) const { return bimap_.find(f); }
193         ///
194         void mergeBiblioInfo(BiblioInfo const & info);
195         ///
196         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
197         ///
198         void addFieldName(docstring const & f) { field_names_.insert(f); }
199         ///
200         void addEntryType(docstring const & f) { entry_types_.insert(f); }
201 private:
202         ///
203         std::set<docstring> field_names_;
204         ///
205         std::set<docstring> entry_types_;
206         /// our map: keys --> BibTeXInfo
207         std::map<docstring, BibTeXInfo> bimap_;
208         /// a possibly sorted list of entries cited in our Buffer.
209         /// do not try to make this a vector<BibTeXInfo *> or anything of
210         /// the sort, because reloads will invalidate those pointers. 
211         std::vector<docstring> cited_entries_;
212 };
213
214 } // namespace lyx
215
216 #endif // BIBLIOINFO_H