-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_archive.py
executable file
·92 lines (82 loc) · 2.89 KB
/
gen_archive.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
#!/usr/bin/env python3
import json
import sys
import os.path
import re
from collections import OrderedDict
from zipfile import ZipFile
archive_name = sys.argv[1]
sections = sys.argv[2:]
manifest = {
'slug': 'un-zeste-de-python',
'title': 'Un zeste de Python',
'version': 2.1,
'description': 'Débuter avec Python',
'type': 'TUTORIAL',
'licence': 'CC BY-SA',
'ready_to_publish': True,
}
from collections import OrderedDict
container = OrderedDict()
# Number of nested levels
document_depth = 0
trans = str.maketrans('','','#`\n')
# Split filenames into a dict that represent file hierarchy
for section in sections:
*path, filename = section.split('/')
document_depth = max(document_depth, len(path))
parent = container
for p in path:
parent = parent.setdefault(p, OrderedDict())
parent[filename] = section
# Prefix for 1st-level titles
title_prefix = '#' * (document_depth + 1)
# Rewrite titles of files
def rewrite_titles(f):
code = False
for i, line in enumerate(f):
if line.startswith('```'):
code = not code
elif not code and line.startswith('#'):
if line.startswith(title_prefix):
line = line[document_depth:]
else:
print('Warning with title {!r} in file {}:{}'.format(line, f.name, i))
elif not code and 'img/' in line:
line = re.sub(r'(!\[.+\]\()img/', r'\1archive:img/', line)
yield line
# Write a file in the archive
def write_file(archive, filename):
with open(filename, 'r') as f:
title = next(f).translate(trans).strip()
content = ''.join(rewrite_titles(f))
archive.writestr(filename, content)
return title
# Recursively construct a document
def make_document(archive, obj, name=None):
if isinstance(obj, str):
extract = {'object': 'extract', 'text': obj}
extract['slug'], _ = os.path.splitext(name)
extract['title'] = write_file(archive, obj)
return extract
container = {'object': 'container', 'ready_to_publish': True}
if name:
container['slug'] = name
keys = list(obj.keys())
if keys[0].startswith('0-'):
container['introduction'] = obj.pop(keys[0])
title = write_file(archive, container['introduction'])
if title.endswith(' -- draft'):
title = title.removesuffix(' -- draft')
container['ready_to_publish'] = False
container['title'] = title
if keys[-1].startswith('x-'):
container['conclusion'] = obj.pop(keys[-1])
write_file(archive, container['conclusion'])
if obj:
container['children'] = [make_document(archive, child, name) for name, child in obj.items()]
return container
with ZipFile(archive_name, 'w') as archive:
document = make_document(archive, container['src'])
document.update(manifest)
archive.writestr('manifest.json', json.dumps(document, indent=4))