def writeCixFileForElement(filename, root): stream = open(filename, 'wb') stream.write('<?xml version="1.0" encoding="UTF-8"?>\n'.encode('ascii')) prettify(root) tree = ElementTree(root) tree.write(stream, encoding="utf-8") stream.close()
def get_cix_string(cix, prettyFormat=True): # Get the CIX. if prettyFormat: prettify(cix) cixstream = StringIO() cixtree = ElementTree(cix) cixstream.write('<?xml version="1.0" encoding="UTF-8"?>\n') cixtree.write(cixstream) cixcontent = cixstream.getvalue() cixstream.close() return cixcontent
def _html_ci_elem(opts, elem, lang=None): # Taken from codeintel2.tree, modified to ensure it keeps all # existing text and tail data. Since this is used on generated # xml content, there is no need to worry about existing newlines # and whitespace, as there will be none existing at this point. def pretty_tree_from_tree(tree, indent_width=2): """Add appropriate .tail and .text values to the given tree so that it will have a pretty serialization. Presumption: This is a CIX 2.0 tree. """ INDENT = ' ' * indent_width def _prettify(elem, indent_level=0): if elem: # i.e. elem has child elements elem.text = '\n' + INDENT * (indent_level + 1) + (elem.text or "") for child in elem: _prettify(child, indent_level + 1) elem[-1].tail = (elem[-1].tail or "") + '\n' + INDENT * indent_level elem.tail = (elem.tail or "") + '\n' + INDENT * indent_level else: # elem.text = None elem.tail = (elem.tail or "") + '\n' + INDENT * indent_level _prettify(tree) return tree def remove_private_elements(elem): """Remove all the private cix elements.""" parent_map = dict((c, p) for p in elem.getiterator() for c in p) for node in list(elem.getiterator()): attributes = node.get("attributes", "").split(" ") if "private" in attributes or "__hidden__" in attributes: # Remove it parentnode = parent_map.get(node) if parentnode is not None: parentnode.remove(node) # Set the css reference file if not opts.css_reference_files: opts.css_reference_files = ["aspn.css", "api.css"] html = Element("html") head = SubElement(html, "head") for css_filename in opts.css_reference_files: SubElement(head, "link", rel="stylesheet", type="text/css", href=css_filename) body = SubElement(html, "body") body_div = SubElement(body, "div", {"id": "body"}) namespace_elements = [] # Remove any private cix elements, as they are not externally visible. remove_private_elements(elem) if elem.tag == "file": for child in elem: for subchild in child: _convertScopeToHtml(body_div, subchild, "", namespace_elements) else: _convertScopeToHtml(body_div, elem, "", namespace_elements) # Now, we can print out the html in a few formats: # Single file - the default and only implemented format at present # File for each namespace and an index - Not done. # Try to build an index, placed in same html file # nav_div = SubElement(body, "div", {"id": "nav"}) # ul = SubElement(nav_div, "ul") # for ns, elem in namespace_elements: # li = SubElement(ul, "li") # a_href = SubElement(li, "a", href="#%s" % (ns)) # a_href.text = ns footer_div = SubElement(body, "div", {"id": "footer"}) pretty_tree_from_tree(html) tree = ElementTree(html) xhtml_header = '<?xml version="1.0"?>\n' \ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' \ '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' stream = sys.stdout if opts.output: stream = file(opts.output, "wb") stream.write(xhtml_header) tree.write(stream) if opts.toc_file: file_href = opts.output or "komodo-js-api.html" toc_node = Element("node", name="Komodo JavaScript API Reference", link=file_href) for ns, elem in namespace_elements: sub_node = SubElement(toc_node, "node", name=ns, link="%s#%s" % ( file_href, ns, )) pretty_tree_from_tree(toc_node) toc_file = open(opts.toc_file, "w") tree = ElementTree(toc_node) tree.write(toc_file)
def _html_ci_elem(opts, elem, lang=None): # Taken from codeintel2.tree, modified to ensure it keeps all # existing text and tail data. Since this is used on generated # xml content, there is no need to worry about existing newlines # and whitespace, as there will be none existing at this point. def pretty_tree_from_tree(tree, indent_width=2): """Add appropriate .tail and .text values to the given tree so that it will have a pretty serialization. Presumption: This is a CIX 2.0 tree. """ INDENT = ' '*indent_width def _prettify(elem, indent_level=0): if elem: # i.e. elem has child elements elem.text = '\n' + INDENT*(indent_level+1) + (elem.text or "") for child in elem: _prettify(child, indent_level+1) elem[-1].tail = (elem[ -1].tail or "") + '\n' + INDENT*indent_level elem.tail = (elem.tail or "") + '\n' + INDENT*indent_level else: # elem.text = None elem.tail = (elem.tail or "") + '\n' + INDENT*indent_level _prettify(tree) return tree def remove_private_elements(elem): """Remove all the private cix elements.""" parent_map = dict((c, p) for p in elem.getiterator() for c in p) for node in list(elem.getiterator()): attributes = node.get("attributes", "").split(" ") if "private" in attributes or "__hidden__" in attributes: # Remove it parentnode = parent_map.get(node) if parentnode is not None: parentnode.remove(node) # Set the css reference file if not opts.css_reference_files: opts.css_reference_files = ["aspn.css", "api.css"] html = Element("html") head = SubElement(html, "head") for css_filename in opts.css_reference_files: SubElement(head, "link", rel="stylesheet", type="text/css", href=css_filename) body = SubElement(html, "body") body_div = SubElement(body, "div", {"id": "body"}) namespace_elements = [] # Remove any private cix elements, as they are not externally visible. remove_private_elements(elem) if elem.tag == "file": for child in elem: for subchild in child: _convertScopeToHtml(body_div, subchild, "", namespace_elements) else: _convertScopeToHtml(body_div, elem, "", namespace_elements) # Now, we can print out the html in a few formats: # Single file - the default and only implemented format at present # File for each namespace and an index - Not done. # Try to build an index, placed in same html file # nav_div = SubElement(body, "div", {"id": "nav"}) # ul = SubElement(nav_div, "ul") # for ns, elem in namespace_elements: # li = SubElement(ul, "li") # a_href = SubElement(li, "a", href="#%s" % (ns)) # a_href.text = ns footer_div = SubElement(body, "div", {"id": "footer"}) pretty_tree_from_tree(html) tree = ElementTree(html) xhtml_header = '<?xml version="1.0"?>\n' \ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' \ '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' stream = sys.stdout if opts.output: stream = file(opts.output, "wb") stream.write(xhtml_header) tree.write(stream) if opts.toc_file: file_href = opts.output or "komodo-js-api.html" toc_node = Element("node", name="Komodo JavaScript API Reference", link=file_href) for ns, elem in namespace_elements: sub_node = SubElement(toc_node, "node", name=ns, link="%s#%s" % ( file_href, ns, )) pretty_tree_from_tree(toc_node) toc_file = open(opts.toc_file, "w") tree = ElementTree(toc_node) tree.write(toc_file)