OSDN Git Service

add wp_imgswap2.py for new OSDN Magazine
[otptools/otptools.git] / count_fig.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import re
5 import os.path
6 import sys
7 import codecs
8
9 fh_in  = codecs.getreader('utf_8')(sys.stdin)
10 fh_out = codecs.getwriter('utf_8')(sys.stdout)
11 sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
12
13 rex1 = re.compile(ur"^☆図([0-9]+)\s+")
14 rex2 = re.compile(ur"\*図([0-9]+)")
15 rex3 = re.compile(ur"\*図([0-9]+)[〜~]([0-9]+)")
16 rex4 = re.compile(ur"\*図([0-9]+)、([0-9]+)")
17
18 counter_ref = 1
19 counter_fig = 1
20 for l in fh_in:
21     m1 = rex1.match(l)
22     m2 = rex2.search(l)
23     m3 = rex3.search(l)
24     m4 = rex4.search(l)
25
26     if m1:
27         l = rex1.sub(ur"☆図%d " % (counter_ref,), l)
28         counter_ref += 1;
29     elif m3:
30         ref1 = int(m3.group(1))
31         ref2 = int(m3.group(2))
32         l = rex3.sub(ur"*図%d~%d" % (counter_fig, counter_fig+ref2-ref1),  l)
33         counter_fig += ref2 - ref1 + 1
34     elif m4:
35         ref1 = int(m4.group(1))
36         ref2 = int(m4.group(2))
37         l = rex4.sub(ur"*図%d、%d" % (counter_fig, counter_fig+ref2-ref1),  l)
38         counter_fig += ref2 - ref1 + 1
39     elif m2:
40         l = rex2.sub(ur"*図%d" % (counter_fig,), l)
41         counter_fig += 1;
42     fh_out.write(l)
43
44