]> git.lyx.org Git - lyx.git/blob - src/BiblioInfo.h
Update my email and status.
[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  *
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 /// \param latex_str a LaTeX command, "cite", "Citep*", etc
31 CitationStyle citationStyleFromString(std::string const & latex_str);
32 /// the other way round
33 std::string citationStyleToString(CitationStyle const &);
34
35
36 /// Class to represent information about a BibTeX or
37 /// bibliography entry.
38 /// This class basically wraps a std::map, and many of its
39 /// methods simply delegate to the corresponding methods of
40 /// std::map.
41 class BibTeXInfo {
42 public:
43         /// The keys are BibTeX fields (e.g., author, title, etc),
44         /// and the values are the associated field values.
45         typedef std::map<docstring, docstring>::const_iterator const_iterator;
46         ///
47         BibTeXInfo() : is_bibtex_(true) {}
48         /// argument sets isBibTeX_, so should be false only if it's coming
49         /// from a bibliography environment
50         BibTeXInfo(bool ib) : is_bibtex_(ib) {}
51         /// constructor that sets the entryType
52         BibTeXInfo(docstring const & key, docstring const & type);
53         /// \return the short form of an authorlist
54         docstring const getAbbreviatedAuthor(bool jurabib_style = false) const;
55         ///
56         docstring const getYear() const;
57         ///
58         docstring const getXRef() const;
59         /// \return formatted BibTeX data suitable for framing.
60         /// \param pointer to crossref information
61         docstring const & getInfo(BibTeXInfo const * const xref,
62                         Buffer const & buf, bool richtext) const;
63         /// \return formatted BibTeX data for a citation label
64         docstring const getLabel(BibTeXInfo const * const xref,
65                 Buffer const & buf, std::string const & format, bool richtext,
66                 docstring before, docstring after, docstring dialog, bool next = false) const;
67         ///
68         const_iterator find(docstring const & f) const { return bimap_.find(f); }
69         ///
70         const_iterator end() const { return bimap_.end(); }
71         /// \return value for field f
72         /// note that this will create an empty field if it does not exist
73         docstring & operator[](docstring const & f)
74                 { return bimap_[f]; }
75         /// \return value for field f
76         /// this one, since it is const, will simply return docstring() if
77         /// we don't have the field and will NOT create an empty field
78         docstring const & operator[](docstring const & field) const;
79         ///
80         docstring const & operator[](std::string const & field) const;
81         ///
82         docstring const & allData() const { return all_data_; }
83         ///
84         void setAllData(docstring const & d) { all_data_ = d; }
85         ///
86         void label(docstring const & d) { label_= d; }
87         ///
88         void key(docstring const & d) { bib_key_= d; }
89         ///
90         docstring const & label() const { return label_; }
91         ///
92         docstring const & key() const { return bib_key_; }
93         /// numerical key for citing this entry. currently used only
94         /// by XHTML output routines.
95         docstring citeNumber() const { return cite_number_; }
96         ///
97         void setCiteNumber(docstring const & num) { cite_number_ = num; }
98         /// a,b,c, etc, for author-year. currently used only by XHTML
99         /// output routines.
100         char modifier() const { return modifier_; }
101         ///
102         void setModifier(char c) { modifier_ = c; }
103         ///
104         docstring entryType() const { return entry_type_; }
105         ///
106         bool isBibTeX() const { return is_bibtex_; }
107 private:
108         /// like operator[], except, if the field is empty, it will attempt
109         /// to get the data from xref BibTeXInfo object, which would normally
110         /// be the one referenced in the crossref field.
111         docstring getValueForKey(std::string const & key,
112                 docstring const & before, docstring const & after, docstring const & dialog,
113                 BibTeXInfo const * const xref = 0) const;
114         /// replace %keys% in a format string with their values
115         /// called from getInfo()
116         /// format strings may contain:
117         ///   %key%, which represents a key
118         ///   {%key%[[format]]}, which prints format if key is non-empty
119         /// the latter may optionally contain an `else' clause as well:
120         ///   {%key%[[if format]][[else format]]}
121         /// material intended only for rich text (HTML) output should be
122         /// wrapped in "{!" and "!}". it will be removed if richtext is
123         /// false.
124         /// a simple macro facility is also available. keys that look like
125         /// "%!key%" are substituted with their definition.
126         /// moreover, keys that look like "%_key%" are treated as translatable
127         /// so that things like "pp." and "vol." can be translated.
128         docstring expandFormat(std::string const & fmt,
129                 BibTeXInfo const * const xref, int & counter,
130                 Buffer const & buf, bool richtext, docstring before = docstring(),
131                 docstring after = docstring(), docstring dialog = docstring(), bool next = false) const;
132         /// true if from BibTeX; false if from bibliography environment
133         bool is_bibtex_;
134         /// the BibTeX key for this entry
135         docstring bib_key_;
136         /// the label that will appear in citations
137         /// this is easily set from bibliography environments, but has
138         /// to be calculated for entries we get from BibTeX
139         docstring label_;
140         /// a single string containing all BibTeX data associated with this key
141         docstring all_data_;
142         /// the BibTeX entry type (article, book, incollection, ...)
143         docstring entry_type_;
144         /// a cache for getInfo()
145         mutable docstring info_;
146         ///
147         docstring cite_number_;
148         ///
149         char modifier_;
150         /// our map: <field, value>
151         std::map <docstring, docstring> bimap_;
152 };
153
154
155 /// Class to represent a collection of bibliographical data, whether
156 /// from BibTeX or from bibliography environments.
157 class BiblioInfo {
158 public:
159         /// bibliography key --> data for that key
160         typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
161         /// \return a sorted vector of bibliography keys
162         std::vector<docstring> const getKeys() const;
163         /// \return a sorted vector of present BibTeX fields
164         std::vector<docstring> const getFields() const;
165         /// \return a sorted vector of BibTeX entry types in use
166         std::vector<docstring> const getEntries() const;
167         /// \return the short form of an authorlist
168         docstring const getAbbreviatedAuthor(docstring const & key) const;
169         /// \return the year from the bibtex data record for \param key
170         /// if \param use_modifier is true, then we will also append any
171         /// modifier for this entry (e.g., 1998b).
172         /// Note that this will get the year from the crossref if it's
173         /// not present in the record itself.
174         docstring const getYear(docstring const & key,
175                         bool use_modifier = false) const;
176         ///
177         docstring const getCiteNumber(docstring const & key) const;
178         /// \return formatted BibTeX data associated with a given key.
179         /// Empty if no info exists.
180         /// Note that this will retrieve data from the crossref as needed.
181         /// If \param richtext is true, then it will output any richtext tags
182         /// marked in the citation format and escape < and > elsewhere.
183         docstring const getInfo(docstring const & key, Buffer const & buf,
184                         bool richtext = false) const;
185         /// \return formatted BibTeX data for citation labels.
186         /// Citation labels can have more than one key.
187         docstring const getLabel(std::vector<docstring> const & keys,
188                 Buffer const & buf, std::string const & style, bool richtext = false,
189                 docstring const & before = docstring(),
190                 docstring const & after = docstring(),
191                 docstring const & dialog = docstring()) const;
192         /// Is this a reference from a bibtex database
193         /// or from a bibliography environment?
194         bool isBibtex(docstring const & key) const;
195         /// Translates the available citation styles into strings for a given
196         /// list of keys, using either numerical or author-year style depending
197         /// upon the active engine.
198         std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys,
199                 std::vector<CitationStyle> const & styles, Buffer const & buf, bool richtext = false,
200                 docstring const & before = docstring(),
201                 docstring const & after = docstring(),
202                 docstring const & dialog = docstring()) const;
203         /// Collects the cited entries from buf.
204         void collectCitedEntries(Buffer const & buf);
205         /// A list of BibTeX keys cited in the current document, sorted by
206         /// the last name of the author.
207         /// Make sure you have called collectCitedEntries() before you try to
208         /// use this. You should probably call it just before you use this.
209         std::vector<docstring> const & citedEntries() const
210                 { return cited_entries_; }
211         ///
212         void makeCitationLabels(Buffer const & buf);
213         ///
214         const_iterator begin() const { return bimap_.begin(); }
215         ///
216         void clear() { bimap_.clear(); }
217         ///
218         bool empty() const { return bimap_.empty(); }
219         ///
220         const_iterator end() const { return bimap_.end(); }
221         ///
222         const_iterator find(docstring const & f) const { return bimap_.find(f); }
223         ///
224         void mergeBiblioInfo(BiblioInfo const & info);
225         ///
226         BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
227         ///
228         void addFieldName(docstring const & f) { field_names_.insert(f); }
229         ///
230         void addEntryType(docstring const & f) { entry_types_.insert(f); }
231 private:
232         ///
233         std::set<docstring> field_names_;
234         ///
235         std::set<docstring> entry_types_;
236         /// our map: keys --> BibTeXInfo
237         std::map<docstring, BibTeXInfo> bimap_;
238         /// a possibly sorted list of entries cited in our Buffer.
239         /// do not try to make this a vector<BibTeXInfo *> or anything of
240         /// the sort, because reloads will invalidate those pointers.
241         std::vector<docstring> cited_entries_;
242 };
243
244 } // namespace lyx
245
246 #endif // BIBLIOINFO_H