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