mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-02-22 10:05:47 +01:00
Removing is_py3 method and duplicated by urllib.
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
CHM File decoding support
|
||||
"""
|
||||
import os
|
||||
from lxml import html
|
||||
|
||||
from ebook_converter.polyglot.urllib import unquote as _unquote
|
||||
from ebook_converter.ebooks.oeb.base import urlquote
|
||||
from ebook_converter.ebooks.chardet import xml_to_unicode
|
||||
from ebook_converter.customize.conversion import InputFormatPlugin
|
||||
from ebook_converter.ptempfile import TemporaryDirectory
|
||||
from ebook_converter.constants import filesystem_encoding
|
||||
@@ -109,10 +113,7 @@ class CHMInput(InputFormatPlugin):
|
||||
return oeb
|
||||
|
||||
def _create_html_root(self, hhcpath, log, encoding):
|
||||
from lxml import html
|
||||
from ebook_converter.polyglot.urllib import unquote as _unquote
|
||||
from ebook_converter.ebooks.oeb.base import urlquote
|
||||
from ebook_converter.ebooks.chardet import xml_to_unicode
|
||||
|
||||
hhcdata = self._read_file(hhcpath)
|
||||
hhcdata = hhcdata.decode(encoding)
|
||||
hhcdata = xml_to_unicode(hhcdata, verbose=True,
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import os, shutil, re
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import urllib.parse
|
||||
|
||||
from ebook_converter.customize.conversion import (OutputFormatPlugin,
|
||||
OptionRecommendation)
|
||||
@@ -514,7 +517,7 @@ class EPUBOutput(OutputFormatPlugin):
|
||||
'''
|
||||
Perform toc link transforms to alleviate slow loading.
|
||||
'''
|
||||
from ebook_converter.ebooks.oeb.base import urldefrag, XPath
|
||||
from ebook_converter.ebooks.oeb.base import XPath
|
||||
from ebook_converter.ebooks.oeb.polish.toc import item_at_top
|
||||
|
||||
def frag_is_at_top(root, frag):
|
||||
@@ -527,7 +530,7 @@ class EPUBOutput(OutputFormatPlugin):
|
||||
|
||||
def simplify_toc_entry(toc):
|
||||
if toc.href:
|
||||
href, frag = urldefrag(toc.href)
|
||||
href, frag = urllib.parse.urldefrag(toc.href)
|
||||
if frag:
|
||||
for x in self.oeb.spine:
|
||||
if x.href == href:
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import re, tempfile, os
|
||||
from functools import partial
|
||||
import functools
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
import urllib.parse
|
||||
|
||||
from ebook_converter.constants import islinux, isbsd
|
||||
from ebook_converter.customize.conversion import (InputFormatPlugin,
|
||||
@@ -97,7 +100,7 @@ class HTMLInput(InputFormatPlugin):
|
||||
import uuid
|
||||
from ebook_converter.ebooks.conversion.plumber import create_oebbook
|
||||
from ebook_converter.ebooks.oeb.base import (DirContainer,
|
||||
rewrite_links, urlnormalize, urldefrag, BINARY_MIME, OEB_STYLES,
|
||||
rewrite_links, urlnormalize, BINARY_MIME, OEB_STYLES,
|
||||
xpath, urlquote)
|
||||
from ebook_converter import guess_type
|
||||
from ebook_converter.ebooks.oeb.transforms.metadata import \
|
||||
@@ -163,7 +166,7 @@ class HTMLInput(InputFormatPlugin):
|
||||
path = path.lower()
|
||||
self.added_resources[path] = href
|
||||
self.urlnormalize, self.DirContainer = urlnormalize, DirContainer
|
||||
self.urldefrag = urldefrag
|
||||
self.urldefrag = urllib.parse.urldefrag
|
||||
self.guess_type, self.BINARY_MIME = guess_type, BINARY_MIME
|
||||
|
||||
self.log('Rewriting HTML links')
|
||||
@@ -176,7 +179,8 @@ class HTMLInput(InputFormatPlugin):
|
||||
item = oeb.manifest.hrefs[href]
|
||||
except KeyError:
|
||||
item = oeb.manifest.hrefs[urlnormalize(href)]
|
||||
rewrite_links(item.data, partial(self.resource_adder, base=dpath))
|
||||
rewrite_links(item.data,
|
||||
functools.partial(self.resource_adder, base=dpath))
|
||||
|
||||
for item in oeb.manifest.values():
|
||||
if item.media_type in self.OEB_STYLES:
|
||||
@@ -186,7 +190,7 @@ class HTMLInput(InputFormatPlugin):
|
||||
dpath = os.path.dirname(path)
|
||||
break
|
||||
css_parser.replaceUrls(item.data,
|
||||
partial(self.resource_adder, base=dpath))
|
||||
functools.partial(self.resource_adder, base=dpath))
|
||||
|
||||
toc = self.oeb.toc
|
||||
self.oeb.auto_generated_toc = True
|
||||
@@ -242,7 +246,6 @@ class HTMLInput(InputFormatPlugin):
|
||||
return link, frag
|
||||
|
||||
def resource_adder(self, link_, base=None):
|
||||
from ebook_converter.polyglot.urllib import quote
|
||||
link, frag = self.link_to_local_path(link_, base=base)
|
||||
if link is None:
|
||||
return link_
|
||||
@@ -287,9 +290,9 @@ class HTMLInput(InputFormatPlugin):
|
||||
# file, therefore we quote it here.
|
||||
if isinstance(bhref, unicode_type):
|
||||
bhref = bhref.encode('utf-8')
|
||||
item.html_input_href = as_unicode(quote(bhref))
|
||||
item.html_input_href = as_unicode(urllib.parse.quote(bhref))
|
||||
if guessed in self.OEB_STYLES:
|
||||
item.override_css_fetch = partial(
|
||||
item.override_css_fetch = functools.partial(
|
||||
self.css_import_handler, os.path.dirname(link))
|
||||
item.data
|
||||
self.added_resources[link] = href
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
import os, re, shutil
|
||||
from os.path import dirname, abspath, relpath as _relpath, exists, basename
|
||||
import os
|
||||
import pkg_resources
|
||||
import re
|
||||
import shutil
|
||||
|
||||
from lxml import etree
|
||||
|
||||
from ebook_converter.customize.conversion import OutputFormatPlugin, OptionRecommendation
|
||||
from ebook_converter import CurrentDir
|
||||
from ebook_converter.ptempfile import PersistentTemporaryDirectory
|
||||
from ebook_converter.customize.conversion import OutputFormatPlugin, OptionRecommendation
|
||||
from ebook_converter.ebooks.oeb.base import element
|
||||
from ebook_converter.polyglot.builtins import unicode_type
|
||||
|
||||
from ebook_converter.polyglot.urllib import unquote
|
||||
from ebook_converter.ptempfile import PersistentTemporaryDirectory
|
||||
from ebook_converter.utils.cleantext import clean_xml_chars
|
||||
|
||||
__license__ = 'GPL 3'
|
||||
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
|
||||
@@ -14,7 +19,7 @@ __docformat__ = 'restructuredtext en'
|
||||
|
||||
|
||||
def relpath(*args):
|
||||
return _relpath(*args).replace(os.sep, '/')
|
||||
return os.path.relpath(*args).replace(os.sep, '/')
|
||||
|
||||
|
||||
class HTMLOutput(OutputFormatPlugin):
|
||||
@@ -47,11 +52,7 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
'''
|
||||
Generate table of contents
|
||||
'''
|
||||
from lxml import etree
|
||||
from ebook_converter.polyglot.urllib import unquote
|
||||
|
||||
from ebook_converter.ebooks.oeb.base import element
|
||||
from ebook_converter.utils.cleantext import clean_xml_chars
|
||||
with CurrentDir(output_dir):
|
||||
def build_node(current_node, parent=None):
|
||||
if parent is None:
|
||||
@@ -60,7 +61,8 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
parent = element(parent, ('ul'))
|
||||
for node in current_node.nodes:
|
||||
point = element(parent, 'li')
|
||||
href = relpath(abspath(unquote(node.href)), dirname(ref_url))
|
||||
href = relpath(os.path.abspath(unquote(node.href)),
|
||||
os.path.dirname(ref_url))
|
||||
if isinstance(href, bytes):
|
||||
href = href.decode('utf-8')
|
||||
link = element(point, 'a', href=clean_xml_chars(href))
|
||||
@@ -131,10 +133,10 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
|
||||
tempdir = os.path.realpath(PersistentTemporaryDirectory())
|
||||
output_file = os.path.join(tempdir,
|
||||
basename(re.sub(r'\.zip', '', output_path)+'.html'))
|
||||
os.path.basename(re.sub(r'\.zip', '', output_path)+'.html'))
|
||||
output_dir = re.sub(r'\.html', '', output_file)+'_files'
|
||||
|
||||
if not exists(output_dir):
|
||||
if not os.path.exists(output_dir):
|
||||
os.makedirs(output_dir)
|
||||
|
||||
css_path = output_dir+os.sep+'calibreHtmlOutBasicCss.css'
|
||||
@@ -145,9 +147,10 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
html_toc = self.generate_html_toc(oeb_book, output_file, output_dir)
|
||||
templite = Templite(template_html_index_data)
|
||||
nextLink = oeb_book.spine[0].href
|
||||
nextLink = relpath(output_dir+os.sep+nextLink, dirname(output_file))
|
||||
cssLink = relpath(abspath(css_path), dirname(output_file))
|
||||
tocUrl = relpath(output_file, dirname(output_file))
|
||||
nextLink = relpath(output_dir+os.sep+nextLink,
|
||||
os.path.dirname(output_file))
|
||||
cssLink = relpath(os.path.abspath(css_path), os.path.dirname(output_file))
|
||||
tocUrl = relpath(output_file, os.path.dirname(output_file))
|
||||
t = templite.render(has_toc=bool(oeb_book.toc.count()),
|
||||
toc=html_toc, meta=meta, nextLink=nextLink,
|
||||
tocUrl=tocUrl, cssLink=cssLink,
|
||||
@@ -158,9 +161,9 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
|
||||
with CurrentDir(output_dir):
|
||||
for item in oeb_book.manifest:
|
||||
path = abspath(unquote(item.href))
|
||||
dir = dirname(path)
|
||||
if not exists(dir):
|
||||
path = os.path.abspath(unquote(item.href))
|
||||
dir = os.path.dirname(path)
|
||||
if not os.path.exists(dir):
|
||||
os.makedirs(dir)
|
||||
if item.spine_position is not None:
|
||||
with open(path, 'wb') as f:
|
||||
@@ -171,8 +174,8 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
item.unload_data_from_memory(memory=path)
|
||||
|
||||
for item in oeb_book.spine:
|
||||
path = abspath(unquote(item.href))
|
||||
dir = dirname(path)
|
||||
path = os.path.abspath(unquote(item.href))
|
||||
dir = os.path.dirname(path)
|
||||
root = item.data.getroottree()
|
||||
|
||||
# get & clean HTML <HEAD>-data
|
||||
@@ -191,18 +194,18 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
# generate link to next page
|
||||
if item.spine_position+1 < len(oeb_book.spine):
|
||||
nextLink = oeb_book.spine[item.spine_position+1].href
|
||||
nextLink = relpath(abspath(nextLink), dir)
|
||||
nextLink = relpath(os.path.abspath(nextLink), dir)
|
||||
else:
|
||||
nextLink = None
|
||||
|
||||
# generate link to previous page
|
||||
if item.spine_position > 0:
|
||||
prevLink = oeb_book.spine[item.spine_position-1].href
|
||||
prevLink = relpath(abspath(prevLink), dir)
|
||||
prevLink = relpath(os.path.abspath(prevLink), dir)
|
||||
else:
|
||||
prevLink = None
|
||||
|
||||
cssLink = relpath(abspath(css_path), dir)
|
||||
cssLink = relpath(os.path.abspath(css_path), dir)
|
||||
tocUrl = relpath(output_file, dir)
|
||||
firstContentPageLink = oeb_book.spine[0].href
|
||||
|
||||
@@ -222,8 +225,8 @@ class HTMLOutput(OutputFormatPlugin):
|
||||
item.unload_data_from_memory(memory=path)
|
||||
|
||||
zfile = zipfile.ZipFile(output_path, "w")
|
||||
zfile.add_dir(output_dir, basename(output_dir))
|
||||
zfile.write(output_file, basename(output_file), zipfile.ZIP_DEFLATED)
|
||||
zfile.add_dir(output_dir, os.path.basename(output_dir))
|
||||
zfile.write(output_file, os.path.basename(output_file), zipfile.ZIP_DEFLATED)
|
||||
|
||||
if opts.extract_to:
|
||||
if os.path.exists(opts.extract_to):
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import os, re
|
||||
import os
|
||||
import re
|
||||
|
||||
from lxml import etree
|
||||
|
||||
from ebook_converter.customize.conversion import (OutputFormatPlugin,
|
||||
OptionRecommendation)
|
||||
from ebook_converter import CurrentDir
|
||||
from ebook_converter.polyglot.urllib import unquote
|
||||
from ebook_converter.ebooks.oeb.base import OPF_MIME, NCX_MIME, PAGE_MAP_MIME, OEB_STYLES
|
||||
from ebook_converter.ebooks.oeb.normalize_css import condense_sheet
|
||||
|
||||
|
||||
__license__ = 'GPL 3'
|
||||
@@ -21,14 +26,10 @@ class OEBOutput(OutputFormatPlugin):
|
||||
recommendations = {('pretty_print', True, OptionRecommendation.HIGH)}
|
||||
|
||||
def convert(self, oeb_book, output_path, input_plugin, opts, log):
|
||||
from ebook_converter.polyglot.urllib import unquote
|
||||
from lxml import etree
|
||||
|
||||
self.log, self.opts = log, opts
|
||||
if not os.path.exists(output_path):
|
||||
os.makedirs(output_path)
|
||||
from ebook_converter.ebooks.oeb.base import OPF_MIME, NCX_MIME, PAGE_MAP_MIME, OEB_STYLES
|
||||
from ebook_converter.ebooks.oeb.normalize_css import condense_sheet
|
||||
with CurrentDir(output_path):
|
||||
results = oeb_book.to_opf2(page_map=True)
|
||||
for key in (OPF_MIME, NCX_MIME, PAGE_MAP_MIME):
|
||||
|
||||
Reference in New Issue
Block a user