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