-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorez-ctx
executable file
·97 lines (93 loc) · 3.73 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
#!/usr/bin/python3
import sys
import yaml
def orez_output(text):
sys.stdout.write(text)
def snippet_reference_format(ref):
name = ref["name"].strip("'").replace("\n", "\\blank[0em]").replace(' ', '\\ ')
hash = ref["hash"]
if "ids" in ref:
orez_output("/BTEX\\Callee{\\# %s @}" % name)
for id in ref["ids"]: # 尾随夷祖 \at 生成的页码
orez_output("\\ <\\at[%s%s]>" % (hash, id))
orez_output("/ETEX")
else:
orez_output('/BTEX\\Callee{\\# %s @}\\ <\\at[%s]>/ETEX' % (name, hash))
def emissions_format(emissions):
for a in emissions:
e = a["emission"]
# 要为 e_name 增加 '=> ' 前缀,若 e_name 为多行文本,为了美观,
# 那么每个换行符后需要增加三个空白字符作为 padding
e_name = e["name"].strip("'").replace("\n", "\n ")
e_hash = e["hash"]
orez_output("\n/BTEX\\OrezSymbol{=>}/ETEX /BTEX")
if "id" in e:
orez_output("\\Emission{%s}\\ <\\at[%s%d]>" \
% (e_name, e_hash, e["id"]))
else:
orez_output("\\Emission{%s}\\ <\\at[%s]>" \
% (e_name, e_hash))
orez_output("/ETEX")
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 e["type"] == 0:
content = e["content"].strip("'")
if len(content) > 0:
orez_output(content)
if e == x[-1]:
orez_output("\n")
else:
orez_output("\n\n")
elif e["type"] == 1:
# 代码块开始
language = e["language"].upper();
orez_output("\\start{0}\n".format(language))
# 代码块名
name = e["name"].strip("'").replace("\n", "\\blank[0em]").replace(' ', '\\ ')
hash = e["hash"]
if 'id' in e:
orez_output("/BTEX\\pagereference"
"[{0}{1}]/ETEX".format(hash, e["id"]))
else:
orez_output("/BTEX\\pagereference[{0}]/ETEX".format(hash))
orez_output("/BTEX\\SnippetName{@ %s \\#}/ETEX" % name)
# 标签引用和运算符
if "operator" in e:
if "tag_reference" in e:
tag_ref = e["tag_reference"]
tag_ref_hash = hash + "-" + tag_ref["hash"]
orez_output(" /BTEX\\in[{0}]/ETEX".format(tag_ref_hash))
orez_output(" /BTEX\\OrezSymbol{$%s$}/ETEX" % e["operator"].replace("^", "\\wedge"))
# 标签:与上面的标签引用在 ConTeXt 层面有强耦合
if "tag" in e:
tag = e["tag"]
tag_name = tag["name"].strip("'")
tag_hash = hash + "-" + tag["hash"]
orez_output("\n/BTEX\\reference[%s]{<%s>}\\OrezReference{<%s>}/ETEX" \
% (tag_hash, tag_name, tag_name))
# 有名片段内容
if e["content"] is None:
e["content"] = [{'text':""}]
for p in e["content"]:
if "text" in p:
orez_output(p["text"].strip("'"))
else:
snippet_reference_format(p["reference"])
# 出射域
if "emissions" in e:
emissions_format(e["emissions"])
# 收尾
if e == x[-1]:
orez_output("\n\\stop{0}\n".format(language))
else:
orez_output("\n\\stop{0}\n\n".format(language))
else:
sys.stderr.write("Unknown snippet type!");