summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/ejpecp/mgetmref.py
blob: 8910556c8292b578cb4d00b18cef6600e8136ec7 (plain)
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
#! /usr/bin/env python
##################################################################################
#
#  getmref.py - gets the references links to MathSciNet throught the BatchMRef:
#                                 http://www.ams.org/batchref?qdata=xmldocument
#
#  Copyright (C) 2004 Sigitas Tolusis, VTeX Ltd. and Jim Pitman, Dept. Statistics,
#  U.C. Berkeley
#  E-mail: sigitas@vtex.let
#  http://www.stat.berkeley.edu/users/pitman
#
#  This program 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; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  Requires python ver. 2.2
#
#  Usage:
#    getmref.py <bbl or tex file>
#
#  Program (description):
#    - makes inputfile copy to <inputfilename>.getmref.bak;
#    - for each successful bibitem reference search adds line \MR{<mrid>},
#      where <mrid> is data from XML tag <mrid> without front symbols "MR";
#    - writes all adds to <inputfilename>;
#    - generates log file <inputfilename>.getmref.log;
#    - writes to stdout log info
#
#  Changes:
#    2004/04/26 - \bibitem line removed from the query
#
#
###################################################################################
SVNinfo = "$Id: getmref.py 46 2006-03-30 07:02:14Z sigitas $"

import sys, urllib, re, os.path, time, string
from xml.dom.minidom import parseString
import xml.parsers.expat as par

starttime = time.time()
res = re.search(r'\S+:\s\S+\s+(.*?)\s.*\$', SVNinfo)
if res:
  ver = res.group(1)
else:
  ver = '0.0'
print "# getmref, v. %s #" % ver

#
# bbl file parsing /begin
#

def escapetex(instr):
  res = reduce(lambda a,b: string.replace(a, b[0], b[1]), (instr, ("\\&", '&amp;'), ("<", '&lt;'), (">", '&gt;')))
  return res

def query(instring, bibID, address = 'http://www.ams.org/batchmref'):
  domas = None; res = None; err = 0
  escapetexstring = escapetex(instring)
  querystring = r'''<?xml version = "1.0" encoding = "UTF-8"?>
<mref_batch>
<mref_item outtype="tex">
<inref>
%s
</inref>
<myid>%s</myid>
</mref_item>
</mref_batch>''' % (escapetexstring, bibID)
  try:
    indom = parseString(querystring)
  except par.ExpatError, err:
    print >>sys.stderr,"[parse query]: %s" % querystring
    print >>sys.stderr,sys.exc_info()
    pass
  else:
    queryinfo = {}
    queryinfo['qdata'] = querystring
    queryval = urllib.urlencode(queryinfo)
    try:
      batchmref = urllib.urlopen(address, queryval)
      res = batchmref.read()
      domas = parseString(res)
    except err:
      print >>sys.stderr,"[parse res]: %s" % res
      print >>sys.stderr,sys.exc_info()
      pass
  return domas, res, err

def remcomm(line):
  "Removes TeX comments"
  bibre = re.compile(r'\s*(.*?)(?<!\\)%.*\n$')
  fmtline = re.sub('^%.*\n$','', line)
  if fmtline:
    matchobj = bibre.search(fmtline)
    if matchobj:
      return matchobj.groups(1)[0]
    else:
      return fmtline
  else:
    return fmtline

def formatbibitem(bibID, domas):
  errstring = None; outtype = None; mrid = None; myid = bibID; outref = None; err = 0
  try:
    mref = domas.getElementsByTagName("mref_batch")[0]
    mref_errors = mref.getElementsByTagName("batch_error")
    if len(mref_errors):
      errlist =  [ mref_error.childNodes[0].nodeValue() for mref_error in mref_errors ]
      errstring = ''.join(errlist)
      err = -2
    else:
      mref_items = [item for item in mref.getElementsByTagName("mref_item")]
      matches = mref_items[0].getElementsByTagName("matches")[0].childNodes[0]._get_nodeValue()
      if matches == '1':
        for item in mref_items:
          outtype = dict(item.attributes.items())["outtype"]
          mrid = item.getElementsByTagName("mrid")[0].childNodes[0]._get_nodeValue()
          err = 0
          if mrid[:2] == "MR":
            mrid = mrid[2:]
          myids = item.getElementsByTagName("myid")
          if len(myids):
            myid = myids[0].childNodes[0]._get_nodeValue()
          else:
            myid = bibID
          outref = string.strip(item.getElementsByTagName("outref")[0].childNodes[0]._get_nodeValue())
      else:
        err = -1
  except:
    err = -3
    print >>sys.stderr,"[formatbibitem]: %s" % bibID
    print >>sys.stderr,sys.exc_info()
    pass
  return mrid, outref, err


