Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/converter/textbook-converter/textbook_converter/__main__.py
3855 views
1
import argparse
2
import os
3
import sys
4
5
from pathlib import Path
6
7
from .converter import convert, merge, standalone, yml_to_dict
8
9
10
parser = argparse.ArgumentParser(
11
prog='textbook_converter',
12
description='Convert notebook to Mathigon markdown'
13
)
14
15
parser.add_argument('toc_file', nargs=1, type=str, help='path to toc yaml')
16
parser.add_argument('-n', '--notebooks', nargs=1, type=str, help='directory where notebooks are located')
17
parser.add_argument('-o', '--output', nargs=1, type=str, help='directory to store converted notebook')
18
19
args = parser.parse_args()
20
21
toc_file_path = args.toc_file[0]
22
notebooks_dir = args.notebooks[0] if args.notebooks else None
23
output_dir = args.output[0] if args.output else None
24
25
toc_chapters = yml_to_dict(toc_file_path)
26
nb_dir_path = Path(toc_file_path).parent if notebooks_dir is None else Path(notebooks_dir)
27
output_path = output_dir or nb_dir_path
28
shared_dir = os.path.join(output_path, 'shared')
29
30
for chapter in toc_chapters:
31
is_problem_set = chapter['url'].startswith('/problem-sets')
32
chapter_url = chapter['url'][1:] if chapter['url'].startswith('/') else chapter['url']
33
chapter_output = os.path.join(output_path, chapter_url)
34
35
if len(chapter['sections']):
36
for section in chapter['sections']:
37
section_url = section['url'][1:] if section['url'].startswith('/') else section['url']
38
convert(
39
os.path.join(nb_dir_path, section_url) + '.ipynb',
40
output_dir=chapter_output,
41
shared_dir=shared_dir,
42
section_id=section['id'],
43
is_problem_set=is_problem_set
44
)
45
if is_problem_set:
46
standalone(chapter_output, section)
47
48
if not is_problem_set:
49
merge(chapter_output, toc_file_path)
50
51