1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-20 13:11:27 +02:00

Removing couple of "buildins" polyglot types

This commit is contained in:
2020-04-20 20:22:50 +02:00
parent eac0b98d6f
commit c867f0321b
36 changed files with 85 additions and 109 deletions
+2 -3
View File
@@ -13,7 +13,6 @@ from ebook_converter.utils.zipfile import ZipFile
from ebook_converter import (extract, walk, isbytestring, filesystem_encoding,
get_types_map)
from ebook_converter.constants import __version__
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3'
@@ -1023,7 +1022,7 @@ OptionRecommendation(name='search_replace',
def dump_input(self, ret, output_dir):
out_dir = os.path.join(self.opts.debug_pipeline, 'input')
if isinstance(ret, string_or_bytes):
if isinstance(ret, (str, bytes)):
shutil.copytree(output_dir, out_dir)
else:
if not os.path.exists(out_dir):
@@ -1216,7 +1215,7 @@ OptionRecommendation(name='search_replace',
transform_css_rules = ()
if self.opts.transform_css_rules:
transform_css_rules = self.opts.transform_css_rules
if isinstance(transform_css_rules, string_or_bytes):
if isinstance(transform_css_rules, (str, bytes)):
transform_css_rules = json.loads(transform_css_rules)
flattener = CSSFlattener(fbase=fbase, fkey=fkey,
lineh=line_height,
+2 -2
View File
@@ -2,7 +2,7 @@ import re, random, unicodedata, numbers
from collections import namedtuple
from contextlib import contextmanager
from math import ceil, sqrt, cos, sin, atan2
from ebook_converter.polyglot.builtins import iteritems, itervalues, string_or_bytes
from ebook_converter.polyglot.builtins import iteritems, itervalues
from itertools import chain
from PyQt5.Qt import (
@@ -270,7 +270,7 @@ def format_fields(mi, prefs):
@contextmanager
def preserve_fields(obj, fields):
if isinstance(fields, string_or_bytes):
if isinstance(fields, (str, bytes)):
fields = fields.split()
null = object()
mem = {f:getattr(obj, f, null) for f in fields}
@@ -11,7 +11,6 @@ from ebook_converter.ebooks.docx.writer.lists import ListsManager
from ebook_converter.ebooks.oeb.stylizer import Stylizer as Sz, Style as St
from ebook_converter.ebooks.oeb.base import XPath, barename
from ebook_converter.utils.localization import lang_as_iso639_1
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3'
@@ -527,7 +526,7 @@ class Convert(object):
self.add_block_tag(tagname, html_tag, tag_style, stylizer, float_spec=float_spec)
for child in html_tag.iterchildren():
if isinstance(getattr(child, 'tag', None), string_or_bytes):
if isinstance(getattr(child, 'tag', None), (str, bytes)):
self.process_tag(child, stylizer, float_spec=float_spec)
else: # Comment/PI/etc.
tail = getattr(child, 'tail', None)
+2 -3
View File
@@ -15,7 +15,6 @@ from ebook_converter.utils.localization import lang_as_iso639_1
from ebook_converter.utils.xml_parse import safe_xml_fromstring
from ebook_converter.utils.img import save_cover_data_to
from ebook_converter.ebooks.oeb.base import urlnormalize
from ebook_converter.polyglot.builtins import string_or_bytes
from ebook_converter.polyglot.binary import as_base64_unicode
@@ -408,9 +407,9 @@ class FB2MLizer(object):
elem = elem_tree
# Ensure what we are converting is not a string and that the fist tag is part of the XHTML namespace.
if not isinstance(elem_tree.tag, string_or_bytes) or namespace(elem_tree.tag) != XHTML_NS:
if not isinstance(elem_tree.tag, (str, bytes)) or namespace(elem_tree.tag) != XHTML_NS:
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail:
return [elem.tail]
return []
+8 -8
View File
@@ -13,7 +13,7 @@ from ebook_converter.ebooks.oeb.base import (
XHTML, XHTML_NS, SVG_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize)
from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.utils.logging import default_log
from ebook_converter.polyglot.builtins import string_or_bytes, as_bytes
from ebook_converter.polyglot.builtins import as_bytes
__license__ = 'GPL 3'
@@ -94,7 +94,7 @@ class OEB2HTML(object):
for el in root.iter():
attribs = el.attrib
try:
if not isinstance(el.tag, string_or_bytes):
if not isinstance(el.tag, (str, bytes)):
continue
except:
continue
@@ -155,10 +155,10 @@ class OEB2HTMLNoCSSizer(OEB2HTML):
'''
# We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in (XHTML_NS, SVG_NS):
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
and elem.tail:
return [elem.tail]
return ['']
@@ -244,10 +244,10 @@ class OEB2HTMLInlineCSSizer(OEB2HTML):
'''
# We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in (XHTML_NS, SVG_NS):
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
and elem.tail:
return [elem.tail]
return ['']
@@ -347,10 +347,10 @@ class OEB2HTMLClassCSSizer(OEB2HTML):
'''
# We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in (XHTML_NS, SVG_NS):
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
and elem.tail:
return [elem.tail]
return ['']
@@ -37,7 +37,7 @@ from ebook_converter.ebooks.lrf.pylrs.pylrs import (
RuledLine, Span, Sub, Sup, TextBlock
)
from ebook_converter.ptempfile import PersistentTemporaryFile
from ebook_converter.polyglot.builtins import getcwd, itervalues, string_or_bytes
from ebook_converter.polyglot.builtins import getcwd, itervalues
from ebook_converter.polyglot.urllib import unquote
from PIL import Image as PILImage
@@ -1118,7 +1118,7 @@ class HTMLConverter(object):
ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth'])) / 2)
for prop in ('topskip', 'footskip', 'sidemargin'):
if isinstance(ans[prop], string_or_bytes):
if isinstance(ans[prop], (str, bytes)):
ans[prop] = int(ans[prop])
if ans[prop] < 0:
ans[prop] = 0
+2 -2
View File
@@ -4,7 +4,7 @@ from ebook_converter.ebooks.lrf.fonts import get_font
from ebook_converter.ebooks.lrf.pylrs.pylrs import TextBlock, Text, CR, Span, \
CharButton, Plot, Paragraph, \
LrsTextTag
from ebook_converter.polyglot.builtins import string_or_bytes, native_string_type
from ebook_converter.polyglot.builtins import native_string_type
__license__ = 'GPL v3'
@@ -40,7 +40,7 @@ def tokens(tb):
yield 2, None
elif isinstance(x, Text):
yield x.text, cattrs(attrs, {})
elif isinstance(x, string_or_bytes):
elif isinstance(x, (str, bytes)):
yield x, cattrs(attrs, {})
elif isinstance(x, (CharButton, LrsTextTag)):
if x.contents:
+1 -2
View File
@@ -1,7 +1,6 @@
"""
elements.py -- replacements and helpers for ElementTree
"""
from ebook_converter.polyglot.builtins import string_or_bytes
class ElementWriter(object):
@@ -25,7 +24,7 @@ class ElementWriter(object):
def _writeAttribute(self, f, name, value):
f.write(' %s="' % str(name))
if not isinstance(value, string_or_bytes):
if not isinstance(value, (str, bytes)):
value = str(value)
value = self._encodeCdata(value)
value = value.replace('"', '&quot;')
+2 -2
View File
@@ -9,7 +9,7 @@ import codecs
import os
from .pylrfopt import tagListOptimizer
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes
from ebook_converter.polyglot.builtins import iteritems
PYLRF_VERSION = "1.0"
@@ -397,7 +397,7 @@ class LrfTag(object):
for f in self.format:
if isinstance(f, dict):
p = f[p]
elif isinstance(f, string_or_bytes):
elif isinstance(f, (str, bytes)):
if isinstance(p, tuple):
writeString(lrf, struct.pack(f, *p))
else:
+8 -8
View File
@@ -51,7 +51,7 @@ DEFAULT_GENREADING = "fs" # default is yes to both lrf and lrs
from ebook_converter import __appname__, __version__
from ebook_converter import entity_to_unicode
from ebook_converter.polyglot.builtins import string_or_bytes, iteritems, native_string_type
from ebook_converter.polyglot.builtins import iteritems, native_string_type
class LrsError(Exception):
@@ -90,7 +90,7 @@ def ElementWithReading(tag, text, reading=False):
if text is None:
readingText = ""
elif isinstance(text, string_or_bytes):
elif isinstance(text, (str, bytes)):
readingText = text
else:
# assumed to be a sequence of (name, sortas)
@@ -148,7 +148,7 @@ class Delegator(object):
"""
for setting in d.getSettings():
if isinstance(setting, string_or_bytes):
if isinstance(setting, (str, bytes)):
setting = (d, setting)
delegates = \
self.delegatedSettingsDict.setdefault(setting[1], [])
@@ -286,7 +286,7 @@ class LrsContainer(object):
(content.__class__.__name__,
self.__class__.__name__))
if convertText and isinstance(content, string_or_bytes):
if convertText and isinstance(content, (str, bytes)):
content = Text(content)
content.setParent(self)
@@ -580,14 +580,14 @@ class Book(Delegator):
ts.attrs['baselineskip'] = rescale(ts.attrs['baselineskip'])
def renderLrs(self, lrsFile, encoding="UTF-8"):
if isinstance(lrsFile, string_or_bytes):
if isinstance(lrsFile, (str, bytes)):
lrsFile = codecs.open(lrsFile, "wb", encoding=encoding)
self.render(lrsFile, outputEncodingName=encoding)
lrsFile.close()
def renderLrf(self, lrfFile):
self.appendReferencedObjects(self)
if isinstance(lrfFile, string_or_bytes):
if isinstance(lrfFile, (str, bytes)):
lrfFile = open(lrfFile, "wb")
lrfWriter = LrfWriter(self.sourceencoding)
@@ -1488,7 +1488,7 @@ class Paragraph(LrsContainer):
LrsContainer.__init__(self, [Text, CR, DropCaps, CharButton,
LrsSimpleChar1, bytes, str])
if text is not None:
if isinstance(text, string_or_bytes):
if isinstance(text, (str, bytes)):
text = Text(text)
self.append(text)
@@ -1807,7 +1807,7 @@ class Span(LrsSimpleChar1, LrsContainer):
def __init__(self, text=None, **attrs):
LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, str])
if text is not None:
if isinstance(text, string_or_bytes):
if isinstance(text, (str, bytes)):
text = Text(text)
self.append(text)
+2 -2
View File
@@ -6,7 +6,7 @@ import re
from ebook_converter import force_unicode
from ebook_converter.ebooks.metadata import MetaInformation
from ebook_converter.polyglot.builtins import string_or_bytes, int_to_byte
from ebook_converter.polyglot.builtins import int_to_byte
title_pat = re.compile(br'\{\\info.*?\{\\title(.*?)(?<!\\)\}', re.DOTALL)
author_pat = re.compile(br'\{\\info.*?\{\\author(.*?)(?<!\\)\}', re.DOTALL)
@@ -156,7 +156,7 @@ def create_metadata(stream, options):
md.append(r'{\title %s}'%(title,))
if options.authors:
au = options.authors
if not isinstance(au, string_or_bytes):
if not isinstance(au, (str, bytes)):
au = ', '.join(au)
author = encode(au)
md.append(r'{\author %s}'%(author,))
+2 -2
View File
@@ -12,7 +12,7 @@ from ebook_converter.ebooks.metadata.book.base import Metadata
from ebook_converter.ebooks.metadata.opf2 import dump_dict
from ebook_converter.utils.date import parse_date, isoformat, now
from ebook_converter.utils.localization import canonicalize_lang, lang_as_iso639_1
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -477,7 +477,7 @@ def metadata_to_xmp_packet(mi):
'authors':('dc:creator', True), 'tags':('dc:subject', False), 'publisher':('dc:publisher', False),
}):
val = mi.get(prop) or ()
if isinstance(val, string_or_bytes):
if isinstance(val, (str, bytes)):
val = [val]
create_sequence_property(dc, tag, val, ordered)
if not mi.is_null('pubdate'):
+4 -5
View File
@@ -11,7 +11,6 @@ from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.ebooks.oeb.transforms.flatcss import KeyMapper
from ebook_converter.ebooks.mobi.utils import convert_color_for_font_tag
from ebook_converter.utils.imghdr import identify
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3'
@@ -143,7 +142,7 @@ class MobiMLizer(object):
return self.fnums[self.fmap[ptsize]]
def mobimlize_measure(self, ptsize):
if isinstance(ptsize, string_or_bytes):
if isinstance(ptsize, (str, bytes)):
return ptsize
embase = self.profile.fbase
if round(ptsize) < embase:
@@ -186,7 +185,7 @@ class MobiMLizer(object):
parent = bstate.nested[-1] if bstate.nested else bstate.body
indent = istate.indent
left = istate.left
if isinstance(indent, string_or_bytes):
if isinstance(indent, (str, bytes)):
indent = 0
if indent < 0 and abs(indent) < left:
left += indent
@@ -307,7 +306,7 @@ class MobiMLizer(object):
inline = bstate.inline
content = self.preize_text(text, pre_wrap=istate.pre_wrap) if istate.preserve or istate.pre_wrap else [text]
for item in content:
if isinstance(item, string_or_bytes):
if isinstance(item, (str, bytes)):
if len(inline) == 0:
inline.text = (inline.text or '') + item
else:
@@ -318,7 +317,7 @@ class MobiMLizer(object):
def mobimlize_elem(self, elem, stylizer, bstate, istates,
ignore_valign=False):
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) != XHTML_NS:
return
style = stylizer.style(elem)
@@ -9,7 +9,6 @@ from ebook_converter.ebooks.mobi.utils import is_guide_ref_start
from ebook_converter.ebooks.oeb.base import (
OEB_DOCS, XHTML, XHTML_NS, XML_NS, namespace, prefixname, urlnormalize
)
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3'
@@ -306,7 +305,7 @@ class Serializer(object):
def serialize_elem(self, elem, item, nsrmap=NSRMAP):
buf = self.buf
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in nsrmap:
return
tag = prefixname(elem.tag, nsrmap)
+2 -2
View File
@@ -15,7 +15,7 @@ from odf.namespaces import TEXTNS as odTEXTNS
from ebook_converter import CurrentDir, walk
from ebook_converter.ebooks.oeb.base import _css_logger
from ebook_converter.utils.xml_parse import safe_xml_fromstring
from ebook_converter.polyglot.builtins import string_or_bytes, getcwd, as_bytes
from ebook_converter.polyglot.builtins import getcwd, as_bytes
__license__ = 'GPL v3'
@@ -250,7 +250,7 @@ class Extract(ODF2XHTML):
# first load the odf structure
self.lines = []
self._wfunc = self._wlines
if isinstance(odffile, string_or_bytes) \
if isinstance(odffile, (str, bytes)) \
or hasattr(odffile, 'read'): # Added by Kovid
self.document = odLoad(odffile)
else:
+3 -3
View File
@@ -17,7 +17,7 @@ from ebook_converter import (isbytestring, as_unicode, get_types_map)
from ebook_converter.ebooks.oeb.parse_utils import barename, XHTML_NS, namespace, XHTML, parse_html, NotHTML
from ebook_converter.utils.cleantext import clean_xml_chars
from ebook_converter.utils.short_uuid import uuid4
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes, itervalues
from ebook_converter.polyglot.builtins import iteritems, itervalues
from ebook_converter.polyglot.urllib import unquote as urlunquote
@@ -1035,7 +1035,7 @@ class Manifest(object):
mt = self.media_type.lower()
except Exception:
mt = 'application/octet-stream'
if not isinstance(data, string_or_bytes):
if not isinstance(data, (str, bytes)):
pass # already parsed
elif mt in OEB_DOCS:
data = self._parse_xhtml(data)
@@ -1296,7 +1296,7 @@ class Spine(object):
self.page_progression_direction = None
def _linear(self, linear):
if isinstance(linear, string_or_bytes):
if isinstance(linear, (str, bytes)):
linear = linear.lower()
if linear is None or linear in ('yes', 'true'):
linear = True
+3 -3
View File
@@ -5,7 +5,7 @@ from css_parser.css import PropertyValue
from css_parser import profile as cssprofiles, CSSParser
from ebook_converter.tinycss.fonts3 import parse_font, serialize_font_family
from ebook_converter.ebooks.oeb.base import css_text
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -125,10 +125,10 @@ def normalize_font(cssvalue, font_family_as_list=False):
ans = {k:DEFAULTS[k] for k in composition}
ans.update(parse_font(val))
if font_family_as_list:
if isinstance(ans['font-family'], string_or_bytes):
if isinstance(ans['font-family'], (str, bytes)):
ans['font-family'] = [x.strip() for x in ans['font-family'].split(',')]
else:
if not isinstance(ans['font-family'], string_or_bytes):
if not isinstance(ans['font-family'], (str, bytes)):
ans['font-family'] = serialize_font_family(ans['font-family'])
return ans
+3 -3
View File
@@ -6,7 +6,7 @@ from ebook_converter import xml_replace_entities, force_unicode
from ebook_converter.utils.xml_parse import safe_xml_fromstring
from ebook_converter.constants import filesystem_encoding
from ebook_converter.ebooks.chardet import xml_to_unicode, strip_encoding_declarations
from ebook_converter.polyglot.builtins import iteritems, itervalues, string_or_bytes
from ebook_converter.polyglot.builtins import iteritems, itervalues
__license__ = 'GPL v3'
@@ -96,7 +96,7 @@ def html5_parse(data, max_nesting_depth=100):
# Check that the asinine HTML 5 algorithm did not result in a tree with
# insane nesting depths
for x in data.iterdescendants():
if isinstance(x.tag, string_or_bytes) and not len(x): # Leaf node
if isinstance(x.tag, (str, bytes)) and not len(x): # Leaf node
depth = node_depth(x)
if depth > max_nesting_depth:
raise ValueError('HTML 5 parsing resulted in a tree with nesting'
@@ -294,7 +294,7 @@ def parse_html(data, log=None, decoder=None, preprocessor=None,
nroot = etree.Element(XHTML('html'),
nsmap={None: XHTML_NS}, attrib=attrib)
for elem in data.iterdescendants():
if isinstance(elem.tag, string_or_bytes) and \
if isinstance(elem.tag, (str, bytes)) and \
namespace(elem.tag) == ns:
elem.tag = XHTML(barename(elem.tag))
for elem in data:
+3 -4
View File
@@ -1,5 +1,4 @@
import copy, os, re
from ebook_converter.polyglot.builtins import string_or_bytes
import urllib.parse
from ebook_converter.ebooks.oeb.base import barename, XPNSMAP, XPath, OPF, XHTML, OEB_DOCS
@@ -393,9 +392,9 @@ def merge_html(container, names, master, insert_page_breaks=False):
first_child = ''
for first_child in children:
if not isinstance(first_child, string_or_bytes):
if not isinstance(first_child, (str, bytes)):
break
if isinstance(first_child, string_or_bytes):
if isinstance(first_child, (str, bytes)):
# body contained only text, no tags
first_child = body.makeelement(XHTML('p'))
first_child.text, children[0] = children[0], first_child
@@ -431,7 +430,7 @@ def merge_html(container, names, master, insert_page_breaks=False):
a.set('href', '#' + amap[q])
for child in children:
if isinstance(child, string_or_bytes):
if isinstance(child, (str, bytes)):
add_text(master_body, child)
else:
master_body.append(copy.deepcopy(child))
@@ -16,7 +16,7 @@ from ebook_converter.ebooks.oeb.base import (XHTML, XHTML_NS, CSS_MIME, OEB_STYL
from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.utils.filenames import ascii_filename, ascii_text
from ebook_converter.utils.icu import numeric_sort_key
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -351,7 +351,7 @@ class CSSFlattener(object):
cssdict[property] = "%0.5fem" % (value / fsize)
def flatten_node(self, node, stylizer, names, styles, pseudo_styles, psize, item_id, recurse=True):
if not isinstance(node.tag, string_or_bytes) \
if not isinstance(node.tag, (str, bytes)) \
or namespace(node.tag) != XHTML_NS:
return
tag = barename(node.tag)
@@ -6,7 +6,6 @@ from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS
from ebook_converter.ebooks.oeb.base import CSS_MIME
from ebook_converter.ebooks.oeb.base import namespace
from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3'
@@ -95,7 +94,7 @@ class CaseMangler(object):
last = child
def mangle_elem(self, elem, stylizer):
if not isinstance(elem.tag, string_or_bytes) or \
if not isinstance(elem.tag, (str, bytes)) or \
namespace(elem.tag) != XHTML_NS:
return
children = list(elem)
+1 -2
View File
@@ -5,7 +5,6 @@ Read the header data from a pdb file.
import re
import struct
import time
from ebook_converter.polyglot.builtins import long_type
__license__ = 'GPL v3'
@@ -83,6 +82,6 @@ class PdbHeaderBuilder(object):
offset = 78 + (8 * nrecords) + 2
for id, record in enumerate(section_lengths):
out_stream.write(struct.pack('>LBBBB', long_type(offset), 0, 0, 0, 0))
out_stream.write(struct.pack('>LBBBB', int(offset), 0, 0, 0, 0))
offset += record
out_stream.write(b'\x00\x00')
+2 -3
View File
@@ -8,7 +8,6 @@ from functools import partial
from ebook_converter.ebooks.htmlz.oeb2html import OEB2HTML
from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, rewrite_links
from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3'
@@ -110,10 +109,10 @@ class MarkdownMLizer(OEB2HTML):
'''
# We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) != XHTML_NS:
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail:
return [elem.tail]
return ['']
+2 -2
View File
@@ -8,7 +8,7 @@ from ebook_converter.ebooks.metadata.opf2 import OPFCreator
from ebook_converter.ebooks.conversion.preprocess import DocAnalysis
from ebook_converter.utils.cleantext import clean_ascii_chars
from ebook_converter.polyglot.builtins import iteritems, long_type
from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3'
@@ -62,7 +62,7 @@ def split_txt(txt, epub_split_size_kb=0):
txt = txt.encode('utf-8')
length_byte = len(txt)
# Calculating the average chunk value for easy splitting as EPUB (+2 as a safe margin)
chunk_size = long_type(length_byte / (int(length_byte / (epub_split_size_kb * 1024)) + 2))
chunk_size = int(length_byte / (int(length_byte / (epub_split_size_kb * 1024)) + 2))
# if there are chunks with a superior size then go and break
parts = txt.split(b'\n\n')
lengths = tuple(map(len, parts))
+2 -3
View File
@@ -10,7 +10,6 @@ from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace
from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.ebooks import unit_convert
from ebook_converter.ebooks.textile.unsmarten import unsmarten
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3'
@@ -225,10 +224,10 @@ class TextileMLizer(OEB2HTML):
'''
# We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) != XHTML_NS:
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail:
return [elem.tail]
return ['']
+2 -3
View File
@@ -4,7 +4,6 @@ Transform OEB content into plain text
import re
from lxml import etree
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3'
@@ -189,10 +188,10 @@ class TXTMLizer(object):
'''
from ebook_converter.ebooks.oeb.base import XHTML_NS, barename, namespace
if not isinstance(elem.tag, string_or_bytes) \
if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) != XHTML_NS:
p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail:
return [elem.tail]
return ['']