- parse_dublincore=parse_dublincore)
-
- def build_html(self):
- from django.core.files.base import ContentFile
- from slughifi import slughifi
- from sortify import sortify
- from librarian import html
-
- meta_tags = list(self.tags.filter(
- category__in=('author', 'epoch', 'genre', 'kind')))
- book_tag = self.book_tag()
-
- html_output = self.wldocument(parse_dublincore=False).as_html()
- if html_output:
- self.html_file.save('%s.html' % self.slug,
- ContentFile(html_output.get_string()))
-
- # get ancestor l-tags for adding to new fragments
- ancestor_tags = []
- p = self.parent
- while p:
- ancestor_tags.append(p.book_tag())
- p = p.parent
-
- # Delete old fragments and create them from scratch
- self.fragments.all().delete()
- # Extract fragments
- closed_fragments, open_fragments = html.extract_fragments(self.html_file.path)
- for fragment in closed_fragments.values():
- try:
- theme_names = [s.strip() for s in fragment.themes.split(',')]
- except AttributeError:
- continue
- themes = []
- for theme_name in theme_names:
- if not theme_name:
- continue
- tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name), category='theme')
- if created:
- tag.name = theme_name
- tag.sort_key = sortify(theme_name.lower())
- tag.save()
- themes.append(tag)
- if not themes:
- continue
-
- text = fragment.to_string()
- short_text = truncate_html_words(text, 15)
- if text == short_text:
- short_text = ''
- new_fragment = Fragment.objects.create(anchor=fragment.id, book=self,
- text=text, short_text=short_text)
-
- new_fragment.save()
- new_fragment.tags = set(meta_tags + themes + [book_tag] + ancestor_tags)
- self.save()
- self.html_built.send(sender=self)
- return True
- return False
-
- # Thin wrappers for builder tasks
- def build_cover(self):
- """(Re)builds the cover image."""
- return tasks.build_cover.delay(self.pk)
- def build_pdf(self, *args, **kwargs):
- """(Re)builds PDF."""
- return tasks.build_pdf.delay(self.pk, *args, **kwargs)
- def build_epub(self, *args, **kwargs):
- """(Re)builds EPUB."""
- return tasks.build_epub.delay(self.pk, *args, **kwargs)
- def build_mobi(self, *args, **kwargs):
- """(Re)builds MOBI."""
- return tasks.build_mobi.delay(self.pk, *args, **kwargs)
- def build_fb2(self, *args, **kwargs):
- """(Re)build FB2"""
- return tasks.build_fb2.delay(self.pk, *args, **kwargs)
- def build_txt(self, *args, **kwargs):
- """(Re)builds TXT."""
- return tasks.build_txt.delay(self.pk, *args, **kwargs)