Path: blob/main/converter/textbook-converter/textbook_converter/__main__.py
3855 views
import argparse1import os2import sys34from pathlib import Path56from .converter import convert, merge, standalone, yml_to_dict789parser = argparse.ArgumentParser(10prog='textbook_converter',11description='Convert notebook to Mathigon markdown'12)1314parser.add_argument('toc_file', nargs=1, type=str, help='path to toc yaml')15parser.add_argument('-n', '--notebooks', nargs=1, type=str, help='directory where notebooks are located')16parser.add_argument('-o', '--output', nargs=1, type=str, help='directory to store converted notebook')1718args = parser.parse_args()1920toc_file_path = args.toc_file[0]21notebooks_dir = args.notebooks[0] if args.notebooks else None22output_dir = args.output[0] if args.output else None2324toc_chapters = yml_to_dict(toc_file_path)25nb_dir_path = Path(toc_file_path).parent if notebooks_dir is None else Path(notebooks_dir)26output_path = output_dir or nb_dir_path27shared_dir = os.path.join(output_path, 'shared')2829for chapter in toc_chapters:30is_problem_set = chapter['url'].startswith('/problem-sets')31chapter_url = chapter['url'][1:] if chapter['url'].startswith('/') else chapter['url']32chapter_output = os.path.join(output_path, chapter_url)3334if len(chapter['sections']):35for section in chapter['sections']:36section_url = section['url'][1:] if section['url'].startswith('/') else section['url']37convert(38os.path.join(nb_dir_path, section_url) + '.ipynb',39output_dir=chapter_output,40shared_dir=shared_dir,41section_id=section['id'],42is_problem_set=is_problem_set43)44if is_problem_set:45standalone(chapter_output, section)4647if not is_problem_set:48merge(chapter_output, toc_file_path)495051