forked from checkmyws/yslow-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_json
executable file
·58 lines (41 loc) · 1.4 KB
/
to_json
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
from __future__ import unicode_literals
import os, json
def write_json(path, data):
with open(path, 'w') as f:
json.dump(data, f, ensure_ascii=False, encoding="utf-8")
locales = ['en', 'fr']
data = {}
for locale in locales:
data[locale] = []
path = "public/%s" % locale
for dirname, dirnames, filenames in os.walk(path):
if dirname == path:
continue
index = "%s/index.html" % dirname
with open(index, 'r') as f:
_id = f.readline()[:-1]
title = f.readline()[:-1]
tags = f.readline()[:-2]
content = f.readlines()
_id = _id.decode('utf-8')
title = title.decode('utf-8')
tags = tags.decode('utf-8')
tags = tags.split(" ")
content = [c.decode('utf-8') for c in content]
content = "".join(content)
content = content.replace("\n", "")
rule = {
'id': _id.encode('utf-8'),
'title': title.encode('utf-8'),
'tags': [t.encode('utf-8') for t in tags],
'content': content.encode('utf-8')
}
data[locale].append(rule)
json_path = "%s/rule.json" % dirname
write_json(json_path, rule)
json_path = "public/%s/rules.json" % locale
write_json(json_path, data[locale])
json_path = "public/rules.json"
write_json(json_path, data)