-
Notifications
You must be signed in to change notification settings - Fork 2
/
orez-ctx
executable file
·126 lines (113 loc) · 5.04 KB
/
orez-ctx
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
#!/usr/bin/python3
import sys
import yaml
def make_alnum_text(text):
alnum_text = ''
for c in text:
if c.isalnum():
alnum_text += c
return alnum_text
def orez_output(text):
sys.stdout.write(text)
def format_code_frag_ref(ref):
name = ref['NAME'].strip("'").replace('\\', '\\blank[0em]').replace(' ', '\\ ')
name_alnum = make_alnum_text(name)
if 'ID_ARRAY' in ref:
orez_output('/BTEX\\Callee{\\# %s @}' % name)
for id in ref['ID_ARRAY']: # 尾随一组 \at 生成的页码
orez_output('\\ <\\at[%s%s]>' % (name_alnum, id))
orez_output('/ETEX')
else: # 源码域名后面是一个 \at 生成的页码
orez_output('/BTEX\\Callee{\\# %s @}\\ <\\at[%s]>/ETEX' % (name, name_alnum))
def lang_mark_warn(code_frag, info):
sys.stderr.write("@ {0} # : ".format(code_frag['NAME'].strip("'")))
sys.stderr.write(info)
sys.stderr.write('\n')
def illegal_code_frag_warning(code_frag):
sys.stderr.write("@ {0} # : ".format(code_frag['NAME'].strip("'")))
sys.stderr.write("Illegal!\n")
if __name__=="__main__":
# 解析 yaml
x = None
if len(sys.argv) > 1:
f = open(sys.argv[1])
x = yaml.full_load(f)
f.close()
else:
x = yaml.safe_load(sys.stdin)
for e in x:
if 'DOC_FRAG' in e:
orez_output(e['DOC_FRAG'].strip("'"))
orez_output("\n\n")
else:
cf = e['CODE_FRAG']
if 'LANG' in cf:
lang = cf['LANG'].upper()
orez_output('\\start{0}\n'.format(lang))
else:
no_lang_mark_warning(cf)
lang_mark_warn(cf, "No language mark for me!")
orez_output('\\starttyping\n')
# 格式化代码段名称
name = cf['NAME'].strip("'").replace('\\', '\\blank[0em]')
name_alnum = make_alnum_text(name)
if 'ID' in cf:
orez_output('/BTEX\\pagereference'
'[{0}{1}]/ETEX'.format(name_alnum, cf['ID']))
else:
orez_output('/BTEX\\pagereference[{0}]/ETEX'.format(name_alnum))
orez_output('/BTEX\\CodeFragmentName{@ %s \\#}/ETEX' \
% name.replace(' ', '\\ '))
# 格式化运算符域
if 'OPERATOR' in cf:
if 'LABEL_REF' in cf:
label_ref = cf['LABEL_REF'].strip("'")
long_label_ref = name_alnum + '-' + make_alnum_text(label_ref)
orez_output(' /BTEX\\in[{0}]/ETEX'.format(long_label_ref))
orez_output(' /BTEX\\OrezSymbol{$%s$}/ETEX'
% cf['OPERATOR'].replace('^', '\\wedge'))
# 格式化源标签,与运算符域的格式化代码有必须的耦合
if 'LABEL' in cf:
label = cf['LABEL'].strip("'")
long_label = name_alnum + '-' + make_alnum_text(label)
orez_output('\n/BTEX\\reference[%s]{<%s>}\\OrezReference{<%s>}/ETEX' \
% (long_label, label, label))
# 代码片段头部与体之间应该有个换行
orez_output("\n")
# 格式化代码片段及代码片段的引用语句
for o_e in cf['CONTENT']:
if 'SNIPPET' in o_e:
orez_output(o_e['SNIPPET'].strip("'"))
elif 'INDENT' in o_e:
orez_output(o_e['INDENT'])
elif 'REF' in o_e:
format_code_frag_ref(o_e['REF'])
orez_output('\n')
else:
illegal_code_frag_warning(cf)
# if last element is a piece of code snippet,
# a linebreak should be append
if 'SNIPPET' in cf['CONTENT'][-1]:
orez_output('\n')
# 格式化出射域
if 'EMISSIONS' in cf:
for o_e in cf['EMISSIONS']:
emi = o_e['EMISSION']
# 由于要为 emi_name 增加了 '=> ' 前缀,若 emisi_name 为多行文本,为了美观,
# 那么每个换行符后需要增加三个空白字符作为 padding
emi_name = emi['NAME'].strip("'").replace('\n', '\n ')
emi_name_alnum = make_alnum_text(emi_name)
orez_output('/BTEX\\OrezSymbol{=>}/ETEX /BTEX')
if 'ID' in emi:
orez_output('\\Emission{%s}\\ <\\at[%s%d]>'
% (emi_name, emi_name_alnum, emi['ID']))
else:
orez_output('\\Emission{%s}\\ <\\at[%s]>'
% (emi_name, emi_name_alnum))
orez_output('/ETEX\n')
if 'LANG' in cf:
lang = cf['LANG'].upper()
orez_output('\\stop{0}\n'.format(lang))
else:
orez_output('\\stoptyping\n')
orez_output('\n')