def handlebibitem(lines, bibID, biblabel=None):
  res = 0; err = None; outref = None
  outstring = string.strip(''.join(lines))
  lines[:] = [re.sub(r'\\MR\{.*?\}', '', a) for a in lines]
  biblines = [x for x in [remcomm(a) for a in lines] if x]
  bibstring = re.sub(r'\n', ' ', ''.join(biblines))
  match = re.search(r'\\bibitem\s*?(?:\[.*?\])?\s?\{(?:.*?)\}(.*)(\\endbibitem)?$',bibstring.strip())
  if match:
      querystring = match.group(1).strip()
  else:
      querystring = bibstring
  domas = None
  try:
    domas, xmlres, err = query(querystring, bibID)
  except:
    res = -2
    print >>sys.stderr,"[parse query]: %s" % querystring
    print >>sys.stderr,sys.exc_info()
    print 'Error',
  else:
    mrid, outref, err = formatbibitem(bibID, domas)
    if not mrid:
      print 'Not Found',
      res = -1
    else:
      print mrid,
      if mrid[:2] == "MR":
        outstring = bibstring + '\\MR{%s}' % mrid[2:].rjust(7,'0')
      else:
        outstring = bibstring + '\\MR{%s}' % mrid.rjust(7,'0')
      outstrip, nsub = re.subn(r'\\endbibitem',r'',outstring)
      if nsub:
        outstrip += '\n\\endbibitem'
      outstring = re.sub(r'  ', r' ', outstrip)
  if not outref:
    outref = "Not found!"
  else:
    outref = re.sub(r'(?<!\\)#',r'\#', outref)
  if biblabel:
    print >>datafile, '\\bibitem%s{%s}\n%s\n' % (biblabel, bibID, outref)
  else:
    print >>datafile, '\\bibitem{%s}\n%s\n' % (bibID, outref)
  return '%s\n' % outstring, res

def handleextra(extralines):
  if len(extralines):
    print >>outputfile, ''.join(extralines),

