]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.h
Merge branch 'master' of git.lyx.org:lyx
[lyx.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 "Citation.h"
21
22 #include <map>
23 #include <set>
24 #include <vector>
25
26
27 namespace lyx {
28
29 class Buffer;
30
31 /// \param latex_str a LaTeX command, "cite", "Citep*", etc
32 CitationStyle citationStyleFromString(std::string const & latex_str);
33 /// the other way round
34 std::string citationStyleToString(CitationStyle const &);
35
36
37 /// Class to represent information about a BibTeX or
38 /// bibliography entry.
39 /// This class basically wraps a std::map, and many of its
40 /// methods simply delegate to the corresponding methods of
41 /// std::map.
42 class BibTeXInfo {
43 public:
44         /// The keys are BibTeX fields (e.g., author, title, etc),
45         /// and the values are the associated field values.
46         typedef std::map<docstring, docstring>::const_iterator const_iterator;
47         ///
48         BibTeXInfo() : is_bibtex_(true), modifier_(0) {}
49         /// argument sets isBibTeX_, so should be false only if it's coming
50         /// from a bibliography environment
51         BibTeXInfo(bool ib) : is_bibtex_(ib), modifier_(0) {}
52         /// constructor that sets the entryType
53         BibTeXInfo(docstring const & key, docstring const & type);
54         /// \return the short form of an authorlist, used for sorting
55         docstring const getAbbreviatedAuthor(bool jurabib_style = false) const;
56         /// \return the short form of an authorlist, translated to the
57         /// buffer language.
58         docstring const getAbbreviatedAuthor(Buffer const & buf, bool jurabib_style = false) const;
59         ///
60         docstring const getYear() const;
61         ///
62         docstring const getXRef() const;
63         /// \return formatted BibTeX data suitable for framing.
64         /// \param pointer to crossref information
65         docstring const & getInfo(BibTeXInfo const * const xref,
66                         Buffer const & buf, bool richtext) const;
67         /// \return formatted BibTeX data for a citation label
68         docstring const getLabel(BibTeXInfo const * const xref,
69                 Buffer const & buf, docstring const & format, bool richtext,
70                 const docstring & before, const docstring & after,
71                 const docstring & dialog, bool next = false) const;
72         ///
73         const_iterator find(docstring const & f) const { return bimap_.find(f); }
74         ///
75         const_iterator end() const { return bimap_.end(); }
76         /// \return value for field f
77         /// note that this will create an empty field if it does not exist
78         docstring & operator[](docstring const & f)
79                 { return bimap_[f]; }
80         /// \return value for field f
81         /// this one, since it is const, will simply return docstring() if
82         /// we don't have the field and will NOT create an empty field
83         docstring const & operator[](docstring const & field) const;
84         ///
85         docstring const & operator[](std::string const & field) const;
86         ///
87         docstring const & allData() const { return all_data_; }
88         ///
89         void setAllData(docstring const & d) { all_data_ = d; }
90         ///
91         void label(docstring const & d) { label_= d; }
92         ///
93         void key(docstring const & d) { bib_key_= d; }
94         ///
95         docstring const & label() const { return label_; }
96         ///
97         docstring const & key() const { return bib_key_; }
98         /// numerical key for citing this entry. currently used only
99         /// by XHTML output routines.
100         docstring citeNumber() const { return cite_number_; }
101         ///
102         void setCiteNumber(docstring const & num) { cite_number_ = num; }
103         /// a,b,c, etc, for author-year. currently used only by XHTML
104         /// output routines.
105         char modifier() const { return modifier_; }
106         ///
107         void setModifier(char c) { modifier_ = c; }
108         ///
109         docstring entryType() const { return entry_type_; }
110         ///
111         bool isBibTeX() const { return is_bibtex_; }
112 private:
113         /// like operator[], except, if the field is empty, it will attempt
114         /// to get the data from xref BibTeXInfo object, which would normally
115         /// be the one referenced in the crossref field.
116         docstring getValueForKey(std::string const & key, Buffer const & buf,
117                 docstring const & before, docstring const & after, docstring const & dialog,
118                 BibTeXInfo const * const xref, 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                 BibTeXInfo const * const xref, int & counter,
137                 Buffer const & buf, docstring before = docstring(),
138                 docstring after = docstring(), docstring dialog = docstring(),
139                 bool next = 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         /// bibliography key --> data for that key
170         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
171         /// \return a sorted vector of bibliography keys
172         std::vector<docstring> const getKeys() const;
173         /// \return a sorted vector of present BibTeX fields
174         std::vector<docstring> const getFields() const;
175         /// \return a sorted vector of BibTeX entry types in use
176         std::vector<docstring> const getEntries() const;
177         /// \return the short form of an authorlist
178         docstring const getAbbreviatedAuthor(docstring const & key, Buffer const & buf) const;
179         /// \return the year from the bibtex data record for \param key
180         /// if \param use_modifier is true, then we will also append any
181         /// modifier for this entry (e.g., 1998b).
182         /// Note that this will get the year from the crossref if it's
183         /// not present in the record itself.
184         docstring const getYear(docstring const & key,
185                         bool use_modifier = false) const;
186         /// \return the year from the bibtex data record for \param key
187         /// if \param use_modifier is true, then we will also append any
188         /// modifier for this entry (e.g., 1998b).
189         /// Note that this will get the year from the crossref if it's
190         /// not present in the record itself.
191         /// If no year is found, \return "No year" translated to the buffer
192         /// language.
193         docstring const getYear(docstring const & key, Buffer const & buf,
194                         bool use_modifier = false) const;
195         ///
196         docstring const getCiteNumber(docstring const & key) const;
197         /// \return formatted BibTeX data associated with a given key.
198         /// Empty if no info exists.
199         /// Note that this will retrieve data from the crossref as needed.
200         /// If \param richtext is true, then it will output any richtext tags
201         /// marked in the citation format and escape < and > elsewhere.
202         docstring const getInfo(docstring const & key, Buffer const & buf,
203                         bool richtext = false) const;
204         /// \return formatted BibTeX data for citation labels.
205         /// Citation labels can have more than one key.
206         docstring const getLabel(std::vector<docstring> keys,
207                 Buffer const & buf, std::string const & style, bool for_xhtml,
208                 size_t max_size, docstring const & before, docstring const & after,
209                 docstring const & dialog = docstring()) const;
210         /// Is this a reference from a bibtex database
211         /// or from a bibliography environment?
212         bool isBibtex(docstring const & key) const;
213         /// Translates the available citation styles into strings for a given
214         /// list of keys, using either numerical or author-year style depending
215         /// upon the active engine.
216         std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys,
217                 std::vector<CitationStyle> const & styles, Buffer const & buf,
218                 docstring const & before, docstring const & after, docstring const & dialog,
219           size_t max_size) const;
220         /// A list of BibTeX keys cited in the current document, sorted by
221         /// the last name of the author.
222         /// Make sure you have called collectCitedEntries() before you try to
223         /// use this. You should probably call it just before you use this.
224         std::vector<docstring> const & citedEntries() const
225                 { return cited_entries_; }
226         ///
227         void makeCitationLabels(Buffer const & buf);
228         ///
229         const_iterator begin() const { return bimap_.begin(); }
230         ///
231         void clear() { bimap_.clear(); }
232         ///
233         bool empty() const { return bimap_.empty(); }
234         ///
235         const_iterator end() const { return bimap_.end(); }
236         ///
237         const_iterator find(docstring const & f) const { return bimap_.find(f); }
238         ///
239         void mergeBiblioInfo(BiblioInfo const & info);
240         ///
241         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
242         ///
243         void addFieldName(docstring const & f) { field_names_.insert(f); }
244         ///
245         void addEntryType(docstring const & f) { entry_types_.insert(f); }
246 private:
247         /// Collects the cited entries from buf.
248         void collectCitedEntries(Buffer const & buf);
249         ///
250         std::set<docstring> field_names_;
251         ///
252         std::set<docstring> entry_types_;
253         /// our map: keys --> BibTeXInfo
254         std::map<docstring, BibTeXInfo> bimap_;
255         /// a possibly sorted list of entries cited in our Buffer.
256         /// do not try to make this a vector<BibTeXInfo *> or anything of
257         /// the sort, because reloads will invalidate those pointers.
258         std::vector<docstring> cited_entries_;
259 };
260
261 } // namespace lyx
262
263 #endif // BIBLIOINFO_H