From aec5beb4c36c745fbfb67b3b8e72e87a8e4fdac1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 2 May 2009 18:23:41 -0700 Subject: [PATCH] Plugin for LIT Output --- src/calibre/customize/builtins.py | 3 +- src/calibre/ebooks/lit/output.py | 33 ++++++ src/calibre/ebooks/lit/writer.py | 101 ++++-------------- src/calibre/ebooks/oeb/iterator.py | 2 +- src/calibre/ebooks/oeb/transforms/metadata.py | 20 ++-- 5 files changed, 71 insertions(+), 88 deletions(-) create mode 100644 src/calibre/ebooks/lit/output.py diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 4a968966c7..8272ac90a2 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -292,6 +292,7 @@ from calibre.ebooks.oeb.output import OEBOutput from calibre.ebooks.epub.output import EPUBOutput from calibre.ebooks.mobi.output import MOBIOutput from calibre.ebooks.lrf.output import LRFOutput +from calibre.ebooks.lit.output import LITOutput from calibre.ebooks.txt.output import TXTOutput from calibre.ebooks.pdf.output import PDFOutput from calibre.ebooks.pml.input import PMLInput @@ -311,7 +312,7 @@ from calibre.devices.jetbook.driver import JETBOOK plugins = [HTML2ZIP, EPUBInput, MOBIInput, PDBInput, PDFInput, HTMLInput, TXTInput, OEBOutput, TXTOutput, PDFOutput, LITInput, ComicInput, FB2Input, ODTInput, RTFInput, EPUBOutput, RecipeInput, PMLInput, - PMLOutput, MOBIOutput, LRFOutput] + PMLOutput, MOBIOutput, LRFOutput, LITOutput] plugins += [PRS500, PRS505, PRS700, CYBOOKG3, KINDLE, KINDLE2, BLACKBERRY, EB600, JETBOOK] plugins += [x for x in list(locals().values()) if isinstance(x, type) and \ diff --git a/src/calibre/ebooks/lit/output.py b/src/calibre/ebooks/lit/output.py new file mode 100644 index 0000000000..42be1ecac7 --- /dev/null +++ b/src/calibre/ebooks/lit/output.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import with_statement + +__license__ = 'GPL v3' +__copyright__ = '2009, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + + +from calibre.customize.conversion import OutputFormatPlugin + +class LITOutput(OutputFormatPlugin): + + name = 'LIT Output' + author = 'Marshall T. Vandegrift' + file_type = 'lit' + + def convert(self, oeb, output_path, input_plugin, opts, log): + self.log, self.opts, self.oeb = log, opts, oeb + from calibre.ebooks.oeb.transforms.manglecase import CaseMangler + from calibre.ebooks.oeb.transforms.rasterize import SVGRasterizer + from calibre.ebooks.oeb.transforms.htmltoc import HTMLTOCAdder + from calibre.ebooks.lit.writer import LitWriter + tocadder = HTMLTOCAdder() + tocadder(oeb, opts) + mangler = CaseMangler() + mangler(oeb, opts) + rasterizer = SVGRasterizer() + rasterizer(oeb, opts) + lit = LitWriter() + lit(oeb, output_path) + + diff --git a/src/calibre/ebooks/lit/writer.py b/src/calibre/ebooks/lit/writer.py index 73216057b5..a3ab345c0a 100644 --- a/src/calibre/ebooks/lit/writer.py +++ b/src/calibre/ebooks/lit/writer.py @@ -6,8 +6,6 @@ from __future__ import with_statement __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' -import sys -import os from cStringIO import StringIO from struct import pack from itertools import izip, count, chain @@ -17,7 +15,6 @@ import re import copy import uuid import functools -import logging from urlparse import urldefrag from urllib import unquote as urlunquote from lxml import etree @@ -25,22 +22,14 @@ from calibre.ebooks.lit.reader import DirectoryEntry import calibre.ebooks.lit.maps as maps from calibre.ebooks.oeb.base import OEB_DOCS, XHTML_MIME, OEB_STYLES, \ CSS_MIME, OPF_MIME, XML_NS, XML -from calibre.ebooks.oeb.base import namespace, barename, prefixname, \ - urlnormalize, xpath -from calibre.ebooks.oeb.base import OEBBook -from calibre.ebooks.oeb.profile import Context +from calibre.ebooks.oeb.base import prefixname, \ + urlnormalize from calibre.ebooks.oeb.stylizer import Stylizer -from calibre.ebooks.oeb.transforms.flatcss import CSSFlattener -from calibre.ebooks.oeb.transforms.rasterize import SVGRasterizer -from calibre.ebooks.oeb.transforms.trimmanifest import ManifestTrimmer -from calibre.ebooks.oeb.transforms.htmltoc import HTMLTOCAdder -from calibre.ebooks.oeb.transforms.manglecase import CaseMangler from calibre.ebooks.lit.lzx import Compressor import calibre from calibre import plugins msdes, msdeserror = plugins['msdes'] import calibre.ebooks.lit.mssha1 as mssha1 -from calibre.customize.ui import run_plugins_on_postprocess __all__ = ['LitWriter'] @@ -144,7 +133,7 @@ def warn(x): class ReBinary(object): NSRMAP = {'': None, XML_NS: 'xml'} - + def __init__(self, root, item, oeb, map=HTML_MAP): self.item = item self.logger = oeb.logger @@ -168,7 +157,7 @@ class ReBinary(object): def is_block(self, style): return style['display'] not in ('inline', 'inline-block') - + def tree_to_binary(self, elem, nsrmap=NSRMAP, parents=[], inhead=False, preserve=False): if not isinstance(elem.tag, basestring): @@ -277,7 +266,7 @@ class ReBinary(object): def build_ahc(self): if len(self.anchors) > 6: - self.logger.log_warn("More than six anchors in file %r. " \ + self.logger.warn("More than six anchors in file %r. " \ "Some links may not work properly." % self.item.href) data = StringIO() data.write(unichr(len(self.anchors)).encode('utf-8')) @@ -300,7 +289,7 @@ def preserve(function): self._stream.seek(opos) functools.update_wrapper(wrapper, function) return wrapper - + class LitWriter(object): def __init__(self): # Wow, no options @@ -308,7 +297,7 @@ class LitWriter(object): def _litize_oeb(self): oeb = self._oeb - oeb.metadata.add('calibre-oeb2lit-version', calibre.__version__) + oeb.metadata.add('calibre-version', calibre.__version__) cover = None if oeb.metadata.cover: id = str(oeb.metadata.cover[0]) @@ -319,12 +308,12 @@ class LitWriter(object): else: self._logger.warn('No suitable cover image found.') - def dump(self, oeb, path): + def __call__(self, oeb, path): if hasattr(path, 'write'): return self._dump_stream(oeb, path) with open(path, 'w+b') as stream: return self._dump_stream(oeb, stream) - + def _dump_stream(self, oeb, stream): self._oeb = oeb self._logger = oeb.logger @@ -334,7 +323,7 @@ class LitWriter(object): self._meta = None self._litize_oeb() self._write_content() - + def _write(self, *data): for datum in data: self._stream.write(datum) @@ -346,7 +335,7 @@ class LitWriter(object): def _tell(self): return self._stream.tell() - + def _write_content(self): # Build content sections self._build_sections() @@ -414,13 +403,13 @@ class LitWriter(object): self._write(cchunk, filler, pack('