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