1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/***************************************************************************
copyright : (C) 2003-2006 by Robby Stephenson
email : robby@periapsis.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "bibtexexporter.h"
#include "bibtexhandler.h"
#include "../document.h"
#include "../collections/bibtexcollection.h"
#include "../latin1literal.h"
#include "../filehandler.h"
#include "../stringset.h"
#include "../tellico_debug.h"
#include <config.h>
#include <klocale.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <kcombobox.h>
#include <tqregexp.h>
#include <tqcheckbox.h>
#include <tqlayout.h>
#include <tqgroupbox.h>
#include <tqwhatsthis.h>
#include <tqlabel.h>
#include <tqhbox.h>
using Tellico::Export::BibtexExporter;
BibtexExporter::BibtexExporter() : Tellico::Export::Exporter(),
m_expandMacros(false),
m_packageURL(true),
m_skipEmptyKeys(false),
m_widget(0) {
}
TQString BibtexExporter::formatString() const {
return i18n("Bibtex");
}
TQString BibtexExporter::fileFilter() const {
return i18n("*.bib|Bibtex Files (*.bib)") + TQChar('\n') + i18n("*|All Files");
}
bool BibtexExporter::exec() {
Data::CollPtr c = collection();
if(!c || c->type() != Data::Collection::Bibtex) {
return false;
}
const Data::BibtexCollection* coll = static_cast<const Data::BibtexCollection*>(c.data());
// there are some special attributes
// the entry-type specifies the entry type - book, inproceedings, whatever
TQString typeField;
// the key specifies the cite-key
TQString keyField;
// the crossref bibtex field can reference another entry
TQString crossRefField;
bool hasCrossRefs = false;
const TQString bibtex = TQString::fromLatin1("bibtex");
// keep a list of all the 'ordinary' fields to iterate through later
Data::FieldVec fields;
Data::FieldVec vec = coll->fields();
for(Data::FieldVec::Iterator it = vec.begin(); it != vec.end(); ++it) {
TQString bibtexField = it->property(bibtex);
if(bibtexField == Latin1Literal("entry-type")) {
typeField = it->name();
} else if(bibtexField == Latin1Literal("key")) {
keyField = it->name();
} else if(bibtexField == Latin1Literal("crossref")) {
fields.append(it); // still output crossref field
crossRefField = it->name();
hasCrossRefs = true;
} else if(!bibtexField.isEmpty()) {
fields.append(it);
}
}
if(typeField.isEmpty() || keyField.isEmpty()) {
kdWarning() << "BibtexExporter::exec() - the collection must have fields defining "
"the entry-type and the key of the entry" << endl;
return false;
}
if(fields.isEmpty()) {
kdWarning() << "BibtexExporter::exec() - no bibtex field mapping exists in the collection." << endl;
return false;
}
TQString text = TQString::fromLatin1("@comment{Generated by Tellico ")
+ TQString::fromLatin1(VERSION)
+ TQString::fromLatin1("}\n\n");
if(!coll->preamble().isEmpty()) {
text += TQString::fromLatin1("@preamble{") + coll->preamble() + TQString::fromLatin1("}\n\n");
}
const TQStringList macros = coll->macroList().keys();
if(!m_expandMacros) {
TQMap<TQString, TQString>::ConstIterator macroIt;
for(macroIt = coll->macroList().constBegin(); macroIt != coll->macroList().constEnd(); ++macroIt) {
if(!macroIt.data().isEmpty()) {
text += TQString::fromLatin1("@string{")
+ macroIt.key()
+ TQString::fromLatin1("=")
+ BibtexHandler::exportText(macroIt.data(), macros)
+ TQString::fromLatin1("}\n\n");
}
}
}
// if anything is crossref'd, we have to do an initial scan through the
// whole collection first
StringSet crossRefKeys;
if(hasCrossRefs) {
for(Data::EntryVec::ConstIterator entryIt = entries().begin(); entryIt != entries().end(); ++entryIt) {
crossRefKeys.add(entryIt->field(crossRefField));
}
}
StringSet usedKeys;
Data::ConstEntryVec crossRefs;
TQString type, key, newKey, value;
for(Data::EntryVec::ConstIterator entryIt = entries().begin(); entryIt != entries().end(); ++entryIt) {
type = entryIt->field(typeField);
if(type.isEmpty()) {
kdWarning() << "BibtexExporter::text() - the entry for '" << entryIt->title()
<< "' has no entry-type, skipping it!" << endl;
continue;
}
key = entryIt->field(keyField);
if(key.isEmpty()) {
if(m_skipEmptyKeys) {
continue;
}
key = BibtexHandler::bibtexKey(entryIt.data());
} else {
// check crossrefs, only counts for non-empty keys
// if this entry is crossref'd, add it to the list, and skip it
if(hasCrossRefs && crossRefKeys.has(key)) {
crossRefs.append(entryIt.data());
continue;
}
}
newKey = key;
char c = 'a';
while(usedKeys.has(newKey)) {
// duplicate found!
newKey = key + c;
++c;
}
key = newKey;
usedKeys.add(key);
writeEntryText(text, fields, *entryIt, type, key);
}
// now write out crossrefs
for(Data::ConstEntryVec::Iterator entryIt = crossRefs.begin(); entryIt != crossRefs.end(); ++entryIt) {
// no need to check type
key = entryIt->field(keyField);
newKey = key;
char c = 'a';
while(usedKeys.has(newKey)) {
// duplicate found!
newKey = key + c;
++c;
}
key = newKey;
usedKeys.add(key);
writeEntryText(text, fields, *entryIt, entryIt->field(typeField), key);
}
return FileHandler::writeTextURL(url(), text, options() & ExportUTF8, options() & Export::ExportForce);
}
TQWidget* BibtexExporter::widget(TQWidget* parent_, const char* name_/*=0*/) {
if(m_widget && TQT_BASE_OBJECT(m_widget->parent()) == TQT_BASE_OBJECT(parent_)) {
return m_widget;
}
m_widget = new TQWidget(parent_, name_);
TQVBoxLayout* l = new TQVBoxLayout(m_widget);
TQGroupBox* box = new TQGroupBox(1, Qt::Horizontal, i18n("Bibtex Options"), m_widget);
l->addWidget(box);
m_checkExpandMacros = new TQCheckBox(i18n("Expand string macros"), box);
m_checkExpandMacros->setChecked(m_expandMacros);
TQWhatsThis::add(m_checkExpandMacros, i18n("If checked, the string macros will be expanded and no "
"@string{} entries will be written."));
m_checkPackageURL = new TQCheckBox(i18n("Use URL package"), box);
m_checkPackageURL->setChecked(m_packageURL);
TQWhatsThis::add(m_checkPackageURL, i18n("If checked, any URL fields will be wrapped in a "
"\\url declaration."));
m_checkSkipEmpty = new TQCheckBox(i18n("Skip entries with empty citation keys"), box);
m_checkSkipEmpty->setChecked(m_skipEmptyKeys);
TQWhatsThis::add(m_checkSkipEmpty, i18n("If checked, any entries without a bibtex citation key "
"will be skipped."));
TQHBox* hbox = new TQHBox(box);
TQLabel* l1 = new TQLabel(i18n("Bibtex quotation style:") + ' ', hbox); // add a space for astheticss
m_cbBibtexStyle = new KComboBox(hbox);
m_cbBibtexStyle->insertItem(i18n("Braces"));
m_cbBibtexStyle->insertItem(i18n("Quotes"));
TQString whats = i18n("<qt>The quotation style used when exporting bibtex. All field values will "
" be escaped with either braces or quotation marks.</qt>");
TQWhatsThis::add(l1, whats);
TQWhatsThis::add(m_cbBibtexStyle, whats);
if(BibtexHandler::s_quoteStyle == BibtexHandler::BRACES) {
m_cbBibtexStyle->setCurrentItem(i18n("Braces"));
} else {
m_cbBibtexStyle->setCurrentItem(i18n("Quotes"));
}
l->addStretch(1);
return m_widget;
}
void BibtexExporter::readOptions(TDEConfig* config_) {
TDEConfigGroup group(config_, TQString::fromLatin1("ExportOptions - %1").arg(formatString()));
m_expandMacros = group.readBoolEntry("Expand Macros", m_expandMacros);
m_packageURL = group.readBoolEntry("URL Package", m_packageURL);
m_skipEmptyKeys = group.readBoolEntry("Skip Empty Keys", m_skipEmptyKeys);
if(group.readBoolEntry("Use Braces", true)) {
BibtexHandler::s_quoteStyle = BibtexHandler::BRACES;
} else {
BibtexHandler::s_quoteStyle = BibtexHandler::QUOTES;
}
}
void BibtexExporter::saveOptions(TDEConfig* config_) {
TDEConfigGroup group(config_, TQString::fromLatin1("ExportOptions - %1").arg(formatString()));
m_expandMacros = m_checkExpandMacros->isChecked();
group.writeEntry("Expand Macros", m_expandMacros);
m_packageURL = m_checkPackageURL->isChecked();
group.writeEntry("URL Package", m_packageURL);
m_skipEmptyKeys = m_checkSkipEmpty->isChecked();
group.writeEntry("Skip Empty Keys", m_skipEmptyKeys);
bool useBraces = m_cbBibtexStyle->currentText() == i18n("Braces");
group.writeEntry("Use Braces", useBraces);
if(useBraces) {
BibtexHandler::s_quoteStyle = BibtexHandler::BRACES;
} else {
BibtexHandler::s_quoteStyle = BibtexHandler::QUOTES;
}
}
void BibtexExporter::writeEntryText(TQString& text_, const Data::FieldVec& fields_, const Data::Entry& entry_,
const TQString& type_, const TQString& key_) {
const TQStringList macros = static_cast<const Data::BibtexCollection*>(Data::Document::self()->collection().data())->macroList().keys();
const TQString bibtex = TQString::fromLatin1("bibtex");
const TQString bibtexSep = TQString::fromLatin1("bibtex-separator");
text_ += '@' + type_ + '{' + key_;
TQString value;
Data::FieldVec::ConstIterator fIt, end = fields_.constEnd();
bool format = options() & Export::ExportFormatted;
for(fIt = fields_.constBegin(); fIt != end; ++fIt) {
value = entry_.field(fIt->name(), format);
if(value.isEmpty()) {
continue;
}
// If the entry is formatted as a name and allows multiple values
// insert "and" in between them (e.g. author and editor)
if(fIt->formatFlag() == Data::Field::FormatName
&& fIt->flags() & Data::Field::AllowMultiple) {
value.replace(Data::Field::delimiter(), TQString::fromLatin1(" and "));
} else if(fIt->flags() & Data::Field::AllowMultiple) {
TQString bibsep = fIt->property(bibtexSep);
if(!bibsep.isEmpty()) {
value.replace(Data::Field::delimiter(), bibsep);
}
} else if(fIt->type() == Data::Field::Para) {
// strip HTML from bibtex export
TQRegExp stripHTML(TQString::fromLatin1("<.*>"), true);
stripHTML.setMinimal(true);
value.remove(stripHTML);
} else if(fIt->property(bibtex) == Latin1Literal("pages")) {
TQRegExp rx(TQString::fromLatin1("(\\d)-(\\d)"));
for(int pos = rx.search(value); pos > -1; pos = rx.search(value, pos+2)) {
value.replace(pos, 3, rx.cap(1)+"--"+rx.cap(2));
}
}
if(m_packageURL && fIt->type() == Data::Field::URL) {
bool b = BibtexHandler::s_quoteStyle == BibtexHandler::BRACES;
value = (b ? TQChar('{') : TQChar('"'))
+ TQString::fromLatin1("\\url{") + BibtexHandler::exportText(value, macros) + TQChar('}')
+ (b ? TQChar('}') : TQChar('"'));
} else if(fIt->type() != Data::Field::Number) {
// numbers aren't escaped, nor will they have macros
// if m_expandMacros is true, then macros is empty, so this is ok even then
value = BibtexHandler::exportText(value, macros);
}
text_ += TQString::fromLatin1(",\n ")
+ fIt->property(bibtex)
+ TQString::fromLatin1(" = ")
+ value;
}
text_ += TQString::fromLatin1("\n}\n\n");
}
#include "bibtexexporter.moc"
|