From fca3f98e6a2b3e7dbd98da3618b1f3d805ad8c1e Mon Sep 17 00:00:00 2001 From: John Schember Date: Wed, 1 Apr 2009 20:05:34 -0400 Subject: [PATCH 1/2] Text input plugin --- src/calibre/customize/builtins.py | 3 ++- src/calibre/ebooks/txt/input.py | 42 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/calibre/ebooks/txt/input.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 932261c45d..ab6d772121 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -265,12 +265,13 @@ class MOBIMetadataWriter(MetadataWriterPlugin): from calibre.ebooks.epub.input import EPUBInput from calibre.ebooks.mobi.input import MOBIInput +from calibre.ebooks.txt.input import TXTInput from calibre.ebooks.oeb.output import OEBOutput from calibre.ebooks.txt.output import TXTOutput from calibre.ebooks.pdf.output import PDFOutput from calibre.customize.profiles import input_profiles, output_profiles -plugins = [HTML2ZIP, EPUBInput, MOBIInput, OEBOutput, TXTOutput, PDFOutput] +plugins = [HTML2ZIP, EPUBInput, MOBIInput, TXTInput, OEBOutput, TXTOutput, PDFOutput] plugins += [x for x in list(locals().values()) if isinstance(x, type) and \ x.__name__.endswith('MetadataReader')] plugins += [x for x in list(locals().values()) if isinstance(x, type) and \ diff --git a/src/calibre/ebooks/txt/input.py b/src/calibre/ebooks/txt/input.py new file mode 100644 index 0000000000..a42c72866f --- /dev/null +++ b/src/calibre/ebooks/txt/input.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from __future__ import with_statement + +__license__ = 'GPL 3' +__copyright__ = '2009, John Schember ' +__docformat__ = 'restructuredtext en' + +import os + +from calibre.customize.conversion import InputFormatPlugin +from calibre.ebooks.markdown import markdown +from calibre.ebooks.metadata.opf import OPFCreator +from calibre.ebooks.metadata import MetaInformation +#from calibre.ebooks.metadata.meta import metadata_from_formats + +class TXTInput(InputFormatPlugin): + + name = 'TXT Input' + author = 'John Schember' + description = 'Convert TXT files to HTML' + file_types = set(['txt']) + + def convert(self, stream, options, file_ext, log, + accelerators): + txt = stream.read() + + md = markdown.Markdown( + extensions=['footnotes', 'tables', 'toc'], + safe_mode=False,) + html = ''+md.convert(txt)+'' + with open('index.html', 'wb') as index: + index.write(html.encode('utf-8')) + + #mi = metadata_from_formats([stream.name]) + mi = MetaInformation(_('Unknown'), _('Unknown')) + opf = OPFCreator(os.getcwd(), mi) + opf.create_manifest([('index.html', None)]) + opf.create_spine(['index.html']) + with open('metadata.opf', 'wb') as opffile: + opf.render(opffile) + + return os.path.join(os.getcwd(), 'metadata.opf') From e6e9a2058657c7cb5b7e9cac52553d58ae010e99 Mon Sep 17 00:00:00 2001 From: John Schember Date: Wed, 1 Apr 2009 21:21:26 -0400 Subject: [PATCH 2/2] Txt output mostly fixed --- src/calibre/ebooks/txt/output.py | 6 +++--- src/calibre/ebooks/txt/writer.py | 19 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/calibre/ebooks/txt/output.py b/src/calibre/ebooks/txt/output.py index 7d44172b3f..5e58d47ef1 100644 --- a/src/calibre/ebooks/txt/output.py +++ b/src/calibre/ebooks/txt/output.py @@ -39,10 +39,10 @@ class TXTOutput(OutputFormatPlugin): def convert(self, oeb_book, output_path, input_plugin, opts, log): metadata = TxtMetadata() if opts.prepend_author.lower() == 'true': - metadata.author = opts.authors if opts.authors else authors_to_string(oeb_book.metadata.authors) + metadata.author = opts.authors if opts.authors else authors_to_string(oeb_book.metadata.authors.value) if oeb_book.metadata.authors != [] else _('Unknown') if opts.prepend_title.lower() == 'true': - metadata.title = opts.title if opts.title else oeb_book.metadata.title - + metadata.title = opts.title if opts.title else oeb_book.metadata.title[0].value if oeb_book.metadata.title != [] else _('Unknown') + writer = TxtWriter(TxtNewlines(opts.newline).newline, log) txt = writer.dump(oeb_book.spine, metadata) diff --git a/src/calibre/ebooks/txt/writer.py b/src/calibre/ebooks/txt/writer.py index eabc2d64ed..2fb5f9550a 100644 --- a/src/calibre/ebooks/txt/writer.py +++ b/src/calibre/ebooks/txt/writer.py @@ -22,16 +22,15 @@ class TxtWriter(object): def dump(self, spine, metadata): out = u'' for item in spine: - with open(item, 'r') as itemf: - content = itemf.read().decode(item.encoding) - # Convert newlines to unix style \n for processing. These - # will be changed to the specified type later in the process. - content = self.unix_newlines(content) - content = self.strip_html(content) - content = self.replace_html_symbols(content) - content = self.cleanup_text(content) - content = self.specified_newlines(content) - out += content + content = unicode(item) + # Convert newlines to unix style \n for processing. These + # will be changed to the specified type later in the process. + content = self.unix_newlines(content) + content = self.strip_html(content) + content = self.replace_html_symbols(content) + content = self.cleanup_text(content) + content = self.specified_newlines(content) + out += content # Prepend metadata if metadata.author != None and metadata.author != '':