def _read_toml_md_file(self) -> Tuple[Dict, str]: metadata = {} md = "" if os.path.exists(self.file_path): with codecs.open(self.file_path, "r", 'utf-8') as file: first_line = file_helper.clear_bad_chars( file.readline()).strip() if first_line == "+++": file.seek(0) lines = file.readlines() lines = [ file_helper.clear_bad_chars(line) for line in lines ] toml_lines = itertools.takewhile( lambda x: x.strip() != "+++", lines[1:]) metadata = toml.loads("".join(toml_lines)) md_lines = list( itertools.dropwhile(lambda x: x.strip() != "+++", lines[1:])) md = "".join(md_lines[1:]) # logging.info((toml, md)) if metadata is None: metadata = {} else: logging.warning("No front-matter found.") file.seek(0) md = file.read() return (metadata, md)
def test_clear_bad_chars(): x = """ +++ title = "चिद्गगनचन्द्रिका" unicode_script = "devanagari" +++ कर्रोपपदाग्निहोत्रशास्त्रिणा धनदानन्दनाथदीक्षानामशालिना, विरचितया `दिव्यचकोरिकया' समलंकृता ।। प्रथमविमर्शः ।। """ assert file_helper.clear_bad_chars(x) == x
def dump_to_file(self, metadata, md, dry_run): md = file_helper.clear_bad_chars(md) if len(metadata) > 0: if self.frontmatter_type == MdFile.YAML: self._dump_to_file_yamlmd(metadata, md, dry_run) elif self.frontmatter_type == MdFile.TOML: self._dump_to_file_tomlmd(metadata, md, dry_run) else: if not dry_run: os.makedirs(os.path.dirname(self.file_path), exist_ok=True) with codecs.open(self.file_path, "w", 'utf-8') as out_file_obj: out_file_obj.write(md)