From c9ab0aea463ae1337d20d552ba5d0a37c3b7425f Mon Sep 17 00:00:00 2001 From: stephenmk Date: Wed, 12 Jul 2023 17:53:28 -0500 Subject: [PATCH] Include copy of CSS file in mdict output directory There is also a read-only copy archived in the MDD resource file. This external file is more convenient for the user to edit. --- bot/mdict/exporters/export.py | 58 ++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/bot/mdict/exporters/export.py b/bot/mdict/exporters/export.py index 7aa0a3a..c100f67 100644 --- a/bot/mdict/exporters/export.py +++ b/bot/mdict/exporters/export.py @@ -8,7 +8,6 @@ from pathlib import Path from datetime import datetime from platformdirs import user_documents_dir, user_cache_dir -from bot.targets import Targets from bot.mdict.terms.factory import new_terminator @@ -24,11 +23,10 @@ class Exporter(ABC): def export(self, entries, media_dir, icon_file): self._init_build_media_dir(media_dir) self._init_description_file(entries) - terms = self._get_terms(entries) - print(f"Exporting {len(terms)} Mdict keys...") - self._write_mdx_file(terms) + self._write_mdx_file(entries) self._write_mdd_file() self._write_icon_file(icon_file) + self._write_css_file() self._rm_build_dir() def _get_build_dir(self): @@ -57,21 +55,34 @@ class Exporter(ABC): self._build_media_dir = build_media_dir def _init_description_file(self, entries): - filename = f"{self._target.value}.mdx.description.html" - original_file = os.path.join( - "data", "mdict", "description", filename) - with open(original_file, "r", encoding="utf8") as f: + description_template_file = self._get_description_template_file() + with open(description_template_file, "r", encoding="utf8") as f: description = f.read() description = description.replace( "{{revision}}", self._get_revision(entries)) description = description.replace( "{{attribution}}", self._get_attribution(entries)) build_dir = self._get_build_dir() - description_file = os.path.join(build_dir, filename) + description_file = os.path.join( + build_dir, f"{self._target.value}.mdx.description.html") with open(description_file, "w", encoding="utf8") as f: f.write(description) self._description_file = description_file + def _write_mdx_file(self, entries): + terms = self._get_terms(entries) + print(f"Exporting {len(terms)} Mdict keys...") + out_dir = self._get_out_dir() + out_file = os.path.join(out_dir, f"{self._target.value}.mdx") + params = [ + "mdict", + "-a", self._get_term_file(terms), + "--title", self._get_title_file(), + "--description", self._description_file, + out_file + ] + subprocess.run(params, check=True) + def _get_terms(self, entries): terms = [] entries_len = len(entries) @@ -84,18 +95,6 @@ class Exporter(ABC): print() return terms - def _write_mdx_file(self, terms): - out_dir = self._get_out_dir() - out_file = os.path.join(out_dir, f"{self._target.value}.mdx") - params = [ - "mdict", - "-a", self._get_term_file(terms), - "--title", self._get_title_file(), - "--description", self._description_file, - out_file - ] - subprocess.run(params, check=True) - def _write_mdd_file(self): out_dir = self._get_out_dir() out_file = os.path.join(out_dir, f"{self._target.value}.mdd") @@ -109,7 +108,7 @@ class Exporter(ABC): subprocess.run(params, check=True) def _write_icon_file(self, icon_file): - premade_icon_file = f"data/mdict/icon/{self._target.value}.png" + premade_icon_file = self._get_premade_icon_file() out_dir = self._get_out_dir() out_file = os.path.join(out_dir, f"{self._target.value}.png") if icon_file is not None and Path(icon_file).is_file(): @@ -117,6 +116,11 @@ class Exporter(ABC): elif Path(premade_icon_file).is_file(): shutil.copy(premade_icon_file, out_file) + def _write_css_file(self): + css_file = self._get_css_file() + out_dir = self._get_out_dir() + shutil.copy(css_file, out_dir) + def _get_out_dir(self): if self._out_dir is not None: return self._out_dir @@ -148,6 +152,16 @@ class Exporter(ABC): "data", "mdict", "css", f"{self._target.value}.css") + def _get_premade_icon_file(self): + return os.path.join( + "data", "mdict", "icon", + f"{self._target.value}.png") + + def _get_description_template_file(self): + return os.path.join( + "data", "mdict", "description", + f"{self._target.value}.mdx.description.html") + def _rm_build_dir(self): build_dir = self._get_build_dir() shutil.rmtree(build_dir)