summaryrefslogtreecommitdiff
path: root/Master/texmf-doc/source/chinese/texlive-zh-cn/xcp.py
blob: a256f846756733247a21f26180900bf6153d42cd (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
# xcp.py: XeTeX Chinese Proprocessor 0.2.2
# Changelog:
#   0.2.2: prevent unnecessary processing with '@'
#          before '{'         
#   0.2.1: minor upgrade to fix comment handling

import codecs, sys

LINE_END = '\n'
MARK_START = '\\begin{document}'
MARK_END = '\\end{document}'

VERB_START = '\\begin{verbatim}'
VERB_END = '\\end{verbatim}'

try:
    f = codecs.open(sys.argv[1], encoding='utf-8')
except IOError:
    print "file", sys.argv[1], "no exist"
    sys.exit()

start = 0
stdout = sys.stdout

def output(x):
    stdout.write(x.encode('utf-8'))

def output_line_end():
    output(LINE_END)

def isch(x):
    # Punct & Radicals
    if x >= 0x2e80 and x <= 0x33ff:
        return 1

    # Fullwidth Latin Characters
    if x >= 0xff00 and x <= 0xffef:
        return 1

    # CJK Unified Ideographs &
    # CJK Unified Ideographs Extension A
    if x >= 0x4e00 and x <= 0x9fbb:
        return 1
    # CJK Compatibility Ideographs
    if x >= 0xf900 and x <= 0xfad9:
        return 1

    # CJK Unified Ideographs Extension B
    if x >= 0x20000 and x <= 0x2a6d6:
        return 1

    # CJK Compatibility Supplement
    if x >= 0x2f8000 and x <= 0x2fa1d:
        return 1

    return 0

ch = 0
b = 0
lc = 0
comment = 0
enable = 1
level = 0
verbatim = 0

for line in f:
    if verbatim == 0 and line.find(MARK_END) != -1:
        output_line_end()
        start = 0

    if start > 0:
        if line.find(VERB_START) != -1:
            verbatim = 1
        
        if line.find(VERB_END) != -1:
            verbatim = 0

        # if last \zh{ has not yet been closed
        if ch == 1 and (isch(ord(line[0])) == 0 or line[0] == '\n'):
            ch = 0
            output('}')

        if isch(lc) and isch(ord(line[0])):
            output('%')

        if b == 0:
            b = 1
        else:
            output_line_end()
     
        # if line.find('\\XeTeX') != -1:
        #    output(line[:-1])
        #    continue

        if line[0] == LINE_END:
            lc = ord(LINE_END)

        line_len = len(line)
        for i in range(line_len):
            c = line[i]
            if c == '%' and lc != ord('\\'):
                comment = 1

            if c == LINE_END:
                comment = 0
                break
 
            n = ord(c)
            if comment == 1:
                output(c)
                lc = n
                continue

            if c == '@' and i + 1 < line_len and line[i + 1] == '{':
                enable = 0
                level = 0
                continue

            # match parens
            if enable == 0 and c == '{':
                level += 1

            if enable == 0 and c == '}':
                level -= 1

            if enable == 0 and level == 0:
                enable = 1

            # output directly without processing
            if enable == 0:
                output(c)
                lc = n
                continue
           
            # chinese starts
            if ch == 0 and isch(n) == 1:
                output('\\zh{')
                ch = 1

            elif ch == 1 and isch(n) == 0:
                output('}')
                ch = 0
            lc = n
            output(c)
    else:
        output(line)

    if line.find(MARK_START) != -1:
        start = 1