Ejemplo n.º 1
0
 def write_md(self, name, refs={}, chapter_num=1):
     path = "cases/{}".format(name)
     converter = LaTeX2Markdown(load_tex(path).split('\n'),
                                refs=refs,
                                chapter_num=chapter_num)
     write_md_case(name, converter.to_markdown())
Ejemplo n.º 2
0
 def run_case(self, name, refs={}, chapter_num=1):
     path = "cases/{}".format(name)
     converter = LaTeX2Markdown(load_tex(path).split('\n'),
                                refs=refs,
                                chapter_num=chapter_num)
     self.assertEqual(converter.to_markdown(), load_md(path))
Ejemplo n.º 3
0
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)