def generate_toc(file_path, tex_path, ignore_exists=False): path = Path(file_path) if path.exists() and not ignore_exists: raise Exception("Path exists") tex = Path(tex_path) toc = get_toc(tex.parent, tex.name) path.mkdir(parents=True, exist_ok=ignore_exists) content = print_to_yaml(toc, tex) a_path = path.joinpath("codio_structure.yml").resolve() write_file(a_path, content)
def generate_toc(file_path, structure_path, ignore_exists=False): path = Path(file_path) if path.exists() and not ignore_exists: raise Exception("Path exists") tex = Path(structure_path) bookdown = str(structure_path).endswith('_bookdown.yml') if bookdown: toc = get_bookdown_toc(tex.parent, tex.name) else: toc = get_latex_toc(tex.parent, tex.name) path.mkdir(parents=True, exist_ok=ignore_exists) content = print_to_yaml(toc, tex, bookdown=bookdown) a_path = path.joinpath("codio_structure.yml").resolve() write_file(a_path, content)
def process_source_code(source_codes, generate_dir): code_dir = generate_dir.joinpath('code') counter = {} for code in source_codes: current = counter.setdefault(code.name, 0) source_dir = code_dir if current: source_dir = source_dir.joinpath(str(current)) source_dir.mkdir(exist_ok=True, parents=True) file_path = source_dir.joinpath(code.name) if file_path.exists(): current += 1 source_dir = source_dir.joinpath(str(current)) file_path = source_dir.joinpath(code.name) logging.info("write {} to {}".format(code.name, file_path)) write_file(file_path, code.source) counter[code.name] = current + 1
def write_md_case(name, content): dn = os.path.dirname(os.path.realpath(__file__)) fn = os.path.join(dn, 'cases/{}.md'.format(name)) write_file(fn, content)
def convert(config, base_path, yes=False): base_dir = base_path generate_dir = base_dir.joinpath("generate") if not prepare_base_directory(generate_dir, yes): return logging.debug("start converting %s" % generate_dir) guides_dir, content_dir = prepare_structure(generate_dir) transformation_rules, insert_rules = prepare_codio_rules(config) toc = get_toc(Path(config['workspace']['directory']), Path(config['workspace']['tex'])) toc, tokens = codio_transformations(toc, transformation_rules, insert_rules) refs = make_refs(toc, chapter_counter_from=get_ref_chapter_counter_from(config)) refs = override_refs(refs, config) book, metadata = make_metadata_items(config) remove_trinket = config['workspace'].get('removeTrinket', False) remove_exercise = config['workspace'].get('removeExercise', False) chapter = None children_containers = [book["children"]] chapter_num = get_ref_chapter_counter_from(config) - 1 figure_num = 0 exercise_num = 0 pdfs_for_convert = [] source_codes = [] logging.debug("convert selected pages") for item in toc: if item.section_type == CHAPTER: chapter_num += 1 figure_num = 0 exercise_num = 0 slug_name = slugify(item.section_name) chapter = item.section_name else: slug_name = slugify(item.section_name, chapter=chapter) logging.debug("convert page {} - {}".format(slug_name, chapter_num)) converted_md = item.markdown if not converted_md: lines = cleanup_latex(item.lines) md_converter = LaTeX2Markdown(lines, refs=refs, chapter_num=chapter_num, figure_num=figure_num, exercise_num=exercise_num, remove_trinket=remove_trinket, remove_exercise=remove_exercise) converted_md = md_converter.to_markdown() figure_num += md_converter.get_figure_counter() exercise_num += md_converter.get_exercise_counter() if md_converter.get_pdfs_for_convert(): pdfs_for_convert += md_converter.get_pdfs_for_convert() if md_converter.get_source_codes(): source_codes += md_converter.get_source_codes() if slug_name in tokens: for key, value in tokens.get(slug_name).items(): converted_md = converted_md.replace(key, value) md_path = content_dir.joinpath(slug_name + ".md") section, book_item = make_section_items(item, slug_name, md_path, transformation_rules) if item.section_type == CHAPTER or item.codio_section == "start": book_item["children"] = [] if item.section_type == CHAPTER: children_containers = [children_containers[0]] elif item.codio_section == "end" and len(children_containers) > 1: children_containers.pop() children_containers[len(children_containers) - 1].append(book_item) if item.section_type == CHAPTER or item.codio_section == "start": children_containers.append(book_item["children"]) metadata["sections"].append(section) write_file(md_path, converted_md) write_metadata(guides_dir, metadata, book) process_assets(config, generate_dir, pdfs_for_convert, source_codes)