def handlebbl(inputfile, out=sys.stdout, data=sys.stdout):
  print "Job started:",
  total = 0; successful = 0; errors = 0; state = 0; pseudobibID = 0; readbib = ''
  bibl_begin = re.compile(r'\s*\\begin\s*\{thebibliography\}.*$')
  bibre = re.compile(r'^\s*\\bibitem.*')
  bibreF = re.compile(r'\s*\\bibitem\s*(\[.*?\])*?\s?\{(.*?)\}.*$',re.S)
  comments = re.compile(r'\s*%.*$')
  bibl_end = re.compile(r'\s*\\end\s*\{thebibliography\}.*$')
  for line in inputfile:
    if len(readbib):
      readbib += line
      matchobj = bibreF.search(readbib)
      if matchobj:
        line = "%s" % readbib
        readbib = ''
      else:
        continue
    if line:
      if state == 0:
        matchobj = bibl_begin.search(line)
        if matchobj:
          print >>data,matchobj.group(0)
          print >>data,"\\csname bibmessage\\endcsname\n"
          state = 1
        print >>out, line,
        continue
      elif state == 1:
        matchobj = bibre.search(line)
        if matchobj:
          matchobj = bibreF.search(line)
          if matchobj:
            biblabel, bibID = matchobj.groups()
            if not len(bibID):
              pseudobibID += 1
              bibID = '%s' % pseudobibID
            state = 2
            lines = [line]
            extralines = []
            continue
          else:
            readbib = line
            continue
        else:
          print >>out, line,
          continue
      elif state == 2:
        matchobj = bibre.search(line)
        if matchobj:
          matchobj = bibreF.search(line)
          if matchobj:
            total += 1
            print >>data,line
            outstring, sres = handlebibitem(lines, bibID, biblabel)
            if not sres:
              successful += 1
            else:
              errors += 1
            print >>out, outstring,
            handleextra(extralines)
            lines = [line]
            extralines = []
            biblabel, bibID = matchobj.groups()
            if not len(bibID):
              pseudobibID += 1
              bibID = '%s' % pseudobibID
            continue
          else:
            readbib = line
            continue
        else:
          matchobj = bibl_end.search(line)
          if matchobj:
            state = 0
            total += 1
            outstring, sres = handlebibitem(lines, bibID, biblabel)
            if not sres:
              successful += 1
            else:
              errors += 1
            print >>out, outstring,
            handleextra(extralines)
            print >>out, line,
            print >>data,matchobj.group(0)
            continue
          else:
            if line[:-1] == '':
              state = 3
              extralines = [line]
              continue
            matchobj = comments.search(line)
            if matchobj:
              state = 3
              extralines = [line]
              continue
            lines.append(line)
            continue
      elif state == 3:
        matchobj = bibre.search(line)
        if matchobj:
          matchobj = bibreF.search(line)
          if matchobj:
            state = 2
            total += 1
            outstring, sres = handlebibitem(lines, bibID, biblabel)
            if not sres:
               successful += 1
            else:
               errors += 1
            print >>out, outstring,
            handleextra(extralines)
            lines = [line]
            extralines = []
            biblabel, bibID = matchobj.groups()
            if not len(bibID):
              pseudobibID += 1
              bibID = '%s' % pseudobibID
            continue
          else:
            readbib = line
            continue
        else:
          matchobj = bibl_end.search(line)
          if matchobj:
            state = 0
            total += 1
            outstring, sres = handlebibitem(lines, bibID, biblabel)
            if not sres:
              successful += 1
            else:
              errors += 1
            print >>out, outstring,
            handleextra(extralines)
            print >>out, line,
            print >>data,matchobj.group(0)
            continue
          else:
            if line[:-1] == '':
              extralines.append(line)
              continue
            matchobj = comments.search(line)
            if matchobj:
              extralines.append(line)
              continue
            state = 2
            lines.extend(extralines)
            lines.append(line)
            extralines = []
            continue
    else:
      break
  print "Job ended"
  print "Total: %s, found: %s, errors: %s" % (total, successful, errors)
  return (total, successful, errors)

#
# bbl file parsing /end
#

if len(sys.argv) < 2:
  progname = os.path.basename(sys.argv[0])
  print "Usage:\n   %s <bbl or tex file>" % progname
  sys.exit(1)
infilename = sys.argv[1]
filebase = os.path.splitext(infilename)[0]
outfilename = "%s.getmref.tmp" % filebase
datafilename = "%s.getmref.data" % filebase
logfilename = "%s.getmref.log" % filebase

inputfile = file(infilename, 'r')
outputfile = file(outfilename, 'w')
datafile = file(datafilename, 'w')
logfile = file(logfilename, 'w')
if os.path.isfile("%s.getmref.bak" % filebase):
  os.unlink("%s.getmref.bak" % filebase)

sys.stderr = file("%s.getmref.err" % filebase, 'w')
total = 0; successful = 0; errors = 0
print >>logfile, "File: %s" % infilename
try:
  total, successful, errors = handlebbl(inputfile, outputfile, datafile)
except:
  print >>sys.stderr,"[handlebbl]"
  print >>sys.stderr,sys.exc_info()
print >>logfile, "   total: %s, found: %s, errors: %s, time: %ss" % (total, successful,
 errors, int(round(time.time()-starttime)))

inputfile.close()
outputfile.close()
datafile.close()
logfile.close()
sys.stderr.close()
sys.stderr = sys.__stderr__
if os.path.isfile("%s.getmref.err" % filebase):
  if not os.stat("%s.getmref.err" % filebase)[6]:
    os.unlink("%s.getmref.err" % filebase)
if os.path.isfile("%s.getmref.bak" % filebase):
  os.unlink("%s.getmref.bak" % filebase)
os.rename(infilename, "%s.getmref.bak" % filebase)

#mes modif
#os.rename(outfilename, infilename)
f=open(outfilename,"r")
g=open(infilename,"w")
x=f.read()
g.write(re.sub(r"\r"," ",x))

#fin de la modif

print 'Job completed in %ss' % int(round(time.time()-starttime))