4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
10 * \author Julien Rioux
12 * Full author contact details are available in file CREDITS.
18 #include "support/docstring.h"
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 &);
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
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;
48 BibTeXInfo() : is_bibtex_(true) {}
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) {}
52 /// constructor that sets the entryType
53 BibTeXInfo(docstring const & key, docstring const & type);
54 /// \return the short form of an authorlist
55 docstring const getAbbreviatedAuthor(bool jurabib_style = false, std::string lang = "en") const;
57 docstring const getYear() const;
59 docstring const getXRef() const;
60 /// \return formatted BibTeX data suitable for framing.
61 /// \param pointer to crossref information
62 docstring const & getInfo(BibTeXInfo const * const xref,
63 Buffer const & buf, bool richtext) const;
64 /// \return formatted BibTeX data for a citation label
65 docstring const getLabel(BibTeXInfo const * const xref,
66 Buffer const & buf, std::string const & format, bool richtext,
67 docstring before, docstring after, docstring dialog, bool next = false) const;
69 const_iterator find(docstring const & f) const { return bimap_.find(f); }
71 const_iterator end() const { return bimap_.end(); }
72 /// \return value for field f
73 /// note that this will create an empty field if it does not exist
74 docstring & operator[](docstring const & f)
76 /// \return value for field f
77 /// this one, since it is const, will simply return docstring() if
78 /// we don't have the field and will NOT create an empty field
79 docstring const & operator[](docstring const & field) const;
81 docstring const & operator[](std::string const & field) const;
83 docstring const & allData() const { return all_data_; }
85 void setAllData(docstring const & d) { all_data_ = d; }
87 void label(docstring const & d) { label_= d; }
89 void key(docstring const & d) { bib_key_= d; }
91 docstring const & label() const { return label_; }
93 docstring const & key() const { return bib_key_; }
94 /// numerical key for citing this entry. currently used only
95 /// by XHTML output routines.
96 docstring citeNumber() const { return cite_number_; }
98 void setCiteNumber(docstring const & num) { cite_number_ = num; }
99 /// a,b,c, etc, for author-year. currently used only by XHTML
101 char modifier() const { return modifier_; }
103 void setModifier(char c) { modifier_ = c; }
105 docstring entryType() const { return entry_type_; }
107 bool isBibTeX() const { return is_bibtex_; }
109 /// like operator[], except, if the field is empty, it will attempt
110 /// to get the data from xref BibTeXInfo object, which would normally
111 /// be the one referenced in the crossref field.
112 docstring getValueForKey(std::string const & key,
113 docstring const & before, docstring const & after, docstring const & dialog,
114 BibTeXInfo const * const xref = 0, std::string lang = "en") const;
115 /// replace %keys% in a format string with their values
116 /// called from getInfo()
117 /// format strings may contain:
118 /// %key%, which represents a key
119 /// {%key%[[format]]}, which prints format if key is non-empty
120 /// the latter may optionally contain an `else' clause as well:
121 /// {%key%[[if format]][[else format]]}
122 /// Material intended only for rich text (HTML) output should be
123 /// wrapped in "{!" and "!}". These markers are to be postprocessed
124 /// by processRichtext(); this step is left as a separate routine since
125 /// expandFormat() is recursive while postprocessing should be done
126 /// only once on the final string (just like convertLaTeXCommands).
127 /// a simple macro facility is also available. keys that look like
128 /// "%!key%" are substituted with their definition.
129 /// moreover, keys that look like "%_key%" are treated as translatable
130 /// so that things like "pp." and "vol." can be translated.
131 docstring expandFormat(std::string const & fmt,
132 BibTeXInfo const * const xref, int & counter,
133 Buffer const & buf, docstring before = docstring(),
134 docstring after = docstring(), docstring dialog = docstring(),
135 bool next = false) const;
136 /// true if from BibTeX; false if from bibliography environment
138 /// the BibTeX key for this entry
140 /// the label that will appear in citations
141 /// this is easily set from bibliography environments, but has
142 /// to be calculated for entries we get from BibTeX
144 /// a single string containing all BibTeX data associated with this key
146 /// the BibTeX entry type (article, book, incollection, ...)
147 docstring entry_type_;
148 /// a cache for getInfo()
149 mutable docstring info_;
150 /// a cache for getInfo(richtext = true)
151 mutable docstring info_richtext_;
153 docstring cite_number_;
156 /// our map: <field, value>
157 std::map <docstring, docstring> bimap_;
161 /// Class to represent a collection of bibliographical data, whether
162 /// from BibTeX or from bibliography environments.
165 /// bibliography key --> data for that key
166 typedef std::map<docstring, BibTeXInfo>::const_iterator const_iterator;
167 /// \return a sorted vector of bibliography keys
168 std::vector<docstring> const getKeys() const;
169 /// \return a sorted vector of present BibTeX fields
170 std::vector<docstring> const getFields() const;
171 /// \return a sorted vector of BibTeX entry types in use
172 std::vector<docstring> const getEntries() const;
173 /// \return the short form of an authorlist
174 docstring const getAbbreviatedAuthor(docstring const & key, std::string lang = "en") const;
175 /// \return the year from the bibtex data record for \param key
176 /// if \param use_modifier is true, then we will also append any
177 /// modifier for this entry (e.g., 1998b).
178 /// Note that this will get the year from the crossref if it's
179 /// not present in the record itself.
180 docstring const getYear(docstring const & key,
181 bool use_modifier = false, std::string lang = "en") const;
183 docstring const getCiteNumber(docstring const & key) const;
184 /// \return formatted BibTeX data associated with a given key.
185 /// Empty if no info exists.
186 /// Note that this will retrieve data from the crossref as needed.
187 /// If \param richtext is true, then it will output any richtext tags
188 /// marked in the citation format and escape < and > elsewhere.
189 docstring const getInfo(docstring const & key, Buffer const & buf,
190 bool richtext = false) const;
191 /// \return formatted BibTeX data for citation labels.
192 /// Citation labels can have more than one key.
193 docstring const getLabel(std::vector<docstring> const & keys,
194 Buffer const & buf, std::string const & style, bool richtext = false,
195 docstring const & before = docstring(),
196 docstring const & after = docstring(),
197 docstring const & dialog = docstring()) const;
198 /// Is this a reference from a bibtex database
199 /// or from a bibliography environment?
200 bool isBibtex(docstring const & key) const;
201 /// Translates the available citation styles into strings for a given
202 /// list of keys, using either numerical or author-year style depending
203 /// upon the active engine.
204 std::vector<docstring> const getCiteStrings(std::vector<docstring> const & keys,
205 std::vector<CitationStyle> const & styles, Buffer const & buf, bool richtext = false,
206 docstring const & before = docstring(),
207 docstring const & after = docstring(),
208 docstring const & dialog = docstring()) const;
209 /// Collects the cited entries from buf.
210 void collectCitedEntries(Buffer const & buf);
211 /// A list of BibTeX keys cited in the current document, sorted by
212 /// the last name of the author.
213 /// Make sure you have called collectCitedEntries() before you try to
214 /// use this. You should probably call it just before you use this.
215 std::vector<docstring> const & citedEntries() const
216 { return cited_entries_; }
218 void makeCitationLabels(Buffer const & buf);
220 const_iterator begin() const { return bimap_.begin(); }
222 void clear() { bimap_.clear(); }
224 bool empty() const { return bimap_.empty(); }
226 const_iterator end() const { return bimap_.end(); }
228 const_iterator find(docstring const & f) const { return bimap_.find(f); }
230 void mergeBiblioInfo(BiblioInfo const & info);
232 BibTeXInfo & operator[](docstring const & f) { return bimap_[f]; }
234 void addFieldName(docstring const & f) { field_names_.insert(f); }
236 void addEntryType(docstring const & f) { entry_types_.insert(f); }
239 std::set<docstring> field_names_;
241 std::set<docstring> entry_types_;
242 /// our map: keys --> BibTeXInfo
243 std::map<docstring, BibTeXInfo> bimap_;
244 /// a possibly sorted list of entries cited in our Buffer.
245 /// do not try to make this a vector<BibTeXInfo *> or anything of
246 /// the sort, because reloads will invalidate those pointers.
247 std::vector<docstring> cited_entries_;
252 #endif // BIBLIOINFO_H