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