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