1 """
2 = BibTeX Parser (version 1.5.1) =
3
4 MoinMoin processor for [http://www.bibtex.org BibTeX] entries.
5
6 Copyright (C) 2004-2007 Jun Hu <j.hu@tue.nl>
7
8 Copyright (C) 2004 Alexandre Duret-Lutz <adl@gnu.org>
9
10 This module is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation;
13
14 [[TableOfContents]]
15
16 == Purpose ==
17
18 This module will run BibTeX blocks such as the following through
19 bibtex2html.
20
21 {{{
22 { { {#!bibtex
23 @Book{aho.74,
24 author= {Alfred V. Aho and John E. Hopcroft and Jeffrey D. Ullman},
25 title = {The Design and Analysis of Computer Algorithms},
26 publisher= {Addison-Wesley},
27 year = {1974},
28 }
29 } } }
30 }}}
31
32 Several BibTeX entries can be listed into the same block (and they
33 can \cite each other). The 'comment' element can be used to
34 display a comment below each entry.
35
36 == HlinsDatabase ==
37
38 If your wiki contains a page named `HlinsDatabase`, then the output
39 of bibtex2html will be processed to add hyperlinks, using the
40 contents of `HlinsDatabase` as input. It will search for:
41 * first level bullet lists that are formatted as wiki external links:{{{
42 * [url name]
43 }}}
44 * first level bullet lists that contains a '=': {{{
45 * name = url
46 }}}
47 * and first level description lists {{{
48 name:: url
49 }}}
50
51 to build a mapping list.
52
53 For example, if there is a line {{{
54 * [http://www.google.com Google]
55 }}}
56
57 or {{{
58 * Google = http://www.google.com
59 }}}
60
61 or {{{
62 Google:: http://www.google.com
63 }}}
64
65 All the appearance of 'Google' in the bibtex2html output will be
66 replaced with {{{'<a href="http://www.google.com">Google</a>'}}}. The
67 white spaces around mapping pairs will be stripped.
68
69 HTML tags are not allowed to appear in the keywords to prevent
70 abuse.
71
72 == URL links and moin attachments ==
73
74 Bibtex2html uses some additional fields such as "ftp", "http",
75 "url", "ps" ,"dvi", "rtf", "pdf", "documenturl", "urlps" and "urldvi"
76 in bibtex database. Web links are automatically inserted.
77 The link name depends on the nature of the link:
78 * it is the file suffix, whenever this suffix is .dvi, .ps, .pdf,
79 .rtf, .txt or .html, possibly followed by a compression sufix,
80 .gz, .Z or .zip;
81 * otherwise the name of the link is either http or ftp depending
82 on the protocol.
83
84 This parser also supports moin attachment links in these fields.
85 for example:
86 {{{
87 { { {#!bibtex abstract=off usekeys=off keywords=off style=alpha
88 @INPROCEEDINGS{Hu+JanseETAL-UserEvalDistInte:05,
89 author = {Hu, J. and Janse, M.D. and Kong, H.},
90 title = {User Evaluation on a Distributed Interactive Movie},
91 booktitle = {HCI International 2005 (accepted)},
92 year = {2005},
93 pdf = {attachment:HCII2005.pdf},
94 ps = {attachment:HCII2005.ps.gz},
95 }
96 } } }
97 }}}
98
99 == Parameters ==
100
101 style = [plain|unsrt|alpha|abbrv]:: default style is `plain`.
102 hlinsdb = <WikiPage>:: <WikiPage> will be used as input, instead
103 of the default `HlinsDatabase`
104 keywords = [on|off]:: whether to show keywords (if any),
105 default is `on`
106 abstract = [on|off]:: whether to show abstract (if any),
107 default is `on`
108 usekeys = [on|off]:: whether to use the cite keys from
109 the BibTeX input file (and not the ones generated by the BibTeX
110 style file). default is `on`.
111
112 == Download and installation ==
113 * bibtex2html: http://www.lri.fr/~filliatr/bibtex2html/
114 * [:/bibtex.py]. Please refer to MoinMoin documentation about how
115 to install a paser.
116 * The locations of bibtex2html and bibtex need to be configured.
117 Please add the following lines in wiki configuration:{{{
118 bibtex_bibtex = '/path/to/bibtex'
119 bibtex_bibtex2html = '/path/to/bibtex2html'
120 }}} Sometime it is also necessary to specify where the BibTeX style
121 files (.bst) are located. Then it is necessary to add a line in
122 wiki configuration: {{{
123 bibtex_bstfiles = '/path/to/bstfiles'
124 }}} Also see HelpOnConfiguration.
125
126
127 == Examples ==
128 * Formatted references:
129 * http://spot.lip6.fr/wiki/SpotReferences
130 * http://wiki.id.tue.nl/JunHu/Publications
131 * hyperlink database:
132 *http://wiki.id.tue.nl/HlinsDatabase
133
134
135
136 == Revision history ==
137 1.5.1 By JunHu
138 Thanks to Joachim Selke (http://www.joachim-selke.de) again for the suggestion
139 of moving the configuration lines in the code to wiki configuration. According
140 to Joachim, "This should simplify updates of bibtex.py a lot."
141
142
143 1.5.0 By JunHu
144 Version number jumps to 1.5.0 serials. Nothing really special
145 about this jump, just to indicate that this parser works well
146 in MoinMoin 1.5.
147
148 Thanks to Joachim Selke (http://www.joachim-selke.de) for the
149 ideas to improve the parser (quote from the email from Joachim):
150
151 * The documentation says that attachments can be included e.g. by using
152 the line `pdf = {attachment:foo.pdf}`. Unfortunately, on my system
153 this does not work. I had to use `pdf = {./attachment:foo.pdf}`. Thus,
154 I suggest the following change to bibtex.py:
155
156 Replace line 208 by
157 if not url.startswith('attachment:'):
158
159 Replace line 211 by
160 url = url[11:]
161
162 * I use MoinMoin on a server where no TeX system is installed and where
163 I am not allowed to install it. Therefore, I had to put the binaries
164 for bibtex and bibtex2html somewhere in my server directory and add
165 their locations to the lines 177 and 178 in bibtex.py. But this was
166 not enough to get bibtex to work. I also had to specify where the
167 BibTeX style files (.bst) are located. This can be set using the
168 environment variable BSTINPUTS. Thus, I suggest the following
169 additions to bibtex.py:
170
171 Insert at line 179:
172 bstfiles = '/path/to/bstfiles'
173
174 Insert at line 246:
175 os.environ["BSTINPUTS"] = bstfiles
176
177 Thank you, Joachim.
178
179
180 1.3.4 By JunHu
181 Added possibilities to refer to moin attachments in bibtex2html
182 additional field whose name is "ftp", "http", "url", "ps" ,
183 "dvi", "rtf", "pdf", "documenturl", "urlps" or "urldvi".
184 Limitation: Only the attachments attached to the same page is
185 allowed.
186
187 1.3.3-3 By JunHu
188 ReimarBauer had got some new ideas - formatting the link database
189 items as wiki external links in first level bullet lists. Thank
190 you very much for the nice idea and the example code.
191
192 1.3.3-2 By JunHu
193 ReimarBauer suggested to use the MoinMoin convention for
194 hyperlink database formatting. "In my eyes it is not good
195 to have special list rules to remember. ". I agree.
196
197 So the old formatting
198 rules from hlins and the ones introduced in 1.3.3 are dropped in
199 this version. However, in my eyes, a description list does not
200 look bad either, so we will use first level bullet lists, and
201 first level description lists as well.
202
203 1.3.3-1 By JunHu
204 This version does the hyperlink substitution by itself, and hlins is
205 not used anymore.
206
207 1.3.0 By JunHu, Rewritten for MoinMoin 1.3 as a parser. This version
208 has introduced some parameters.
209
210 Known bugs:
211 * For some strange reasons, hlins has problems with blank lines in
212 `HlinsDatabase`.
213
214 Please notice that if anything went wrong during processing, you
215 would possibly get an empty reslut.
216
217 hlins: http://www.lsv.ens-cachan.fr/~treinen/hlins/
218
219 1.2.0 By Alexandre Duret-Lutz
220
221
222 == Known Bugs ==
223 * This parser does not render CJK characters correctly - it might be
224 just a limitation of bibtex.
225
226 """
227
228 Dependencies = []
229
230 import os,re
231 from MoinMoin.Page import Page
232 from MoinMoin import wikiutil
233 from MoinMoin.action import AttachFile
234
235 param = '--quiet -noheader -nofooter -nobibsource -nodoc -note comment -dl -i'
236
237 class Parser:
238
239 extensions = ['.bib']
240
241 def __init__(self, raw, request, **kw):
242 cfg = request.cfg
243 try:
244 self.bibtex2html = cfg.bibtex_bibtex2html
245 self.bibtex = cfg.bibtex_bibtex
246 except AttributeError:
247 pass
248
249 try:
250 self.bstfiles = cfg.bibtex_bstfiles
251 except AttributeError:
252 pass
253
254 self.raw = raw.encode('utf-8')
255 self.request = request
256 self.form = request.form
257 self._ = request.getText
258 self.out = kw.get('out', request)
259 attrs, msg = wikiutil.parseAttributes(request, kw.get('format_args',''))
260
261 self.style = attrs.get('style','"plain"')[1:-1].lower()
262 self.hlinsdb = attrs.get('hlinsdb','"HlinsDatabase"')[1:-1]
263
264 self.abstract = self.keywords = self.usekeys = True
265 if attrs.get('abstract','"on"')[1:-1].lower() in ('off', 'no', 'none', 'false'):
266 self.abstract = False
267 if attrs.get('keywords','"on"')[1:-1].lower() in ('off', 'no', 'none', 'false'):
268 self.keywords = False
269 if attrs.get('usekeys','"on"')[1:-1].lower() in ('off', 'no', 'none', 'false'):
270 self.usekeys = False
271
272 def wikirepl(self, match):
273 url = match.group('url')
274
275 if not url.startswith('attachment:'):
276 return match.group()
277
278 url = url[11:]
279
280 attname=wikiutil.taintfilename(self.formatter.text(url))
281 current_pagename=self.formatter.page.page_name
282 attachment_path = os.path.join(AttachFile.getAttachDir(self.request,current_pagename), attname)
283 if not os.path.exists(attachment_path):
284 import urllib
285 linktext = self.request.getText('Upload new attachment "%(filename)s"')
286 return wikiutil.link_tag(self.request,
287 '%s?action=AttachFile&rename=%s' %
288 (current_pagename,
289 urllib.quote_plus(attname)),
290 linktext % {'filename': attname})
291
292 return ( match.group('pre') +
293 AttachFile.getAttachUrl(current_pagename,attname,self.request) +
294 match.group('suf'))
295
296
297 def format(self, formatter):
298 self.formatter = formatter
299
300 text = self.raw
301 output =''
302
303 settings = '-s ' + self.style
304
305 if not self.abstract:
306 settings = settings + ' -noabstract'
307 if not self.keywords:
308 settings = settings + ' -nokeywords'
309 if self.usekeys:
310 settings = settings + ' -use-keys'
311
312 try:
313 os.environ["BSTINPUTS"] = self.bstfiles
314 except AttributeError:
315 pass
316
317 try:
318 pin, pout, perr = os.popen3('%s -c %s %s %s ' % (self.bibtex2html, self.bibtex, param, settings))
319 pin.write(text)
320 pin.flush()
321 pin.close()
322 output = '\n'.join(pout.readlines())
323 pout.close()
324 perr.close()
325 except AttributeError:
326 output='''The locations of bibtex2html and bibtex have not been configured.
327 Please add the following lines in wiki configuration:
328
329 bibtex_bibtex = '/path/to/bibtex'
330 bibtex_bibtex2html = '/path/to/bibtex2html'
331
332 Sometime it is also necessary to specify where the BibTeX style files (.bst)
333 are located. Then it is necessary to add a line in wiki configuration:
334
335 bstfiles = '/path/to/bstfiles'
336
337 (Also see HelpOnConfiguration).'''
338 self.out.write(formatter.preformatted(1)+ output+ formatter.preformatted(0))
339 return
340 except IOError:
341 output = '\n'.join(perr.readlines())
342 pin.close()
343 pout.close()
344 perr.close()
345 self.out.write(formatter.preformatted(1)+ output+ formatter.preformatted(0))
346 return
347
348 output = wikiutil.decodeUnknownInput(output)
349
350
351 p = re.compile("""(?P<pre>\<a\s+href=")
352 (?P<url>.*?)
353 (?P<suf>"\>(\.dvi|\.ps|\.pdf|\.rtf|\.txt|\.html)(\.gz|\.Z|\.zip)?\</a\>)""",
354 re.I|re.X)
355
356 output = p.sub(self.wikirepl, output)
357
358
359 page = Page(self.request, self.hlinsdb)
360 quotes = wikiutil.decodeUnknownInput(page.get_raw_body())
361 if not self.request.user.may.read(self.hlinsdb):
362 quotes = ''
363
364 if quotes != '' and output != '':
365 for line in quotes.splitlines():
366 name,link = '',''
367 if line.startswith(' *'):
368 line = line[2:].strip()
369 if line.startswith('[[') and line.endswith(']]') and line.find('|')>-1:
370 link,name = line[2:-2].split('|',1)
371 elif line.find('=')>-1:
372 name,link = line.split('=',1)
373 elif line.startswith(' ') and line.find(':: ')>-1:
374 name,link = line.split(':: ',1)
375
376 name,link = name.strip(),link.strip()
377 if name == '' or link == '' :
378 continue
379
380 url=formatter.url(1, link)+' ' + name + formatter.url(0)
381
382
383 name = name.replace('<','<').replace('>','>')
384 name = re.escape(name)
385
386
387 name = name.replace('\\ ', '(( )|\s)+')
388
389 output = re.compile(name).sub(url, output)
390
391
392
393
394 output = output.replace('Springer-Verlag', '©Springer-Verlag')
395
396 self.out.write(formatter.rawHTML(output))