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)