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