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)'),
)
# 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'):
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
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):
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
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()