-
Notifications
You must be signed in to change notification settings - Fork 1
/
bibtex.py
executable file
·405 lines (310 loc) · 14.5 KB
/
bibtex.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""
= BibTeX Parser (version 1.5.3) =
MoinMoin processor for [http://www.bibtex.org BibTeX] entries.
Copyright (C) 2004-2007 Jun Hu <j.hu@tue.nl>
Copyright (C) 2004 Alexandre Duret-Lutz <adl@gnu.org>
Copyright (C) 2010-2011 Bruno Lopes <blopesvieira@gmail.com>
This module is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation;
[[TableOfContents]]
== Purpose ==
This module will run BibTeX blocks such as the following through
bibtex2html.
{{{
{ { {#!bibtex
@Book{aho.74,
author= {Alfred V. Aho and John E. Hopcroft and Jeffrey D. Ullman},
title = {The Design and Analysis of Computer Algorithms},
publisher= {Addison-Wesley},
year = {1974},
}
} } }
}}}
Several BibTeX entries can be listed into the same block (and they
can \cite each other). The 'comment' element can be used to
display a comment below each entry.
== HlinsDatabase ==
If your wiki contains a page named `HlinsDatabase`, then the output
of bibtex2html will be processed to add hyperlinks, using the
contents of `HlinsDatabase` as input. It will search for:
* first level bullet lists that are formatted as wiki external links:{{{
* [url name]
}}}
* first level bullet lists that contains a '=': {{{
* name = url
}}}
* and first level description lists {{{
name:: url
}}}
to build a mapping list.
For example, if there is a line {{{
* [http://www.google.com Google]
}}}
or {{{
* Google = http://www.google.com
}}}
or {{{
Google:: http://www.google.com
}}}
All the appearance of 'Google' in the bibtex2html output will be
replaced with {{{'<a href="http://www.google.com">Google</a>'}}}. The
white spaces around mapping pairs will be stripped.
HTML tags are not allowed to appear in the keywords to prevent
abuse.
== URL links and moin attachments ==
Bibtex2html uses some additional fields such as "ftp", "http",
"url", "ps" ,"dvi", "rtf", "pdf", "documenturl", "urlps" and "urldvi"
in bibtex database. Web links are automatically inserted.
The link name depends on the nature of the link:
* it is the file suffix, whenever this suffix is .dvi, .ps, .pdf,
.rtf, .txt or .html, possibly followed by a compression sufix,
.gz, .Z or .zip;
* otherwise the name of the link is either http or ftp depending
on the protocol.
This parser also supports moin attachment links in these fields.
for example:
{{{
{ { {#!bibtex abstract=off usekeys=off keywords=off style=alpha
@INPROCEEDINGS{Hu+JanseETAL-UserEvalDistInte:05,
author = {Hu, J. and Janse, M.D. and Kong, H.},
title = {User Evaluation on a Distributed Interactive Movie},
booktitle = {HCI International 2005 (accepted)},
year = {2005},
pdf = {attachment:HCII2005.pdf},
ps = {attachment:HCII2005.ps.gz},
}
} } }
}}}
== Parameters ==
style = [plain|unsrt|alpha|abbrv]:: default style is `plain`.
hlinsdb = <WikiPage>:: <WikiPage> will be used as input, instead
of the default `HlinsDatabase`
keywords = [on|off]:: whether to show keywords (if any),
default is `on`
abstract = [on|off]:: whether to show abstract (if any),
default is `on`
usekeys = [on|off]:: whether to use the cite keys from
the BibTeX input file (and not the ones generated by the BibTeX
style file). default is `on`.
== Download and installation ==
* bibtex2html: http://www.lri.fr/~filliatr/bibtex2html/
* [:/bibtex.py]. Please refer to MoinMoin documentation about how
to install a paser.
* The locations of bibtex2html and bibtex need to be configured.
Please add the following lines in wiki configuration:{{{
bibtex_bibtex = '/path/to/bibtex'
bibtex_bibtex2html = '/path/to/bibtex2html'
}}} Sometime it is also necessary to specify where the BibTeX style
files (.bst) are located. Then it is necessary to add a line in
wiki configuration: {{{
bibtex_bstfiles = '/path/to/bstfiles'
}}} Also see HelpOnConfiguration.
== Examples ==
* Formatted references:
* http://spot.lip6.fr/wiki/SpotReferences
* http://id00243.id.tue.nl/JunHu/Publications
* hyperlink database:
*http://id00243.id.tue.nl/HlinsDatabase
== Revision history ==
1.5.3 By BrunoLopes
Support to lower-case BibTeX style file names.
1.5.2 By BrunoLopes
Support to MoinMoin up to 1.9.
1.5.1 By JunHu
Thanks to Joachim Selke (http://www.joachim-selke.de) again for the suggestion
of moving the configuration lines in the code to wiki configuration. According
to Joachim, "This should simplify updates of bibtex.py a lot."
1.5.0 By JunHu
Version number jumps to 1.5.0 serials. Nothing really special
about this jump, just to indicate that this parser works well
in MoinMoin 1.5.
Thanks to Joachim Selke (http://www.joachim-selke.de) for the
ideas to improve the parser (quote from the email from Joachim):
* The documentation says that attachments can be included e.g. by using
the line `pdf = {attachment:foo.pdf}`. Unfortunately, on my system
this does not work. I had to use `pdf = {./attachment:foo.pdf}`. Thus,
I suggest the following change to bibtex.py:
Replace line 208 by
if not url.startswith('attachment:'):
Replace line 211 by
url = url[11:]
* I use MoinMoin on a server where no TeX system is installed and where
I am not allowed to install it. Therefore, I had to put the binaries
for bibtex and bibtex2html somewhere in my server directory and add
their locations to the lines 177 and 178 in bibtex.py. But this was
not enough to get bibtex to work. I also had to specify where the
BibTeX style files (.bst) are located. This can be set using the
environment variable BSTINPUTS. Thus, I suggest the following
additions to bibtex.py:
Insert at line 179:
bstfiles = '/path/to/bstfiles'
Insert at line 246:
os.environ["BSTINPUTS"] = bstfiles
Thank you, Joachim.
1.3.4 By JunHu
Added possibilities to refer to moin attachments in bibtex2html
additional field whose name is "ftp", "http", "url", "ps" ,
"dvi", "rtf", "pdf", "documenturl", "urlps" or "urldvi".
Limitation: Only the attachments attached to the same page is
allowed.
1.3.3-3 By JunHu
ReimarBauer had got some new ideas - formatting the link database
items as wiki external links in first level bullet lists. Thank
you very much for the nice idea and the example code.
1.3.3-2 By JunHu
ReimarBauer suggested to use the MoinMoin convention for
hyperlink database formatting. "In my eyes it is not good
to have special list rules to remember. ". I agree.
So the old formatting
rules from hlins and the ones introduced in 1.3.3 are dropped in
this version. However, in my eyes, a description list does not
look bad either, so we will use first level bullet lists, and
first level description lists as well.
1.3.3-1 By JunHu
This version does the hyperlink substitution by itself, and hlins is
not used anymore.
1.3.0 By JunHu, Rewritten for MoinMoin 1.3 as a parser. This version
has introduced some parameters.
Known bugs:
* For some strange reasons, hlins has problems with blank lines in
`HlinsDatabase`.
Please notice that if anything went wrong during processing, you
would possibly get an empty reslut.
hlins: http://www.lsv.ens-cachan.fr/~treinen/hlins/
1.2.0 By Alexandre Duret-Lutz
== Known Bugs ==
* This parser does not render CJK characters correctly - it might be
just a limitation of bibtex.
"""
from MoinMoin.parser._ParserBase import ParserBase
Dependencies = ['user']
import os,re
from MoinMoin.Page import Page
from MoinMoin import wikiutil
from MoinMoin.action import AttachFile
param = '--quiet -noheader -nofooter -nobibsource -nodoc -note comment -dl -i'
class Parser:
extensions = ['.bib']
def __init__(self, raw, request, **kw):
cfg = request.cfg
try:
self.bibtex2html = cfg.bibtex_bibtex2html
self.bibtex = cfg.bibtex_bibtex
except AttributeError:
pass
try:
self.bstfiles = cfg.bibtex_bstfiles
except AttributeError:
pass
self.raw = raw.encode('utf-8')
self.request = request
self.form = request.form
self._ = request.getText
self.out = kw.get('out', request)
attrs, msg = wikiutil.parseAttributes(request, kw.get('format_args',''))
self.style = attrs.get('style','"plain"')[1:-1]
self.hlinsdb = attrs.get('hlinsdb','"HlinsDatabase"')[1:-1]
self.abstract = self.keywords = self.usekeys = True
if attrs.get('abstract','"on"')[1:-1].lower() in ('off', 'no', 'none', 'false'):
self.abstract = False
if attrs.get('keywords','"on"')[1:-1].lower() in ('off', 'no', 'none', 'false'):
self.keywords = False
if attrs.get('usekeys','"on"')[1:-1].lower() in ('off', 'no', 'none', 'false'):
self.usekeys = False
def wikirepl(self, match):
url = match.group('url')
if not url.startswith('attachment:'):
return match.group()
url = url[11:]
attname=wikiutil.taintfilename(self.formatter.text(url))
current_pagename=self.formatter.page.page_name
attachment_path = os.path.join(AttachFile.getAttachDir(self.request,current_pagename), attname)
if not os.path.exists(attachment_path):
import urllib
linktext = self.request.getText('Upload new attachment "%(filename)s"')
return wikiutil.link_tag(self.request,
'%s?action=AttachFile&rename=%s' %
(current_pagename,
urllib.quote_plus(attname)),
linktext % {'filename': attname})
return ( match.group('pre') +
AttachFile.getAttachUrl(current_pagename,attname,self.request) +
match.group('suf'))
def format(self, formatter):
self.formatter = formatter
text = self.raw
output =''
settings = '-s ' + self.style
if not self.abstract:
settings = settings + ' -noabstract'
if not self.keywords:
settings = settings + ' -nokeywords'
if self.usekeys:
settings = settings + ' -use-keys'
try:
os.environ["BSTINPUTS"] = self.bstfiles
except AttributeError:
pass
try:
pin, pout, perr = os.popen3('%s -c %s %s %s ' % (self.bibtex2html, self.bibtex, param, settings))
pin.write(text)
pin.flush()
pin.close()
output = '\n'.join(pout.readlines())
pout.close()
perr.close()
except AttributeError:
output='''The locations of bibtex2html and bibtex have not been configured.
Please add the following lines in wiki configuration:
bibtex_bibtex = '/path/to/bibtex'
bibtex_bibtex2html = '/path/to/bibtex2html'
Sometime it is also necessary to specify where the BibTeX style files (.bst)
are located. Then it is necessary to add a line in wiki configuration:
bstfiles = '/path/to/bstfiles'
(Also see HelpOnConfiguration).'''
self.out.write(formatter.preformatted(1)+ output+ formatter.preformatted(0))
return
except IOError:
output = '\n'.join(perr.readlines())
pin.close()
pout.close()
perr.close()
self.out.write(formatter.preformatted(1)+ output+ formatter.preformatted(0))
return
output = wikiutil.decodeUnknownInput(output)
#attachments
p = re.compile("""(?P<pre>\<a\s+href=")
(?P<url>.*?)
(?P<suf>"\>(\.dvi|\.ps|\.pdf|\.rtf|\.txt|\.html)(\.gz|\.Z|\.zip)?\</a\>)""",
re.I|re.X)
output = p.sub(self.wikirepl, output)
#process the hyperlinks db
page = Page(self.request, self.hlinsdb)
quotes = wikiutil.decodeUnknownInput(page.get_raw_body())
if not self.request.user.may.read(self.hlinsdb):
quotes = ''
if quotes != '' and output != '':
for line in quotes.splitlines():
name,link = '',''
if line.startswith(' *'):
line = line[2:].strip()
if line.startswith('[[') and line.endswith(']]') and line.find('|')>-1:
link,name = line[2:-2].split('|',1)
elif line.find('=')>-1:
name,link = line.split('=',1)
elif line.startswith(' ') and line.find(':: ')>-1:
name,link = line.split(':: ',1)
name,link = name.strip(),link.strip()
if name == '' or link == '' :
continue
url=formatter.url(1, link)+' ' + name + formatter.url(0)
# no html markup tags allowed
name = name.replace('<','<').replace('>','>')
name = re.escape(name)
# to fit white spaces
name = name.replace('\\ ', '(( )|\s)+')
output = re.compile(name).sub(url, output)
# To please Springer-Verlag.
output = output.replace('Springer-Verlag', '©Springer-Verlag')
self.out.write(formatter.rawHTML(output))