From d928b950aced899366e9f5e1f1441659cecf11f4 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 7 Mar 2020 16:37:39 -0800 Subject: [PATCH] Add a few metadata methods, just proof-of-concept for now. I know I don't want to create a full-blown metadata editor, but a couple methods for basic metadata types and simple adds/removes could be useful. --- epubfile.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/epubfile.py b/epubfile.py index cc7accd..2822b75 100644 --- a/epubfile.py +++ b/epubfile.py @@ -908,6 +908,25 @@ class Epub: titles = [str(t.contents[0]) for t in titles if len(t.contents) == 1] return titles + @writes + def remove_metadata_of_type(self, tag_name): + for meta in self.opf.metadata.find_all({tag_name}): + if meta.get('id'): + for refines in self.opf.metadata.find_all('meta', {'refines': f'#{meta["id"]}'}): + refines.extract() + meta.extract() + + @writes + def set_languages(self, languages): + ''' + A list like ['en', 'fr', 'ko']. + ''' + self.remove_metadata_of_type('dc:language') + for language in languages: + element = f'{language}' + element = bs4.BeautifulSoup(element, 'html.parser') + self.opf.metadata.append(element) + # UTILITIES ############################################################################ @writes