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