-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
executable file
·188 lines (167 loc) · 6.17 KB
/
convert.py
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
#!/usr/bin/env python3
from xml.dom import minidom
from string import ascii_lowercase
# By Johannes Wilm
# based on https://bitbucket.org/fbennett/citeproc-js/src/default/makejson.py
# by Frank Bennett
# See license info
# https://bitbucket.org/fbennett/citeproc-js/src/default/LICENSE
UNLISTED_STYLES = ['jats'] # styles that should not appear in user facing lists of styles but that are nevertheless valid
class XMLWalker:
def __init__(self, xmlstring):
dom = minidom.parseString(xmlstring).documentElement
self.title_node = dom.getElementsByTagName("title")
if len(self.title_node):
self.title = self.title_node[0].firstChild.nodeValue
else:
self.title = ''
lines = dom.getElementsByTagName("info")[0].toprettyxml().split('\n')
self.license_info = '\n'.join([line for line in lines if line.strip()])
self.output = self.walk_xml(dom)
def walk_xml(self, elem):
obj = {}
obj["n"] = elem.nodeName
if elem.attributes:
for key in elem.attributes.keys():
if key in ["xmlns", "href"]:
pass
else:
if not "a" in obj:
obj["a"] = {}
obj["a"][key] = elem.attributes[key].value
if len(elem.childNodes) > 0:
obj["c"] = []
for child in elem.childNodes:
if child.nodeName == "#comment":
pass
elif child.nodeName == "#text":
if (
len(elem.childNodes) == 1 and
elem.nodeName in ["term", "single", "multiple"]
):
obj["c"].append(child.wholeText)
else:
obj["c"].append(self.walk_xml(child))
return obj
if __name__ == "__main__":
import json
import sys
import os
import shutil
dirs = ['./build/styles-master/', './src/extra_styles/']
out_dir = './build/styles/'
out_relative_path = './styles/'
license_txt = (
'These styles are taken from '
'https://github.com/citation-style-language/styles/\n'
'The licenses of the individual styles are licenses as follows: \n\n'
)
styles_js_preamble = ''
styles = {}
styles_js_body = ''
style_list = []
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
os.makedirs(out_dir)
index = 0
for dir in dirs:
for file in os.listdir(os.fsencode(dir)):
filename = os.fsdecode(file)
if filename.endswith(".csl"):
id = filename[:-4]
index += 1
if index > 200:
index = 1
js_id = (
ascii_lowercase[index // 26] +
ascii_lowercase[index % 26]
)
if js_id == "do":
js_id = "aa"
walker = XMLWalker(open(os.path.join(dir, filename)).read())
if not js_id in styles:
styles_js_preamble += 'import {} from "{}"\n'.format(
js_id,
os.path.join(out_relative_path, js_id + '.csljson')
)
styles[js_id] = {}
styles[js_id][id] = walker.output
styles_js_body += ' "{}": {},\n'.format(
id,
js_id
)
if not id in UNLISTED_STYLES:
title = walker.title.replace('\\', '\\\\').replace('"', '\\"')
style_list.append([
title,
' "{}": "{}",\n'.format(
id,
title
)
])
license_txt += '{}\n'.format(id)
license_txt += walker.license_info
license_txt += '\n\n---\n\n'
for style in styles.items():
id = style[0]
with open(os.path.join(out_dir, id + '.csljson'), 'w') as out_file:
json.dump(style[1], out_file)
sorted_style_list = sorted(style_list, key=lambda k: k[0])
styles_js_options = ''
for style in sorted_style_list:
styles_js_options += style[1]
styles_js = (
styles_js_preamble +
'\nexport const styleLocations = {\n' +
styles_js_body[:-2] +
'\n}\n' +
'\nexport const styles = {\n' +
styles_js_options[:-2] +
'\n}\n'
)
with open('build/styles.js', 'w') as out_file:
out_file.write(styles_js)
with open('style_licenses.txt', 'w') as out_file:
out_file.write(license_txt)
dir = './build/locales-master/'
out_dir = './build/locales/'
out_relative_path = './locales/'
license_txt = (
'These locales are taken from '
'https://github.com/citation-style-language/locales\n'
'The licenses of the individual locales are licenses as follows: \n\n'
)
locales_js_preamble = ''
locales_js_body = ''
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
os.makedirs(out_dir)
for file in os.listdir(os.fsencode(dir)):
filename = os.fsdecode(file)
if filename.endswith(".xml"):
id = filename[8:-4]
js_id = id.replace('-', '_')
walker = XMLWalker(open(os.path.join(dir, filename)).read())
out_file = open(os.path.join(out_dir, id + '.csljson'), 'w')
json.dump(walker.output, out_file)
locales_js_preamble += 'import {} from "{}"\n'.format(
js_id,
os.path.join(out_relative_path, id + '.csljson')
)
locales_js_body += ' "{}": {},\n'.format(
id,
js_id
)
license_txt += '{}\n'.format(id)
license_txt += walker.license_info
license_txt += '\n\n---\n\n'
locales_js = (
locales_js_preamble +
'\nexport const locales = {\n' +
locales_js_body[:-1] +
'}'
)
with open('build/locales.js', 'w') as out_file:
out_file.write(locales_js)
with open('locale_licenses.txt', 'w') as out_file:
out_file.write(license_txt)