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