diff --git a/resources/images/news/20minutos.png b/resources/images/news/20minutos.png new file mode 100644 index 0000000000..3e656913c7 Binary files /dev/null and b/resources/images/news/20minutos.png differ diff --git a/resources/recipes/20minutos.recipe b/resources/recipes/20minutos.recipe new file mode 100644 index 0000000000..d7657f77c7 --- /dev/null +++ b/resources/recipes/20minutos.recipe @@ -0,0 +1,68 @@ +__license__ = 'GPL v3' +__copyright__ = '2011, Darko Miletic ' +''' +www.20minutos.es +''' + +from calibre.web.feeds.news import BasicNewsRecipe + +class t20Minutos(BasicNewsRecipe): + title = '20 Minutos' + __author__ = 'Darko Miletic' + description = 'Diario de informacion general y local mas leido de Espania, noticias de ultima hora de Espania, el mundo, local, deportes, noticias curiosas y mas' + publisher = '20 Minutos Online SL' + category = 'news, politics, Spain' + oldest_article = 2 + max_articles_per_feed = 200 + no_stylesheets = True + encoding = 'utf8' + use_embedded_content = True + language = 'es' + remove_empty_feeds = True + publication_type = 'newspaper' + masthead_url = 'http://estaticos.20minutos.es/css4/img/ui/logo-301x54.png' + extra_css = """ + body{font-family: Arial,Helvetica,sans-serif } + img{margin-bottom: 0.4em; display:block} + """ + + conversion_options = { + 'comment' : description + , 'tags' : category + , 'publisher' : publisher + , 'language' : language + } + + remove_tags = [dict(attrs={'class':'mf-viral'})] + remove_attributes=['border'] + + feeds = [ + (u'Principal' , u'http://20minutos.feedsportal.com/c/32489/f/478284/index.rss') + ,(u'Cine' , u'http://20minutos.feedsportal.com/c/32489/f/478285/index.rss') + ,(u'Internacional' , u'http://20minutos.feedsportal.com/c/32489/f/492689/index.rss') + ,(u'Deportes' , u'http://20minutos.feedsportal.com/c/32489/f/478286/index.rss') + ,(u'Nacional' , u'http://20minutos.feedsportal.com/c/32489/f/492688/index.rss') + ,(u'Economia' , u'http://20minutos.feedsportal.com/c/32489/f/492690/index.rss') + ,(u'Tecnologia' , u'http://20minutos.feedsportal.com/c/32489/f/478292/index.rss') + ] + + def preprocess_html(self, soup): + for item in soup.findAll(style=True): + del item['style'] + for item in soup.findAll('a'): + limg = item.find('img') + if item.string is not None: + str = item.string + item.replaceWith(str) + else: + if limg: + item.name = 'div' + item.attrs = [] + else: + str = self.tag_to_string(item) + item.replaceWith(str) + for item in soup.findAll('img'): + if not item.has_key('alt'): + item['alt'] = 'image' + return soup + diff --git a/resources/recipes/adevarul.recipe b/resources/recipes/adevarul.recipe index ea0f2826ce..eec3ca771a 100644 --- a/resources/recipes/adevarul.recipe +++ b/resources/recipes/adevarul.recipe @@ -32,16 +32,25 @@ class Adevarul(BasicNewsRecipe): } keep_only_tags = [ dict(name='div', attrs={'class':'article_header'}) - ,dict(name='div', attrs={'class':'bd'}) + ,dict(name='div', attrs={'class':'bb-tu first-t bb-article-body'}) ] - remove_tags = [ dict(name='div', attrs={'class':'bb-wg-article_related_attachements'}) + remove_tags = [ + dict(name='li', attrs={'class':'author'}) + ,dict(name='li', attrs={'class':'date'}) + ,dict(name='li', attrs={'class':'comments'}) + ,dict(name='div', attrs={'class':'bb-wg-article_related_attachements'}) ,dict(name='div', attrs={'class':'bb-md bb-md-article_comments'}) - ,dict(name='form', attrs={'id':'bb-comment-create-form'}) - ] + ,dict(name='form', attrs={'id':'bb-comment-create-form'}) + ,dict(name='div', attrs={'id':'mediatag'}) + ,dict(name='div', attrs={'id':'ft'}) + ,dict(name='div', attrs={'id':'comment_wrapper'}) + ] - remove_tags_after = [ dict(name='form', attrs={'id':'bb-comment-create-form'}) ] + remove_tags_after = [ + dict(name='div', attrs={'id':'comment_wrapper'}), + ] feeds = [ (u'\u0218tiri', u'http://www.adevarul.ro/rss/latest') ] diff --git a/resources/recipes/espn.recipe b/resources/recipes/espn.recipe index 178dbf27a8..34c772f767 100644 --- a/resources/recipes/espn.recipe +++ b/resources/recipes/espn.recipe @@ -41,7 +41,8 @@ class ESPN(BasicNewsRecipe): ''' - feeds = [('Top Headlines', 'http://sports.espn.go.com/espn/rss/news'), + feeds = [ + ('Top Headlines', 'http://sports.espn.go.com/espn/rss/news'), 'http://sports.espn.go.com/espn/rss/nfl/news', 'http://sports.espn.go.com/espn/rss/nba/news', 'http://sports.espn.go.com/espn/rss/mlb/news', @@ -107,10 +108,11 @@ class ESPN(BasicNewsRecipe): if match and 'soccernet' not in url and 'bassmaster' not in url: return 'http://sports.espn.go.com/espn/print?'+match.group(1)+'&type=story' else: - if match and 'soccernet' in url: - splitlist = url.split("&", 5) - newurl = 'http://soccernet.espn.go.com/print?'+match.group(1)+'&type=story' + '&' + str(splitlist[2] ) - return newurl + if 'soccernet' in url: + match = re.search(r'/id/(\d+)/', url) + if match: + return \ + 'http://soccernet.espn.go.com/print?id=%s&type=story' % match.group(1) #else: # if 'bassmaster' in url: # return url diff --git a/resources/recipes/flickr.recipe b/resources/recipes/flickr.recipe new file mode 100644 index 0000000000..5b0276d28c --- /dev/null +++ b/resources/recipes/flickr.recipe @@ -0,0 +1,48 @@ +__license__ = 'GPL v3' +__author__ = 'Ricardo Jurado' +__copyright__ = 'Ricardo Jurado' +__version__ = 'v0.1' +__date__ = '22 February 2011' + +''' +http://blog.flickr.net/ +''' + + +from calibre.web.feeds.news import BasicNewsRecipe + +class AdvancedUserRecipe1297031650(BasicNewsRecipe): + + title = u'Flickr Blog' + masthead_url = 'http://flickrtheblog.files.wordpress.com/2008/11/flickblog_logo.gif' + cover_url = 'http://flickrtheblog.files.wordpress.com/2008/11/flickblog_logo.gif' + publisher = u'' + + __author__ = 'Ricardo Jurado' + description = 'Pictures Blog' + category = 'Blog,Pictures' + + oldest_article = 120 + max_articles_per_feed = 10 + no_stylesheets = True + use_embedded_content = False + encoding = 'UTF-8' + remove_javascript = True + language = 'en' + + extra_css = """ + p{text-align: justify; font-size: 100%} + body{ text-align: left; font-size:100% } + h2{font-family: sans-serif; font-size:130%; font-weight:bold; text-align: justify; } + .published{font-family:Arial,Helvetica,sans-serif; font-size:80%; } + .posted{font-family:Arial,Helvetica,sans-serif; font-size:80%; } + """ + + keep_only_tags = [ + dict(name='div', attrs={'class':'entry'}) + ] + + feeds = [ + (u'BLOG', u'http://feeds.feedburner.com/Flickrblog'), + #(u'BLOG', u'http://blog.flickr.net/es/feed/atom/') + ] diff --git a/resources/recipes/flickr_es.recipe b/resources/recipes/flickr_es.recipe new file mode 100644 index 0000000000..1d9c2062eb --- /dev/null +++ b/resources/recipes/flickr_es.recipe @@ -0,0 +1,47 @@ +__license__ = 'GPL v3' +__author__ = 'Ricardo Jurado' +__copyright__ = 'Ricardo Jurado' +__version__ = 'v0.1' +__date__ = '22 February 2011' + +''' +http://blog.flickr.net/ +''' + + +from calibre.web.feeds.news import BasicNewsRecipe + +class AdvancedUserRecipe1297031650(BasicNewsRecipe): + + title = u'Flickr Blog' + masthead_url = 'http://flickrtheblog.files.wordpress.com/2008/11/flickblog_logo.gif' + cover_url = 'http://flickrtheblog.files.wordpress.com/2008/11/flickblog_logo.gif' + publisher = u'' + + __author__ = 'Ricardo Jurado' + description = 'Pictures Blog' + category = 'Blog,Pictures' + + oldest_article = 120 + max_articles_per_feed = 10 + no_stylesheets = True + use_embedded_content = False + encoding = 'UTF-8' + remove_javascript = True + language = 'es' + + extra_css = """ + p{text-align: justify; font-size: 100%} + body{ text-align: left; font-size:100% } + h2{font-family: sans-serif; font-size:130%; font-weight:bold; text-align: justify; } + .published{font-family:Arial,Helvetica,sans-serif; font-size:80%; } + .posted{font-family:Arial,Helvetica,sans-serif; font-size:80%; } + """ + + keep_only_tags = [ + dict(name='div', attrs={'class':'entry'}) + ] + + feeds = [ + (u'BLOG', u'http://blog.flickr.net/es/feed/atom/') + ] diff --git a/resources/recipes/gizmodo.recipe b/resources/recipes/gizmodo.recipe index 4233ef66b7..f6d3fcb782 100644 --- a/resources/recipes/gizmodo.recipe +++ b/resources/recipes/gizmodo.recipe @@ -17,10 +17,9 @@ class Gizmodo(BasicNewsRecipe): max_articles_per_feed = 100 no_stylesheets = True encoding = 'utf-8' - use_embedded_content = False + use_embedded_content = True language = 'en' masthead_url = 'http://cache.gawkerassets.com/assets/gizmodo.com/img/logo.png' - extra_css = ' body{font-family: "Lucida Grande",Helvetica,Arial,sans-serif} img{margin-bottom: 1em} ' conversion_options = { 'comment' : description @@ -29,13 +28,12 @@ class Gizmodo(BasicNewsRecipe): , 'language' : language } - remove_attributes = ['width','height'] - keep_only_tags = [dict(attrs={'class':'content permalink'})] - remove_tags_before = dict(name='h1') - remove_tags = [dict(attrs={'class':'contactinfo'})] - remove_tags_after = dict(attrs={'class':'contactinfo'}) + feeds = [(u'Articles', u'http://feeds.gawker.com/gizmodo/vip?format=xml')] + + remove_tags = [ + {'class': 'feedflare'}, + ] - feeds = [(u'Articles', u'http://feeds.gawker.com/gizmodo/full')] def preprocess_html(self, soup): return self.adeify_images(soup) diff --git a/resources/recipes/gsp.recipe b/resources/recipes/gsp.recipe index 90a8eecfe6..efc76ee71e 100644 --- a/resources/recipes/gsp.recipe +++ b/resources/recipes/gsp.recipe @@ -1,20 +1,43 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python + +__license__ = 'GPL v3' +__copyright__ = u'2011, Silviu Cotoar\u0103' +''' +gsp.ro +''' + from calibre.web.feeds.news import BasicNewsRecipe -class AdvancedUserRecipe1286351181(BasicNewsRecipe): - title = u'gsp.ro' - __author__ = 'bucsie' - oldest_article = 2 +class GSP(BasicNewsRecipe): + title = u'Gazeta Sporturilor' + language = 'ro' + __author__ = u'Silviu Cotoar\u0103' + description = u'Gazeta Sporturilor' + publisher = u'Gazeta Sporturilor' + category = 'Ziare,Sport,Stiri,Romania' + oldest_article = 5 max_articles_per_feed = 100 - language='ro' - cover_url ='http://www.gsp.ro/images/sigla_rosu.jpg' + no_stylesheets = True + use_embedded_content = False + encoding = 'utf-8' + remove_javascript = True + cover_url = 'http://www.gsp.ro/images/logo.jpg' - remove_tags = [ - dict(name='div', attrs={'class':['related_articles', 'articol_noteaza straight_line dotted_line_top', 'comentarii','mai_multe_articole']}), - dict(name='div', attrs={'id':'icons'}) - ] - remove_tags_after = dict(name='div', attrs={'id':'adoceanintactrovccmgpmnyt'}) + conversion_options = { + 'comments' : description + ,'tags' : category + ,'language' : language + ,'publisher' : publisher + } - feeds = [(u'toate stirile', u'http://www.gsp.ro/index.php?section=section&screen=rss')] + keep_only_tags = [ dict(name='h1', attrs={'class':'serif title_2'}) + ,dict(name='div', attrs={'id':'only_text'}) + ,dict(name='span', attrs={'class':'block poza_principala'}) + ] + + feeds = [ (u'\u0218tiri', u'http://www.gsp.ro/rss.xml') ] + + def preprocess_html(self, soup): + return self.adeify_images(soup) - def print_version(self, url): - return 'http://www1.gsp.ro/print/' + url[(url.rindex('/')+1):] diff --git a/resources/recipes/lifehacker.recipe b/resources/recipes/lifehacker.recipe index 42e32497be..ff95efc50a 100644 --- a/resources/recipes/lifehacker.recipe +++ b/resources/recipes/lifehacker.recipe @@ -16,15 +16,9 @@ class Lifehacker(BasicNewsRecipe): max_articles_per_feed = 100 no_stylesheets = True encoding = 'utf-8' - use_embedded_content = False + use_embedded_content = True language = 'en' masthead_url = 'http://cache.gawkerassets.com/assets/lifehacker.com/img/logo.png' - extra_css = ''' - body{font-family: "Lucida Grande",Helvetica,Arial,sans-serif} - img{margin-bottom: 1em} - h1{font-family :Arial,Helvetica,sans-serif; font-size:large} - h2{font-family :Arial,Helvetica,sans-serif; font-size:x-small} - ''' conversion_options = { 'comment' : description , 'tags' : category @@ -32,20 +26,12 @@ class Lifehacker(BasicNewsRecipe): , 'language' : language } - remove_attributes = ['width', 'height', 'style'] - remove_tags_before = dict(name='h1') - keep_only_tags = [dict(id='container')] - remove_tags_after = dict(attrs={'class':'post-body'}) remove_tags = [ - dict(id="sharemenu"), - {'class': 'related'}, + {'class': 'feedflare'}, ] - feeds = [(u'Articles', u'http://feeds.gawker.com/lifehacker/full')] + feeds = [(u'Articles', u'http://feeds.gawker.com/lifehacker/vip?format=xml')] def preprocess_html(self, soup): return self.adeify_images(soup) - def print_version(self, url): - return url.replace('#!', '?_escaped_fragment_=') - diff --git a/resources/recipes/nytimes.recipe b/resources/recipes/nytimes.recipe index 7e313e5727..0a5c310af4 100644 --- a/resources/recipes/nytimes.recipe +++ b/resources/recipes/nytimes.recipe @@ -88,8 +88,8 @@ class NYTimes(BasicNewsRecipe): if headlinesOnly: title='New York Times Headlines' - description = 'Headlines from the New York Times' - needs_subscription = False + description = 'Headlines from the New York Times. Needs a subscription from http://www.nytimes.com' + needs_subscription = 'optional' elif webEdition: title='New York Times (Web)' description = 'New York Times on the Web' diff --git a/resources/recipes/nytimes_sub.recipe b/resources/recipes/nytimes_sub.recipe index 4077065d91..d24307c887 100644 --- a/resources/recipes/nytimes_sub.recipe +++ b/resources/recipes/nytimes_sub.recipe @@ -96,18 +96,18 @@ class NYTimes(BasicNewsRecipe): if headlinesOnly: title='New York Times Headlines' description = 'Headlines from the New York Times' - needs_subscription = False + needs_subscription = True elif webEdition: title='New York Times (Web)' description = 'New York Times on the Web' needs_subscription = True elif replaceKindleVersion: - title='The New York Times' + title='The New York Times' description = 'Today\'s New York Times' needs_subscription = True else: title='New York Times' - description = 'Today\'s New York Times' + description = 'Today\'s New York Times. Needs subscription from http://www.nytimes.com' needs_subscription = True @@ -676,7 +676,7 @@ class NYTimes(BasicNewsRecipe): if hlines: for hline in hlines: hline.extract() - + #find all section headers hlines = runAround.findAll('h6') if hlines: diff --git a/resources/template-functions.json b/resources/template-functions.json index 332ce1ddea..5d9b6a11a3 100644 --- a/resources/template-functions.json +++ b/resources/template-functions.json @@ -15,6 +15,7 @@ "template": "def evaluate(self, formatter, kwargs, mi, locals, template):\n template = template.replace('[[', '{').replace(']]', '}')\n return formatter.__class__().safe_format(template, kwargs, 'TEMPLATE', mi)\n", "print": "def evaluate(self, formatter, kwargs, mi, locals, *args):\n print args\n return None\n", "titlecase": "def evaluate(self, formatter, kwargs, mi, locals, val):\n return titlecase(val)\n", + "sublist": "def evaluate(self, formatter, kwargs, mi, locals, val, start_index, end_index, sep):\n if not val:\n return ''\n si = int(start_index)\n ei = int(end_index)\n val = val.split(sep)\n try:\n if ei == 0:\n return sep.join(val[si:])\n else:\n return sep.join(val[si:ei])\n except:\n return ''\n", "test": "def evaluate(self, formatter, kwargs, mi, locals, val, value_if_set, value_not_set):\n if val:\n return value_if_set\n else:\n return value_not_set\n", "eval": "def evaluate(self, formatter, kwargs, mi, locals, template):\n from formatter import eval_formatter\n template = template.replace('[[', '{').replace(']]', '}')\n return eval_formatter.safe_format(template, locals, 'EVAL', None)\n", "multiply": "def evaluate(self, formatter, kwargs, mi, locals, x, y):\n x = float(x if x else 0)\n y = float(y if y else 0)\n return unicode(x * y)\n", diff --git a/resources/templates/html.css b/resources/templates/html.css index e9b683ca34..79c80583bf 100644 --- a/resources/templates/html.css +++ b/resources/templates/html.css @@ -391,11 +391,6 @@ noembed, param, link { display: none; } -/* Page breaks at body tags, to help out with LIT-generation */ -body { - page-break-before: always; -} - /* Explicit line-breaks are blocks, sure... */ br { display: block; diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 99f837e80b..22b337915c 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -571,7 +571,7 @@ from calibre.devices.binatone.driver import README from calibre.devices.hanvon.driver import N516, EB511, ALEX, AZBOOKA, THEBOOK from calibre.devices.edge.driver import EDGE from calibre.devices.teclast.driver import TECLAST_K3, NEWSMY, IPAPYRUS, \ - SOVOS, PICO, SUNSTECH_EB700, ARCHOS7O, STASH + SOVOS, PICO, SUNSTECH_EB700, ARCHOS7O, STASH, WEXLER from calibre.devices.sne.driver import SNE from calibre.devices.misc import PALMPRE, AVANT, SWEEX, PDNOVEL, \ GEMEI, VELOCITYMICRO, PDNOVEL_KOBO, LUMIREAD, ALURATEK_COLOR, \ @@ -679,7 +679,7 @@ plugins += [ ELONEX, TECLAST_K3, NEWSMY, - PICO, SUNSTECH_EB700, ARCHOS7O, SOVOS, STASH, + PICO, SUNSTECH_EB700, ARCHOS7O, SOVOS, STASH, WEXLER, IPAPYRUS, EDGE, SNE, diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index f2f7885034..bbe24125b8 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -594,7 +594,7 @@ def main(args=sys.argv): if remove_plugin(opts.remove_plugin): print 'Plugin removed' else: - print 'No custom pluginnamed', opts.remove_plugin + print 'No custom plugin named', opts.remove_plugin if opts.customize_plugin is not None: name, custom = opts.customize_plugin.split(',') plugin = find_plugin(name.strip()) diff --git a/src/calibre/devices/android/driver.py b/src/calibre/devices/android/driver.py index 99679283a7..95633cbe58 100644 --- a/src/calibre/devices/android/driver.py +++ b/src/calibre/devices/android/driver.py @@ -74,6 +74,9 @@ class ANDROID(USBMS): # T-Mobile 0x0408 : { 0x03ba : [0x0109], }, + # Xperia + 0x13d3 : { 0x3304 : [0x0001, 0x0002] }, + } EBOOK_DIR_MAIN = ['eBooks/import', 'wordplayer/calibretransfer', 'Books'] EXTRA_CUSTOMIZATION_MESSAGE = _('Comma separated list of directories to ' @@ -83,7 +86,7 @@ class ANDROID(USBMS): VENDOR_NAME = ['HTC', 'MOTOROLA', 'GOOGLE_', 'ANDROID', 'ACER', 'GT-I5700', 'SAMSUNG', 'DELL', 'LINUX', 'GOOGLE', 'ARCHOS', - 'TELECHIP', 'HUAWEI', 'T-MOBILE', ] + 'TELECHIP', 'HUAWEI', 'T-MOBILE', 'SEMC'] WINDOWS_MAIN_MEM = ['ANDROID_PHONE', 'A855', 'A853', 'INC.NEXUS_ONE', '__UMS_COMPOSITE', '_MB200', 'MASS_STORAGE', '_-_CARD', 'SGH-I897', 'GT-I9000', 'FILE-STOR_GADGET', 'SGH-T959', 'SAMSUNG_ANDROID', diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index 52f0563c7b..f1c0d3f3d3 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -78,9 +78,13 @@ class KOBO(USBMS): else self._main_prefix # Determine the firmware version - f = open(self.normalize_path(self._main_prefix + '.kobo/version'), 'r') - self.fwversion = f.readline().split(',')[2] - f.close() + try: + with open(self.normalize_path(self._main_prefix + '.kobo/version'), + 'rb') as f: + self.fwversion = f.readline().split(',')[2] + except: + self.fwversion = 'unknown' + if self.fwversion != '1.0' and self.fwversion != '1.4': self.has_kepubs = True debug_print('Version of firmware: ', self.fwversion, 'Has kepubs:', self.has_kepubs) @@ -161,7 +165,7 @@ class KOBO(USBMS): return changed connection = sqlite.connect(self.normalize_path(self._main_prefix + '.kobo/KoboReader.sqlite')) - + # return bytestrings if the content cannot the decoded as unicode connection.text_factory = lambda x: unicode(x, "utf-8", "ignore") @@ -234,7 +238,7 @@ class KOBO(USBMS): debug_print('delete_via_sql: ContentID: ', ContentID, 'ContentType: ', ContentType) connection = sqlite.connect(self.normalize_path(self._main_prefix + '.kobo/KoboReader.sqlite')) - + # return bytestrings if the content cannot the decoded as unicode connection.text_factory = lambda x: unicode(x, "utf-8", "ignore") @@ -511,7 +515,7 @@ class KOBO(USBMS): # the last book from the collection the list of books is empty # and the removal of the last book would not occur connection = sqlite.connect(self.normalize_path(self._main_prefix + '.kobo/KoboReader.sqlite')) - + # return bytestrings if the content cannot the decoded as unicode connection.text_factory = lambda x: unicode(x, "utf-8", "ignore") diff --git a/src/calibre/devices/teclast/driver.py b/src/calibre/devices/teclast/driver.py index 2cca0085d7..1bbab8e120 100644 --- a/src/calibre/devices/teclast/driver.py +++ b/src/calibre/devices/teclast/driver.py @@ -104,3 +104,14 @@ class STASH(TECLAST_K3): VENDOR_NAME = 'STASH' WINDOWS_MAIN_MEM = WINDOWS_CARD_A_MEM = 'W950' +class WEXLER(TECLAST_K3): + + name = 'Wexler device interface' + gui_name = 'Wexler' + description = _('Communicate with the Wexler reader.') + + FORMATS = ['epub', 'fb2', 'pdf', 'txt'] + + VENDOR_NAME = 'WEXLER' + WINDOWS_MAIN_MEM = WINDOWS_CARD_A_MEM = 'T7001' + diff --git a/src/calibre/ebooks/comic/input.py b/src/calibre/ebooks/comic/input.py index c9b11e31f2..7710d41fb3 100755 --- a/src/calibre/ebooks/comic/input.py +++ b/src/calibre/ebooks/comic/input.py @@ -304,6 +304,10 @@ class ComicInput(InputFormatPlugin): help=_('Specify the image size as widthxheight pixels. Normally,' ' an image size is automatically calculated from the output ' 'profile, this option overrides it.')), + OptionRecommendation(name='dont_add_comic_pages_to_toc', recommended_value=False, + help=_('When converting a CBC do not add links to each page to' + ' the TOC. Note this only applies if the TOC has more than one' + ' section')), ]) recommendations = set([ @@ -449,10 +453,11 @@ class ComicInput(InputFormatPlugin): wrappers = comic[2] stoc = toc.add_item(href(wrappers[0]), None, comic[0], play_order=po) - for i, x in enumerate(wrappers): - stoc.add_item(href(x), None, - _('Page')+' %d'%(i+1), play_order=po) - po += 1 + if not opts.dont_add_comic_pages_to_toc: + for i, x in enumerate(wrappers): + stoc.add_item(href(x), None, + _('Page')+' %d'%(i+1), play_order=po) + po += 1 opf.set_toc(toc) m, n = open('metadata.opf', 'wb'), open('toc.ncx', 'wb') opf.render(m, n, 'toc.ncx') diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index 1d263eb762..9a0c3f3c7f 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -984,7 +984,9 @@ OptionRecommendation(name='sr3_replace', flattener = CSSFlattener(fbase=fbase, fkey=fkey, lineh=line_height, untable=self.output_plugin.file_type in ('mobi','lit'), - unfloat=self.output_plugin.file_type in ('mobi', 'lit')) + unfloat=self.output_plugin.file_type in ('mobi', 'lit'), + page_break_on_body=self.output_plugin.file_type in ('mobi', + 'lit')) flattener(self.oeb, self.opts) self.opts.insert_blank_line = oibl self.opts.remove_paragraph_spacing = orps diff --git a/src/calibre/ebooks/lit/output.py b/src/calibre/ebooks/lit/output.py index 423fb9ce7c..0b07bc7705 100644 --- a/src/calibre/ebooks/lit/output.py +++ b/src/calibre/ebooks/lit/output.py @@ -22,7 +22,8 @@ class LITOutput(OutputFormatPlugin): from calibre.ebooks.oeb.transforms.htmltoc import HTMLTOCAdder from calibre.ebooks.lit.writer import LitWriter from calibre.ebooks.oeb.transforms.split import Split - split = Split(split_on_page_breaks=True, max_flow_size=0) + split = Split(split_on_page_breaks=True, max_flow_size=0, + remove_css_pagebreaks=False) split(self.oeb, self.opts) diff --git a/src/calibre/ebooks/metadata/book/__init__.py b/src/calibre/ebooks/metadata/book/__init__.py index 82de7400d7..033a78d611 100644 --- a/src/calibre/ebooks/metadata/book/__init__.py +++ b/src/calibre/ebooks/metadata/book/__init__.py @@ -83,6 +83,10 @@ CALIBRE_METADATA_FIELDS = frozenset([ 'application_id', # An application id, currently set to the db_id. 'db_id', # the calibre primary key of the item. 'formats', # list of formats (extensions) for this book + # a dict of user category names, where the value is a list of item names + # from the book that are in that category + 'user_categories', + ] ) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index 4cca94a6c6..b47cc373a7 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -30,6 +30,7 @@ NULL_VALUES = { 'author_sort_map': {}, 'authors' : [_('Unknown')], 'title' : _('Unknown'), + 'user_categories' : {}, 'language' : 'und' } diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index dfb902b5b9..d34a563110 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -470,6 +470,13 @@ def serialize_user_metadata(metadata_elem, all_user_metadata, tail='\n'+(' '*8)) metadata_elem.append(meta) +def dump_user_categories(cats): + if not cats: + cats = {} + from calibre.ebooks.metadata.book.json_codec import object_to_unicode + return json.dumps(object_to_unicode(cats), ensure_ascii=False, + skipkeys=True) + class OPF(object): # {{{ MIMETYPE = 'application/oebps-package+xml' @@ -524,6 +531,9 @@ class OPF(object): # {{{ publication_type = MetadataField('publication_type', is_dc=False) timestamp = MetadataField('timestamp', is_dc=False, formatter=parse_date, renderer=isoformat) + user_categories = MetadataField('user_categories', is_dc=False, + formatter=json.loads, + renderer=dump_user_categories) def __init__(self, stream, basedir=os.getcwdu(), unquote_urls=True, @@ -994,7 +1004,7 @@ class OPF(object): # {{{ for attr in ('title', 'authors', 'author_sort', 'title_sort', 'publisher', 'series', 'series_index', 'rating', 'isbn', 'tags', 'category', 'comments', - 'pubdate'): + 'pubdate', 'user_categories'): val = getattr(mi, attr, None) if val is not None and val != [] and val != (None, None): setattr(self, attr, val) @@ -1175,6 +1185,10 @@ class OPFCreator(Metadata): a(CAL_ELEM('calibre:timestamp', self.timestamp.isoformat())) if self.publication_type is not None: a(CAL_ELEM('calibre:publication_type', self.publication_type)) + if self.user_categories: + from calibre.ebooks.metadata.book.json_codec import object_to_unicode + a(CAL_ELEM('calibre:user_categories', + json.dumps(object_to_unicode(self.user_categories)))) manifest = E.manifest() if self.manifest is not None: for ref in self.manifest: @@ -1299,6 +1313,8 @@ def metadata_to_opf(mi, as_string=True): meta('publication_type', mi.publication_type) if mi.title_sort: meta('title_sort', mi.title_sort) + if mi.user_categories: + meta('user_categories', dump_user_categories(mi.user_categories)) serialize_user_metadata(metadata, mi.get_all_user_metadata(False)) diff --git a/src/calibre/ebooks/metadata/sources/base.py b/src/calibre/ebooks/metadata/sources/base.py index 74e184cc66..54d7d49d6d 100644 --- a/src/calibre/ebooks/metadata/sources/base.py +++ b/src/calibre/ebooks/metadata/sources/base.py @@ -7,7 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import re +import re, threading from calibre.customize import Plugin from calibre.utils.logging import ThreadSafeLog, FileStream @@ -30,7 +30,21 @@ class Source(Plugin): touched_fields = frozenset() + def __init__(self, *args, **kwargs): + Plugin.__init__(self, *args, **kwargs) + self._isbn_to_identifier_cache = {} + self.cache_lock = threading.RLock() + # Utility functions {{{ + + def cache_isbn_to_identifier(self, isbn, identifier): + with self.cache_lock: + self._isbn_to_identifier_cache[isbn] = identifier + + def cached_isbn_to_identifier(self, isbn): + with self.cache_lock: + return self._isbn_to_identifier_cache.get(isbn, None) + def get_author_tokens(self, authors, only_first_author=True): ''' Take a list of authors and return a list of tokens useful for an diff --git a/src/calibre/ebooks/metadata/sources/google.py b/src/calibre/ebooks/metadata/sources/google.py index 498c7574ea..0720b21ded 100644 --- a/src/calibre/ebooks/metadata/sources/google.py +++ b/src/calibre/ebooks/metadata/sources/google.py @@ -13,6 +13,7 @@ from functools import partial from lxml import etree +from calibre.ebooks.metadata import check_isbn from calibre.ebooks.metadata.sources.base import Source from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.chardet import xml_to_unicode @@ -69,6 +70,7 @@ def to_metadata(browser, log, entry_, timeout): id_url = entry_id(entry_)[0].text + google_id = id_url.split('/')[-1] title_ = ': '.join([x.text for x in title(entry_)]).strip() authors = [x.text.strip() for x in creator(entry_) if x.text] if not authors: @@ -78,6 +80,7 @@ def to_metadata(browser, log, entry_, timeout): return None mi = Metadata(title_, authors) + mi.identifiers = {'google':google_id} try: raw = get_details(browser, id_url, timeout) feed = etree.fromstring(xml_to_unicode(clean_ascii_chars(raw), @@ -103,9 +106,12 @@ def to_metadata(browser, log, entry_, timeout): t = str(x.text).strip() if t[:5].upper() in ('ISBN:', 'LCCN:', 'OCLC:'): if t[:5].upper() == 'ISBN:': - isbns.append(t[5:]) + t = check_isbn(t[5:]) + if t: + isbns.append(t) if isbns: mi.isbn = sorted(isbns, key=len)[-1] + mi.all_isbns = isbns # Tags try: @@ -133,20 +139,6 @@ def to_metadata(browser, log, entry_, timeout): return mi -def get_all_details(br, log, entries, abort, result_queue, timeout): - for i in entries: - try: - ans = to_metadata(br, log, i, timeout) - if isinstance(ans, Metadata): - result_queue.put(ans) - except: - log.exception( - 'Failed to get metadata for identify entry:', - etree.tostring(i)) - if abort.is_set(): - break - - class GoogleBooks(Source): name = 'Google Books' @@ -185,6 +177,36 @@ class GoogleBooks(Source): 'min-viewability':'none', }) + def cover_url_from_identifiers(self, identifiers): + goog = identifiers.get('google', None) + if goog is None: + isbn = identifiers.get('isbn', None) + goog = self.cached_isbn_to_identifier(isbn) + if goog is not None: + return ('http://books.google.com/books?id=%s&printsec=frontcover&img=1' % + goog) + + def is_cover_image_valid(self, raw): + # When no cover is present, returns a PNG saying image not available + # Try for example google identifier llNqPwAACAAJ + # I have yet to see an actual cover in PNG format + return raw and len(raw) > 17000 and raw[1:4] != 'PNG' + + def get_all_details(self, br, log, entries, abort, result_queue, timeout): + for i in entries: + try: + ans = to_metadata(br, log, i, timeout) + if isinstance(ans, Metadata): + result_queue.put(ans) + for isbn in ans.all_isbns: + self.cache_isbn_to_identifier(isbn, + ans.identifiers['google']) + except: + log.exception( + 'Failed to get metadata for identify entry:', + etree.tostring(i)) + if abort.is_set(): + break def identify(self, log, result_queue, abort, title=None, authors=None, identifiers={}, timeout=5): @@ -207,8 +229,8 @@ class GoogleBooks(Source): return as_unicode(e) # There is no point running these queries in threads as google - # throttles requests returning Forbidden errors - get_all_details(br, log, entries, abort, result_queue, timeout) + # throttles requests returning 403 Forbidden errors + self.get_all_details(br, log, entries, abort, result_queue, timeout) return None @@ -218,8 +240,14 @@ if __name__ == '__main__': title_test) test_identify_plugin(GoogleBooks.name, [ + ( - {'title': 'Great Expectations', 'authors':['Charles Dickens']}, - [title_test('Great Expectations', exact=True)] + {'identifiers':{'isbn': '0743273567'}}, + [title_test('The great gatsby', exact=True)] ), + + #( + # {'title': 'Great Expectations', 'authors':['Charles Dickens']}, + # [title_test('Great Expectations', exact=True)] + #), ]) diff --git a/src/calibre/ebooks/mobi/reader.py b/src/calibre/ebooks/mobi/reader.py index 7a566776d7..9c52a18691 100644 --- a/src/calibre/ebooks/mobi/reader.py +++ b/src/calibre/ebooks/mobi/reader.py @@ -242,9 +242,11 @@ class MobiReader(object): self.debug = debug self.embedded_mi = None self.base_css_rules = textwrap.dedent(''' - blockquote { margin: 0em 0em 0em 2em; text-align: justify } + body { text-align: justify } - p { margin: 0em; text-align: justify; text-indent: 1.5em } + blockquote { margin: 0em 0em 0em 2em; } + + p { margin: 0em; text-indent: 1.5em } .bold { font-weight: bold } diff --git a/src/calibre/ebooks/mobi/writer.py b/src/calibre/ebooks/mobi/writer.py index 0c33dffef2..2be699e525 100644 --- a/src/calibre/ebooks/mobi/writer.py +++ b/src/calibre/ebooks/mobi/writer.py @@ -2256,22 +2256,22 @@ class MobiWriter(object): return sectionIndices, sectionParents def _generate_section_article_indices(self, i, section, entries, sectionIndices, sectionParents): - sectionArticles = list(section.iter())[1:] - # Iterate over the section's articles + sectionArticles = list(section.iter())[1:] + # Iterate over the section's articles - for (j, article) in enumerate(sectionArticles): - # Recompute offset and length for each article - offset, length = self._compute_offset_length(i, article, entries) - if self.opts.verbose > 2 : - self._oeb.logger.info( "article %02d: offset = 0x%06X length = 0x%06X" % (j, offset, length) ) + for (j, article) in enumerate(sectionArticles): + # Recompute offset and length for each article + offset, length = self._compute_offset_length(i, article, entries) + if self.opts.verbose > 2 : + self._oeb.logger.info( "article %02d: offset = 0x%06X length = 0x%06X" % (j, offset, length) ) - ctoc_map_index = i + j + 1 + ctoc_map_index = i + j + 1 - #hasAuthor = self._ctoc_map[ctoc_map_index].get('authorOffset') - #hasDescription = self._ctoc_map[ctoc_map_index].get('descriptionOffset') - mySectionParent = sectionParents[sectionIndices[i-1]] - myNewArticle = MobiArticle(mySectionParent, offset, length, ctoc_map_index ) - mySectionParent.addArticle( myNewArticle ) + #hasAuthor = self._ctoc_map[ctoc_map_index].get('authorOffset') + #hasDescription = self._ctoc_map[ctoc_map_index].get('descriptionOffset') + mySectionParent = sectionParents[sectionIndices[i-1]] + myNewArticle = MobiArticle(mySectionParent, offset, length, ctoc_map_index ) + mySectionParent.addArticle( myNewArticle ) def _add_book_chapters(self, myDoc, indxt, indices): chapterCount = myDoc.documentStructure.chapterCount() diff --git a/src/calibre/ebooks/oeb/output.py b/src/calibre/ebooks/oeb/output.py index 585b56c7b6..6709141a01 100644 --- a/src/calibre/ebooks/oeb/output.py +++ b/src/calibre/ebooks/oeb/output.py @@ -32,6 +32,12 @@ class OEBOutput(OutputFormatPlugin): for key in (OPF_MIME, NCX_MIME, PAGE_MAP_MIME): href, root = results.pop(key, [None, None]) if root is not None: + if key == OPF_MIME: + try: + self.workaround_nook_cover_bug(root) + except: + self.log.exception('Something went wrong while trying to' + ' workaround Nook cover bug, ignoring') raw = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True) if key == OPF_MIME: @@ -49,3 +55,24 @@ class OEBOutput(OutputFormatPlugin): with open(path, 'wb') as f: f.write(str(item)) item.unload_data_from_memory(memory=path) + + def workaround_nook_cover_bug(self, root): # {{{ + cov = root.xpath('//*[local-name() = "meta" and @name="cover" and' + ' @content != "cover"]') + if len(cov) == 1: + manpath = ('//*[local-name() = "manifest"]/*[local-name() = "item" ' + ' and @id="%s" and @media-type]') + cov = cov[0] + covid = cov.get('content') + manifest_item = root.xpath(manpath%covid) + has_cover = root.xpath(manpath%'cover') + if len(manifest_item) == 1 and not has_cover and \ + manifest_item[0].get('media-type', + '').startswith('image/'): + self.log.warn('The cover image has an id != "cover". Renaming' + ' to work around Nook Color bug') + manifest_item = manifest_item[0] + manifest_item.set('id', 'cover') + cov.set('content', 'cover') + # }}} + diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index db6bdf0a7a..368f5eb289 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -100,12 +100,13 @@ def FontMapper(sbase=None, dbase=None, dkey=None): class CSSFlattener(object): def __init__(self, fbase=None, fkey=None, lineh=None, unfloat=False, - untable=False): + untable=False, page_break_on_body=False): self.fbase = fbase self.fkey = fkey self.lineh = lineh self.unfloat = unfloat self.untable = untable + self.page_break_on_body = page_break_on_body @classmethod def config(cls, cfg): @@ -139,6 +140,8 @@ class CSSFlattener(object): bs.append('margin-right : %fpt'%\ float(self.context.margin_right)) bs.extend(['padding-left: 0pt', 'padding-right: 0pt']) + if self.page_break_on_body: + bs.extend(['page-break-before: always']) if self.context.change_justification != 'original': bs.append('text-align: '+ self.context.change_justification) body.set('style', '; '.join(bs)) diff --git a/src/calibre/ebooks/oeb/transforms/split.py b/src/calibre/ebooks/oeb/transforms/split.py index 4633131dc0..69de740ddc 100644 --- a/src/calibre/ebooks/oeb/transforms/split.py +++ b/src/calibre/ebooks/oeb/transforms/split.py @@ -38,11 +38,12 @@ class SplitError(ValueError): class Split(object): def __init__(self, split_on_page_breaks=True, page_breaks_xpath=None, - max_flow_size=0): + max_flow_size=0, remove_css_pagebreaks=True): self.split_on_page_breaks = split_on_page_breaks self.page_breaks_xpath = page_breaks_xpath self.max_flow_size = max_flow_size self.page_break_selectors = None + self.remove_css_pagebreaks = remove_css_pagebreaks if self.page_breaks_xpath is not None: self.page_break_selectors = [(XPath(self.page_breaks_xpath), False)] @@ -83,12 +84,16 @@ class Split(object): if before and before != 'avoid': self.page_break_selectors.add((CSSSelector(rule.selectorText), True)) + if self.remove_css_pagebreaks: + rule.style.removeProperty('page-break-before') except: pass try: if after and after != 'avoid': self.page_break_selectors.add((CSSSelector(rule.selectorText), False)) + if self.remove_css_pagebreaks: + rule.style.removeProperty('page-break-after') except: pass page_breaks = set([]) diff --git a/src/calibre/gui2/convert/comic_input.py b/src/calibre/gui2/convert/comic_input.py index f7f8023c0e..ed8053b8e6 100644 --- a/src/calibre/gui2/convert/comic_input.py +++ b/src/calibre/gui2/convert/comic_input.py @@ -22,7 +22,8 @@ class PluginWidget(Widget, Ui_Form): ['colors', 'dont_normalize', 'keep_aspect_ratio', 'right2left', 'despeckle', 'no_sort', 'no_process', 'landscape', 'dont_sharpen', 'disable_trim', 'wide', 'output_format', - 'dont_grayscale', 'comic_image_size'] + 'dont_grayscale', 'comic_image_size', + 'dont_add_comic_pages_to_toc'] ) self.db, self.book_id = db, book_id for x in get_option('output_format').option.choices: diff --git a/src/calibre/gui2/convert/comic_input.ui b/src/calibre/gui2/convert/comic_input.ui index 52c0ad2bb5..676032942f 100644 --- a/src/calibre/gui2/convert/comic_input.ui +++ b/src/calibre/gui2/convert/comic_input.ui @@ -14,7 +14,7 @@ Form - + &Number of Colors: @@ -24,7 +24,7 @@ - + 8 @@ -37,70 +37,70 @@ - + Disable &normalize - + Keep &aspect ratio - + Disable &Sharpening - + Disable &Trimming - + &Wide - + &Landscape - + &Right to left - + Don't so&rt - + De&speckle - + Qt::Vertical @@ -120,7 +120,7 @@ - + &Output format: @@ -130,7 +130,7 @@ - + @@ -140,7 +140,7 @@ - + Override image &size: @@ -150,9 +150,16 @@ - + + + + + Don't add links to &pages to the Table of Contents for CBC files + + + diff --git a/src/calibre/gui2/dialogs/drm_error.ui b/src/calibre/gui2/dialogs/drm_error.ui index ff28ef5a48..c4b9a1cfdb 100644 --- a/src/calibre/gui2/dialogs/drm_error.ui +++ b/src/calibre/gui2/dialogs/drm_error.ui @@ -44,7 +44,8 @@ <p>This book is locked by <b>DRM</b>. To learn more about DRM and why you cannot read or convert this book in calibre, -<a href="http://bugs.calibre-ebook.com/wiki/DRM">click here</a>. + <a href="http://drmfree.calibre-ebook.com/about#drm">click here</a>.<p>A large number of recent, DRM free releases are + available at <a href="http://drmfree.calibre-ebook.com">Open Books</a>. true diff --git a/src/calibre/gui2/dialogs/tag_categories.py b/src/calibre/gui2/dialogs/tag_categories.py index 307baffb5b..af6632bb02 100644 --- a/src/calibre/gui2/dialogs/tag_categories.py +++ b/src/calibre/gui2/dialogs/tag_categories.py @@ -73,16 +73,17 @@ class TagCategories(QDialog, Ui_TagCategories): if idx == 0: continue for n in category_values[idx](): - t = Item(name=n, label=label, index=len(self.all_items),icon=category_icons[idx], exists=True) + t = Item(name=n, label=label, index=len(self.all_items), + icon=category_icons[idx], exists=True) self.all_items.append(t) - self.all_items_dict[label+':'+n] = t + self.all_items_dict[icu_lower(label+':'+n)] = t self.categories = dict.copy(db.prefs.get('user_categories', {})) if self.categories is None: self.categories = {} for cat in self.categories: for item,l in enumerate(self.categories[cat]): - key = ':'.join([l[1], l[0]]) + key = icu_lower(':'.join([l[1], l[0]])) t = self.all_items_dict.get(key, None) if l[1] in self.category_labels: if t is None: @@ -231,6 +232,12 @@ class TagCategories(QDialog, Ui_TagCategories): def accept(self): self.save_category() + for cat in sorted(self.categories.keys(), key=sort_key): + components = cat.split('.') + for i in range(0,len(components)): + c = '.'.join(components[0:i+1]) + if c not in self.categories: + self.categories[c] = [] QDialog.accept(self) def save_category(self): diff --git a/src/calibre/gui2/dialogs/tag_list_editor.py b/src/calibre/gui2/dialogs/tag_list_editor.py index 6c3ebb22d5..cee9eb42b9 100644 --- a/src/calibre/gui2/dialogs/tag_list_editor.py +++ b/src/calibre/gui2/dialogs/tag_list_editor.py @@ -58,10 +58,12 @@ class TagListEditor(QDialog, Ui_TagListEditor): self.to_rename = {} self.to_delete = set([]) + self.original_names = {} self.all_tags = {} for k,v in data: self.all_tags[v] = k + self.original_names[k] = v for tag in sorted(self.all_tags.keys(), key=key): item = ListWidgetItem(tag) item.setData(Qt.UserRole, self.all_tags[tag]) diff --git a/src/calibre/gui2/email.py b/src/calibre/gui2/email.py index 426747e044..c84b3180f7 100644 --- a/src/calibre/gui2/email.py +++ b/src/calibre/gui2/email.py @@ -209,7 +209,6 @@ class EmailMixin(object): # {{{ def __init__(self): self.emailer = Emailer(self.job_manager) - self.emailer.start() def send_by_mail(self, to, fmts, delete_from_library, send_ids=None, do_auto_convert=True, specific_format=None): @@ -255,6 +254,8 @@ class EmailMixin(object): # {{{ to_s = list(repeat(to, len(attachments))) if attachments: + if not self.emailer.is_alive(): + self.emailer.start() self.emailer.send_mails(jobnames, Dispatcher(partial(self.email_sent, remove=remove)), attachments, to_s, subjects, texts, attachment_names) @@ -325,6 +326,8 @@ class EmailMixin(object): # {{{ files, auto = self.library_view.model().\ get_preferred_formats_from_ids([id_], fmts) return files + if not self.emailer.is_alive(): + self.emailer.start() sent_mails = self.emailer.email_news(mi, remove, get_fmts, self.email_sent) if sent_mails: diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index 196ef16b08..f7d76f2b70 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -7,17 +7,19 @@ __docformat__ = 'restructuredtext en' from PyQt4.Qt import QApplication, QFont, QFontInfo, QFontDialog -from calibre.gui2.preferences import ConfigWidgetBase, test_widget +from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList from calibre.gui2.preferences.look_feel_ui import Ui_Form from calibre.gui2 import config, gprefs, qt_app from calibre.utils.localization import available_translations, \ get_language, get_lang from calibre.utils.config import prefs +from calibre.utils.icu import sort_key class ConfigWidget(ConfigWidgetBase, Ui_Form): def genesis(self, gui): self.gui = gui + db = gui.library_view.model().db r = self.register @@ -61,6 +63,15 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): r('tags_browser_partition_method', gprefs, choices=choices) r('tags_browser_collapse_at', gprefs) + choices = set([k for k in db.field_metadata.all_field_keys() + if db.field_metadata[k]['is_category'] and + db.field_metadata[k]['datatype'] in ['text', 'series', 'enumeration']]) + choices -= set(['authors', 'publisher', 'formats', 'news']) + self.opt_categories_using_hierarchy.update_items_cache(choices) + r('categories_using_hierarchy', db.prefs, setting=CommaSeparatedList, + choices=sorted(list(choices), key=sort_key)) + + self.current_font = None self.change_font_button.clicked.connect(self.change_font) diff --git a/src/calibre/gui2/preferences/look_feel.ui b/src/calibre/gui2/preferences/look_feel.ui index 3f2bb3e145..bc965b89fa 100644 --- a/src/calibre/gui2/preferences/look_feel.ui +++ b/src/calibre/gui2/preferences/look_feel.ui @@ -7,7 +7,7 @@ 0 0 670 - 392 + 422 @@ -136,7 +136,7 @@ - Tags browser category partitioning method: + Tags browser category &partitioning method: opt_tags_browser_partition_method @@ -157,7 +157,7 @@ if you never want subcategories - Collapse when more items than: + &Collapse when more items than: opt_tags_browser_collapse_at @@ -190,6 +190,28 @@ up into sub-categories. If the partition method is set to disable, this value is + + + + Categories with &hierarchical items: + + + opt_categories_using_hierarchy + + + + + + + A comma-separated list of columns in which items containing +periods are displayed in the tag browser trees. For example, if +this box contains 'tags' then tags of the form 'Mystery.English' +and 'Mystery.Thriller' will be displayed with English and Thriller +both under 'Mystery'. If 'tags' is not in this box, +then the tags will be displayed each on their own line. + + + @@ -275,6 +297,13 @@ up into sub-categories. If the partition method is set to disable, this value is + + + MultiCompleteLineEdit + QLineEdit +
calibre/gui2/complete.h
+
+
diff --git a/src/calibre/gui2/store_download.py b/src/calibre/gui2/store_download.py index f9460df17b..241ff2f10a 100644 --- a/src/calibre/gui2/store_download.py +++ b/src/calibre/gui2/store_download.py @@ -167,9 +167,10 @@ class StoreDownloadMixin(object): def __init__(self): self.store_downloader = StoreDownloader(self.job_manager) - self.store_downloader.start() def download_from_store(self, url='', save_as_loc='', add_to_lib=True): + if not self.store_downloader.is_alive(): + self.store_downloader.start() self.store_downloader.download_from_store(Dispatcher(self.downloaded_from_store), self.library_view.model().db, url, save_as_loc, add_to_lib) self.status_bar.show_message(_('Downloading') + ' ' + url, 3000) diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index 3af3271921..06f01a1649 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en' Browsing book collection by tags. ''' -import traceback +import traceback, copy, cPickle from itertools import izip from functools import partial @@ -16,7 +16,7 @@ from PyQt4.Qt import Qt, QTreeView, QApplication, pyqtSignal, QFont, QSize, \ QIcon, QPoint, QVBoxLayout, QHBoxLayout, QComboBox, QTimer,\ QAbstractItemModel, QVariant, QModelIndex, QMenu, QFrame,\ QPushButton, QWidget, QItemDelegate, QString, QLabel, \ - QShortcut, QKeySequence, SIGNAL + QShortcut, QKeySequence, SIGNAL, QMimeData from calibre.ebooks.metadata import title_sort from calibre.gui2 import config, NONE, gprefs @@ -25,7 +25,7 @@ from calibre.utils.config import tweaks from calibre.utils.icu import sort_key, upper, lower, strcmp from calibre.utils.search_query_parser import saved_searches from calibre.utils.formatter import eval_formatter -from calibre.gui2 import error_dialog +from calibre.gui2 import error_dialog, question_dialog from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.dialogs.tag_categories import TagCategories from calibre.gui2.dialogs.tag_list_editor import TagListEditor @@ -70,15 +70,19 @@ TAG_SEARCH_STATES = {'clear': 0, 'mark_plus': 1, 'mark_minus': 2} class TagsView(QTreeView): # {{{ - refresh_required = pyqtSignal() - tags_marked = pyqtSignal(object) - user_category_edit = pyqtSignal(object) - tag_list_edit = pyqtSignal(object, object) - saved_search_edit = pyqtSignal(object) - author_sort_edit = pyqtSignal(object, object) - tag_item_renamed = pyqtSignal() - search_item_renamed = pyqtSignal() - drag_drop_finished = pyqtSignal(object, object) + refresh_required = pyqtSignal() + tags_marked = pyqtSignal(object) + edit_user_category = pyqtSignal(object) + delete_user_category = pyqtSignal(object) + del_item_from_user_cat = pyqtSignal(object, object, object) + add_item_to_user_cat = pyqtSignal(object, object, object) + add_subcategory = pyqtSignal(object) + tag_list_edit = pyqtSignal(object, object) + saved_search_edit = pyqtSignal(object) + author_sort_edit = pyqtSignal(object, object) + tag_item_renamed = pyqtSignal() + search_item_renamed = pyqtSignal() + drag_drop_finished = pyqtSignal(object, object) def __init__(self, parent=None): QTreeView.__init__(self, parent=None) @@ -94,7 +98,8 @@ class TagsView(QTreeView): # {{{ self.setItemDelegate(TagDelegate(self)) self.made_connections = False self.setAcceptDrops(True) - self.setDragDropMode(self.DropOnly) + self.setDragEnabled(True) + self.setDragDropMode(self.DragDrop) self.setDropIndicatorShown(True) self.setAutoExpandDelay(500) self.pane_is_visible = False @@ -103,6 +108,7 @@ class TagsView(QTreeView): # {{{ else: self.collapse_model = gprefs['tags_browser_partition_method'] self.search_icon = QIcon(I('search.png')) + self.user_category_icon = QIcon(I('tb_folder.png')) def set_pane_is_visible(self, to_what): pv = self.pane_is_visible @@ -216,14 +222,29 @@ class TagsView(QTreeView): # {{{ self.tag_list_edit.emit(category, key) return if action == 'manage_categories': - self.user_category_edit.emit(category) + self.edit_user_category.emit(category) return if action == 'search': self._toggle(index, set_to=search_state) return + if action == 'add_to_category': + self.add_item_to_user_cat.emit(category, + getattr(index, 'original_name', index.name), + index.category) + return + if action == 'add_subcategory': + self.add_subcategory.emit(key) + return if action == 'search_category': self.tags_marked.emit(key + ':' + search_state) return + if action == 'delete_user_category': + self.delete_user_category.emit(key) + return + if action == 'delete_item_from_user_category': + self.del_item_from_user_cat.emit(key, + getattr(index, 'original_name', index.name), index.category) + return if action == 'manage_searches': self.saved_search_edit.emit(category) return @@ -254,17 +275,18 @@ class TagsView(QTreeView): # {{{ if index.isValid(): item = index.internalPointer() - tag_name = '' + tag = None if item.type == TagTreeItem.TAG: - tag_item = item - tag_name = item.tag.name - tag_id = item.tag.id - item = item.parent + tag = item.tag + can_edit = getattr(tag, 'can_edit', True) + while item.type != TagTreeItem.CATEGORY: + item = item.parent if item.type == TagTreeItem.CATEGORY: - while item.parent != self._model.root_item: - item = item.parent + if not item.category_key.startswith('@'): + while item.parent != self._model.root_item: + item = item.parent category = unicode(item.name.toString()) key = item.category_key # Verify that we are working with a field that we know something about @@ -272,35 +294,76 @@ class TagsView(QTreeView): # {{{ return True # Did the user click on a leaf node? - if tag_name: + if tag: # If the user right-clicked on an editable item, then offer # the possibility of renaming that item. - if key in ['authors', 'tags', 'series', 'publisher', 'search'] or \ - (self.db.field_metadata[key]['is_custom'] and \ - self.db.field_metadata[key]['datatype'] != 'rating'): + if can_edit: # Add the 'rename' items - self.context_menu.addAction(_('Rename %s')%tag_name, - partial(self.context_menu_handler, action='edit_item', - category=tag_item, index=index)) + self.context_menu.addAction(_('Rename %s')%tag.name, + partial(self.context_menu_handler, action='edit_item', + index=index)) if key == 'authors': - self.context_menu.addAction(_('Edit sort for %s')%tag_name, + self.context_menu.addAction(_('Edit sort for %s')%tag.name, partial(self.context_menu_handler, - action='edit_author_sort', index=tag_id)) + action='edit_author_sort', index=tag.id)) + m = self.context_menu.addMenu(self.user_category_icon, + _('Add %s to user category')%tag.name) + nt = self.model().category_node_tree + def add_node_tree(tree_dict, m, path): + p = path[:] + for k in sorted(tree_dict.keys(), key=sort_key): + p.append(k) + n = k[1:] if k.startswith('@') else k + m.addAction(self.user_category_icon, n, + partial(self.context_menu_handler, + 'add_to_category', + category='.'.join(p), + index=tag)) + if len(tree_dict[k]): + tm = m.addMenu(self.user_category_icon, + _('Children of %s')%n) + add_node_tree(tree_dict[k], tm, p) + p.pop() + add_node_tree(nt, m, []) + + if key.startswith('@') and not item.is_gst: + self.context_menu.addAction(self.user_category_icon, + _('Remove %s from category %s')%(tag.name, item.py_name), + partial(self.context_menu_handler, + action='delete_item_from_user_category', + key = key, index = tag)) # Add the search for value items self.context_menu.addAction(self.search_icon, - _('Search for %s')%tag_name, + _('Search for %s')%tag.name, partial(self.context_menu_handler, action='search', search_state=TAG_SEARCH_STATES['mark_plus'], index=index)) self.context_menu.addAction(self.search_icon, - _('Search for everything but %s')%tag_name, + _('Search for everything but %s')%tag.name, partial(self.context_menu_handler, action='search', search_state=TAG_SEARCH_STATES['mark_minus'], index=index)) self.context_menu.addSeparator() + elif key.startswith('@') and not item.is_gst: + if item.can_edit: + self.context_menu.addAction(self.user_category_icon, + _('Rename %s')%item.py_name, + partial(self.context_menu_handler, action='edit_item', + index=index)) + self.context_menu.addAction(self.user_category_icon, + _('Add sub-category to %s')%item.py_name, + partial(self.context_menu_handler, + action='add_subcategory', key=key)) + self.context_menu.addAction(self.user_category_icon, + _('Delete user category %s')%item.py_name, + partial(self.context_menu_handler, + action='delete_user_category', key=key)) + self.context_menu.addSeparator() # Hide/Show/Restore categories - self.context_menu.addAction(_('Hide category %s') % category, - partial(self.context_menu_handler, action='hide', category=category)) + if not key.startswith('@') or key.find('.') < 0: + self.context_menu.addAction(_('Hide category %s') % category, + partial(self.context_menu_handler, action='hide', + category=category)) if self.hidden_categories: m = self.context_menu.addMenu(_('Show category')) for col in sorted(self.hidden_categories, key=sort_key): @@ -323,21 +386,23 @@ class TagsView(QTreeView): # {{{ self.db.field_metadata[key]['is_custom']: self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='open_editor', - category=tag_name, key=key)) + category=getattr(tag, 'original_name', tag.name) + if tag else None, key=key)) elif key == 'authors': self.context_menu.addAction(_('Manage %s')%category, partial(self.context_menu_handler, action='edit_author_sort')) elif key == 'search': self.context_menu.addAction(_('Manage Saved Searches'), partial(self.context_menu_handler, action='manage_searches', - category=tag_name)) + category=tag.name if tag else None)) # Always show the user categories editor self.context_menu.addSeparator() - if category in self.db.prefs.get('user_categories', {}).keys(): + if key.startswith('@') and \ + key[1:] in self.db.prefs.get('user_categories', {}).keys(): self.context_menu.addAction(_('Manage User Categories'), partial(self.context_menu_handler, action='manage_categories', - category=category)) + category=key[1:])) else: self.context_menu.addAction(_('Manage User Categories'), partial(self.context_menu_handler, action='manage_categories', @@ -376,20 +441,32 @@ class TagsView(QTreeView): # {{{ index = self.indexAt(event.pos()) if not index.isValid(): return + src_is_tb = event.mimeData().hasFormat('application/calibre+from_tag_browser') item = index.internalPointer() flags = self._model.flags(index) if item.type == TagTreeItem.TAG and flags & Qt.ItemIsDropEnabled: - self.setDropIndicatorShown(True) - else: - if item.type == TagTreeItem.CATEGORY: - fm_dest = self.db.metadata_for_field(item.category_key) - if fm_dest['kind'] == 'user': - md = event.mimeData() + self.setDropIndicatorShown(not src_is_tb) + return + if item.type == TagTreeItem.CATEGORY and not item.is_gst: + fm_dest = self.db.metadata_for_field(item.category_key) + if fm_dest['kind'] == 'user': + if src_is_tb: + if event.dropAction() == Qt.MoveAction: + data = str(event.mimeData().data('application/calibre+from_tag_browser')) + src = cPickle.loads(data) + for s in src: + if s[0] == TagTreeItem.TAG and \ + (not s[1].startswith('@') or s[2]): + return + self.setDropIndicatorShown(True) + return + md = event.mimeData() + if hasattr(md, 'column_name'): fm_src = self.db.metadata_for_field(md.column_name) if md.column_name in ['authors', 'publisher', 'series'] or \ (fm_src['is_custom'] and - fm_src['datatype'] in ['series', 'text'] and - not fm_src['is_multiple']): + fm_src['datatype'] in ['series', 'text'] and + not fm_src['is_multiple']): self.setDropIndicatorShown(True) def clear(self): @@ -500,6 +577,8 @@ class TagTreeItem(object): # {{{ def category_data(self, role): if role == Qt.DisplayRole: return QVariant(self.py_name + ' [%d]'%len(self.child_tags())) + if role == Qt.EditRole: + return QVariant(self.py_name) if role == Qt.DecorationRole: return self.icon if role == Qt.FontRole: @@ -515,7 +594,13 @@ class TagTreeItem(object): # {{{ name = tag.sort tt_author = True else: - name = tag.name + p = self + while p.parent.type != self.ROOT: + p = p.parent + if p.category_key.startswith('@'): + name = getattr(tag, 'original_name', tag.name) + else: + name = tag.name tt_author = False if role == Qt.DisplayRole: if tag.count == 0: @@ -523,7 +608,7 @@ class TagTreeItem(object): # {{{ else: return QVariant('[%d] %s'%(tag.count, name)) if role == Qt.EditRole: - return QVariant(tag.name) + return QVariant(getattr(tag, 'original_name', tag.name)) if role == Qt.DecorationRole: return self.icon_state_map[tag.state] if role == Qt.ToolTipRole: @@ -550,12 +635,12 @@ class TagTreeItem(object): # {{{ def child_tags(self): res = [] - for t in self.children: - if t.type == TagTreeItem.CATEGORY: - for c in t.children: - res.append(c) - else: - res.append(t) + def recurse(nodes, res): + for t in nodes: + if t.type != TagTreeItem.CATEGORY: + res.append(t) + recurse(t.children, res) + recurse(self.children, res) return res # }}} @@ -590,31 +675,177 @@ class TagsModel(QAbstractItemModel): # {{{ data = self.get_node_tree(config['sort_tags_by']) gst = db.prefs.get('grouped_search_terms', {}) self.root_item = TagTreeItem() + self.category_nodes = [] + + last_category_node = None + category_node_map = {} + self.category_node_tree = {} for i, r in enumerate(self.row_map): if self.hidden_categories and self.categories[i] in self.hidden_categories: continue + is_gst = False if r.startswith('@') and r[1:] in gst: tt = _(u'The grouped search term name is "{0}"').format(r[1:]) + is_gst = True elif r == 'news': tt = '' else: tt = _(u'The lookup/search name is "{0}"').format(r) - TagTreeItem(parent=self.root_item, - data=self.categories[i], - category_icon=self.category_icon_map[r], - tooltip=tt, category_key=r) + + if r.startswith('@'): + path_parts = [p.strip() for p in r.split('.') if p.strip()] + path = '' + last_category_node = self.root_item + tree_root = self.category_node_tree + for i,p in enumerate(path_parts): + path += p + if path not in category_node_map: + node = TagTreeItem(parent=last_category_node, + data=p[1:] if i == 0 else p, + category_icon=self.category_icon_map[r], + tooltip=tt if path == r else path, + category_key=path) + last_category_node = node + category_node_map[path] = node + self.category_nodes.append(node) + node.can_edit = (not is_gst) and (i == (len(path_parts)-1)) + node.is_gst = is_gst + if not is_gst: + tree_root[p] = {} + tree_root = tree_root[p] + else: + last_category_node = category_node_map[path] + tree_root = tree_root[p] + path += '.' + else: + node = TagTreeItem(parent=self.root_item, + data=self.categories[i], + category_icon=self.category_icon_map[r], + tooltip=tt, category_key=r) + node.is_gst = False + category_node_map[r] = node + last_category_node = node + self.category_nodes.append(node) self.refresh(data=data) def break_cycles(self): self.db = self.root_item = None def mimeTypes(self): - return ["application/calibre+from_library"] + return ["application/calibre+from_library", + 'application/calibre+from_tag_browser'] + + def mimeData(self, indexes): + data = [] + for idx in indexes: + if idx.isValid(): + # get some useful serializable data + node = idx.internalPointer() + if node.type == TagTreeItem.CATEGORY: + d = (node.type, node.py_name, node.category_key) + else: + t = node.tag + p = node + while p.type != TagTreeItem.CATEGORY: + p = p.parent + d = (node.type, p.category_key, p.is_gst, + getattr(t, 'original_name', t.name), t.category, t.id) + data.append(d) + else: + data.append(None) + raw = bytearray(cPickle.dumps(data, -1)) + ans = QMimeData() + ans.setData('application/calibre+from_tag_browser', raw) + return ans def dropMimeData(self, md, action, row, column, parent): - if not md.hasFormat("application/calibre+from_library") or \ - action != Qt.CopyAction: + fmts = set([unicode(x) for x in md.formats()]) + if not fmts.intersection(set(self.mimeTypes())): return False + if "application/calibre+from_library" in fmts: + if action != Qt.CopyAction: + return False + return self.do_drop_from_library(md, action, row, column, parent) + elif 'application/calibre+from_tag_browser' in fmts: + return self.do_drop_from_tag_browser(md, action, row, column, parent) + + def do_drop_from_tag_browser(self, md, action, row, column, parent): + if not parent.isValid(): + return False + dest = parent.internalPointer() + if dest.type != TagTreeItem.CATEGORY: + return False + if not md.hasFormat('application/calibre+from_tag_browser'): + return False + data = str(md.data('application/calibre+from_tag_browser')) + src = cPickle.loads(data) + for s in src: + if s[0] != TagTreeItem.TAG: + return False + return self.move_or_copy_item_to_user_category(src, dest, action) + + def move_or_copy_item_to_user_category(self, src, dest, action): + ''' + src is a list of tuples representing items to copy. The tuple is + (type, containing category key, category key is global search term, + full name, category key, id) + The 'id' member is ignored, and can be None. + The type must be TagTreeItem.TAG + dest is the TagTreeItem node to receive the items + action is Qt.CopyAction or Qt.MoveAction + ''' + user_cats = self.db.prefs.get('user_categories', {}) + parent_node = None + copied_node = None + for s in src: + src_parent, src_parent_is_gst, src_name, src_cat = s[1:5] + parent_node = src_parent + if src_parent.startswith('@'): + is_uc = True + src_parent = src_parent[1:] + else: + is_uc = False + dest_key = dest.category_key[1:] + if dest_key not in user_cats: + continue + new_cat = [] + # delete the item if the source is a user category and action is move + if is_uc and not src_parent_is_gst and src_parent in user_cats and \ + action == Qt.MoveAction: + for tup in user_cats[src_parent]: + if src_name == tup[0] and src_cat == tup[1]: + continue + new_cat.append(list(tup)) + user_cats[src_parent] = new_cat + else: + copied_node = (src_parent, src_name) + + # Now add the item to the destination user category + add_it = True + if not is_uc and src_cat == 'news': + src_cat = 'tags' + for tup in user_cats[dest_key]: + if src_name == tup[0] and src_cat == tup[1]: + add_it = False + if add_it: + user_cats[dest_key].append([src_name, src_cat, 0]) + + self.db.prefs.set('user_categories', user_cats) + self.tags_view.recount() + + if parent_node is not None: + m = self.tags_view.model() + if copied_node is not None: + path = m.find_item_node(parent_node, copied_node[1], None, + equals_match=True) + else: + path = m.find_category_node(parent_node) + idx = m.index_for_path(path) + self.tags_view.setExpanded(idx, True) + m.show_item_at_index(idx) + return True + + def do_drop_from_library(self, md, action, row, column, parent): idx = parent if idx.isValid(): node = self.data(idx, Qt.UserRole) @@ -754,10 +985,15 @@ class TagsModel(QAbstractItemModel): # {{{ for user_cat in sorted(self.db.prefs.get('user_categories', {}).keys(), key=sort_key): cat_name = '@' + user_cat # add the '@' to avoid name collision - try: - tb_cats.add_user_category(label=cat_name, name=user_cat) - except ValueError: - traceback.print_exc() + while True: + try: + tb_cats.add_user_category(label=cat_name, name=user_cat) + dot = cat_name.rfind('.') + if dot < 0: + break + cat_name = cat_name[:dot] + except ValueError: + break for cat in sorted(self.db.prefs.get('grouped_search_terms', {}).keys(), key=sort_key): @@ -794,7 +1030,7 @@ class TagsModel(QAbstractItemModel): # {{{ data = self.get_node_tree(sort_by) # get category data if data is None: return False - row_index = -1 + collapse = gprefs['tags_browser_collapse_at'] collapse_model = self.collapse_model if collapse == 0: @@ -810,53 +1046,43 @@ class TagsModel(QAbstractItemModel): # {{{ collapse_template = tweaks['categories_collapsed_popularity_template'] collapse_letter = collapse_letter_sk = None - for i, r in enumerate(self.row_map): - if self.hidden_categories and self.categories[i] in self.hidden_categories: - continue - row_index += 1 - category = self.root_item.children[row_index] - names = [] - states = [] - children = category.child_tags() - states = [t.tag.state for t in children] - names = [t.tag.name for names in children] - state_map = dict(izip(names, states)) - category_index = self.index(row_index, 0, QModelIndex()) + def process_one_node(category, state_map, collapse_letter, collapse_letter_sk): + category_index = self.createIndex(category.row(), 0, category) category_node = category_index.internalPointer() - if len(category.children) > 0: - self.beginRemoveRows(category_index, 0, - len(category.children)-1) - category.children = [] - self.endRemoveRows() - cat_len = len(data[r]) + key = category_node.category_key + if key not in data: + return ((collapse_letter, collapse_letter_sk)) + cat_len = len(data[key]) if cat_len <= 0: - continue + return ((collapse_letter, collapse_letter_sk)) - self.beginInsertRows(category_index, 0, len(data[r])-1) - clear_rating = True if r not in self.categories_with_ratings and \ - not self.db.field_metadata[r]['is_custom'] and \ - not self.db.field_metadata[r]['kind'] == 'user' \ + fm = self.db.field_metadata[key] + clear_rating = True if key not in self.categories_with_ratings and \ + not fm['is_custom'] and \ + not fm['kind'] == 'user' \ else False - tt = r if self.db.field_metadata[r]['kind'] == 'user' else None - for idx,tag in enumerate(data[r]): + tt = key if fm['kind'] == 'user' else None + for idx,tag in enumerate(data[key]): if clear_rating: tag.avg_rating = None - tag.state = state_map.get(tag.name, 0) + tag.state = state_map.get((tag.name, tag.category), 0) if collapse_model != 'disable' and cat_len > collapse: if collapse_model == 'partition': if (idx % collapse) == 0: d = {'first': tag} if cat_len > idx + collapse: - d['last'] = data[r][idx+collapse-1] + d['last'] = data[key][idx+collapse-1] else: - d['last'] = data[r][cat_len-1] + d['last'] = data[key][cat_len-1] name = eval_formatter.safe_format(collapse_template, d, 'TAG_VIEW', None) + self.beginInsertRows(category_index, 999999, 1) #len(data[key])-1) sub_cat = TagTreeItem(parent=category, data = name, tooltip = None, category_icon = category_node.icon, category_key=category_node.category_key) + self.endInsertRows() else: ts = tag.sort if not ts: @@ -877,12 +1103,65 @@ class TagsModel(QAbstractItemModel): # {{{ category_icon = category_node.icon, tooltip = None, category_key=category_node.category_key) - t = TagTreeItem(parent=sub_cat, data=tag, tooltip=tt, - icon_map=self.icon_state_map) + node_parent = sub_cat else: - t = TagTreeItem(parent=category, data=tag, tooltip=tt, + node_parent = category + + components = [t for t in tag.name.split('.')] + if key in ['authors', 'publisher', 'news', 'formats', 'rating'] or \ + key not in self.db.prefs.get('categories_using_hierarchy', []) or\ + len(components) == 1 or \ + fm['kind'] == 'user': + self.beginInsertRows(category_index, 999999, 1) + TagTreeItem(parent=node_parent, data=tag, tooltip=tt, icon_map=self.icon_state_map) - self.endInsertRows() + self.endInsertRows() + tag.can_edit = key != 'formats' and (key == 'news' or \ + self.db.field_metadata[tag.category]['datatype'] in \ + ['text', 'series', 'enumeration']) + else: + for i,comp in enumerate(components): + child_map = dict([(t.tag.name, t) for t in node_parent.children + if t.type != TagTreeItem.CATEGORY]) + if comp in child_map: + node_parent = child_map[comp] + node_parent.tag.count += tag.count + node_parent.tag.use_prefix = True + else: + if i < len(components)-1: + t = copy.copy(tag) + t.original_name = '.'.join(components[:i+1]) + t.can_edit = False + else: + t = tag + t.original_name = t.name + t.can_edit = True + t.use_prefix = True + t.name = comp + self.beginInsertRows(category_index, 999999, 1) + node_parent = TagTreeItem(parent=node_parent, data=t, + tooltip=tt, icon_map=self.icon_state_map) + self.endInsertRows() + + return ((collapse_letter, collapse_letter_sk)) + + for category in self.category_nodes: + if len(category.children) > 0: + child_map = category.children + states = [c.tag.state for c in category.child_tags()] + names = [(c.tag.name, c.tag.category) for c in category.child_tags()] + state_map = dict(izip(names, states)) + ctags = [c for c in child_map if c.type == TagTreeItem.CATEGORY] + start = len(ctags) + self.beginRemoveRows(self.createIndex(category.row(), 0, category), + start, len(child_map)-1) + category.children = ctags + self.endRemoveRows() + else: + state_map = {} + + collapse_letter, collapse_letter_sk = process_one_node(category, + state_map, collapse_letter, collapse_letter_sk) return True def columnCount(self, parent): @@ -907,7 +1186,40 @@ class TagsModel(QAbstractItemModel): # {{{ _('An item cannot be set to nothing. Delete it instead.')).exec_() return False item = index.internalPointer() - key = item.parent.category_key + if item.type == TagTreeItem.CATEGORY and item.category_key.startswith('@'): + user_cats = self.db.prefs.get('user_categories', {}) + ckey = item.category_key[1:] + dotpos = ckey.rfind('.') + if dotpos < 0: + nkey = val + else: + nkey = ckey[:dotpos+1] + val + for c in user_cats: + if c.startswith(ckey): + if len(c) == len(ckey): + if nkey in user_cats: + error_dialog(self.tags_view, _('Rename user category'), + _('The name %s is already used'%nkey), show=True) + return False + user_cats[nkey] = user_cats[ckey] + del user_cats[ckey] + elif c[len(ckey)] == '.': + rest = c[len(ckey):] + if (nkey + rest) in user_cats: + error_dialog(self.tags_view, _('Rename user category'), + _('The name %s is already used')%(nkey+rest), show=True) + return False + user_cats[nkey + rest] = user_cats[ckey + rest] + del user_cats[ckey + rest] + self.db.prefs.set('user_categories', user_cats) + self.tags_view.set_new_model() + # must not use 'self' below because the model has changed! + p = self.tags_view.model().find_category_node('@' + nkey) + self.tags_view.model().show_item_at_path(p) + return True + + key = item.tag.category + name = getattr(item.tag, 'original_name', item.tag.name) # make certain we know about the item's category if key not in self.db.field_metadata: return False @@ -938,10 +1250,54 @@ class TagsModel(QAbstractItemModel): # {{{ label=self.db.field_metadata[key]['label']) self.tags_view.tag_item_renamed.emit() item.tag.name = val + self.rename_item_in_all_user_categories(name, key, val) self.refresh() # Should work, because no categories can have disappeared self.show_item_at_path(path) return True + def rename_item_in_all_user_categories(self, item_name, item_category, new_name): + ''' + Search all user categories for items named item_name with category + item_category and rename them to new_name. The caller must arrange to + redisplay the tree as appropriate (recount or set_new_model) + ''' + user_cats = self.db.prefs.get('user_categories', {}) + for k in user_cats.keys(): + new_contents = [] + for tup in user_cats[k]: + if tup[0] == item_name and tup[1] == item_category: + new_contents.append([new_name, item_category, 0]) + else: + new_contents.append(tup) + user_cats[k] = new_contents + self.db.prefs.set('user_categories', user_cats) + + def delete_item_from_all_user_categories(self, item_name, item_category): + ''' + Search all user categories for items named item_name with category + item_category and delete them. The caller must arrange to redisplay the + tree as appropriate (recount or set_new_model) + ''' + user_cats = self.db.prefs.get('user_categories', {}) + for cat in user_cats.keys(): + self.delete_item_from_user_category(cat, item_name, item_category, + user_categories=user_cats) + self.db.prefs.set('user_categories', user_cats) + + def delete_item_from_user_category(self, category, item_name, item_category, + user_categories=None): + if user_categories is not None: + user_cats = user_categories + else: + user_cats = self.db.prefs.get('user_categories', {}) + new_contents = [] + for tup in user_cats[category]: + if tup[0] != item_name or tup[1] != item_category: + new_contents.append(tup) + user_cats[category] = new_contents + if user_categories is None: + self.db.prefs.set('user_categories', user_cats) + def headerData(self, *args): return NONE @@ -950,6 +1306,8 @@ class TagsModel(QAbstractItemModel): # {{{ if index.isValid(): node = self.data(index, Qt.UserRole) if node.type == TagTreeItem.TAG: + if getattr(node.tag, 'can_edit', True): + ans |= Qt.ItemIsDragEnabled fm = self.db.metadata_for_field(node.tag.category) if node.tag.category in \ ('tags', 'series', 'authors', 'rating', 'publisher') or \ @@ -961,7 +1319,7 @@ class TagsModel(QAbstractItemModel): # {{{ return ans def supportedDropActions(self): - return Qt.CopyAction + return Qt.CopyAction|Qt.MoveAction def path_for_index(self, index): ans = [] @@ -1022,27 +1380,22 @@ class TagsModel(QAbstractItemModel): # {{{ def reset_all_states(self, except_=None): update_list = [] - def process_tag(tag_index, tag_item): - tag = tag_item.tag - if tag is except_: - self.dataChanged.emit(tag_index, tag_index) - return - if tag.state != 0 or tag in update_list: - tag.state = 0 - update_list.append(tag) - self.dataChanged.emit(tag_index, tag_index) + def process_tag(tag_item): + if tag_item.type != TagTreeItem.CATEGORY: + tag = tag_item.tag + if tag is except_: + tag_index = self.createIndex(tag_item.row(), 0, tag_item) + self.dataChanged.emit(tag_index, tag_index) + elif tag.state != 0 or tag in update_list: + tag_index = self.createIndex(tag_item.row(), 0, tag_item) + tag.state = 0 + update_list.append(tag) + self.dataChanged.emit(tag_index, tag_index) + for t in tag_item.children: + process_tag(t) - def process_level(category_index): - for j in xrange(self.rowCount(category_index)): - tag_index = self.index(j, 0, category_index) - tag_item = tag_index.internalPointer() - if tag_item.type == TagTreeItem.CATEGORY: - process_level(tag_index) - else: - process_tag(tag_index, tag_item) - - for i in xrange(self.rowCount(QModelIndex())): - process_level(self.index(i, 0, QModelIndex())) + for t in self.root_item.children: + process_tag(t) def clear_state(self): self.reset_all_states() @@ -1073,14 +1426,10 @@ class TagsModel(QAbstractItemModel): # {{{ # They will be 'checked' in both places, but we want to put the node # into the search string only once. The nodes_seen set helps us do that nodes_seen = set() - row_index = -1 - for i, key in enumerate(self.row_map): - if self.hidden_categories and self.categories[i] in self.hidden_categories: - continue - row_index += 1 - category_item = self.root_item.children[row_index] - for tag_item in category_item.child_tags(): + for node in self.category_nodes: + key = node.category_key + for tag_item in node.child_tags(): tag = tag_item.tag if tag.state != TAG_SEARCH_STATES['clear']: prefix = ' not ' if tag.state == TAG_SEARCH_STATES['mark_minus'] \ @@ -1089,30 +1438,34 @@ class TagsModel(QAbstractItemModel): # {{{ if tag.name and tag.name[0] == u'\u2605': # char is a star. Assume rating ans.append('%s%s:%s'%(prefix, category, len(tag.name))) else: + name = getattr(tag, 'original_name', tag.name) + use_prefix = getattr(tag, 'use_prefix', False) if category == 'tags': - if tag.name in tags_seen: + if name in tags_seen: continue - tags_seen.add(tag.name) + tags_seen.add(name) if tag in nodes_seen: continue nodes_seen.add(tag) - ans.append('%s%s:"=%s"'%(prefix, category, - tag.name.replace(r'"', r'\"'))) + ans.append('%s%s:"=%s%s"'%(prefix, category, + '.' if use_prefix else '', + name.replace(r'"', r'\"'))) return ans - def find_item_node(self, key, txt, start_path): + def find_item_node(self, key, txt, start_path, equals_match=False): ''' Search for an item (a node) in the tags browser list that matches both - the key (exact case-insensitive match) and txt (contains case- - insensitive match). Returns the path to the node. Note that paths are to - a location (second item, fourth item, 25 item), not to a node. If + the key (exact case-insensitive match) and txt (not equals_match => + case-insensitive contains match; equals_match => case_insensitive + equal match). Returns the path to the node. Note that paths are to a + location (second item, fourth item, 25 item), not to a node. If start_path is None, the search starts with the topmost node. If the tree is changed subsequent to calling this method, the path can easily refer to a different node or no node at all. ''' if not txt: return None - txt = lower(txt) + txt = lower(txt) if not equals_match else txt self.path_found = None if start_path is None: start_path = [] @@ -1124,9 +1477,14 @@ class TagsModel(QAbstractItemModel): # {{{ tag = tag_item.tag if tag is None: return False - if lower(tag.name).find(txt) >= 0: + name = getattr(tag, 'original_name', tag.name) + if (equals_match and strcmp(name, txt) == 0) or \ + (not equals_match and lower(name).find(txt) >= 0): self.path_found = path return True + for i,c in enumerate(tag_item.children): + if process_tag(depth+1, self.createIndex(i, 0, c), c, start_path): + return True return False def process_level(depth, category_index, start_path): @@ -1136,15 +1494,14 @@ class TagsModel(QAbstractItemModel): # {{{ return False if path[depth] > start_path[depth]: start_path = path - if key and strcmp(category_index.internalPointer().category_key, key) != 0: - return False + my_key = category_index.internalPointer().category_key for j in xrange(self.rowCount(category_index)): tag_index = self.index(j, 0, category_index) tag_item = tag_index.internalPointer() if tag_item.type == TagTreeItem.CATEGORY: if process_level(depth+1, tag_index, start_path): return True - else: + elif not key or strcmp(key, my_key) == 0: if process_tag(depth+1, tag_index, tag_item, start_path): return True return False @@ -1154,7 +1511,7 @@ class TagsModel(QAbstractItemModel): # {{{ break return self.path_found - def find_category_node(self, key): + def find_category_node(self, key, parent=QModelIndex()): ''' Search for an category node (a top-level node) in the tags browser list that matches the key (exact case-insensitive match). Returns the path to @@ -1163,11 +1520,17 @@ class TagsModel(QAbstractItemModel): # {{{ if not key: return None - for i in xrange(self.rowCount(QModelIndex())): - idx = self.index(i, 0, QModelIndex()) - ckey = idx.internalPointer().category_key - if strcmp(ckey, key) == 0: - return self.path_for_index(idx) + for i in xrange(self.rowCount(parent)): + idx = self.index(i, 0, parent) + node = idx.internalPointer() + if node.type == TagTreeItem.CATEGORY: + ckey = node.category_key + if strcmp(ckey, key) == 0: + return self.path_for_index(idx) + if len(node.children): + v = self.find_category_node(key, idx) + if v is not None: + return v return None def show_item_at_path(self, path, box=False): @@ -1221,16 +1584,56 @@ class TagBrowserMixin(object): # {{{ self.tags_view.set_database(db, self.tag_match, self.sort_by) self.tags_view.tags_marked.connect(self.search.set_search_string) self.tags_view.tag_list_edit.connect(self.do_tags_list_edit) - self.tags_view.user_category_edit.connect(self.do_user_categories_edit) + self.tags_view.edit_user_category.connect(self.do_edit_user_categories) + self.tags_view.delete_user_category.connect(self.do_delete_user_category) + self.tags_view.del_item_from_user_cat.connect(self.do_del_item_from_user_cat) + self.tags_view.add_subcategory.connect(self.do_add_subcategory) + self.tags_view.add_item_to_user_cat.connect(self.do_add_item_to_user_cat) self.tags_view.saved_search_edit.connect(self.do_saved_search_edit) self.tags_view.author_sort_edit.connect(self.do_author_sort_edit) self.tags_view.tag_item_renamed.connect(self.do_tag_item_renamed) self.tags_view.search_item_renamed.connect(self.saved_searches_changed) self.tags_view.drag_drop_finished.connect(self.drag_drop_finished) self.edit_categories.clicked.connect(lambda x: - self.do_user_categories_edit()) + self.do_edit_user_categories()) - def do_user_categories_edit(self, on_category=None): + def do_add_subcategory(self, on_category_key, new_category_name=None): + ''' + Add a subcategory to the category 'on_category'. If new_category_name is + None, then a default name is shown and the user is offered the + opportunity to edit the name. + ''' + db = self.library_view.model().db + user_cats = db.prefs.get('user_categories', {}) + + # Ensure that the temporary name we will use is not already there + i = 0 + if new_category_name is not None: + new_name = new_category_name.replace('.', '') + else: + new_name = _('New Category').replace('.', '') + n = new_name + while True: + new_cat = on_category_key[1:] + '.' + n + if new_cat not in user_cats: + break + i += 1 + n = new_name + unicode(i) + # Add the new category + user_cats[new_cat] = [] + db.prefs.set('user_categories', user_cats) + self.tags_view.set_new_model() + m = self.tags_view.model() + idx = m.index_for_path(m.find_category_node('@' + new_cat)) + m.show_item_at_index(idx) + # Open the editor on the new item to rename it + if new_category_name is None: + self.tags_view.edit(idx) + + def do_edit_user_categories(self, on_category=None): + ''' + Open the user categories editor. + ''' db = self.library_view.model().db d = TagCategories(self, db, on_category) if d.exec_() == d.Accepted: @@ -1240,9 +1643,89 @@ class TagBrowserMixin(object): # {{{ db.field_metadata.add_user_category('@' + k, k) db.data.change_search_locations(db.field_metadata.get_search_terms()) self.tags_view.set_new_model() - self.tags_view.recount() + + def do_delete_user_category(self, category_name): + ''' + Delete the user category named category_name. Any leading '@' is removed + ''' + if category_name.startswith('@'): + category_name = category_name[1:] + db = self.library_view.model().db + user_cats = db.prefs.get('user_categories', {}) + cat_keys = sorted(user_cats.keys(), key=sort_key) + has_children = False + found = False + for k in cat_keys: + if k == category_name: + found = True + has_children = len(user_cats[k]) + elif k.startswith(category_name + '.'): + has_children = True + if not found: + return error_dialog(self.tags_view, _('Delete user category'), + _('%s is not a user category')%category_name, show=True) + if has_children: + if not question_dialog(self.tags_view, _('Delete user category'), + _('%s contains items. Do you really ' + 'want to delete it?')%category_name): + return + for k in cat_keys: + if k == category_name: + del user_cats[k] + elif k.startswith(category_name + '.'): + del user_cats[k] + db.prefs.set('user_categories', user_cats) + self.tags_view.set_new_model() + + def do_del_item_from_user_cat(self, user_cat, item_name, item_category): + ''' + Delete the item (item_name, item_category) from the user category with + key user_cat. Any leading '@' characters are removed + ''' + if user_cat.startswith('@'): + user_cat = user_cat[1:] + db = self.library_view.model().db + user_cats = db.prefs.get('user_categories', {}) + if user_cat not in user_cats: + error_dialog(self.tags_view, _('Remove category'), + _('User category %s does not exist')%user_cat, + show=True) + return + self.tags_view.model().delete_item_from_user_category(user_cat, + item_name, item_category) + self.tags_view.recount() + + def do_add_item_to_user_cat(self, dest_category, src_name, src_category): + ''' + Add the item src_name in src_category to the user category + dest_category. Any leading '@' is removed + ''' + db = self.library_view.model().db + user_cats = db.prefs.get('user_categories', {}) + + if dest_category.startswith('@'): + dest_category = dest_category[1:] + if dest_category not in user_cats: + return error_dialog(self.tags_view, _('Add to user category'), + _('A user category %s does not exist')%dest_category, show=True) + + # Now add the item to the destination user category + add_it = True + if src_category == 'news': + src_category = 'tags' + for tup in user_cats[dest_category]: + if src_name == tup[0] and src_category == tup[1]: + add_it = False + if add_it: + user_cats[dest_category].append([src_name, src_category, 0]) + db.prefs.set('user_categories', user_cats) + self.tags_view.recount() def do_tags_list_edit(self, tag, category): + ''' + Open the 'manage_X' dialog where X == category. If tag is not None, the + dialog will position the editor on that item. + ''' db=self.library_view.model().db if category == 'tags': result = db.get_tags_with_ids() @@ -1267,6 +1750,8 @@ class TagBrowserMixin(object): # {{{ if d.result() == d.Accepted: to_rename = d.to_rename # dict of new text to old id to_delete = d.to_delete # list of ids + orig_name = d.original_names # dict of id: name + rename_func = None if category == 'tags': rename_func = db.rename_tag @@ -1280,15 +1765,19 @@ class TagBrowserMixin(object): # {{{ else: rename_func = partial(db.rename_custom_item, label=cc_label) delete_func = partial(db.delete_custom_item_using_id, label=cc_label) + m = self.tags_view.model() if rename_func: for item in to_delete: delete_func(item) + m.delete_item_from_all_user_categories(orig_name[item], category) for old_id in to_rename: rename_func(old_id, new_name=unicode(to_rename[old_id])) + m.rename_item_in_all_user_categories(orig_name[old_id], + category, unicode(to_rename[old_id])) # Clean up the library view self.do_tag_item_renamed() - self.tags_view.set_new_model() # does a refresh for free + self.tags_view.recount() def do_tag_item_renamed(self): # Clean up library view and search @@ -1304,6 +1793,9 @@ class TagBrowserMixin(object): # {{{ # refreshing the tags view happens at the emit()/call() site def do_author_sort_edit(self, parent, id): + ''' + Open the manage authors dialog + ''' db = self.library_view.model().db editor = EditAuthorsDialog(parent, db, id) d = editor.exec_() diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 61d8676cfd..85f58be218 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -635,7 +635,8 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ mb.stop() self.hide_windows() - self.emailer.stop() + if self.emailer.is_alive(): + self.emailer.stop() try: try: if self.content_server is not None: diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 45b96bb69f..e626d446d2 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -124,9 +124,16 @@ def _match(query, value, matchkind): for t in value: t = icu_lower(t) try: ### ignore regexp exceptions, required because search-ahead tries before typing is finished - if ((matchkind == EQUALS_MATCH and query == t) or - (matchkind == REGEXP_MATCH and re.search(query, t, re.I)) or ### search unanchored - (matchkind == CONTAINS_MATCH and query in t)): + if (matchkind == EQUALS_MATCH): + if query[0] == '.': + if t.startswith(query[1:]): + ql = len(query) - 1 + if (len(t) == ql) or (t[ql:ql+1] == '.'): + return True + elif query == t: + return True + elif ((matchkind == REGEXP_MATCH and re.search(query, t, re.I)) or ### search unanchored + (matchkind == CONTAINS_MATCH and query in t)): return True except re.error: pass @@ -415,13 +422,25 @@ class ResultCache(SearchQueryParser): # {{{ if self.db_prefs is None: return res user_cats = self.db_prefs.get('user_categories', []) - if location not in user_cats: - return res c = set(candidates) - for (item, category, ign) in user_cats[location]: - s = self.get_matches(category, '=' + item, candidates=c) - c -= s - res |= s + l = location.rfind('.') + if l > 0: + alt_loc = location[0:l] + alt_item = location[l+1:] + else: + alt_loc = None + for key in user_cats: + if key == location or key.startswith(location + '.'): + for (item, category, ign) in user_cats[key]: + s = self.get_matches(category, '=' + item, candidates=c) + c -= s + res |= s + elif key == alt_loc: + for (item, category, ign) in user_cats[key]: + if item == alt_item: + s = self.get_matches(category, '=' + item, candidates=c) + c -= s + res |= s if query == 'false': return candidates - res return res diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index bd89e12044..c53d938297 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -174,6 +174,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.prefs = DBPrefs(self) defs = self.prefs.defaults defs['gui_restriction'] = defs['cs_restriction'] = '' + defs['categories_using_hierarchy'] = [] # Migrate saved search and user categories to db preference scheme def migrate_preference(key, default): @@ -812,6 +813,21 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): index_is_id=index_is_id), extra=self.get_custom_extra(idx, label=meta['label'], index_is_id=index_is_id)) + + user_cats = self.prefs['user_categories'] + user_cat_vals = {} + for ucat in user_cats: + res = [] + for name,cat,ign in user_cats[ucat]: + v = mi.get(cat, None) + if isinstance(v, list): + if name in v: + res.append([name,cat]) + elif name == v: + res.append([name,cat]) + user_cat_vals[ucat] = res + mi.user_categories = user_cat_vals + if get_cover: mi.cover = self.cover(id, index_is_id=True, as_path=True) return mi @@ -1406,7 +1422,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): # temporarily duplicating the categories lists. taglist = {} for c in categories.keys(): - taglist[c] = dict(map(lambda t:(t.name, t), categories[c])) + taglist[c] = dict(map(lambda t:(icu_lower(t.name), t), categories[c])) muc = self.prefs.get('grouped_search_make_user_categories', []) gst = self.prefs.get('grouped_search_terms', {}) @@ -1422,8 +1438,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): for user_cat in sorted(user_categories.keys(), key=sort_key): items = [] for (name,label,ign) in user_categories[user_cat]: - if label in taglist and name in taglist[label]: - items.append(taglist[label][name]) + n = icu_lower(name) + if label in taglist and n in taglist[label]: + items.append(taglist[label][n]) # else: do nothing, to not include nodes w zero counts cat_name = '@' + user_cat # add the '@' to avoid name collision # Not a problem if we accumulate entries in the icon map @@ -2434,7 +2451,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): stream.seek(0) mi = get_metadata(stream, format, use_libprs_metadata=False) stream.seek(0) - mi.series_index = 1.0 + if not mi.series_index: + mi.series_index = 1.0 mi.tags = [_('News')] if arg['add_title_tag']: mi.tags += [arg['title']] diff --git a/src/calibre/library/field_metadata.py b/src/calibre/library/field_metadata.py index 9b481a89d0..aff2803452 100644 --- a/src/calibre/library/field_metadata.py +++ b/src/calibre/library/field_metadata.py @@ -32,7 +32,7 @@ category_icon_map = { 'news' : 'news.png', 'tags' : 'tags.png', 'custom:' : 'column.png', - 'user:' : 'drawer.png', + 'user:' : 'tb_folder.png', 'search' : 'search.png' } diff --git a/src/calibre/manual/gui.rst b/src/calibre/manual/gui.rst index 3718f830f3..25b1778fd3 100644 --- a/src/calibre/manual/gui.rst +++ b/src/calibre/manual/gui.rst @@ -413,6 +413,27 @@ The Book Details display shows you extra information and the cover for the curre .. _jobs: +.. _tag_browser: + +Tag Browser +------------- +.. image:: images/tag_browser.png + +The Tag Browser allows you to easily browse your collection by Author/Tags/Series/etc. If you click on any Item in the Tag Browser, for example, the Author name, Isaac Asimov, then the list of books to the right is restricted to books by that author. Clicking once again on Isaac Asimov will restrict the list of books to books not by Isaac Asimov. A third click will remove the restriction. If you hold down the Ctrl or Shift keys and click on multiple items, then restrictions based on multiple items are created. For example you could Hold Ctrl and click on the tags History and Europe for find books on European history. The Tag Browser works by constructing search expressions that are automatically entered into the Search bar. It is a good way to learn how to construct basic search expressions. + +There is a search bar at the top of the Tag Browser that allows you to easily find any item in the Tag Browser. In addition, you can right click on any item and choose to hide it or rename it or open a "Manage x" dialog that allows you to manage items of that kind. For example the "Manage Authors" dialog allows you to rename authors and control how their names are sorted. + +For convenience, you can drag and drop books from the book list to items in the Tag Browser and that item will be automatically applied to the dropped books. For example, dragging a book to Isaac Asimov will set the author of that book to Isaac Asimov or dragging it to the tag History will add the tag History to its tags. + +The outer-level items in the tag browser such as Authors and Series are called categories. You can create your own categories, called User Categories, which are useful for organizing items. For example, you can use the user categories editor (push the Manage User Categories button) to create a user category called Favorite Authors, then put the items for your favorites into the category. User categories act like built-in categories; you can click on items to search for them. You can search for all items in a category by right-clicking on the category name and choosing "Search for books in ...". + +User categories can have sub-categories. For example, the user category Favorites.Authors is a sub-category of Favorites. You might also have Favorites.Series, in which case there will be two sub-categories under Favorites. Sub-categories can be created using Manage User Categories by entering names like the Favorites example. They can also be created by right-clicking on a user category, choosing "Add sub-category to ...", and entering the category name. + +It is also possible to create hierarchies inside some of the built-in categories (the text categories). These hierarchies show with the small triangle permitting the sub-items to be hidden. To use hierarchies in a category, you must first go to Preferences / Look & Feel and enter the category name(s) into the "Categories with hierarchical items" box. Once this is done, items in that category that contain periods will be shown using the small triangle. For example, assume you create a custom column called "Genre" and indicate that it contains hierarchical items. Once done, items such as Mystery.Thriller and Mystery.English will display as Mystery with the small triangle next to it. Clicking on the triangle will show Thriller and English as sub-items. + +You can drag and drop items in the Tag browser onto user categories to add them to that category. + + Jobs ----- .. image:: images/jobs.png diff --git a/src/calibre/manual/images/tag_browser.png b/src/calibre/manual/images/tag_browser.png new file mode 100644 index 0000000000..818ca6964d Binary files /dev/null and b/src/calibre/manual/images/tag_browser.png differ diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 518f2ed140..03491c038a 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -396,6 +396,34 @@ class BuiltinListitem(BuiltinFormatterFunction): except: return '' +class BuiltinSublist(BuiltinFormatterFunction): + name = 'sublist' + arg_count = 4 + doc = _('sublist(val, start_index, end_index, separator) -- interpret the ' + ' value as a list of items separated by `separator`, returning a ' + ' new list made from the `start_index`th to the `end_index`th item. ' + 'The first item is number zero. If an index is negative, then it ' + 'counts from the end of the list. As a special case, an end_index ' + 'of zero is assumed to be the length of the list. Examples using ' + 'basic template mode and assuming a #genre value if A.B.C: ' + '{#genre:sublist(-1,0,.)} returns C
' + '{#genre:sublist(0,1,.)} returns A
' + '{#genre:sublist(0,-1,.)} returns A.B') + + def evaluate(self, formatter, kwargs, mi, locals, val, start_index, end_index, sep): + if not val: + return '' + si = int(start_index) + ei = int(end_index) + val = val.split(sep) + try: + if ei == 0: + return sep.join(val[si:]) + else: + return sep.join(val[si:ei]) + except: + return '' + class BuiltinUppercase(BuiltinFormatterFunction): name = 'uppercase' arg_count = 1 @@ -447,6 +475,7 @@ builtin_re = BuiltinRe() builtin_shorten = BuiltinShorten() builtin_strcat = BuiltinStrcat() builtin_strcmp = BuiltinStrcmp() +builtin_sublist = BuiltinSublist() builtin_substr = BuiltinSubstr() builtin_subtract = BuiltinSubtract() builtin_switch = BuiltinSwitch() diff --git a/src/calibre/web/feeds/templates.py b/src/calibre/web/feeds/templates.py index eefd897614..225a78be5c 100644 --- a/src/calibre/web/feeds/templates.py +++ b/src/calibre/web/feeds/templates.py @@ -136,7 +136,7 @@ class FeedTemplate(Template): head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) - body = BODY(style='page-break-before:always') + body = BODY() body.append(self.get_navbar(f, feeds)) div = DIV( @@ -322,7 +322,7 @@ class TouchscreenFeedTemplate(Template): head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) - body = BODY(style='page-break-before:always') + body = BODY() div = DIV( top_navbar, H2(feed.title, CLASS('feed_title')) diff --git a/src/odf/attrconverters.py b/src/odf/attrconverters.py index 0117324bba..b75f80a2dd 100644 --- a/src/odf/attrconverters.py +++ b/src/odf/attrconverters.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2006-2008 Søren Roug, European Environment Agency +# Copyright (C) 2006-2010 Søren Roug, European Environment Agency # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -40,6 +40,9 @@ def cnv_boolean(attribute, arg, element): # Potentially accept color values def cnv_color(attribute, arg, element): + """ A RGB color in conformance with §5.9.11 of [XSL], that is a RGB color in notation “#rrggbb”, where + rr, gg and bb are 8-bit hexadecimal digits. + """ return str(arg) def cnv_configtype(attribute, arg, element): @@ -55,9 +58,15 @@ def cnv_data_source_has_labels(attribute, arg, element): # Understand different date formats def cnv_date(attribute, arg, element): + """ A dateOrDateTime value is either an [xmlschema-2] date value or an [xmlschema-2] dateTime + value. + """ return str(arg) def cnv_dateTime(attribute, arg, element): + """ A dateOrDateTime value is either an [xmlschema-2] date value or an [xmlschema-2] dateTime + value. + """ return str(arg) def cnv_double(attribute, arg, element): @@ -67,11 +76,31 @@ def cnv_duration(attribute, arg, element): return str(arg) def cnv_family(attribute, arg, element): + """ A style family """ if str(arg) not in ("text", "paragraph", "section", "ruby", "table", "table-column", "table-row", "table-cell", "graphic", "presentation", "drawing-page", "chart"): raise ValueError, "'%s' not allowed" % str(arg) return str(arg) +def __save_prefix(attribute, arg, element): + prefix = arg.split(':',1)[0] + if prefix == arg: + return unicode(arg) + namespace = element.get_knownns(prefix) + if namespace is None: + #raise ValueError, "'%s' is an unknown prefix" % str(prefix) + return unicode(arg) + p = element.get_nsprefix(namespace) + return unicode(arg) + +def cnv_formula(attribute, arg, element): + """ A string containing a formula. Formulas do not have a predefined syntax, but the string should + begin with a namespace prefix, followed by a “:” (COLON, U+003A) separator, followed by the text + of the formula. The namespace bound to the prefix determines the syntax and semantics of the + formula. + """ + return __save_prefix(attribute, arg, element) + def cnv_ID(attribute, arg, element): return str(arg) @@ -89,6 +118,9 @@ def cnv_legend_position(attribute, arg, element): pattern_length = re.compile(r'-?([0-9]+(\.[0-9]*)?|\.[0-9]+)((cm)|(mm)|(in)|(pt)|(pc)|(px))') def cnv_length(attribute, arg, element): + """ A (positive or negative) physical length, consisting of magnitude and unit, in conformance with the + Units of Measure defined in §5.9.13 of [XSL]. + """ global pattern_length if not pattern_length.match(arg): raise ValueError, "'%s' is not a valid length" % arg @@ -120,12 +152,12 @@ def cnv_namespacedToken(attribute, arg, element): if not pattern_namespacedToken.match(arg): raise ValueError, "'%s' is not a valid namespaced token" % arg - return arg + return __save_prefix(attribute, arg, element) -# Must accept string as argument -# NCName is defined in http://www.w3.org/TR/REC-xml-names/#NT-NCName -# Essentially an XML name minus ':' def cnv_NCName(attribute, arg, element): + """ NCName is defined in http://www.w3.org/TR/REC-xml-names/#NT-NCName + Essentially an XML name minus ':' + """ if type(arg) in types.StringTypes: return make_NCName(arg) else: @@ -226,6 +258,7 @@ attrconverters = { ((ANIMNS,u'name'), None): cnv_string, ((ANIMNS,u'sub-item'), None): cnv_string, ((ANIMNS,u'value'), None): cnv_string, +# ((DBNS,u'type'), None): cnv_namespacedToken, ((CHARTNS,u'attached-axis'), None): cnv_string, ((CHARTNS,u'class'), (CHARTNS,u'grid')): cnv_major_minor, ((CHARTNS,u'class'), None): cnv_namespacedToken, @@ -288,7 +321,7 @@ attrconverters = { ((CHARTNS,u'values-cell-range-address'), None): cnv_string, ((CHARTNS,u'vertical'), None): cnv_boolean, ((CHARTNS,u'visible'), None): cnv_boolean, - ((CONFIGNS,u'name'), None): cnv_string, + ((CONFIGNS,u'name'), None): cnv_formula, ((CONFIGNS,u'type'), None): cnv_configtype, ((DR3DNS,u'ambient-color'), None): cnv_string, ((DR3DNS,u'back-scale'), None): cnv_string, @@ -369,11 +402,11 @@ attrconverters = { ((DRAWNS,u'decimal-places'), None): cnv_string, ((DRAWNS,u'display'), None): cnv_string, ((DRAWNS,u'display-name'), None): cnv_string, - ((DRAWNS,u'distance'), None): cnv_string, + ((DRAWNS,u'distance'), None): cnv_lengthorpercent, ((DRAWNS,u'dots1'), None): cnv_integer, - ((DRAWNS,u'dots1-length'), None): cnv_length, + ((DRAWNS,u'dots1-length'), None): cnv_lengthorpercent, ((DRAWNS,u'dots2'), None): cnv_integer, - ((DRAWNS,u'dots2-length'), None): cnv_length, + ((DRAWNS,u'dots2-length'), None): cnv_lengthorpercent, ((DRAWNS,u'end-angle'), None): cnv_double, ((DRAWNS,u'end'), None): cnv_string, ((DRAWNS,u'end-color'), None): cnv_string, @@ -383,7 +416,7 @@ attrconverters = { ((DRAWNS,u'end-line-spacing-horizontal'), None): cnv_string, ((DRAWNS,u'end-line-spacing-vertical'), None): cnv_string, ((DRAWNS,u'end-shape'), None): cnv_IDREF, - ((DRAWNS,u'engine'), None): cnv_string, + ((DRAWNS,u'engine'), None): cnv_namespacedToken, ((DRAWNS,u'enhanced-path'), None): cnv_string, ((DRAWNS,u'escape-direction'), None): cnv_string, ((DRAWNS,u'extrusion-allowed'), None): cnv_boolean, @@ -604,7 +637,7 @@ attrconverters = { ((FORMNS,u'button-type'), None): cnv_string, ((FORMNS,u'command'), None): cnv_string, ((FORMNS,u'command-type'), None): cnv_string, - ((FORMNS,u'control-implementation'), None): cnv_string, + ((FORMNS,u'control-implementation'), None): cnv_namespacedToken, ((FORMNS,u'convert-empty-to-null'), None): cnv_boolean, ((FORMNS,u'current-selected'), None): cnv_boolean, ((FORMNS,u'current-state'), None): cnv_string, @@ -800,8 +833,8 @@ attrconverters = { ((PRESENTATIONNS,u'user-transformed'), None): cnv_boolean, ((PRESENTATIONNS,u'verb'), None): cnv_nonNegativeInteger, ((PRESENTATIONNS,u'visibility'), None): cnv_string, - ((SCRIPTNS,u'event-name'), None): cnv_string, - ((SCRIPTNS,u'language'), None): cnv_string, + ((SCRIPTNS,u'event-name'), None): cnv_formula, + ((SCRIPTNS,u'language'), None): cnv_formula, ((SCRIPTNS,u'macro-name'), None): cnv_string, ((SMILNS,u'accelerate'), None): cnv_double, ((SMILNS,u'accumulate'), None): cnv_string, @@ -1087,7 +1120,7 @@ attrconverters = { ((SVGNS,u'y2'), None): cnv_lengthorpercent, ((TABLENS,u'acceptance-state'), None): cnv_string, ((TABLENS,u'add-empty-lines'), None): cnv_boolean, - ((TABLENS,u'algorithm'), None): cnv_string, + ((TABLENS,u'algorithm'), None): cnv_formula, ((TABLENS,u'align'), None): cnv_string, ((TABLENS,u'allow-empty-cell'), None): cnv_boolean, ((TABLENS,u'application-data'), None): cnv_string, @@ -1106,7 +1139,7 @@ attrconverters = { ((TABLENS,u'cell-range'), None): cnv_string, ((TABLENS,u'column'), None): cnv_integer, ((TABLENS,u'comment'), None): cnv_string, - ((TABLENS,u'condition'), None): cnv_string, + ((TABLENS,u'condition'), None): cnv_formula, ((TABLENS,u'condition-source'), None): cnv_string, ((TABLENS,u'condition-source-range-address'), None): cnv_string, ((TABLENS,u'contains-error'), None): cnv_boolean, @@ -1144,13 +1177,13 @@ attrconverters = { ((TABLENS,u'end-x'), None): cnv_length, ((TABLENS,u'end-y'), None): cnv_length, ((TABLENS,u'execute'), None): cnv_boolean, - ((TABLENS,u'expression'), None): cnv_string, + ((TABLENS,u'expression'), None): cnv_formula, ((TABLENS,u'field-name'), None): cnv_string, ((TABLENS,u'field-number'), None): cnv_nonNegativeInteger, ((TABLENS,u'field-number'), None): cnv_string, ((TABLENS,u'filter-name'), None): cnv_string, ((TABLENS,u'filter-options'), None): cnv_string, - ((TABLENS,u'formula'), None): cnv_string, + ((TABLENS,u'formula'), None): cnv_formula, ((TABLENS,u'function'), None): cnv_string, ((TABLENS,u'function'), None): cnv_string, ((TABLENS,u'grand-total'), None): cnv_string, @@ -1290,7 +1323,7 @@ attrconverters = { ((TEXTNS,u'combine-entries-with-pp'), None): cnv_boolean, ((TEXTNS,u'comma-separated'), None): cnv_boolean, ((TEXTNS,u'cond-style-name'), None): cnv_StyleNameRef, - ((TEXTNS,u'condition'), None): cnv_string, + ((TEXTNS,u'condition'), None): cnv_formula, ((TEXTNS,u'connection-name'), None): cnv_string, ((TEXTNS,u'consecutive-numbering'), None): cnv_boolean, ((TEXTNS,u'continue-numbering'), None): cnv_boolean, @@ -1321,7 +1354,7 @@ attrconverters = { ((TEXTNS,u'first-row-start-column'), None): cnv_string, ((TEXTNS,u'fixed'), None): cnv_boolean, ((TEXTNS,u'footnotes-position'), None): cnv_string, - ((TEXTNS,u'formula'), None): cnv_string, + ((TEXTNS,u'formula'), None): cnv_formula, ((TEXTNS,u'global'), None): cnv_boolean, ((TEXTNS,u'howpublished'), None): cnv_string, ((TEXTNS,u'id'), None): cnv_ID, @@ -1437,7 +1470,10 @@ attrconverters = { class AttrConverters: def convert(self, attribute, value, element): - conversion = attrconverters.get((attribute,element), None) + """ Based on the element, figures out how to check/convert the attribute value + All values are converted to string + """ + conversion = attrconverters.get((attribute, element.qname), None) if conversion is not None: return conversion(attribute, value, element) else: diff --git a/src/odf/element.py b/src/odf/element.py index f0938ba53e..aad698045e 100644 --- a/src/odf/element.py +++ b/src/odf/element.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# Copyright (C) 2007-2008 Søren Roug, European Environment Agency +# Copyright (C) 2007-2010 Søren Roug, European Environment Agency # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -112,6 +112,9 @@ class Node(xml.dom.Node): return self.childNodes[-1] def insertBefore(self, newChild, refChild): + """ Inserts the node newChild before the existing child node refChild. + If refChild is null, insert newChild at the end of the list of children. + """ if newChild.nodeType not in self._child_node_types: raise IllegalChild, "%s cannot be child of %s" % (newChild.tagName, self.tagName) if newChild.parentNode is not None: @@ -135,21 +138,26 @@ class Node(xml.dom.Node): newChild.parentNode = self return newChild - def appendChild(self, node): - if node.nodeType == self.DOCUMENT_FRAGMENT_NODE: - for c in tuple(node.childNodes): + def appendChild(self, newChild): + """ Adds the node newChild to the end of the list of children of this node. + If the newChild is already in the tree, it is first removed. + """ + if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: + for c in tuple(newChild.childNodes): self.appendChild(c) ### The DOM does not clearly specify what to return in this case - return node - if node.nodeType not in self._child_node_types: - raise IllegalChild, "<%s> is not allowed in %s" % ( node.tagName, self.tagName) - if node.parentNode is not None: - node.parentNode.removeChild(node) - _append_child(self, node) - node.nextSibling = None - return node + return newChild + if newChild.nodeType not in self._child_node_types: + raise IllegalChild, "<%s> is not allowed in %s" % ( newChild.tagName, self.tagName) + if newChild.parentNode is not None: + newChild.parentNode.removeChild(newChild) + _append_child(self, newChild) + newChild.nextSibling = None + return newChild def removeChild(self, oldChild): + """ Removes the child node indicated by oldChild from the list of children, and returns it. + """ #FIXME: update ownerDocument.element_dict or find other solution try: self.childNodes.remove(oldChild) @@ -191,8 +199,8 @@ def _append_child(self, node): node.__dict__["parentNode"] = self class Childless: - """Mixin that makes childless-ness easy to implement and avoids - the complexity of the Node methods that deal with children. + """ Mixin that makes childless-ness easy to implement and avoids + the complexity of the Node methods that deal with children. """ attributes = None @@ -207,6 +215,7 @@ class Childless: return None def appendChild(self, node): + """ Raises an error """ raise xml.dom.HierarchyRequestErr( self.tagName + " nodes cannot have children") @@ -214,14 +223,17 @@ class Childless: return False def insertBefore(self, newChild, refChild): + """ Raises an error """ raise xml.dom.HierarchyRequestErr( self.tagName + " nodes do not have children") def removeChild(self, oldChild): + """ Raises an error """ raise xml.dom.NotFoundErr( self.tagName + " nodes do not have children") def replaceChild(self, newChild, oldChild): + """ Raises an error """ raise xml.dom.HierarchyRequestErr( self.tagName + " nodes do not have children") @@ -247,8 +259,12 @@ class CDATASection(Childless, Text): nodeType = Node.CDATA_SECTION_NODE def toXml(self,level,f): + """ Generate XML output of the node. If the text contains "]]>", then + escape it by going out of CDATA mode (]]>), then write the string + and then go into CDATA mode again. (' % self.data) + f.write('' % self.data.replace(']]>',']]>]]>" % (r[1].lower().replace('-',''), self.tagName) + def get_knownns(self, prefix): + """ Odfpy maintains a list of known namespaces. In some cases a prefix is used, and + we need to know which namespace it resolves to. + """ + global nsdict + for ns,p in nsdict.items(): + if p == prefix: return ns + return None + def get_nsprefix(self, namespace): + """ Odfpy maintains a list of known namespaces. In some cases we have a namespace URL, + and needs to look up or assign the prefix for it. + """ if namespace is None: namespace = "" prefix = _nsassign(namespace) if not self.namespaces.has_key(namespace): @@ -339,6 +367,9 @@ class Element(Node): self.ownerDocument.rebuild_caches(element) def addText(self, text, check_grammar=True): + """ Adds text to an element + Setting check_grammar=False turns off grammar checking + """ if check_grammar and self.qname not in grammar.allows_text: raise IllegalText, "The <%s> element does not allow text" % self.tagName else: @@ -346,6 +377,9 @@ class Element(Node): self.appendChild(Text(text)) def addCDATA(self, cdata, check_grammar=True): + """ Adds CDATA to an element + Setting check_grammar=False turns off grammar checking + """ if check_grammar and self.qname not in grammar.allows_text: raise IllegalText, "The <%s> element does not allow text" % self.tagName else: @@ -403,17 +437,18 @@ class Element(Node): # if allowed_attrs and (namespace, localpart) not in allowed_attrs: # raise AttributeError, "Attribute %s:%s is not allowed in element <%s>" % ( prefix, localpart, self.tagName) c = AttrConverters() - self.attributes[prefix + ":" + localpart] = c.convert((namespace, localpart), value, self.qname) + self.attributes[(namespace, localpart)] = c.convert((namespace, localpart), value, self) def getAttrNS(self, namespace, localpart): prefix = self.get_nsprefix(namespace) - return self.attributes.get(prefix + ":" + localpart) + return self.attributes.get((namespace, localpart)) def removeAttrNS(self, namespace, localpart): - prefix = self.get_nsprefix(namespace) - del self.attributes[prefix + ":" + localpart] + del self.attributes[(namespace, localpart)] def getAttribute(self, attr): + """ Get an attribute value. The method knows which namespace the attribute is in + """ allowed_attrs = self.allowed_attributes() if allowed_attrs is None: if type(attr) == type(()): @@ -432,8 +467,9 @@ class Element(Node): if level == 0: for namespace, prefix in self.namespaces.items(): f.write(' xmlns:' + prefix + '="'+ _escape(str(namespace))+'"') - for attkey in self.attributes.keys(): - f.write(' '+_escape(str(attkey))+'='+_quoteattr(unicode(self.attributes[attkey]).encode('utf-8'))) + for qname in self.attributes.keys(): + prefix = self.get_nsprefix(qname[0]) + f.write(' '+_escape(str(prefix+':'+qname[1]))+'='+_quoteattr(unicode(self.attributes[qname]).encode('utf-8'))) f.write('>') def write_close_tag(self, level, f): @@ -445,8 +481,9 @@ class Element(Node): if level == 0: for namespace, prefix in self.namespaces.items(): f.write(' xmlns:' + prefix + '="'+ _escape(str(namespace))+'"') - for attkey in self.attributes.keys(): - f.write(' '+_escape(str(attkey))+'='+_quoteattr(unicode(self.attributes[attkey]).encode('utf-8'))) + for qname in self.attributes.keys(): + prefix = self.get_nsprefix(qname[0]) + f.write(' '+_escape(str(prefix+':'+qname[1]))+'='+_quoteattr(unicode(self.attributes[qname]).encode('utf-8'))) if self.childNodes: f.write('>') for element in self.childNodes: @@ -464,6 +501,7 @@ class Element(Node): return accumulator def getElementsByType(self, element): + """ Gets elements based on the type, which is function from text.py, draw.py etc. """ obj = element(check_grammar=False) return self._getElementsByObj(obj,[]) diff --git a/src/odf/grammar.py b/src/odf/grammar.py index 09ec02cbaa..d5d8d5970e 100644 --- a/src/odf/grammar.py +++ b/src/odf/grammar.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2006-2009 Søren Roug, European Environment Agency +# Copyright (C) 2006-2010 Søren Roug, European Environment Agency # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public diff --git a/src/odf/load.py b/src/odf/load.py index 1f0e45ea23..e48fcaa412 100644 --- a/src/odf/load.py +++ b/src/odf/load.py @@ -63,8 +63,8 @@ class LoadParser(handler.ContentHandler): self.level = self.level + 1 # Add any accumulated text content - content = ''.join(self.data).strip() - if len(content) > 0: + content = ''.join(self.data) + if len(content.strip()) > 0: self.parent.addText(content, check_grammar=False) self.data = [] # Create the element diff --git a/src/odf/namespaces.py b/src/odf/namespaces.py index 3109210bb5..96ea958e79 100644 --- a/src/odf/namespaces.py +++ b/src/odf/namespaces.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2006-2009 Søren Roug, European Environment Agency +# Copyright (C) 2006-2010 Søren Roug, European Environment Agency # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -17,7 +17,7 @@ # # Contributor(s): # -TOOLSVERSION = u"ODFPY/0.9.2dev" +TOOLSVERSION = u"ODFPY/0.9.4dev" ANIMNS = u"urn:oasis:names:tc:opendocument:xmlns:animation:1.0" DBNS = u"urn:oasis:names:tc:opendocument:xmlns:database:1.0" @@ -28,19 +28,23 @@ DCNS = u"http://purl.org/dc/elements/1.1/" DOMNS = u"http://www.w3.org/2001/xml-events" DR3DNS = u"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" DRAWNS = u"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" +FIELDNS = u"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" FONS = u"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" FORMNS = u"urn:oasis:names:tc:opendocument:xmlns:form:1.0" +GRDDLNS = u"http://www.w3.org/2003/g/data-view#" KOFFICENS = u"http://www.koffice.org/2005/" MANIFESTNS = u"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" MATHNS = u"http://www.w3.org/1998/Math/MathML" METANS = u"urn:oasis:names:tc:opendocument:xmlns:meta:1.0" NUMBERNS = u"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" OFFICENS = u"urn:oasis:names:tc:opendocument:xmlns:office:1.0" +OFNS = u"urn:oasis:names:tc:opendocument:xmlns:of:1.2" OOONS = u"http://openoffice.org/2004/office" OOOWNS = u"http://openoffice.org/2004/writer" OOOCNS = u"http://openoffice.org/2004/calc" PRESENTATIONNS = u"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" RDFANS = u"http://docs.oasis-open.org/opendocument/meta/rdfa#" +RPTNS = u"http://openoffice.org/2005/report" SCRIPTNS = u"urn:oasis:names:tc:opendocument:xmlns:script:1.0" SMILNS = u"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" STYLENS = u"urn:oasis:names:tc:opendocument:xmlns:style:1.0" @@ -50,7 +54,8 @@ TEXTNS = u"urn:oasis:names:tc:opendocument:xmlns:text:1.0" XFORMSNS = u"http://www.w3.org/2002/xforms" XLINKNS = u"http://www.w3.org/1999/xlink" XMLNS = u"http://www.w3.org/XML/1998/namespace" - +XSDNS = u"http://www.w3.org/2001/XMLSchema" +XSINS = u"http://www.w3.org/2001/XMLSchema-instance" nsdict = { ANIMNS: u'anim', @@ -61,19 +66,23 @@ nsdict = { DOMNS: u'dom', DR3DNS: u'dr3d', DRAWNS: u'draw', + FIELDNS: u'field', FONS: u'fo', FORMNS: u'form', + GRDDLNS: u'grddl', KOFFICENS: u'koffice', MANIFESTNS: u'manifest', MATHNS: u'math', METANS: u'meta', NUMBERNS: u'number', OFFICENS: u'office', + OFNS: u'of', OOONS: u'ooo', OOOWNS: u'ooow', OOOCNS: u'oooc', PRESENTATIONNS: u'presentation', RDFANS: u'rdfa', + RPTNS: u'rpt', SCRIPTNS: u'script', SMILNS: u'smil', STYLENS: u'style', @@ -83,4 +92,6 @@ nsdict = { XFORMSNS: u'xforms', XLINKNS: u'xlink', XMLNS: u'xml', + XSDNS: u'xsd', + XSINS: u'xsi', } diff --git a/src/odf/odf2xhtml.py b/src/odf/odf2xhtml.py index 6e3e753ebb..390d407d16 100644 --- a/src/odf/odf2xhtml.py +++ b/src/odf/odf2xhtml.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# Copyright (C) 2006-2007 Søren Roug, European Environment Agency +# Copyright (C) 2006-2010 Søren Roug, European Environment Agency # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -20,15 +20,18 @@ # #import pdb #pdb.set_trace() -import zipfile -from xml.sax import handler, expatreader -from xml.sax.xmlreader import InputSource +from xml.sax import handler from xml.sax.saxutils import escape, quoteattr -from cStringIO import StringIO +from xml.dom import Node -from namespaces import DCNS, DRAWNS, FONS, \ - METANS, NUMBERNS, OFFICENS, PRESENTATIONNS, \ - STYLENS, SVGNS, TABLENS, TEXTNS, XLINKNS +from opendocument import load + +from namespaces import ANIMNS, CHARTNS, CONFIGNS, DCNS, DR3DNS, DRAWNS, FONS, \ + FORMNS, MATHNS, METANS, NUMBERNS, OFFICENS, PRESENTATIONNS, SCRIPTNS, \ + SMILNS, STYLENS, SVGNS, TABLENS, TEXTNS, XLINKNS + +if False: # Added by Kovid + DR3DNS, MATHNS, CHARTNS, CONFIGNS, ANIMNS, FORMNS, SMILNS, SCRIPTNS # Handling of styles # @@ -72,8 +75,8 @@ class StyleToCSS: (FONS,u"border-left"): self.c_fo, (FONS,u"border-right"): self.c_fo, (FONS,u"border-top"): self.c_fo, - (FONS,u"break-after"): self.c_break, - (FONS,u"break-before"): self.c_break, + (FONS,u"break-after"): self.c_break, # Added by Kovid + (FONS,u"break-before"): self.c_break,# Added by Kovid (FONS,u"color"): self.c_fo, (FONS,u"font-family"): self.c_fo, (FONS,u"font-size"): self.c_fo, @@ -136,7 +139,7 @@ class StyleToCSS: selector = rule[1] sdict[selector] = val - def c_break(self, ruleset, sdict, rule, val): + def c_break(self, ruleset, sdict, rule, val): # Added by Kovid property = 'page-' + rule[1] values = {'auto': 'auto', 'column': 'always', 'page': 'always', 'even-page': 'left', 'odd-page': 'right', @@ -346,13 +349,16 @@ class ODF2XHTML(handler.ContentHandler): self.elements = { (DCNS, 'title'): (self.s_processcont, self.e_dc_title), (DCNS, 'language'): (self.s_processcont, self.e_dc_contentlanguage), - (DCNS, 'creator'): (self.s_processcont, self.e_dc_metatag), + (DCNS, 'creator'): (self.s_processcont, self.e_dc_creator), (DCNS, 'description'): (self.s_processcont, self.e_dc_metatag), (DCNS, 'date'): (self.s_processcont, self.e_dc_metatag), + (DRAWNS, 'custom-shape'): (self.s_custom_shape, self.e_custom_shape), (DRAWNS, 'frame'): (self.s_draw_frame, self.e_draw_frame), (DRAWNS, 'image'): (self.s_draw_image, None), (DRAWNS, 'fill-image'): (self.s_draw_fill_image, None), (DRAWNS, "layer-set"):(self.s_ignorexml, None), + (DRAWNS, 'object'): (self.s_draw_object, None), + (DRAWNS, 'object-ole'): (self.s_draw_object_ole, None), (DRAWNS, 'page'): (self.s_draw_page, self.e_draw_page), (DRAWNS, 'text-box'): (self.s_draw_textbox, self.e_draw_textbox), (METANS, 'creation-date'):(self.s_processcont, self.e_dc_metatag), @@ -364,7 +370,9 @@ class ODF2XHTML(handler.ContentHandler): (NUMBERNS, "date-style"):(self.s_ignorexml, None), (NUMBERNS, "number-style"):(self.s_ignorexml, None), (NUMBERNS, "text-style"):(self.s_ignorexml, None), + (OFFICENS, "annotation"):(self.s_ignorexml, None), (OFFICENS, "automatic-styles"):(self.s_office_automatic_styles, None), + (OFFICENS, "document"):(self.s_office_document_content, self.e_office_document_content), (OFFICENS, "document-content"):(self.s_office_document_content, self.e_office_document_content), (OFFICENS, "forms"):(self.s_ignorexml, None), (OFFICENS, "master-styles"):(self.s_office_master_styles, None), @@ -374,6 +382,7 @@ class ODF2XHTML(handler.ContentHandler): (OFFICENS, "styles"):(self.s_office_styles, None), (OFFICENS, "text"):(self.s_office_text, self.e_office_text), (OFFICENS, "scripts"):(self.s_ignorexml, None), + (OFFICENS, "settings"):(self.s_ignorexml, None), (PRESENTATIONNS, "notes"):(self.s_ignorexml, None), # (STYLENS, "default-page-layout"):(self.s_style_default_page_layout, self.e_style_page_layout), (STYLENS, "default-page-layout"):(self.s_ignorexml, None), @@ -389,8 +398,8 @@ class ODF2XHTML(handler.ContentHandler): # (STYLENS, "header-style"):(self.s_style_header_style, None), (STYLENS, "master-page"):(self.s_style_master_page, None), (STYLENS, "page-layout-properties"):(self.s_style_handle_properties, None), -# (STYLENS, "page-layout"):(self.s_style_page_layout, self.e_style_page_layout), - (STYLENS, "page-layout"):(self.s_ignorexml, None), + (STYLENS, "page-layout"):(self.s_style_page_layout, self.e_style_page_layout), +# (STYLENS, "page-layout"):(self.s_ignorexml, None), (STYLENS, "paragraph-properties"):(self.s_style_handle_properties, None), (STYLENS, "style"):(self.s_style_style, self.e_style_style), (STYLENS, "table-cell-properties"):(self.s_style_handle_properties, None), @@ -407,6 +416,10 @@ class ODF2XHTML(handler.ContentHandler): (TEXTNS, "alphabetical-index-source"):(self.s_text_x_source, self.e_text_x_source), (TEXTNS, "bibliography-configuration"):(self.s_ignorexml, None), (TEXTNS, "bibliography-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, 'bookmark'): (self.s_text_bookmark, None), + (TEXTNS, 'bookmark-start'): (self.s_text_bookmark, None), + (TEXTNS, 'bookmark-ref'): (self.s_text_bookmark_ref, self.e_text_a), + (TEXTNS, 'bookmark-ref-start'): (self.s_text_bookmark_ref, None), (TEXTNS, 'h'): (self.s_text_h, self.e_text_h), (TEXTNS, "illustration-index-source"):(self.s_text_x_source, self.e_text_x_source), (TEXTNS, 'line-break'):(self.s_text_line_break, None), @@ -430,10 +443,66 @@ class ODF2XHTML(handler.ContentHandler): (TEXTNS, "user-index-source"):(self.s_text_x_source, self.e_text_x_source), } if embedable: - self.elements[(OFFICENS, u"text")] = (None,None) - self.elements[(OFFICENS, u"spreadsheet")] = (None,None) - self.elements[(OFFICENS, u"presentation")] = (None,None) - self.elements[(OFFICENS, u"document-content")] = (None,None) + self.make_embedable() + self._resetobject() + + def set_plain(self): + """ Tell the parser to not generate CSS """ + self.generate_css = False + + def set_embedable(self): + """ Tells the converter to only output the parts inside the """ + self.elements[(OFFICENS, u"text")] = (None,None) + self.elements[(OFFICENS, u"spreadsheet")] = (None,None) + self.elements[(OFFICENS, u"presentation")] = (None,None) + self.elements[(OFFICENS, u"document-content")] = (None,None) + + + def add_style_file(self, stylefilename, media=None): + """ Add a link to an external style file. + Also turns of the embedding of styles in the HTML + """ + self.use_internal_css = False + self.stylefilename = stylefilename + if media: + self.metatags.append('\n' % (stylefilename,media)) + else: + self.metatags.append('\n' % (stylefilename)) + + def _resetfootnotes(self): + # Footnotes and endnotes + self.notedict = {} + self.currentnote = 0 + self.notebody = '' + + def _resetobject(self): + self.lines = [] + self._wfunc = self._wlines + self.xmlfile = '' + self.title = '' + self.language = '' + self.creator = '' + self.data = [] + self.tagstack = TagStack() + self.htmlstack = [] + self.pstack = [] + self.processelem = True + self.processcont = True + self.listtypes = {} + self.headinglevels = [0, 0,0,0,0,0, 0,0,0,0,0] # level 0 to 10 + self.use_internal_css = True + self.cs = StyleToCSS() + self.anchors = {} + + # Style declarations + self.stylestack = [] + self.styledict = {} + self.currentstyle = None + + self._resetfootnotes() + + # Tags from meta.xml + self.metatags = [] def writeout(self, s): @@ -447,6 +516,7 @@ class ODF2XHTML(handler.ContentHandler): def opentag(self, tag, attrs={}, block=False): """ Create an open HTML tag """ + self.htmlstack.append((tag,attrs,block)) a = [] for key,val in attrs.items(): a.append('''%s=%s''' % (key, quoteattr(val))) @@ -458,6 +528,8 @@ class ODF2XHTML(handler.ContentHandler): self.writeout("\n") def closetag(self, tag, block=True): + """ Close an open HTML tag """ + self.htmlstack.pop() self.writeout("" % tag) if block == True: self.writeout("\n") @@ -468,17 +540,13 @@ class ODF2XHTML(handler.ContentHandler): a.append('''%s=%s''' % (key, quoteattr(val))) self.writeout("<%s %s/>\n" % (tag, " ".join(a))) +#-------------------------------------------------- +# Interface to parser #-------------------------------------------------- def characters(self, data): if self.processelem and self.processcont: self.data.append(data) - def handle_starttag(self, tag, method, attrs): - method(tag,attrs) - - def handle_endtag(self, tag, attrs, method): - method(tag, attrs) - def startElementNS(self, tag, qname, attrs): self.pstack.append( (self.processelem, self.processcont) ) if self.processelem: @@ -499,6 +567,13 @@ class ODF2XHTML(handler.ContentHandler): self.unknown_endtag(tag, attrs) self.processelem, self.processcont = self.pstack.pop() +#-------------------------------------------------- + def handle_starttag(self, tag, method, attrs): + method(tag,attrs) + + def handle_endtag(self, tag, attrs, method): + method(tag, attrs) + def unknown_starttag(self, tag, attrs): pass @@ -512,18 +587,21 @@ class ODF2XHTML(handler.ContentHandler): self.processelem = False def s_ignorecont(self, tag, attrs): + """ Stop processing the text nodes """ self.processcont = False def s_processcont(self, tag, attrs): + """ Start processing the text nodes """ self.processcont = True def classname(self, attrs): """ Generate a class name from a style name """ - c = attrs[(TEXTNS,'style-name')] + c = attrs.get((TEXTNS,'style-name'),'') c = c.replace(".","_") return c def get_anchor(self, name): + """ Create a unique anchor id for a href name """ if not self.anchors.has_key(name): # Changed by Kovid self.anchors[name] = "anchor%d" % (len(self.anchors) + 1) @@ -543,8 +621,8 @@ class ODF2XHTML(handler.ContentHandler): def e_dc_title(self, tag, attrs): """ Get the title from the meta data and create a HTML """ - self.metatags.append('<title>%s\n' % escape(''.join(self.data))) self.title = ''.join(self.data) + #self.metatags.append('%s\n' % escape(self.title)) self.data = [] def e_dc_metatag(self, tag, attrs): @@ -556,13 +634,57 @@ class ODF2XHTML(handler.ContentHandler): def e_dc_contentlanguage(self, tag, attrs): """ Set the content language. Identifies the targeted audience """ - self.metatags.append('\n' % ''.join(self.data)) + self.language = ''.join(self.data) + self.metatags.append('\n' % escape(self.language)) self.data = [] + def e_dc_creator(self, tag, attrs): + """ Set the content creator. Identifies the targeted audience + """ + self.creator = ''.join(self.data) + self.metatags.append('\n' % escape(self.creator)) + self.data = [] + + def s_custom_shape(self, tag, attrs): + """ A is made into a
in HTML which is then styled + """ + anchor_type = attrs.get((TEXTNS,'anchor-type'),'notfound') + htmltag = 'div' + name = "G-" + attrs.get( (DRAWNS,'style-name'), "") + if name == 'G-': + name = "PR-" + attrs.get( (PRESENTATIONNS,'style-name'), "") + name = name.replace(".","_") + if anchor_type == "paragraph": + style = 'position:absolute;' + elif anchor_type == 'char': + style = "position:absolute;" + elif anchor_type == 'as-char': + htmltag = 'div' + style = '' + else: + style = "position: absolute;" + if attrs.has_key( (SVGNS,"width") ): + style = style + "width:" + attrs[(SVGNS,"width")] + ";" + if attrs.has_key( (SVGNS,"height") ): + style = style + "height:" + attrs[(SVGNS,"height")] + ";" + if attrs.has_key( (SVGNS,"x") ): + style = style + "left:" + attrs[(SVGNS,"x")] + ";" + if attrs.has_key( (SVGNS,"y") ): + style = style + "top:" + attrs[(SVGNS,"y")] + ";" + if self.generate_css: + self.opentag(htmltag, {'class': name, 'style': style}) + else: + self.opentag(htmltag) + + def e_custom_shape(self, tag, attrs): + """ End the + """ + self.closetag('div') + def s_draw_frame(self, tag, attrs): """ A is made into a
in HTML which is then styled """ - anchor_type = attrs.get((TEXTNS,'anchor-type'),'char') + anchor_type = attrs.get((TEXTNS,'anchor-type'),'notfound') htmltag = 'div' name = "G-" + attrs.get( (DRAWNS,'style-name'), "") if name == 'G-': @@ -576,7 +698,7 @@ class ODF2XHTML(handler.ContentHandler): htmltag = 'div' style = '' else: - style = "position: absolute;" + style = "position:absolute;" if attrs.has_key( (SVGNS,"width") ): style = style + "width:" + attrs[(SVGNS,"width")] + ";" if attrs.has_key( (SVGNS,"height") ): @@ -620,6 +742,30 @@ class ODF2XHTML(handler.ContentHandler): htmlattrs['style'] = "display: block;" self.emptytag('img', htmlattrs) + def s_draw_object(self, tag, attrs): + """ A is embedded object in the document (e.g. spreadsheet in presentation). + """ + return # Added by Kovid + objhref = attrs[(XLINKNS,"href")] + # Remove leading "./": from "./Object 1" to "Object 1" +# objhref = objhref [2:] + + # Not using os.path.join since it fails to find the file on Windows. +# objcontentpath = '/'.join([objhref, 'content.xml']) + + for c in self.document.childnodes: + if c.folder == objhref: + self._walknode(c.topnode) + + def s_draw_object_ole(self, tag, attrs): + """ A is embedded OLE object in the document (e.g. MS Graph). + """ + class_id = attrs[(DRAWNS,"class-id")] + if class_id and class_id.lower() == "00020803-0000-0000-c000-000000000046": ## Microsoft Graph 97 Chart + tagattrs = { 'name':'object_ole_graph', 'class':'ole-graph' } + self.opentag('a', tagattrs) + self.closetag('a', tagattrs) + def s_draw_page(self, tag, attrs): """ A is a slide in a presentation. We use a
element in HTML. Therefore if you convert a ODP file, you get a series of
s. @@ -655,13 +801,9 @@ class ODF2XHTML(handler.ContentHandler): def html_body(self, tag, attrs): self.writedata() - if self.generate_css: + if self.generate_css and self.use_internal_css: self.opentag('style', {'type':"text/css"}, True) self.writeout('/**/\n') self.closetag('style') @@ -669,6 +811,16 @@ class ODF2XHTML(handler.ContentHandler): self.closetag('head') self.opentag('body', block=True) + # background-color: white removed by Kovid for #9118 + # Specifying an explicit bg color prevents ebook readers + # from successfully inverting colors + default_styles = """ +img { width: 100%; height: 100%; } +* { padding: 0; margin: 0; } +body { margin: 0 1em; } +ol, ul { padding-left: 2em; } +""" + def generate_stylesheet(self): for name in self.stylestack: styles = self.styledict.get(name) @@ -688,6 +840,7 @@ class ODF2XHTML(handler.ContentHandler): styles = parentstyle self.styledict[name] = styles # Write the styles to HTML + self.writeout(self.default_styles) for name in self.stylestack: styles = self.styledict.get(name) css2 = self.cs.convert_styles(styles) @@ -729,6 +882,7 @@ class ODF2XHTML(handler.ContentHandler): self.emptytag('meta', { 'http-equiv':"Content-Type", 'content':"text/html;charset=UTF-8"}) for metaline in self.metatags: self.writeout(metaline) + self.writeout('%s\n' % escape(self.title)) def e_office_document_content(self, tag, attrs): """ Last tag """ @@ -773,7 +927,7 @@ class ODF2XHTML(handler.ContentHandler): """ Copy all attributes to a struct. We will later convert them to CSS2 """ - if self.currentstyle is None: + if self.currentstyle is None: # Added by Kovid return for key,attr in attrs.items(): self.styledict[self.currentstyle][key] = attr @@ -799,7 +953,7 @@ class ODF2XHTML(handler.ContentHandler): def s_style_font_face(self, tag, attrs): """ It is possible that the HTML browser doesn't know how to show a particular font. Luckily ODF provides generic fallbacks - Unluckily they are not the same as CSS2. + Unfortunately they are not the same as CSS2. CSS2: serif, sans-serif, cursive, fantasy, monospace ODF: roman, swiss, modern, decorative, script, system """ @@ -850,7 +1004,7 @@ class ODF2XHTML(handler.ContentHandler): """ name = attrs[(STYLENS,'name')] name = name.replace(".","_") - self.currentstyle = "@page " + name + self.currentstyle = ".PL-" + name self.stylestack.append(self.currentstyle) self.styledict[self.currentstyle] = {} @@ -881,7 +1035,7 @@ class ODF2XHTML(handler.ContentHandler): self.s_ignorexml(tag, attrs) # Short prefixes for class selectors - familyshort = {'drawing-page':'DP', 'paragraph':'P', 'presentation':'PR', + _familyshort = {'drawing-page':'DP', 'paragraph':'P', 'presentation':'PR', 'text':'S', 'section':'D', 'table':'T', 'table-cell':'TD', 'table-column':'TC', 'table-row':'TR', 'graphic':'G' } @@ -897,7 +1051,7 @@ class ODF2XHTML(handler.ContentHandler): name = name.replace(".","_") family = attrs[(STYLENS,'family')] htmlfamily = self.familymap.get(family,'unknown') - sfamily = self.familyshort.get(family,'X') + sfamily = self._familyshort.get(family,'X') name = "%s%s-%s" % (self.autoprefix, sfamily, name) parent = attrs.get( (STYLENS,'parent-style-name') ) self.currentstyle = special_styles.get(name,"."+name) @@ -942,6 +1096,7 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def s_table_table_cell(self, tag, attrs): + """ Start a table cell """ #FIXME: number-columns-repeated § 8.1.3 #repeated = int(attrs.get( (TABLENS,'number-columns-repeated'), 1)) htmlattrs = {} @@ -959,11 +1114,13 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def e_table_table_cell(self, tag, attrs): + """ End a table cell """ self.writedata() self.closetag('td') self.purgedata() def s_table_table_column(self, tag, attrs): + """ Start a table column """ c = attrs.get( (TABLENS,'style-name'), None) repeated = int(attrs.get( (TABLENS,'number-columns-repeated'), 1)) htmlattrs = {} @@ -974,6 +1131,7 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def s_table_table_row(self, tag, attrs): + """ Start a table row """ #FIXME: table:number-rows-repeated c = attrs.get( (TABLENS,'style-name'), None) htmlattrs = {} @@ -983,6 +1141,7 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def e_table_table_row(self, tag, attrs): + """ End a table row """ self.writedata() self.closetag('tr') self.purgedata() @@ -997,10 +1156,28 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def e_text_a(self, tag, attrs): + """ End an anchor or bookmark reference """ self.writedata() self.closetag('a', False) self.purgedata() + def s_text_bookmark(self, tag, attrs): + """ Bookmark definition """ + name = attrs[(TEXTNS,'name')] + html_id = self.get_anchor(name) + self.writedata() + self.opentag('span', {'id':html_id}) + self.closetag('span', False) + self.purgedata() + + def s_text_bookmark_ref(self, tag, attrs): + """ Bookmark reference """ + name = attrs[(TEXTNS,'ref-name')] + html_id = "#" + self.get_anchor(name) + self.writedata() + self.opentag('a', {'href':html_id}) + self.purgedata() + def s_text_h(self, tag, attrs): """ Headings start """ level = int(attrs[(TEXTNS,'outline-level')]) @@ -1018,13 +1195,19 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def e_text_h(self, tag, attrs): - """ Headings end """ + """ Headings end + Side-effect: If there is no title in the metadata, then it is taken + from the first heading of any level. + """ self.writedata() level = int(attrs[(TEXTNS,'outline-level')]) if level > 6: level = 6 # Heading levels go only to 6 in XHTML if level < 1: level = 1 lev = self.headinglevels[1:level+1] outline = '.'.join(map(str,lev) ) + heading = ''.join(self.data) + if self.title == '': self.title = heading + # Changed by Kovid tail = ''.join(self.data) anchor = self.get_anchor("%s.%s" % ( outline, tail)) anchor2 = self.get_anchor(tail) # Added by kovid to fix #7506 @@ -1036,12 +1219,14 @@ class ODF2XHTML(handler.ContentHandler): self.purgedata() def s_text_line_break(self, tag, attrs): + """ Force a line break (
) """ self.writedata() self.emptytag('br') self.purgedata() def s_text_list(self, tag, attrs): - """ To know which level we're at, we have to count the number + """ Start a list (
    or
      ) + To know which level we're at, we have to count the number of elements on the tagstack. """ name = attrs.get( (TEXTNS,'style-name') ) @@ -1055,12 +1240,13 @@ class ODF2XHTML(handler.ContentHandler): name = self.tagstack.rfindattr( (TEXTNS,'style-name') ) list_class = "%s_%d" % (name, level) if self.generate_css: - self.opentag('%s' % self.listtypes.get(list_class,'UL'), {'class': list_class }) + self.opentag('%s' % self.listtypes.get(list_class,'ul'), {'class': list_class }) else: - self.opentag('%s' % self.listtypes.get(list_class,'UL')) + self.opentag('%s' % self.listtypes.get(list_class,'ul')) self.purgedata() def e_text_list(self, tag, attrs): + """ End a list """ self.writedata() name = attrs.get( (TEXTNS,'style-name') ) level = self.tagstack.count_tags(tag) + 1 @@ -1072,14 +1258,16 @@ class ODF2XHTML(handler.ContentHandler): # textbox itself may be nested within another list. name = self.tagstack.rfindattr( (TEXTNS,'style-name') ) list_class = "%s_%d" % (name, level) - self.closetag(self.listtypes.get(list_class,'UL')) + self.closetag(self.listtypes.get(list_class,'ul')) self.purgedata() def s_text_list_item(self, tag, attrs): + """ Start list item """ self.opentag('li') self.purgedata() def e_text_list_item(self, tag, attrs): + """ End list item """ self.writedata() self.closetag('li') self.purgedata() @@ -1191,7 +1379,7 @@ class ODF2XHTML(handler.ContentHandler): if specialtag is None: specialtag = 'p' self.writedata() - if not self.data: + if not self.data: # Added by Kovid # Give substance to empty paragraphs, as rendered by OOo self.writeout(' ') self.closetag(specialtag) @@ -1254,55 +1442,30 @@ class ODF2XHTML(handler.ContentHandler): #----------------------------------------------------------------------------- def load(self, odffile): - self._odffile = odffile + """ Loads a document into the parser and parses it. + The argument can either be a filename or a document in memory. + """ + self.lines = [] + self._wfunc = self._wlines + if isinstance(odffile, basestring) \ + or hasattr(odffile, 'read'): # Added by Kovid + self.document = load(odffile) + else: + self.document = odffile + self._walknode(self.document.topnode) - def parseodf(self): - self.xmlfile = '' - self.title = '' - self.data = [] - self.tagstack = TagStack() - self.pstack = [] - self.processelem = True - self.processcont = True - self.listtypes = {} - self.headinglevels = [0, 0,0,0,0,0, 0,0,0,0,0] # level 0 to 10 - self.cs = StyleToCSS() - self.anchors = {} + def _walknode(self, node): + if node.nodeType == Node.ELEMENT_NODE: + self.startElementNS(node.qname, node.tagName, node.attributes) + for c in node.childNodes: + self._walknode(c) + self.endElementNS(node.qname, node.tagName) + if node.nodeType == Node.TEXT_NODE or node.nodeType == Node.CDATA_SECTION_NODE: + self.characters(unicode(node)) - # Style declarations - self.stylestack = [] - self.styledict = {} - self.currentstyle = None - - # Footnotes and endnotes - self.notedict = {} - self.currentnote = 0 - self.notebody = '' - - # Tags from meta.xml - self.metatags = [] - - # Extract the interesting files - z = zipfile.ZipFile(self._odffile) - - # For some reason Trac has trouble when xml.sax.make_parser() is used. - # Could it be because PyXML is installed, and therefore a different parser - # might be chosen? By calling expatreader directly we avoid this issue - parser = expatreader.create_parser() - parser.setFeature(handler.feature_namespaces, 1) - parser.setContentHandler(self) - parser.setErrorHandler(handler.ErrorHandler()) - inpsrc = InputSource() - - for xmlfile in ('meta.xml', 'styles.xml', 'content.xml'): - self.xmlfile = xmlfile - content = z.read(xmlfile) - inpsrc.setByteStream(StringIO(content)) - parser.parse(inpsrc) - z.close() def odf2xhtml(self, odffile): - """ Load a file and return XHTML + """ Load a file and return the XHTML """ self.load(odffile) return self.xhtml() @@ -1311,9 +1474,8 @@ class ODF2XHTML(handler.ContentHandler): if s != '': self.lines.append(s) def xhtml(self): - self.lines = [] - self._wfunc = self._wlines - self.parseodf() + """ Returns the xhtml + """ return ''.join(self.lines) def _writecss(self, s): @@ -1323,11 +1485,127 @@ class ODF2XHTML(handler.ContentHandler): pass def css(self): - self._wfunc = self._writenothing - self.parseodf() + """ Returns the CSS content """ self._csslines = [] self._wfunc = self._writecss self.generate_stylesheet() res = ''.join(self._csslines) + self._wfunc = self._wlines del self._csslines return res + + def save(self, outputfile, addsuffix=False): + """ Save the HTML under the filename. + If the filename is '-' then save to stdout + We have the last style filename in self.stylefilename + """ + if outputfile == '-': + import sys # Added by Kovid + outputfp = sys.stdout + else: + if addsuffix: + outputfile = outputfile + ".html" + outputfp = file(outputfile, "w") + outputfp.write(self.xhtml().encode('us-ascii','xmlcharrefreplace')) + outputfp.close() + + +class ODF2XHTMLembedded(ODF2XHTML): + """ The ODF2XHTML parses an ODF file and produces XHTML""" + + def __init__(self, lines, generate_css=True, embedable=False): + self._resetobject() + self.lines = lines + + # Tags + self.generate_css = generate_css + self.elements = { +# (DCNS, 'title'): (self.s_processcont, self.e_dc_title), +# (DCNS, 'language'): (self.s_processcont, self.e_dc_contentlanguage), +# (DCNS, 'creator'): (self.s_processcont, self.e_dc_metatag), +# (DCNS, 'description'): (self.s_processcont, self.e_dc_metatag), +# (DCNS, 'date'): (self.s_processcont, self.e_dc_metatag), + (DRAWNS, 'frame'): (self.s_draw_frame, self.e_draw_frame), + (DRAWNS, 'image'): (self.s_draw_image, None), + (DRAWNS, 'fill-image'): (self.s_draw_fill_image, None), + (DRAWNS, "layer-set"):(self.s_ignorexml, None), + (DRAWNS, 'page'): (self.s_draw_page, self.e_draw_page), + (DRAWNS, 'object'): (self.s_draw_object, None), + (DRAWNS, 'object-ole'): (self.s_draw_object_ole, None), + (DRAWNS, 'text-box'): (self.s_draw_textbox, self.e_draw_textbox), +# (METANS, 'creation-date'):(self.s_processcont, self.e_dc_metatag), +# (METANS, 'generator'):(self.s_processcont, self.e_dc_metatag), +# (METANS, 'initial-creator'): (self.s_processcont, self.e_dc_metatag), +# (METANS, 'keyword'): (self.s_processcont, self.e_dc_metatag), + (NUMBERNS, "boolean-style"):(self.s_ignorexml, None), + (NUMBERNS, "currency-style"):(self.s_ignorexml, None), + (NUMBERNS, "date-style"):(self.s_ignorexml, None), + (NUMBERNS, "number-style"):(self.s_ignorexml, None), + (NUMBERNS, "text-style"):(self.s_ignorexml, None), +# (OFFICENS, "automatic-styles"):(self.s_office_automatic_styles, None), +# (OFFICENS, "document-content"):(self.s_office_document_content, self.e_office_document_content), + (OFFICENS, "forms"):(self.s_ignorexml, None), +# (OFFICENS, "master-styles"):(self.s_office_master_styles, None), + (OFFICENS, "meta"):(self.s_ignorecont, None), +# (OFFICENS, "presentation"):(self.s_office_presentation, self.e_office_presentation), +# (OFFICENS, "spreadsheet"):(self.s_office_spreadsheet, self.e_office_spreadsheet), +# (OFFICENS, "styles"):(self.s_office_styles, None), +# (OFFICENS, "text"):(self.s_office_text, self.e_office_text), + (OFFICENS, "scripts"):(self.s_ignorexml, None), + (PRESENTATIONNS, "notes"):(self.s_ignorexml, None), +## (STYLENS, "default-page-layout"):(self.s_style_default_page_layout, self.e_style_page_layout), +# (STYLENS, "default-page-layout"):(self.s_ignorexml, None), +# (STYLENS, "default-style"):(self.s_style_default_style, self.e_style_default_style), +# (STYLENS, "drawing-page-properties"):(self.s_style_handle_properties, None), +# (STYLENS, "font-face"):(self.s_style_font_face, None), +## (STYLENS, "footer"):(self.s_style_footer, self.e_style_footer), +## (STYLENS, "footer-style"):(self.s_style_footer_style, None), +# (STYLENS, "graphic-properties"):(self.s_style_handle_properties, None), +# (STYLENS, "handout-master"):(self.s_ignorexml, None), +## (STYLENS, "header"):(self.s_style_header, self.e_style_header), +## (STYLENS, "header-footer-properties"):(self.s_style_handle_properties, None), +## (STYLENS, "header-style"):(self.s_style_header_style, None), +# (STYLENS, "master-page"):(self.s_style_master_page, None), +# (STYLENS, "page-layout-properties"):(self.s_style_handle_properties, None), +## (STYLENS, "page-layout"):(self.s_style_page_layout, self.e_style_page_layout), +# (STYLENS, "page-layout"):(self.s_ignorexml, None), +# (STYLENS, "paragraph-properties"):(self.s_style_handle_properties, None), +# (STYLENS, "style"):(self.s_style_style, self.e_style_style), +# (STYLENS, "table-cell-properties"):(self.s_style_handle_properties, None), +# (STYLENS, "table-column-properties"):(self.s_style_handle_properties, None), +# (STYLENS, "table-properties"):(self.s_style_handle_properties, None), +# (STYLENS, "text-properties"):(self.s_style_handle_properties, None), + (SVGNS, 'desc'): (self.s_ignorexml, None), + (TABLENS, 'covered-table-cell'): (self.s_ignorexml, None), + (TABLENS, 'table-cell'): (self.s_table_table_cell, self.e_table_table_cell), + (TABLENS, 'table-column'): (self.s_table_table_column, None), + (TABLENS, 'table-row'): (self.s_table_table_row, self.e_table_table_row), + (TABLENS, 'table'): (self.s_table_table, self.e_table_table), + (TEXTNS, 'a'): (self.s_text_a, self.e_text_a), + (TEXTNS, "alphabetical-index-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, "bibliography-configuration"):(self.s_ignorexml, None), + (TEXTNS, "bibliography-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, 'h'): (self.s_text_h, self.e_text_h), + (TEXTNS, "illustration-index-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, 'line-break'):(self.s_text_line_break, None), + (TEXTNS, "linenumbering-configuration"):(self.s_ignorexml, None), + (TEXTNS, "list"):(self.s_text_list, self.e_text_list), + (TEXTNS, "list-item"):(self.s_text_list_item, self.e_text_list_item), + (TEXTNS, "list-level-style-bullet"):(self.s_text_list_level_style_bullet, self.e_text_list_level_style_bullet), + (TEXTNS, "list-level-style-number"):(self.s_text_list_level_style_number, self.e_text_list_level_style_number), + (TEXTNS, "list-style"):(None, None), + (TEXTNS, "note"):(self.s_text_note, None), + (TEXTNS, "note-body"):(self.s_text_note_body, self.e_text_note_body), + (TEXTNS, "note-citation"):(None, self.e_text_note_citation), + (TEXTNS, "notes-configuration"):(self.s_ignorexml, None), + (TEXTNS, "object-index-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, 'p'): (self.s_text_p, self.e_text_p), + (TEXTNS, 's'): (self.s_text_s, None), + (TEXTNS, 'span'): (self.s_text_span, self.e_text_span), + (TEXTNS, 'tab'): (self.s_text_tab, None), + (TEXTNS, "table-index-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, "table-of-content-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, "user-index-source"):(self.s_text_x_source, self.e_text_x_source), + (TEXTNS, "page-number"):(None, None), + } + diff --git a/src/odf/opendocument.py b/src/odf/opendocument.py index 9fd16229f6..63196382d5 100644 --- a/src/odf/opendocument.py +++ b/src/odf/opendocument.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2006-2009 Søren Roug, European Environment Agency +# Copyright (C) 2006-2010 Søren Roug, European Environment Agency # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -41,7 +41,7 @@ IS_IMAGE = 1 # We need at least Python 2.2 assert sys.version_info[0]>=2 and sys.version_info[1] >= 2 -sys.setrecursionlimit=50 +#sys.setrecursionlimit(100) #The recursion limit is set conservative so mistakes like # s=content() s.addElement(s) won't eat up too much processor time. @@ -128,12 +128,12 @@ class OpenDocument: self.element_dict[element.qname] = [] self.element_dict[element.qname].append(element) if element.qname == (STYLENS, u'style'): - self._register_stylename(element) # Add to style dictionary + self.__register_stylename(element) # Add to style dictionary styleref = element.getAttrNS(TEXTNS,u'style-name') if styleref is not None and self._styles_ooo_fix.has_key(styleref): element.setAttrNS(TEXTNS,u'style-name', self._styles_ooo_fix[styleref]) - def _register_stylename(self, element): + def __register_stylename(self, element): ''' Register a style. But there are three style dictionaries: office:styles, office:automatic-styles and office:master-styles Chapter 14 @@ -165,7 +165,7 @@ class OpenDocument: """ Generates the full document as an XML file Always written as a bytestream in UTF-8 encoding """ - self._replaceGenerator() + self.__replaceGenerator() xml=StringIO() xml.write(_XMLPROLOGUE) self.topnode.toXml(0, xml) @@ -197,8 +197,10 @@ class OpenDocument: x.write_close_tag(0, xml) return xml.getvalue() - def manifestxml(self): - """ Generates the manifest.xml file """ + def __manifestxml(self): + """ Generates the manifest.xml file + The self.manifest isn't avaible unless the document is being saved + """ xml=StringIO() xml.write(_XMLPROLOGUE) self.manifest.toXml(0,xml) @@ -206,7 +208,7 @@ class OpenDocument: def metaxml(self): """ Generates the meta.xml file """ - self._replaceGenerator() + self.__replaceGenerator() x = DocumentMeta() x.addElement(self.meta) xml=StringIO() @@ -344,7 +346,7 @@ class OpenDocument: self.thumbnail = filecontent def addObject(self, document, objectname=None): - """ Add an object. The object must be an OpenDocument class + """ Adds an object (subdocument). The object must be an OpenDocument class The return value will be the folder in the zipfile the object is stored in """ self.childobjects.append(document) @@ -367,15 +369,16 @@ class OpenDocument: zi.compress_type = zipfile.ZIP_STORED zi.external_attr = UNIXPERMS self._z.writestr(zi, fileobj) - if hasPictures: - self.manifest.addElement(manifest.FileEntry(fullpath="%sPictures/" % folder,mediatype="")) + # According to section 17.7.3 in ODF 1.1, the pictures folder should not have a manifest entry +# if hasPictures: +# self.manifest.addElement(manifest.FileEntry(fullpath="%sPictures/" % folder, mediatype="")) # Look in subobjects subobjectnum = 1 for subobject in object.childobjects: self._savePictures(subobject,'%sObject %d/' % (folder, subobjectnum)) subobjectnum += 1 - def _replaceGenerator(self): + def __replaceGenerator(self): """ Section 3.1.1: The application MUST NOT export the original identifier belonging to the application that created the document. """ @@ -385,22 +388,29 @@ class OpenDocument: self.meta.addElement(meta.Generator(text=TOOLSVERSION)) def save(self, outputfile, addsuffix=False): - """ Save the document under the filename """ + """ Save the document under the filename. + If the filename is '-' then save to stdout + """ if outputfile == '-': outputfp = zipfile.ZipFile(sys.stdout,"w") else: if addsuffix: outputfile = outputfile + odmimetypes.get(self.mimetype,'.xxx') outputfp = zipfile.ZipFile(outputfile, "w") - self._zipwrite(outputfp) + self.__zipwrite(outputfp) outputfp.close() def write(self, outputfp): + """ User API to write the ODF file to an open file descriptor + Writes the ZIP format + """ zipoutputfp = zipfile.ZipFile(outputfp,"w") - self._zipwrite(zipoutputfp) + self.__zipwrite(zipoutputfp) - def _zipwrite(self, outputfp): - """ Write the document to an open file pointer """ + def __zipwrite(self, outputfp): + """ Write the document to an open file pointer + This is where the real work is done + """ self._z = outputfp self._now = time.localtime()[:6] self.manifest = manifest.Manifest() @@ -438,7 +448,7 @@ class OpenDocument: zi = zipfile.ZipInfo("META-INF/manifest.xml", self._now) zi.compress_type = zipfile.ZIP_DEFLATED zi.external_attr = UNIXPERMS - self._z.writestr(zi, self.manifestxml() ) + self._z.writestr(zi, self.__manifestxml() ) del self._z del self._now del self.manifest @@ -464,8 +474,8 @@ class OpenDocument: self._z.writestr(zi, object.contentxml() ) # Write settings - if self == object and self.settings.hasChildNodes(): - self.manifest.addElement(manifest.FileEntry(fullpath="settings.xml",mediatype="text/xml")) + if object.settings.hasChildNodes(): + self.manifest.addElement(manifest.FileEntry(fullpath="%ssettings.xml" % folder, mediatype="text/xml")) zi = zipfile.ZipInfo("%ssettings.xml" % folder, self._now) zi.compress_type = zipfile.ZIP_DEFLATED zi.external_attr = UNIXPERMS @@ -473,7 +483,7 @@ class OpenDocument: # Write meta if self == object: - self.manifest.addElement(manifest.FileEntry(fullpath="meta.xml",mediatype="text/xml")) + self.manifest.addElement(manifest.FileEntry(fullpath="meta.xml", mediatype="text/xml")) zi = zipfile.ZipInfo("meta.xml", self._now) zi.compress_type = zipfile.ZIP_DEFLATED zi.external_attr = UNIXPERMS @@ -497,6 +507,7 @@ class OpenDocument: return element.Text(data) def createCDATASection(self, data): + """ Method to create a CDATA section """ return element.CDATASection(cdata) def getMediaType(self): @@ -504,12 +515,14 @@ class OpenDocument: return self.mimetype def getStyleByName(self, name): + """ Finds a style object based on the name """ ncname = make_NCName(name) if self._styles_dict == {}: self.rebuild_caches() return self._styles_dict.get(ncname, None) def getElementsByType(self, element): + """ Gets elements based on the type, which is function from text.py, draw.py etc. """ obj = element(check_grammar=False) if self.element_dict == {}: self.rebuild_caches() @@ -517,53 +530,59 @@ class OpenDocument: # Convenience functions def OpenDocumentChart(): + """ Creates a chart document """ doc = OpenDocument('application/vnd.oasis.opendocument.chart') doc.chart = Chart() doc.body.addElement(doc.chart) return doc def OpenDocumentDrawing(): + """ Creates a drawing document """ doc = OpenDocument('application/vnd.oasis.opendocument.graphics') doc.drawing = Drawing() doc.body.addElement(doc.drawing) return doc def OpenDocumentImage(): + """ Creates an image document """ doc = OpenDocument('application/vnd.oasis.opendocument.image') doc.image = Image() doc.body.addElement(doc.image) return doc def OpenDocumentPresentation(): + """ Creates a presentation document """ doc = OpenDocument('application/vnd.oasis.opendocument.presentation') doc.presentation = Presentation() doc.body.addElement(doc.presentation) return doc def OpenDocumentSpreadsheet(): + """ Creates a spreadsheet document """ doc = OpenDocument('application/vnd.oasis.opendocument.spreadsheet') doc.spreadsheet = Spreadsheet() doc.body.addElement(doc.spreadsheet) return doc def OpenDocumentText(): + """ Creates a text document """ doc = OpenDocument('application/vnd.oasis.opendocument.text') doc.text = Text() doc.body.addElement(doc.text) return doc +def OpenDocumentTextMaster(): + """ Creates a text master document """ + doc = OpenDocument('application/vnd.oasis.opendocument.text-master') + doc.text = Text() + doc.body.addElement(doc.text) + return doc -def load(odffile): +def __loadxmlparts(z, manifest, doc, objectpath): from load import LoadParser from xml.sax import make_parser, handler - z = zipfile.ZipFile(odffile) - mimetype = z.read('mimetype') - doc = OpenDocument(mimetype, add_generator=False) - # Look in the manifest file to see if which of the four files there are - manifestpart = z.read('META-INF/manifest.xml') - manifest = manifestlist(manifestpart) - for xmlfile in ('settings.xml', 'meta.xml', 'content.xml', 'styles.xml'): + for xmlfile in (objectpath+'settings.xml', objectpath+'meta.xml', objectpath+'content.xml', objectpath+'styles.xml'): if not manifest.has_key(xmlfile): continue try: @@ -580,7 +599,19 @@ def load(odffile): parser.parse(inpsrc) del doc._parsing except KeyError, v: pass - # FIXME: Add subobjects correctly here + +def load(odffile): + """ Load an ODF file into memory + Returns a reference to the structure + """ + z = zipfile.ZipFile(odffile) + mimetype = z.read('mimetype') + doc = OpenDocument(mimetype, add_generator=False) + + # Look in the manifest file to see if which of the four files there are + manifestpart = z.read('META-INF/manifest.xml') + manifest = manifestlist(manifestpart) + __loadxmlparts(z, manifest, doc, '') for mentry,mvalue in manifest.items(): if mentry[:9] == "Pictures/" and len(mentry) > 9: doc.addPicture(mvalue['full-path'], mvalue['media-type'], z.read(mentry)) @@ -588,6 +619,13 @@ def load(odffile): doc.addThumbnail(z.read(mentry)) elif mentry in ('settings.xml', 'meta.xml', 'content.xml', 'styles.xml'): pass + # Load subobjects into structure + elif mentry[:7] == "Object " and len(mentry) < 11 and mentry[-1] == "/": + subdoc = OpenDocument(mvalue['media-type'], add_generator=False) + doc.addObject(subdoc, "/" + mentry[:-1]) + __loadxmlparts(z, manifest, subdoc, mentry) + elif mentry[:7] == "Object ": + pass # Don't load subobjects as opaque objects else: if mvalue['full-path'][-1] == '/': doc._extra.append(OpaqueObject(mvalue['full-path'], mvalue['media-type'], None)) @@ -612,4 +650,5 @@ def load(odffile): elif mimetype[:42] == 'application/vnd.oasis.opendocument.formula': doc.formula = b[0].firstChild return doc + # vim: set expandtab sw=4 :