def make_nav_point(sec): nav_point = Element('navPoint', attributes={'id': 'id_' + uuid.uuid4().hex}) nav_label = Element('navLabel') nav_point.children.append(nav_label) text_ = Element('text') nav_label.children.append(text_) text_.children.append(sec.title) content = Element('content') nav_point.children.append(content) first_sub = None for subsection in sec.subs: sub = make_nav_point(subsection) nav_point.children.append(sub) first_sub = first_sub or sub if sec.href: content.attributes[(None, 'src')] = sec.href elif first_sub: content.attributes[(None, 'src')] = first_sub.children[1].attributes[(None, 'src')] # return nav_point
def to_nav_li_element(sec): li = Element((None, 'li')) if sec.href: a_or_span = Element((None, 'a')) a_or_span.attributes[(None, 'href')] = sec.href else: a_or_span = Element((None, 'span')) a_or_span.children.append(sec.title) li.children.append(a_or_span) if sec.subs: _ol = Element((None, 'ol')) if sec.hidden_subs: _ol.attributes[(None, 'hidden')] = '' for one in sec.subs: _ol.children.append(to_nav_li_element(one)) li.children.append(_ol) return li
def _get_opf_xmlstring(self, toc_path): package = Element('package', prefixes={OPF_NS: None}, attributes={'version': '3.0'}) package.attributes[(URI_XML, 'lang')] = 'en' # unique - identifier = "pub-id" if self._find_unique_id(): package.attributes['unique-identifier'] = self._find_unique_id() # Metadata package.children.append(self._make_metadata_element()) # manifest manifest = self._make_manifest_element() package.children.append(manifest) self._process_items_properties(manifest) for item in manifest.children: if item.attributes[(None, 'href')] == toc_path: item.attributes[(None, 'properties')] = 'nav' if item.attributes[(None, 'href')] == self.cover_image: item.attributes[(None, 'properties')] = 'cover-image' # Find ncx id for spine toc_ncx_item_e_id = self._find_ncx_id(manifest.children) # Spine spine = self._make_spine_element() package.children.append(spine) spine.attributes['toc'] = toc_ncx_item_e_id return Xl(root=pretty_insert(package, dont_do_when_one_child=True)).string()
def _make_spine_element(self): spine = Element('spine') for joint in self.spine: itemref = Element('itemref', attributes={(None, 'idref'): self._find_id(joint.path)}) if joint.linear is True: itemref.attributes[(None, 'linear')] = 'yes' elif joint.linear is False: itemref.attributes[(None, 'linear')] = 'no' spine.children.append(itemref) return spine
def make_item(path, file_): item = Element('item', attributes={(None, 'href'): path}) item.attributes[(None, 'media-type')] = file_.mime or mimes.map_from_extension[ os.path.splitext(path)[1]] identification = xml_identify(path) new_id = identification i = 1 while new_id in ids: new_id = identification + '_' + str(i) item.attributes[(None, 'id')] = new_id ids.append(new_id) return item
def to_element(self): e = Element((None, 'meta')) e.attributes[(None, 'property')] = 'dcterms:{}'.format(self.__class__.__name__) for attr_name, value in self._attrs.items(): uri = None if ':' in attr_name: prefix, attr = attr_name.split(':') uri = namespace_map[prefix] e.prefixes[uri] = prefix else: attr = attr_name e.attributes[(uri, attr)] = value e.children.append(self.text) return e
def to_element(self): e = Element((URI_DC, self.__class__.__name__.lower())) e.prefixes[URI_DC] = "dc" for attr_name, value in self._attrs.items(): if ":" in attr_name: prefix, attr = attr_name.split(":") uri = namespace_map[prefix] e.prefixes[uri] = prefix e.attributes[(uri, attr)] = value else: attr = attr_name e.attributes[(None, attr)] = value e.children.append(self._text) return e
def _get_opf_xmlstring(self): package = Element('package', prefixes={OPF_NS: None}, attributes={'version': '2.0'}) # unique - identifier = "pub-id" if self._find_unique_id(): package.attributes['unique-identifier'] = self._find_unique_id() # Metadata package.children.append(self._make_metadata_element()) # Manifest manifest = self._make_manifest_element() package.children.append(manifest) # Find ncx id for spine toc_ncx_item_e_id = self._find_ncx_id(manifest.children) # Spine spine = self._make_spine_element() package.children.append(spine) spine.attributes['toc'] = toc_ncx_item_e_id return Xl(root=pretty_insert(package, dont_do_when_one_child=True)).string()
def _get_container_xmlstring(opf_path): e = Element('container') e.attributes['version'] = '1.0' e.prefixes['urn:oasis:names:tc:opendocument:xmlns:container'] = None rootfiles = Element('rootfiles') e.children.append(rootfiles) rootfile = Element('rootfile') rootfiles.children.append(rootfile) rootfile.attributes['full-path'] = opf_path rootfile.attributes['media-type'] = 'application/oebps-package+xml' return Xl(root=pretty_insert(e, dont_do_when_one_child=True)).string()