def testConvert(self): xml_path = os.path.join(self.data_dir, "ej-fra-converted.xml") xml = etree.parse(xml_path).getroot() convert_to_xhtml(xml) with PortableNamedTemporaryFile(suffix=".xml") as tf: save_xml(tf.name, xml) txt = load_txt(tf.name) self.maxDiff = None self.assertEqual( txt, load_txt(os.path.join(self.data_dir, "ej-fra-converted.xhtml")), )
def sphinx_lexicon_loader(input_path): txt = load_txt(input_path) for line in txt.split("\n"): parts = line.strip().split("\t") if len(parts) < 2: continue key = normalize("NFKC", parts[0]) value = normalize("NFKC", parts[1]) yield key, value
def create_epub(input_path, output_path, unpacked=False): if os.path.isdir(output_path): shutil.rmtree(output_path) ensure_dirs(output_path) input_dirname = os.path.dirname(input_path) if unpacked: os.mkdir(output_path) copy = copy_file_to_dir save = save_txt_to_dir else: copy = copy_file_to_zip save = save_txt_zip # mimetype file copy(output_path, MIMETYPE_ORIGIN_PATH, MIMETYPE_DEST_PATH) # container.xml file container_template = load_txt(CONTAINER_ORIGIN_PATH) container_txt = pystache.render(container_template, {"package_path": PACKAGE_DEST_PATH}) save(output_path, CONTAINER_DEST_PATH, container_txt) # the SMIL and all the files referenced in the SMIL package_data = extract_files_from_SMIL(input_path) package_template = load_txt(PACKAGE_ORIGIN_PATH) package_txt = pystache.render(package_template, package_data) save(output_path, PACKAGE_DEST_PATH, package_txt) for entry in package_data["media"]: origin_path = os.path.join(input_dirname, entry["origin_path"]) if not os.path.exists(origin_path): LOGGER.warning("Cannot find file %s to copy into EPUB file", origin_path) continue dest_path = os.path.join(EPUB_PATH, entry["dest_path"]) copy(output_path, origin_path, dest_path) # CSS file copy(output_path, STYLESHEET_ORIGIN_PATH, STYLESHEET_DEST_PATH)