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