mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Implement dc:description
This commit is contained in:
parent
d13fd2a27b
commit
d6412787ab
@ -526,6 +526,27 @@ def read_last_modified(root, prefixes, refines):
|
|||||||
continue
|
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):
|
def read_metadata(root):
|
||||||
ans = Metadata(_('Unknown'), [_('Unknown')])
|
ans = Metadata(_('Unknown'), [_('Unknown')])
|
||||||
prefixes, refines = read_prefixes(root), read_refines(root)
|
prefixes, refines = read_prefixes(root), read_refines(root)
|
||||||
@ -557,6 +578,7 @@ def read_metadata(root):
|
|||||||
lm = read_last_modified(root, prefixes, refines)
|
lm = read_last_modified(root, prefixes, refines)
|
||||||
if not is_date_undefined(lm):
|
if not is_date_undefined(lm):
|
||||||
ans.last_modified = lm
|
ans.last_modified = lm
|
||||||
|
ans.comments = read_comments(root, prefixes, refines) or ans.comments
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def get_metadata(stream):
|
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_authors(root, prefixes, refines, authors)
|
||||||
set_pubdate(root, prefixes, refines, mi.pubdate)
|
set_pubdate(root, prefixes, refines, mi.pubdate)
|
||||||
set_timestamp(root, prefixes, refines, mi.timestamp)
|
set_timestamp(root, prefixes, refines, mi.timestamp)
|
||||||
|
set_comments(root, prefixes, refines, mi.comments)
|
||||||
|
|
||||||
pretty_print_opf(root)
|
pretty_print_opf(root)
|
||||||
|
|
||||||
|
@ -15,7 +15,8 @@ from calibre.ebooks.metadata.opf3 import (
|
|||||||
read_refines, set_title, read_title_sort, read_languages, set_languages,
|
read_refines, set_title, read_title_sort, read_languages, set_languages,
|
||||||
read_authors, Author, set_authors, ensure_prefix, read_prefixes,
|
read_authors, Author, set_authors, ensure_prefix, read_prefixes,
|
||||||
read_book_producers, set_book_producers, read_timestamp, set_timestamp,
|
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
|
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)
|
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><span>one</span></dc:description><dc:description> xxx</dc:description>''')
|
||||||
|
self.ae('<span>one</span>\nxxx', rt(root))
|
||||||
|
# }}}
|
||||||
|
|
||||||
# Run tests {{{
|
# Run tests {{{
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user