Skip to content

Commit

Permalink
Import <audio> references
Browse files Browse the repository at this point in the history
  • Loading branch information
abdnh committed Sep 16, 2023
1 parent 23f9655 commit bbe5acf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.7] - 2023-09-15

### Fixed

- Fixed audio references in the form `<audio id="{blob_id}"/>` not being imported.

## [1.5.6] - 2023-08-25

### Fixed
Expand Down Expand Up @@ -106,6 +112,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Initial release

[1.5.7]: https://github.com/abdnh/AnkiApp-importer/compare/1.5.6...1.5.7
[1.5.6]: https://github.com/abdnh/AnkiApp-importer/compare/1.5.5...1.5.6
[1.5.5]: https://github.com/abdnh/AnkiApp-importer/compare/1.5.4...1.5.5
[1.5.4]: https://github.com/abdnh/AnkiApp-importer/compare/1.5.3...1.5.4
Expand Down
26 changes: 20 additions & 6 deletions src/ankiapp_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ class AnkiAppImporterCanceledException(AnkiAppImporterException):
class AnkiAppImporter:
def __init__(self, mw: AnkiQt, filename: str):
self.mw = mw
self.BLOB_REF_PATTERNS = (
re.compile(r"{{blob (?P<fname>.*?)}}"),
# AnkiApp uses a form like `<audio id="{blob_id}" type="{mime_type}" />` too
# TODO: extract the type attribute
# quoted case
re.compile(
r"(?i)(<(?:img|audio)\b[^>]* id=(?P<str>[\"'])(?P<fname>[^>]+?)(?P=str)[^>]*>)"
),
# unquoted case
re.compile(
r"(?i)(<(?:img|audio)\b[^>]* id=(?!['\"])(?P<fname>[^ >]+)[^>]*?>)"
),
# Use Anki's HTML media patterns too for completeness
*(re.compile(p) for p in mw.col.media.html_media_regexps),
)
self.config = mw.addonManager.getConfig(__name__)
self.con = sqlite3.connect(filename)
self._extract_notetypes()
Expand Down Expand Up @@ -336,10 +351,8 @@ def _extract_cards(self) -> None:
self.cards[ID] = Card(layout_id, deck, fields, tags)
self._cancel_if_needed()

BLOB_REF_RE = re.compile(r"{{blob (.*?)}}")

def _repl_blob_ref(self, match: Match[str]) -> str:
blob_id = match.group(1)
blob_id = match.group("fname")
media_obj = None
if blob_id in self.media:
media_obj = self.media[blob_id]
Expand Down Expand Up @@ -393,9 +406,10 @@ def import_to_anki(self) -> int:

for card in self.cards.values():
for field_name, contents in card.fields.items():
card.fields[field_name] = self.BLOB_REF_RE.sub(
self._repl_blob_ref, contents
)
for ref_re in self.BLOB_REF_PATTERNS:
card.fields[field_name] = ref_re.sub(
self._repl_blob_ref, card.fields[field_name]
)

notetype = self.notetypes[card.layout_id]
assert notetype.mid is not None
Expand Down

0 comments on commit bbe5acf

Please sign in to comment.