Skip to content

Commit

Permalink
store basedir in a global, for images
Browse files Browse the repository at this point in the history
  • Loading branch information
rupa committed Feb 8, 2012
1 parent 8bbfb33 commit 1f996fa
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

import curses.wrapper, curses.ascii
import formatter, htmllib, locale, os, StringIO, re, readline, zipfile
import base64, webbrowser
import base64, sys, webbrowser

from BeautifulSoup import BeautifulSoup

locale.setlocale(locale.LC_ALL, 'en_US.utf-8')

basedir = ''

def open_image(name, s):
''' open an image in webbrowser with a data url '''
ext = os.path.splitext(name)[1]
Expand All @@ -36,14 +38,14 @@ def open_image(name, s):
'.jpeg': 'image/jpeg',
'.png': 'image/png',
}[ext]
except KeyError as e:
except KeyError as ex:
return 'error: unrecognized extension {0}'.format(ext)
try:
webbrowser.open_new_tab('data:{0};base64,{1}'.format(
mime,
base64.b64encode(s)
))
except IOError as e:
except (IOError, OSError) as ex:
return 'error: {0}'.format(ex)

def textify(fl, html_snippet, img_size=(80, 45)):
Expand All @@ -52,7 +54,10 @@ class Parser(htmllib.HTMLParser):
def anchor_end(self):
self.anchor = None
def handle_image(self, source, alt, ismap, alight, width, height):
self.handle_data('[img="{0}" "{1}"]'.format(source, alt))
global basedir
self.handle_data(
'[img="{0}{1}" "{2}"]'.format(basedir, source, alt)
)

class Formatter(formatter.AbstractFormatter):
pass
Expand All @@ -70,6 +75,7 @@ def send_label_data(self, data):
return o.getvalue()

def table_of_contents(fl):
global basedir

# find opf file
soup = BeautifulSoup(fl.read('META-INF/container.xml'))
Expand Down

0 comments on commit 1f996fa

Please sign in to comment.