Implement dc:description

This commit is contained in:
Kovid Goyal 2016-06-23 14:11:15 +05:30
parent d13fd2a27b
commit d6412787ab
2 changed files with 35 additions and 1 deletions

View File

@ -526,6 +526,27 @@ def read_last_modified(root, prefixes, refines):
continue
# }}}
# Comments {{{
def read_comments(root, prefixes, refines):
ans = ''
for dc in XPath('./opf:metadata/dc:description')(root):
if dc.text:
ans += '\n' + dc.text.strip()
return ans.strip()
def set_comments(root, prefixes, refines, val):
for dc in XPath('./opf:metadata/dc:description')(root):
remove_element(dc, refines)
m = XPath('./opf:metadata')(root)[0]
if val:
val = val.strip()
if val:
c = m.makeelement(DC('description'))
c.text = val
m.append(c)
# }}}
def read_metadata(root):
ans = Metadata(_('Unknown'), [_('Unknown')])
prefixes, refines = read_prefixes(root), read_refines(root)
@ -557,6 +578,7 @@ def read_metadata(root):
lm = read_last_modified(root, prefixes, refines)
if not is_date_undefined(lm):
ans.last_modified = lm
ans.comments = read_comments(root, prefixes, refines) or ans.comments
return ans
def get_metadata(stream):
@ -575,6 +597,7 @@ def apply_metadata(root, mi, cover_prefix='', cover_data=None, apply_null=False,
set_authors(root, prefixes, refines, authors)
set_pubdate(root, prefixes, refines, mi.pubdate)
set_timestamp(root, prefixes, refines, mi.timestamp)
set_comments(root, prefixes, refines, mi.comments)
pretty_print_opf(root)

View File

@ -15,7 +15,8 @@ from calibre.ebooks.metadata.opf3 import (
read_refines, set_title, read_title_sort, read_languages, set_languages,
read_authors, Author, set_authors, ensure_prefix, read_prefixes,
read_book_producers, set_book_producers, read_timestamp, set_timestamp,
read_pubdate, set_pubdate, CALIBRE_PREFIX, read_last_modified
read_pubdate, set_pubdate, CALIBRE_PREFIX, read_last_modified, read_comments,
set_comments
)
TEMPLATE = '''<package xmlns="http://www.idpf.org/2007/opf" version="3.0" prefix="calibre: %s" unique-identifier="uid"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">{metadata}</metadata></package>''' % CALIBRE_PREFIX # noqa
@ -172,6 +173,16 @@ class TestOPF3(unittest.TestCase):
self.ae(read_last_modified(root, read_prefixes(root), read_refines(root)).year, 2003)
# }}}
def test_comments(self): # {{{
def rt(root):
return read_comments(root, read_prefixes(root), read_refines(root))
def st(root, val):
set_comments(root, read_prefixes(root), read_refines(root), val)
return rt(root)
root = self.get_opf('''<dc:description>&lt;span&gt;one&lt;/span&gt;</dc:description><dc:description> xxx</dc:description>''')
self.ae('<span>one</span>\nxxx', rt(root))
# }}}
# Run tests {{{
def suite():