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