From 14f2e79b2ca9b5359ef95988a4a3a5e510df244e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20St=C4=99pniowski?= Date: Wed, 17 Sep 2008 01:27:19 +0200 Subject: [PATCH] Don't generate HTML files for books with no text. --- apps/catalogue/models.py | 61 ++++++++++--------- lib/librarian/html.py | 10 ++- .../templates/catalogue/book_short.html | 4 +- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 7f6268fdb..b5970e9f1 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -177,38 +177,39 @@ class Book(models.Model): book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False) html_file = NamedTemporaryFile() - html.transform(book.xml_file.path, html_file) - book.html_file.save('%s.html' % book.slug, File(html_file), save=False) - - # Extract fragments - closed_fragments, open_fragments = html.extract_fragments(book.html_file.path) - book_themes = [] - for fragment in closed_fragments.values(): - text = fragment.to_string() - short_text = '' - if (len(MarkupString(text)) > 240): - short_text = unicode(MarkupString(text)[:160]) - new_fragment = Fragment(text=text, short_text=short_text, anchor=fragment.id, book=book) + if html.transform(book.xml_file.path, html_file): + book.html_file.save('%s.html' % book.slug, File(html_file), save=False) + + # Extract fragments + closed_fragments, open_fragments = html.extract_fragments(book.html_file.path) + book_themes = [] + for fragment in closed_fragments.values(): + text = fragment.to_string() + short_text = '' + if (len(MarkupString(text)) > 240): + short_text = unicode(MarkupString(text)[:160]) + new_fragment = Fragment(text=text, short_text=short_text, anchor=fragment.id, book=book) + + try: + theme_names = [s.strip() for s in fragment.themes.split(',')] + except AttributeError: + continue + themes = [] + for theme_name in theme_names: + tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name)) + if created: + tag.name = theme_name + tag.sort_key = slughifi(theme_name) + tag.category = 'theme' + tag.save() + themes.append(tag) + new_fragment.save() + new_fragment.tags = list(book.tags) + themes + book_themes += themes - try: - theme_names = [s.strip() for s in fragment.themes.split(',')] - except AttributeError: - continue - themes = [] - for theme_name in theme_names: - tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name)) - if created: - tag.name = theme_name - tag.sort_key = slughifi(theme_name) - tag.category = 'theme' - tag.save() - themes.append(tag) - new_fragment.save() - new_fragment.tags = list(book.tags) + themes - book_themes += themes + book_themes = set(book_themes) + book.tags = list(book.tags) + list(book_themes) - book_themes = set(book_themes) - book.tags = list(book.tags) + list(book_themes) return book.save() @permalink diff --git a/lib/librarian/html.py b/lib/librarian/html.py index 9763428d0..94514df3c 100644 --- a/lib/librarian/html.py +++ b/lib/librarian/html.py @@ -53,9 +53,13 @@ def transform(input_filename, output_filename): doc = etree.parse(doc_file, parser) result = doc.xslt(style) - add_anchors(result.getroot()) - add_table_of_contents(result.getroot()) - result.write(output_filename, xml_declaration=False, pretty_print=True, encoding='utf-8') + if result.find('//h1') is not None: + add_anchors(result.getroot()) + add_table_of_contents(result.getroot()) + result.write(output_filename, xml_declaration=False, pretty_print=True, encoding='utf-8') + return True + else: + return False class Fragment(object): diff --git a/wolnelektury/templates/catalogue/book_short.html b/wolnelektury/templates/catalogue/book_short.html index 368126db8..41d3212f3 100644 --- a/wolnelektury/templates/catalogue/book_short.html +++ b/wolnelektury/templates/catalogue/book_short.html @@ -9,7 +9,9 @@ {% endif %}

{{ book.title }}

-

Na skróty: {{ formats|join:", "|safe }}

+ {% if formats %} +

Na skróty: {{ formats|join:", "|safe }}

+ {% endif %}

Utwór w kategoriach: {{ tags|join:", "|safe }}

-- 2.20.1