]> git.lyx.org Git - features.git/blob - src/BiblioInfo.h
Cache the getInfo() return value. This will matter shortly.
[features.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         /// \return formatted BibTeX data suitable for framing.
63         docstring const getInfo() const;
64         ///
65         int count(docstring const & f) const { return bimap_.count(f); }
66         ///
67         const_iterator find(docstring const & f) const { return bimap_.find(f); }
68         ///
69         const_iterator end() const { return bimap_.end(); }
70         /// \return value for field f
71         /// note that this will create an empty field if it does not exist
72         docstring & operator[](docstring const & f) 
73                 { return bimap_[f]; }
74         /// \return value for field f
75         /// this one, since it is const, will simply return docstring() if
76         /// we don't have the field and will NOT create an empty field
77         docstring const & operator[](docstring const & field) const;
78         ///
79         docstring const & operator[](std::string const & field) const;
80         ///
81         docstring const & allData() const { return all_data_; }
82         ///
83         void setAllData(docstring const & d) { all_data_ = d; }
84         ///
85         docstring entryType() const { return entry_type_; }
86 private:
87         /// true if from BibTeX; false if from bibliography environment
88         bool is_bibtex_;
89         /// the BibTeX key for this entry
90         docstring bib_key_;
91         /// a single string containing all BibTeX data associated with this key
92         docstring all_data_;
93         /// the BibTeX entry type (article, book, incollection, ...)
94         docstring entry_type_;
95         /// a cache for getInfo()
96         mutable docstring info_;
97         /// our map: <field, value>
98         std::map <docstring, docstring> bimap_;
99 };
100
101
102 /// Class to represent a collection of bibliographical data, whether
103 /// from BibTeX or from bibliography environments.
104 class BiblioInfo {
105 public:
106         /// bibliography key --> data for that key
107         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
108         /// \return a sorted vector of bibliography keys
109         std::vector<docstring> const getKeys() const;
110         /// \return a sorted vector of present BibTeX fields
111         std::vector<docstring> const getFields() const;
112         /// \return a sorted vector of BibTeX entry types in use
113         std::vector<docstring> const getEntries() const;
114         /// \return the short form of an authorlist
115         docstring const getAbbreviatedAuthor(docstring const & key) const;
116         /// \return the year from the bibtex data record
117         docstring const getYear(docstring const & key) const;
118         /// \return formatted BibTeX data associated with a given key.
119         /// Empty if no info exists. 
120         docstring const getInfo(docstring const & key) const;
121         /**
122           * "Translates" the available Citation Styles into strings for a given key,
123           * either numerical or author-year depending upon the active engine. (See
124           * below for those methods.)
125           */
126         std::vector<docstring> const
127                         getCiteStrings(docstring const & key, Buffer const & buf) const;
128         /**
129                 * "Translates" the available Citation Styles into strings for a given key.
130                 * The returned string is displayed by the GUI.
131                 * [XX] is used in place of the actual reference
132                 * Eg, the vector will contain: [XX], Jones et al. [XX], ...
133                 * User supplies :
134                 *  the key,
135                 *  the buffer
136                 */
137         std::vector<docstring> const
138                         getNumericalStrings(docstring const & key, Buffer const & buf) const;
139         /**
140                 * "Translates" the available Citation Styles into strings for a given key.
141                 * The returned string is displayed by the GUI.
142                 * Eg, the vector will contain:
143                 *  Jones et al. (1990), (Jones et al. 1990), Jones et al. 1990, ...
144                 * User supplies :
145                 *  the key,
146                 *  the buffer
147                 */
148         std::vector<docstring> const
149                         getAuthorYearStrings(docstring const & key, Buffer const & buf) const;
150         ///
151         const_iterator begin() const { return bimap_.begin(); }
152         ///
153         void clear() { bimap_.clear(); }
154         ///
155         bool empty() const { return bimap_.empty(); }
156         ///
157         const_iterator end() const { return bimap_.end(); }
158         ///
159         const_iterator find(docstring const & f) const { return bimap_.find(f); }
160         ///
161         void mergeBiblioInfo(BiblioInfo const & info);
162         ///
163         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
164         ///
165         void addFieldName(docstring const & f) { field_names_.insert(f); }
166         ///
167         void addEntryType(docstring const & f) { entry_types_.insert(f); }
168 private:
169         ///
170         std::set<docstring> field_names_;
171         ///
172         std::set<docstring> entry_types_;
173         /// our map: keys --> BibTeXInfo
174         std::map<docstring, BibTeXInfo> bimap_;
175 };
176
177 } // namespace lyx
178
179 #endif // BIBLIOINFO_H