diff --git a/src/calibre/ebooks/htmlz/oeb2html.py b/src/calibre/ebooks/htmlz/oeb2html.py
index 9ee6f76449..11647043d3 100644
--- a/src/calibre/ebooks/htmlz/oeb2html.py
+++ b/src/calibre/ebooks/htmlz/oeb2html.py
@@ -62,24 +62,22 @@ class OEB2HTML(object):
self.links[aid] = 'calibre_link-%s' % len(self.links.keys())
return self.links[aid]
- def rewrite_links(self, tag, attribs, page):
+ def rewrite_link(self, tag, attribs, page):
# Rewrite ids.
if 'id' in attribs:
attribs['id'] = self.get_link_id(page.href, attribs['id'])
# Rewrite links.
- if tag == 'a':
- href = attribs['href']
- href = page.abshref(href)
+ if tag == 'a' and 'href' in attribs:
+ href = page.abshref(attribs['href'])
if self.url_is_relative(href):
- if '#' not in href:
- href += '#'
- if href not in self.links:
- self.links[href] = 'calibre_link-%s' % len(self.links.keys())
- href = '#%s' % self.links[href]
- attribs['href'] = href
+ id = ''
+ if '#' in href:
+ href, n, id = href.partition('#')
+ href = '#%s' % self.get_link_id(href, id)
+ attribs['href'] = href
return attribs
- def rewrite_images(self, tag, attribs, page):
+ def rewrite_image(self, tag, attribs, page):
if tag == 'img':
src = attribs.get('src', None)
if src:
@@ -131,6 +129,10 @@ class OEB2HTMLNoCSSizer(OEB2HTML):
tags = []
tag = barename(elem.tag)
attribs = elem.attrib
+
+ attribs = self.rewrite_link(tag, attribs, page)
+ attribs = self.rewrite_image(tag, attribs, page)
+
if tag == 'body':
tag = 'div'
attribs['id'] = self.get_link_id(page.href, '')
@@ -147,9 +149,6 @@ class OEB2HTMLNoCSSizer(OEB2HTML):
if 'style' in attribs:
del attribs['style']
- attribs = self.rewrite_links(tag, attribs, page)
- attribs = self.rewrite_images(tag, attribs, page)
-
# Turn the rest of the attributes into a string we can write with the tag.
at = ''
for k, v in attribs.items():
@@ -219,6 +218,9 @@ class OEB2HTMLInlineCSSizer(OEB2HTML):
tag = barename(elem.tag)
attribs = elem.attrib
+ attribs = self.rewrite_link(tag, attribs, page)
+ attribs = self.rewrite_image(tag, attribs, page)
+
style_a = '%s' % style
if tag == 'body':
tag = 'div'
@@ -233,9 +235,6 @@ class OEB2HTMLInlineCSSizer(OEB2HTML):
if 'style' in attribs:
del attribs['style']
- attribs = self.rewrite_links(tag, attribs, page)
- attribs = self.rewrite_images(tag, attribs, page)
-
# Turn the rest of the attributes into a string we can write with the tag.
at = ''
for k, v in attribs.items():
@@ -312,6 +311,9 @@ class OEB2HTMLClassCSSizer(OEB2HTML):
tag = barename(elem.tag)
attribs = elem.attrib
+ attribs = self.rewrite_link(tag, attribs, page)
+ attribs = self.rewrite_image(tag, attribs, page)
+
if tag == 'body':
tag = 'div'
attribs['id'] = self.get_link_id(page.href, '')
@@ -321,9 +323,6 @@ class OEB2HTMLClassCSSizer(OEB2HTML):
if 'style' in attribs:
del attribs['style']
- attribs = self.rewrite_links(tag, attribs, page)
- attribs = self.rewrite_images(tag, attribs, page)
-
# Turn the rest of the attributes into a string we can write with the tag.
at = ''
for k, v in attribs.items():
diff --git a/src/calibre/ebooks/txt/markdownml.py b/src/calibre/ebooks/txt/markdownml.py
index c179378049..fe76757eab 100644
--- a/src/calibre/ebooks/txt/markdownml.py
+++ b/src/calibre/ebooks/txt/markdownml.py
@@ -37,7 +37,7 @@ class MarkdownMLizer(object):
if not self.opts.keep_links:
html = re.sub(r'<\s*/*\s*a[^>]*>', '', html)
if not self.opts.keep_image_references:
- html = re.sub(r'<\s*img[^>]*>', '', html)\
+ html = re.sub(r'<\s*img[^>]*>', '', html)
text = html2text(html)
diff --git a/src/calibre/ebooks/txt/output.py b/src/calibre/ebooks/txt/output.py
index d021cbbba6..4e54a97b45 100644
--- a/src/calibre/ebooks/txt/output.py
+++ b/src/calibre/ebooks/txt/output.py
@@ -11,7 +11,7 @@ from lxml import etree
from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation
-from calibre.ebooks.oeb.base import OEB_IMAGES
+from calibre.ebooks.oeb.base import OEB_IMAGES
from calibre.ebooks.txt.txtml import TXTMLizer
from calibre.ebooks.txt.newlines import TxtNewlines, specified_newlines
from calibre.ptempfile import TemporaryDirectory, TemporaryFile