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.
This commit is contained in:
Ethan Dalool 2020-03-07 16:37:39 -08:00
parent a68a69ab99
commit d928b950ac

View file

@ -908,6 +908,25 @@ class Epub:
titles = [str(t.contents[0]) for t in titles if len(t.contents) == 1] titles = [str(t.contents[0]) for t in titles if len(t.contents) == 1]
return titles 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'<dc:language>{language}</dc:language>'
element = bs4.BeautifulSoup(element, 'html.parser')
self.opf.metadata.append(element)
# UTILITIES # UTILITIES
############################################################################ ############################################################################
@writes @writes