From fbe239a368910bfa3f81f17716e9fa3849d7e6dd Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Thu, 16 Apr 2015 18:52:47 +0200 Subject: [PATCH] fix more epub errors against epubcheck (mostly filenames) --- .../commands/telemeta-import-corpus-epub.py | 26 ++++++++++++++++--- .../templates/telemeta/collection_epub.html | 22 +++++++--------- telemeta/views/resource.py | 6 ++--- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/telemeta/management/commands/telemeta-import-corpus-epub.py b/telemeta/management/commands/telemeta-import-corpus-epub.py index 909b8b5a..8fbb21d4 100644 --- a/telemeta/management/commands/telemeta-import-corpus-epub.py +++ b/telemeta/management/commands/telemeta-import-corpus-epub.py @@ -16,6 +16,15 @@ except ImportError: def beautify(string): return os.path.splitext(string)[0].replace('_',' ') +def remove_dir_spaces(root_dir): + for resource in os.listdir(root_dir): + path = os.path.join(root_dir, resource) + if os.path.isdir(path): + new_path = path.replace(' ', '_') + if new_path != path: + os.rename(path, new_path) + remove_dir_spaces(new_path) + def trim_list(list): new = [] for item in list: @@ -32,17 +41,26 @@ class Command(BaseCommand): def handle(self, *args, **options): root_dir = args[-1] + remove_dir_spaces(root_dir) for root, dirs, files in os.walk(root_dir): for media_file in files: + path = os.path.join(root, media_file) + + if ' ' in media_file: + new_media_file = media_file.replace(' ', '_') + new_media_path = os.path.join(root, new_media_file) + os.rename(path, new_media_path) + media_file = new_media_file + print media_file + media_name = os.path.splitext(media_file)[0] media_ext = os.path.splitext(media_file)[1][1:] if media_ext and media_ext in self.media_formats and media_name[0] != '.': root_list = root.split(os.sep) - media_path = os.sep.join(root_list[-4:]) + os.sep + media_file - print media_path + item_name = root_list[-1] collection_name = root_list[-2] corpus_name = root_list[-3] @@ -74,7 +92,7 @@ class Command(BaseCommand): if lines: item.track = lines[2] - item.title = lines[3] + item.title = lines[3][:255] item.save() for related_file in os.listdir(root): @@ -84,7 +102,7 @@ class Command(BaseCommand): print related_path if related_ext in self.image_formats: - related, c = MediaItemRelated.objects.get_or_create(item=item, file=unicode(related_path)) + related, c = MediaItemRelated.objects.get_or_create(item=item, file=related_path) if c: if lines: related.title = lines[4] diff --git a/telemeta/templates/telemeta/collection_epub.html b/telemeta/templates/telemeta/collection_epub.html index 1bc24e12..c01b113a 100644 --- a/telemeta/templates/telemeta/collection_epub.html +++ b/telemeta/templates/telemeta/collection_epub.html @@ -1,28 +1,24 @@ {% for item in items %} - - - - - -
+

+ + {{ item.old_code }} : {{ item.title }} (p. {{ item.track }}) + +
{% for image in item.related.all %} {% if 'image' in image.mime_type %}

- +
{% endif %} {% endfor %} -
- - {{ item.old_code }} : {{ item.title }} (p. {{ item.track }}) - -
- +
+

+ {% endfor %} diff --git a/telemeta/views/resource.py b/telemeta/views/resource.py index 2db47254..d3251f16 100644 --- a/telemeta/views/resource.py +++ b/telemeta/views/resource.py @@ -395,7 +395,7 @@ class CorpusEpubView(View): for collection in corpus.children.all(): items = {} for item in collection.items.all(): - id = item.old_code.split(' ') + id = item.old_code.split('_') if len(id) > 1: id = id[1] items[item] = int(id.split('.')[1]) @@ -405,12 +405,12 @@ class CorpusEpubView(View): if item.file: audio = open(item.file.path, 'r') filename = str(item.file) - epub_item = epub.EpubItem(file_name=cleanup_path(str(item.file)), content=audio.read()) + epub_item = epub.EpubItem(file_name=str(item.file), content=audio.read()) book.add_item(epub_item) for related in item.related.all(): if 'image' in related.mime_type: image = open(related.file.path, 'r') - epub_item = epub.EpubItem(file_name=cleanup_path((str(related.file)), content=image.read()) + epub_item = epub.EpubItem(file_name=str(related.file), content=image.read()) book.add_item(epub_item) context = {'collection': collection, 'site': site, 'items': items} c = epub.EpubHtml(title=collection.title, file_name=collection.code + '.xhtml', lang='fr') -- 2.39.5