Don't generate HTML files for books with no text.
authorMarek Stępniowski <marek@stepniowski.com>
Tue, 16 Sep 2008 23:27:19 +0000 (01:27 +0200)
committerMarek Stępniowski <marek@stepniowski.com>
Tue, 16 Sep 2008 23:27:19 +0000 (01:27 +0200)
apps/catalogue/models.py
lib/librarian/html.py
wolnelektury/templates/catalogue/book_short.html

index 7f6268f..b5970e9 100644 (file)
@@ -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()
         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
         return book.save()
     
     @permalink
index 9763428..94514df 100644 (file)
@@ -53,9 +53,13 @@ def transform(input_filename, output_filename):
     doc = etree.parse(doc_file, parser)
 
     result = doc.xslt(style)
     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):
 
 
 class Fragment(object):
index 368126d..41d3212 100644 (file)
@@ -9,7 +9,9 @@
     {% endif %}
     <div class="book-description">
         <h2><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></h2>
     {% endif %}
     <div class="book-description">
         <h2><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></h2>
-        <p style="margin: 0">Na skróty: {{ formats|join:", "|safe }}</p>
+        {% if formats %}
+            <p style="margin: 0">Na skróty: {{ formats|join:", "|safe }}</p>
+        {% endif %}
         <p style="margin: 0">Utwór w kategoriach: {{ tags|join:", "|safe }}</p>
     </div>
 </div>
         <p style="margin: 0">Utwór w kategoriach: {{ tags|join:", "|safe }}</p>
     </div>
 </div>