def _hook_wave(self, book, appendix, container, code, argv): if code == EB_HOOK_BEGIN_WAVE: self._write_text('<a>') elif code == EB_HOOK_END_WAVE: start_offset = (argv[2] - 1) * EB_SIZE_PAGE + argv[3] end_offset = (argv[4] - 1) * EB_SIZE_PAGE + argv[5] data_size = int(end_offset - start_offset) subbook_id = str(eb_subbook(self.book)) page = int(argv[2]) offset = int(argv[3]) audio_id = _position_to_resource_id([page, offset, data_size]) uri = self.uri_dispatcher.uri('audio', subbook=subbook_id, audio=audio_id) self._write_text(u'</a><hack_attribs href=\"{0}\" />'.format(uri))
def _hook_color(self, book, appendix, container, code, argv): '''Unfortunately, displaying arbitrary images inline in base64 is too much to handle. Resort to a file cache directory. ''' extensions = { EB_HOOK_BEGIN_COLOR_BMP: 'png', EB_HOOK_BEGIN_IN_COLOR_BMP: 'png', EB_HOOK_BEGIN_COLOR_JPEG: 'jpg', EB_HOOK_BEGIN_IN_COLOR_JPEG: 'jpg', } _, _, page, offset = argv eb_set_binary_color_graphic(self.book, (page, offset)) extension = extensions[code] binary_parts = [] while True: chunk = eb_read_binary(self.book, 0) if len(chunk) == 0: break binary_parts.append(chunk) binary = ''.join(binary_parts) subbook_id = str(eb_subbook(self.book)) resource_id = _position_to_resource_id((page, offset)) img_path = self.uri_dispatcher.uri('image', subbook=subbook_id, image=resource_id, ext=extension) # for now if not path.exists(img_path): if extension == 'png': src, dst = StringIO(binary), StringIO() img = Image.open(src) img.save(img_path) elif extension == 'jpg': with open(img_path, 'wb') as file: file.write(binary) self._write_text(u'<div><img src="/{path}" /></div>'.format(path=img_path)) return EB_SUCCESS
def id(self): if self._heading_location: return _position_to_resource_id( list(self._heading_location) + list(self._text_location)) else: return _position_to_resource_id(list(self._text_location))
def end_reference(): subbook_id = str(eb_subbook(self.book)) entry_id = _position_to_resource_id([argv[1], argv[2]]) uri = self.uri_dispatcher.uri('entry', subbook=subbook_id, entry=entry_id) return u'</a><hack_attribs href=\"{0}\" rel=\"subsection\"/>'.format(uri)
def _write_text_anchor(self, book, position): subbook_id = str(eb_subbook(self.book)) entry_id = _position_to_resource_id(position) uri = self.uri_dispatcher.uri('entry', subbook=subbook_id, entry=entry_id) self._write_text(u'<a name=\"{0}\">'.format(uri))
def id(self): if self._heading_location: return _position_to_resource_id(list(self._heading_location) + list(self._text_location)) else: return _position_to_resource_id(list(self._text_location))