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