def set_title(self, title, auto_save = True): """Set the title : Find the right element and set the text """ title_el = self._get_title_el() old_title = title_el.text if title == old_title: #no change return title_el.text = title if self.href is not None: old_href = self.href new_filename = self.opf.find_free_filename(EPUBPackage.sanitize_for_filename(title), ".xhtml") dirname = os.path.dirname(self.opf.href) current_path = os.path.join(dirname, self.href) new_path = os.path.join(dirname, new_filename) if new_path != current_path: os.rename(current_path, new_path) #now set the href link properly self.opf.handle_item_renamed(old_href, new_filename) title_el.set("href", new_filename) self.opf.set_package_changed() if auto_save: self.opf.get_navigation().save()
def set_title(self, title, auto_save=True): """Set the title : Find the right element and set the text """ title_el = self._get_title_el() old_title = title_el.text if title == old_title: #no change return title_el.text = title if self.href is not None: old_href = self.href new_filename = self.opf.find_free_filename( EPUBPackage.sanitize_for_filename(title), ".xhtml") dirname = os.path.dirname(self.opf.href) current_path = os.path.join(dirname, self.href) new_path = os.path.join(dirname, new_filename) if new_path != current_path: os.rename(current_path, new_path) #now set the href link properly self.opf.handle_item_renamed(old_href, new_filename) title_el.set("href", new_filename) self.opf.set_package_changed() if auto_save: self.opf.get_navigation().save()
def get_idevice_el(self, idevice_type): """Returns a tuple with the directory which contains the idevice and it's etree element parsed""" from exe.engine.epubpackage import EPUBPackage idevice_dir = EPUBPackage.find_idevice_dir(idevice_type) if idevice_dir: root_el = etree.parse(idevice_dir/"idevice.xml").getroot() return (idevice_dir, root_el) else: return (None, None)
def createPackage(self): """ Creates a package """ log.debug(u"createPackage") package = EPUBPackage.load(G.application.config.configDir / "normal.epub") package.filename = "" package.name = self.createNewPackageName() package.id = str(uuid.uuid4()) self.loaded[package.name] = package return package
def add_required_files_to_package(self, required_files): for file in required_files: src_path = None if file[:len(EPUBResourceManager.PREFIX_COMMON_FILES)] == EPUBResourceManager.PREFIX_COMMON_FILES: rel_path = file[len(EPUBResourceManager.PREFIX_COMMON_FILES)+1:] src_path = os.path.join(G.application.config.webDir, "templates", rel_path) elif file[:len(EPUBResourceManager.PREFIX_IDEVICE_FILES)] == EPUBResourceManager.PREFIX_IDEVICE_FILES: idevice_trimmed = file[len(EPUBResourceManager.PREFIX_IDEVICE_FILES)+1:] sep_index = idevice_trimmed.index('/') idevice_type = idevice_trimmed[:sep_index] rel_path = idevice_trimmed[sep_index+1:] from exe.engine.epubpackage import EPUBPackage src_path = os.path.join(EPUBPackage.find_idevice_dir(idevice_type), rel_path) self.opf.add_file(src_path, file, auto_update = True)
def createPackage(self): """ Creates a package """ log.debug(u"createPackage") # Make up an initial unique name i = 1 name = u"newPackage" while name in self.loaded: name = u"newPackage" + unicode(i) i += 1 package = EPUBPackage.load(G.application.config.configDir/"normal.epub") package.filename = "" package.name = name package.id = str(uuid.uuid4()) self.loaded[package.name] = package return package
def render_POST(self, request=None): idevices = json.loads(request.args['idevices'][0]) if isinstance(idevices, dict): idevices = [idevices] if isinstance(self.package, EPUBPackage): for idevice in idevices: idevice_dir = EPUBPackage.find_idevice_dir(idevice['id']) idevice_xml_path = os.path.join(idevice_dir, "idevice.xml") idevice_xml = etree.parse(idevice_xml_path) ns = idevice_xml.getroot().nsmap.get(None) visible_str = "" if idevice['visible']: visible_str = "true" else: visible_str = "false" visible_el = idevice_xml.getroot().find(".//{%s}visible" % ns) visible_el.text = visible_str idevice_fd = open(idevice_xml_path, "w") idevice_fd.write(etree.tostring(idevice_xml.getroot(), encoding = "UTF-8", pretty_print = True)) idevice_fd.flush() idevice_fd.close() else: for idevice in idevices: prototype = self.prototypes[idevice['id']] visible = idevice['visible'] lower_title = prototype._title.lower() try: self.config.hiddeniDevices.remove(lower_title) self.config.configParser.delete('idevices', lower_title) except: pass if not visible: self.config.hiddeniDevices.append(lower_title) self.config.configParser.set('idevices', lower_title, '0') return json.dumps({'success': True})
def update_page(self, page_id, new_idevice_id = None, new_idevice_cssclass = None, new_idevice_type = None): """Regenerate script and link elements as they are required for the idevices on the page""" page_path = self._get_page_path(page_id) #According to the epub spec: contents MUST be XHTML not just HTML page_html_el = etree.parse(page_path).getroot() ns_xhtml = page_html_el.nsmap.get(None) #check and see if we need to add the div dom element for the new idevice if new_idevice_id is not None: from exe.engine.epubpackage import EPUBPackage idevice_container_el = EPUBPackage.get_idevice_container_el(page_html_el) idevice_div_el = etree.SubElement(idevice_container_el, "{%s}div" % ns_xhtml) idevice_div_el.set("id", "id%s" % new_idevice_id) idevice_div_el.set("class", new_idevice_cssclass) idevice_div_el.set("data-idevice-type", new_idevice_type) idevice_div_el.text = " " page_idevice_els = page_html_el.findall(".//{%s}*[@data-idevice-type]" % ns_xhtml) required_css = [] required_js = [] for idevice in page_idevice_els: idevice_type = idevice.get("data-idevice-type") idevice_def_el = self.get_idevice_el(idevice_type)[1] self.get_idevice_required_files(idevice_def_el, res_types=["css"], required_files = required_css) self.get_idevice_required_files(idevice_def_el, res_types=["script"], required_files = required_js) #now build the resource list, remove any existing generated script and link elements, add ones we need page_head_el = page_html_el.find("./{%s}head" % ns_xhtml) for auto_item in page_head_el.findall(".//{%s}*[@data-exeres='true']" % ns_xhtml): auto_item.getparent().remove(auto_item) for css_file in required_css: link_el = etree.SubElement(page_head_el, "{%s}link" % ns_xhtml) link_el.set("rel", "stylesheet") link_el.set("type", "text/css") link_el.set("href", css_file) link_el.set("data-exeres", "true") for js_script in required_js: script_el = etree.SubElement(page_head_el, "{%s}script" % ns_xhtml) script_el.set("src", js_script) script_el.set("type", "text/javascript") script_el.set("data-exeres", "true") script_el.text = "\n" """ EPUBResourceManager.clean_html_el(page_html_el) updated_src = etree.tostring(page_html_el, encoding="UTF-8", pretty_print = True) page_fd = open(page_path, "w") page_fd.write(updated_src) page_fd.flush() page_fd.close() """ self.save_page(page_html_el, page_path)