-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibimport.py
177 lines (148 loc) · 4.52 KB
/
bibimport.py
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
#!/usr/local/bin/python
################################################################################
# File : bib.py
# Author : Andrew Howard
# Date : 8 Mar 2001
# Desc : Bibliography file used by restyle.py
#
# CVS info:
# $Source: /cvsroot/playerstage/www/player-www/bibimport.py,v $
# $Author: ahoward $
# $Revision: 1.1.1.1 $
#
# Usage:
# (empty)
#
# Requirements:
# (empty)
#
# Known bugs:
# (empty)
#
# Theory of operation:
# (empty)
#
# Possible enhancements:
# (empty)
################################################################################
import sys
import string
import os
# Define the bibliography
#
bib = {}
# Class for bib entries
#
class BibEntry:
type = ""
key = ""
author = ""
title = ""
booktitle = ""
year = ""
volume = ""
number = ""
pages = "?--?"
publisher = ""
institution = ''
# Make an entry in the bibliography
#
def make_entry(entry):
if entry.type == 'conference':
return ('%s, "%s", <i>%s</i>, pages %s, %s' %
(entry.author, entry.title, entry.booktitle, entry.pages, entry.year))
if entry.type == 'journal':
return ('%s, "%s", <i>%s</i>, %s %s, pages %s, %s' %
(entry.author, entry.title, entry.booktitle,
entry.volume, entry.number, entry.pages, entry.year))
if entry.type == 'collection':
return ('%s, "%s", <i>%s</i>, %s, pages %s, %s' %
(entry.author, entry.title, entry.booktitle,
entry.publisher, entry.pages, entry.year))
if entry.type == 'phdthesis':
return ('%s, "%s", PhD thesis, %s, %s' %
(entry.author, entry.title, entry.school, entry.year))
# Make an author
#
def make_author(words):
count = 0
names = string.split(words)
# Count the total number of ands
#
for n in names:
if n == "and":
count = count + 1
# Replace all but the last one
#
return string.replace(words, ' and ', ', ', count - 1)
# Read in the bibliography
#
def readbib(filename):
file = open(filename, "r")
if not file:
return
entry = BibEntry()
while (1):
line = file.readline();
if not line:
break
line = string.replace(line, '\n', '')
line = string.replace(line, '\r', '')
# Look for start and end of entry
#
if string.lower(line[:15]) == '@inproceedings{':
entry.type = "conference"
entry.key = string.strip(line[15:])[:-1]
elif string.lower(line[:9]) == '@article{':
entry.type = "journal"
entry.key = string.strip(line[9:])[:-1]
elif string.lower(line[:14]) == '@incollection{':
entry.type = "collection"
entry.key = string.strip(line[14:])[:-1]
elif string.lower(line[:11]) == '@phdthesis{':
entry.type = "phdthesis"
entry.key = string.strip(line[11:])[:-1]
elif line[:1] == '}':
if entry.type != "":
bib[entry.key] = make_entry(entry)
entry = BibEntry()
if entry.type == "":
continue
# Parse a line from the body of an entry
#
words = string.split(line, '=', 2)
if len(words) < 2:
continue
# Strip out all those undesitable chars
#
words[0] = string.strip(words[0])
words[1] = string.strip(words[1])
words[1] = string.replace(words[1], '"', '')
words[1] = string.replace(words[1], '{', '')
words[1] = string.replace(words[1], '}', '')
words[1] = string.replace(words[1], ',', '')
words[1] = string.replace(words[1], "\\'", '')
#print entry.key + " [" + words[0] + "] [" + words[1] + "]"
if words[0] == 'author':
entry.author = make_author(words[1])
elif words[0] == 'title':
entry.title = words[1]
elif words[0] == 'booktitle':
entry.booktitle = words[1]
elif words[0] == 'journal':
entry.booktitle = words[1]
elif words[0] == 'year':
entry.year = words[1]
elif words[0] == 'volume':
entry.volume = words[1]
elif words[0] == 'number':
entry.number = words[1]
elif words[0] == 'pages':
entry.pages = words[1]
elif words[0] == 'publisher':
entry.publisher = words[1]
elif words[0] == 'school':
entry.school = words[1]
if __name__ == "__main__":
readbib('../pubs/bib/robotics.bib')
print bib