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