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.
This commit is contained in:
stephenmk 2023-07-12 17:53:28 -05:00
parent 4cd81cda35
commit c9ab0aea46
No known key found for this signature in database
GPG key ID: B6DA730DB06235F1

View file

@ -8,7 +8,6 @@ from pathlib import Path
from datetime import datetime from datetime import datetime
from platformdirs import user_documents_dir, user_cache_dir from platformdirs import user_documents_dir, user_cache_dir
from bot.targets import Targets
from bot.mdict.terms.factory import new_terminator from bot.mdict.terms.factory import new_terminator
@ -24,11 +23,10 @@ class Exporter(ABC):
def export(self, entries, media_dir, icon_file): def export(self, entries, media_dir, icon_file):
self._init_build_media_dir(media_dir) self._init_build_media_dir(media_dir)
self._init_description_file(entries) self._init_description_file(entries)
terms = self._get_terms(entries) self._write_mdx_file(entries)
print(f"Exporting {len(terms)} Mdict keys...")
self._write_mdx_file(terms)
self._write_mdd_file() self._write_mdd_file()
self._write_icon_file(icon_file) self._write_icon_file(icon_file)
self._write_css_file()
self._rm_build_dir() self._rm_build_dir()
def _get_build_dir(self): def _get_build_dir(self):
@ -57,21 +55,34 @@ class Exporter(ABC):
self._build_media_dir = build_media_dir self._build_media_dir = build_media_dir
def _init_description_file(self, entries): def _init_description_file(self, entries):
filename = f"{self._target.value}.mdx.description.html" description_template_file = self._get_description_template_file()
original_file = os.path.join( with open(description_template_file, "r", encoding="utf8") as f:
"data", "mdict", "description", filename)
with open(original_file, "r", encoding="utf8") as f:
description = f.read() description = f.read()
description = description.replace( description = description.replace(
"{{revision}}", self._get_revision(entries)) "{{revision}}", self._get_revision(entries))
description = description.replace( description = description.replace(
"{{attribution}}", self._get_attribution(entries)) "{{attribution}}", self._get_attribution(entries))
build_dir = self._get_build_dir() 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: with open(description_file, "w", encoding="utf8") as f:
f.write(description) f.write(description)
self._description_file = description_file 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): def _get_terms(self, entries):
terms = [] terms = []
entries_len = len(entries) entries_len = len(entries)
@ -84,18 +95,6 @@ class Exporter(ABC):
print() print()
return terms 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): def _write_mdd_file(self):
out_dir = self._get_out_dir() out_dir = self._get_out_dir()
out_file = os.path.join(out_dir, f"{self._target.value}.mdd") out_file = os.path.join(out_dir, f"{self._target.value}.mdd")
@ -109,7 +108,7 @@ class Exporter(ABC):
subprocess.run(params, check=True) subprocess.run(params, check=True)
def _write_icon_file(self, icon_file): 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_dir = self._get_out_dir()
out_file = os.path.join(out_dir, f"{self._target.value}.png") out_file = os.path.join(out_dir, f"{self._target.value}.png")
if icon_file is not None and Path(icon_file).is_file(): 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(): elif Path(premade_icon_file).is_file():
shutil.copy(premade_icon_file, out_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): def _get_out_dir(self):
if self._out_dir is not None: if self._out_dir is not None:
return self._out_dir return self._out_dir
@ -148,6 +152,16 @@ class Exporter(ABC):
"data", "mdict", "css", "data", "mdict", "css",
f"{self._target.value}.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): def _rm_build_dir(self):
build_dir = self._get_build_dir() build_dir = self._get_build_dir()
shutil.rmtree(build_dir) shutil.rmtree(build_dir)