From: Radek Czajka Date: Mon, 17 Jan 2011 16:22:53 +0000 (+0100) Subject: #969: generate txt files during publication X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/9a6638c3bf05511aa43d6511436014921f00b169 #969: generate txt files during publication --- diff --git a/apps/catalogue/management/commands/importbooks.py b/apps/catalogue/management/commands/importbooks.py index 3daca8ac6..71a7eb290 100644 --- a/apps/catalogue/management/commands/importbooks.py +++ b/apps/catalogue/management/commands/importbooks.py @@ -22,6 +22,8 @@ class Command(BaseCommand): help='Print status messages to stdout'), make_option('-E', '--no-build-epub', action='store_false', dest='build_epub', default=True, help='Don\'t build EPUB file'), + make_option('-T', '--no-build-txt', action='store_false', dest='build_txt', default=True, + help='Don\'t build TXT file'), make_option('-w', '--wait-until', dest='wait_until', metavar='TIME', help='Wait until specified time (Y-M-D h:m:s)'), ) @@ -76,7 +78,9 @@ class Command(BaseCommand): # Import book files try: - book = Book.from_xml_file(file_path, overwrite=force, build_epub=options.get('build_epub')) + book = Book.from_xml_file(file_path, overwrite=force, + build_epub=options.get('build_epub'), + build_txt=options.get('build_txt')) files_imported += 1 if os.path.isfile(file_base + '.pdf'): diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 7e3ccfecb..9c943ca75 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -453,8 +453,7 @@ class Book(models.Model): epub_file = StringIO() try: epub.transform(BookImportDocProvider(self), self.slug, output_file=epub_file) - self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue()), save=False) - self.save() + self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue())) FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save() except NoDublinCore: pass @@ -468,6 +467,16 @@ class Book(models.Model): child_book.save() book_descendants += list(child_book.children.all()) + def build_txt(self): + from StringIO import StringIO + from django.core.files.base import ContentFile + from librarian import text + + out = StringIO() + text.transform(open(self.xml_file.path), out) + self.txt_file.save('%s.txt' % self.slug, ContentFile(out.getvalue())) + self.save() + @classmethod def from_xml_file(cls, xml_file, overwrite=False, build_epub=True): @@ -483,7 +492,7 @@ class Book(models.Model): xml_file.close() @classmethod - def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True): + def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True, build_txt=True): from tempfile import NamedTemporaryFile from slughifi import slughifi from markupstring import MarkupString @@ -592,6 +601,9 @@ class Book(models.Model): new_fragment.save() new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags) + if not settings.NO_BUILD_TXT and build_txt: + book.build_txt() + if not settings.NO_BUILD_EPUB and build_epub: book.root_ancestor.build_epub() diff --git a/lib/librarian b/lib/librarian index 5329a22cd..f0c23b2e3 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit 5329a22cd6643da657dd24546b382ada9e048b68 +Subproject commit f0c23b2e3a1a1d7f37d9f00118632d116b3c8582 diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index 186f4b69f..a50763dcc 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -199,6 +199,7 @@ TRANSLATION_REGISTRY = "wolnelektury.translation" MAX_TAG_LIST = 6 NO_BUILD_EPUB = False +NO_BUILD_TXT = False # Load localsettings, if they exist try: