Skip to content
Snippets Groups Projects
Commit 40fd555c authored by Philipp Stadler's avatar Philipp Stadler
Browse files

feat!: add apkg level resources

BREAKING CHANGE: new required field in .apkg-spec.yaml
parent 70eb08ea
Branches
No related tags found
No related merge requests found
.DELETE_ON_ERROR:
ifneq ($(wildcard /bin/export_apkgs),)
# no pipenv needed in container, deps are preinstalled globally
PYTHON := $(shell python-libfaketime | sed 's/export //') python3
......
......@@ -69,6 +69,7 @@ class ApkgSpec(BaseModel):
dict[Literal['import_csv'], ImportCsv]|
dict[Literal['import_images'], ImportImages]
]
resource_paths: list[Path]
@field_serializer('content_version')
def dump_content_version(self, ver: Version):
......@@ -129,6 +130,8 @@ class ApkgExporter:
collection = self.empty_collection()
name_to_ntid = self.add_note_types(collection)
# add content-level resources
self.add_resources(collection, self.spec.resource_paths)
self.add_content(collection, name_to_ntid)
collection.export_anki_package(
# anki fails if this is just an apkg without a dir, but with ./ it's fine
......@@ -171,19 +174,7 @@ class ApkgExporter:
# start by adding resources
with fake_time(format_time(template_spec.template_version)):
for resources_path in template_spec.resource_paths:
# for resources generated using build,
# {{BUILD_PREFIX}} may be used
resources_path = Path(
str(resources_path)
.replace('{{BUILD_PREFIX}}', getenv('BUILD_PREFIX', '')
))
for resource_path in resources_path.glob('*'):
actual_path = col.media.add_file(resource_path)
if actual_path != str(resource_path.name):
raise Exception(f'resource was renamed by Anki from {resource_path} to {actual_path}, expected {resource_path.name} instead')
# convince anki to include media in export
self.resource_pseudo_usages += f'<img src="{actual_path}">'
self.add_resources(col, template_spec.resource_paths)
# then add the card types with the html
final_mod_date = template_spec.note_type.id
......@@ -201,6 +192,26 @@ class ApkgExporter:
col.models.update_dict(note_type)
return name_to_ntid
def add_resources(self, col: Collection, resource_paths: list[Path]):
for resources_path in resource_paths:
# for resources generated using build,
# {{BUILD_PREFIX}} may be used
resources_path = Path(
str(resources_path)
.replace('{{BUILD_PREFIX}}', getenv('BUILD_PREFIX', '')
))
if resources_path.is_file():
self.add_resource(col, resources_path)
elif resources_path.is_dir():
self.add_resources(col, resources_path.glob('*'))
def add_resource(self, col: Collection, resource_path: Path):
actual_path = col.media.add_file(resource_path)
if actual_path != str(resource_path.name):
raise Exception(f'resource was renamed by Anki from {resource_path} to {actual_path}, expected {resource_path.name} instead')
# convince anki to include media in export
self.resource_pseudo_usages += f'<img src="{actual_path}">'
def add_note_type(self, col: Collection, spec: NoteType) -> NotetypeDict:
name = spec.name
fields = list(spec.fields)
......
......@@ -5,4 +5,6 @@ templates:
content:
- import_apkg:
note_type: Q/A Testnotetype
\ No newline at end of file
note_type: Q/A Testnotetype
resource_paths: []
......@@ -15,4 +15,6 @@ content:
- guid
- Question
- Answer
tags: []
\ No newline at end of file
tags: []
resource_paths: []
......@@ -15,4 +15,6 @@ content:
- guid
- Question
- Answer
tags: []
\ No newline at end of file
tags: []
resource_paths: []
1.0.0
2.0.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment