]> git.lyx.org Git - features.git/blob - src/BiblioInfo.h
Merge branch 'master' into biblatex2
[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  * \author Julien Rioux
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef BIBLIOINFO_H
16 #define BIBLIOINFO_H
17
18 #include "support/docstring.h"
19
20 #include "BufferParams.h"
21 #include "Citation.h"
22
23 #include <map>
24 #include <set>
25 #include <vector>
26
27
28 namespace lyx {
29
30 class Buffer;
31
32 /// \param latex_str a LaTeX command, "cite", "Citep*", etc
33 CitationStyle citationStyleFromString(std::string const & latex_str,
34                                       BufferParams const &);
35 /// the other way round
36 std::string citationStyleToString(CitationStyle const &, bool const latex = false);
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         typedef std::vector<BibTeXInfo const *> const BibTeXInfoList;
51         ///
52         BibTeXInfo() : is_bibtex_(true), modifier_(0) {}
53         /// argument sets isBibTeX_, so should be false only if it's coming
54         /// from a bibliography environment
55         BibTeXInfo(bool ib) : is_bibtex_(ib), modifier_(0) {}
56         /// constructor that sets the entryType
57         BibTeXInfo(docstring const & key, docstring const & type);
58         /// \return the an authorlist (short form by default),
59         /// used for sorting.
60         /// This will be translated to the UI language if buf is null
61         /// otherwise, it will be translated to the buffer language.
62         docstring const getAuthorList(Buffer const * buf = 0, bool full = false,
63                                       bool forceshort = false) const;
64         ///
65         docstring const getYear() const;
66         /// \return formatted BibTeX data suitable for framing.
67         /// \param vector of pointers to crossref/xdata information
68         docstring const & getInfo(BibTeXInfoList const xrefs,
69                         Buffer const & buf, CiteItem const & ci) const;
70         /// \return formatted BibTeX data for a citation label
71         docstring const getLabel(BibTeXInfoList const xrefs,
72                 Buffer const & buf, docstring const & format,
73                 CiteItem const & ci, bool next = false, bool second = false) const;
74         ///
75         const_iterator find(docstring const & f) const { return bimap_.find(f); }
76         ///
77         const_iterator end() const { return bimap_.end(); }
78         /// \return value for field f
79         /// note that this will create an empty field if it does not exist
80         docstring & operator[](docstring const & f)
81                 { return bimap_[f]; }
82         /// \return value for field f
83         /// this one, since it is const, will simply return docstring() if
84         /// we don't have the field and will NOT create an empty field
85         docstring const & operator[](docstring const & field) const;
86         ///
87         docstring const & operator[](std::string const & field) const;
88         ///
89         docstring const & allData() const { return all_data_; }
90         ///
91         void setAllData(docstring const & d) { all_data_ = d; }
92         ///
93         void label(docstring const & d) { label_= d; }
94         ///
95         void key(docstring const & d) { bib_key_= d; }
96         ///
97         docstring const & label() const { return label_; }
98         ///
99         docstring const & key() const { return bib_key_; }
100         /// numerical key for citing this entry. currently used only
101         /// by XHTML output routines.
102         docstring citeNumber() const { return cite_number_; }
103         ///
104         void setCiteNumber(docstring const & num) { cite_number_ = num; }
105         /// a,b,c, etc, for author-year. currently used only by XHTML
106         /// output routines.
107         char modifier() const { return modifier_; }
108         ///
109         void setModifier(char c) { modifier_ = c; }
110         ///
111         docstring entryType() const { return entry_type_; }
112         ///
113         bool isBibTeX() const { return is_bibtex_; }
114 private:
115         /// like operator[], except, if the field is empty, it will attempt
116         /// to get the data from xref BibTeXInfo objects, which would normally
117         /// be the one referenced in the crossref or xdata field.
118         docstring getValueForKey(std::string const & key, Buffer const & buf,
119                 CiteItem const & ci, BibTeXInfoList const xrefs, size_t maxsize = 4096) const;
120         /// replace %keys% in a format string with their values
121         /// called from getInfo()
122         /// format strings may contain:
123         ///   %key%, which represents a key
124         ///   {%key%[[format]]}, which prints format if key is non-empty
125         /// the latter may optionally contain an `else' clause as well:
126         ///   {%key%[[if format]][[else format]]}
127         /// Material intended only for rich text (HTML) output should be
128         /// wrapped in "{!" and "!}". These markers are to be postprocessed
129         /// by processRichtext(); this step is left as a separate routine since
130         /// expandFormat() is recursive while postprocessing should be done
131         /// only once on the final string (just like convertLaTeXCommands).
132         /// a simple macro facility is also available. keys that look like
133         /// "%!key%" are substituted with their definition.
134         /// moreover, keys that look like "%_key%" are treated as translatable
135         /// so that things like "pp." and "vol." can be translated.
136         docstring expandFormat(docstring const & fmt,
137                 BibTeXInfoList const xrefs, int & counter,
138                 Buffer const & buf, CiteItem const & ci,
139                 bool next = false, bool second = false) const;
140         /// true if from BibTeX; false if from bibliography environment
141         bool is_bibtex_;
142         /// the BibTeX key for this entry
143         docstring bib_key_;
144         /// the label that will appear in citations
145         /// this is easily set from bibliography environments, but has
146         /// to be calculated for entries we get from BibTeX
147         docstring label_;
148         /// a single string containing all BibTeX data associated with this key
149         docstring all_data_;
150         /// the BibTeX entry type (article, book, incollection, ...)
151         docstring entry_type_;
152         /// a cache for getInfo()
153         mutable docstring info_;
154         /// a cache for getInfo(richtext = true)
155         mutable docstring info_richtext_;
156         ///
157         docstring cite_number_;
158         ///
159         char modifier_;
160         /// our map: <field, value>
161         std::map <docstring, docstring> bimap_;
162 };
163
164
165 /// Class to represent a collection of bibliographical data, whether
166 /// from BibTeX or from bibliography environments.
167 class BiblioInfo {
168 public:
169         ///
170         typedef std::vector<BibTeXInfo const *> BibTeXInfoList;
171         /// bibliography key --> data for that key
172         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
173         /// Get a vector with all external data (crossref, xdata)
174         std::vector<docstring> const getXRefs(BibTeXInfo const & data,
175                                               bool const nested = false) const;
176         /// \return a sorted vector of bibliography keys
177         std::vector<docstring> const getKeys() const;
178         /// \return a sorted vector of present BibTeX fields
179         std::vector<docstring> const getFields() const;
180         /// \return a sorted vector of BibTeX entry types in use
181         std::vector<docstring> const getEntries() const;
182         /// \return authorlist (abbreviated form by default)
183         docstring const getAuthorList(docstring const & key, Buffer const & buf) const;
184         /// \return the year from the bibtex data record for \param key
185         /// if \param use_modifier is true, then we will also append any
186         /// modifier for this entry (e.g., 1998b).
187         /// If no legacy year field is present, check for date (common in
188         /// biblatex) and extract the year from there.
189         /// Note further that this will get the year from the crossref or xdata
190         /// if it's not present in the record itself.
191         docstring const getYear(docstring const & key,
192                         bool use_modifier = false) const;
193         /// \return the year from the bibtex data record for \param key
194         /// if \param use_modifier is true, then we will also append any
195         /// modifier for this entry (e.g., 1998b).
196         /// If no legacy year field is present, check for date (common in
197         /// biblatex) and extract the year from there.
198         /// Note further that this will get the year from the crossref or xdata
199         /// if it's not present in the record itself.
200         /// If no year is found, \return "No year" translated to the buffer
201         /// language.
202         docstring const getYear(docstring const & key, Buffer const & buf,
203                         bool use_modifier = false) const;
204         ///
205         docstring const getCiteNumber(docstring const & key) const;
206         /// \return formatted BibTeX data associated with a given key.
207         /// Empty if no info exists.
208         /// Note that this will retrieve data from the crossref or xdata as needed.
209         /// \param ci contains further context information, such as if it should
210         /// output any richtext tags marked in the citation format and escape < and >
211         /// elsewhere, and the general output context.
212         docstring const getInfo(docstring const & key, Buffer const & buf,
213                         CiteItem const & ci) const;
214         /// \return formatted BibTeX data for citation labels.
215         /// Citation labels can have more than one key.
216         docstring const getLabel(std::vector<docstring> keys, Buffer const & buf,
217                                  std::string const & style, CiteItem const & ci) const;
218         /// Is this a reference from a bibtex database
219         /// or from a bibliography environment?
220         bool isBibtex(docstring const & key) const;
221         /// Translates the available citation styles into strings for a given
222         /// list of keys, using either numerical or author-year style depending
223         /// upon the active engine.
224         std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys,
225                 std::vector<CitationStyle> const & styles, Buffer const & buf,
226                 CiteItem const & ci) const;
227         /// A list of BibTeX keys cited in the current document, sorted by
228         /// the last name of the author.
229         /// Make sure you have called collectCitedEntries() before you try to
230         /// use this. You should probably call it just before you use this.
231         std::vector<docstring> const & citedEntries() const
232                 { return cited_entries_; }
233         ///
234         void makeCitationLabels(Buffer const & buf);
235         ///
236         const_iterator begin() const { return bimap_.begin(); }
237         ///
238         void clear() { bimap_.clear(); }
239         ///
240         bool empty() const { return bimap_.empty(); }
241         ///
242         const_iterator end() const { return bimap_.end(); }
243         ///
244         const_iterator find(docstring const & f) const { return bimap_.find(f); }
245         ///
246         void mergeBiblioInfo(BiblioInfo const & info);
247         ///
248         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
249         ///
250         void addFieldName(docstring const & f) { field_names_.insert(f); }
251         ///
252         void addEntryType(docstring const & f) { entry_types_.insert(f); }
253 private:
254         /// Collects the cited entries from buf.
255         void collectCitedEntries(Buffer const & buf);
256         ///
257         std::set<docstring> field_names_;
258         ///
259         std::set<docstring> entry_types_;
260         /// our map: keys --> BibTeXInfo
261         std::map<docstring, BibTeXInfo> bimap_;
262         /// a possibly sorted list of entries cited in our Buffer.
263         /// do not try to make this a vector<BibTeXInfo *> or anything of
264         /// the sort, because reloads will invalidate those pointers.
265         std::vector<docstring> cited_entries_;
266 };
267
268 } // namespace lyx
269
270 #endif // BIBLIOINFO_H