Example #1
0
def add_anchor_lower_id(html, anchor_link_text="¶"):
    from xml.etree.cElementTree import Element

    from defusedxml import cElementTree as ElementTree
    from ipython_genutils import py3compat
    from nbconvert.filters.strings import _convert_header_id, html2text

    try:
        h = ElementTree.fromstring(
            py3compat.cast_bytes_py2(html, encoding="utf-8"))
    except Exception:
        # failed to parse, just return it unmodified
        return html
    link = _convert_header_id(html2text(h))
    h.set("id", slugify(link))
    a = Element("a", {"class": "anchor-link", "href": "#" + link})
    try:
        # Test if the anchor link text is HTML (e.g. an image)
        a.append(ElementTree.fromstring(anchor_link_text))
    except Exception:
        # If we fail to parse, assume we've just got regular text
        a.text = anchor_link_text
    h.append(a)

    # Known issue of Python3.x, ElementTree.tostring() returns a byte string
    # instead of a text string.  See issue http://bugs.python.org/issue10942
    # Workaround is to make sure the bytes are casted to a string.
    return py3compat.decode(ElementTree.tostring(h), "utf-8")
def add_anchor(html, anchor_link_text=u'¶'):
    """Add an id and an anchor-link to an html header

    For use on markdown headings
    """
    try:
        h = ElementTree.fromstring(
            py3compat.cast_bytes_py2(html, encoding='utf-8'))
    except Exception:
        # failed to parse, just return it unmodified
        return html
    link = _convert_header_id(html2text(h))
    h.set('id', link)
    a = Element("a", {"class": "anchor-link", "href": "#" + link})
    try:
        # Test if the anchor link text is HTML (e.g. an image)
        a.append(ElementTree.fromstring(anchor_link_text))
    except Exception:
        # If we fail to parse, assume we've just got regular text
        a.text = anchor_link_text
    h.append(a)

    # Known issue of Python3.x, ElementTree.tostring() returns a byte string
    # instead of a text string.  See issue http://bugs.python.org/issue10942
    # Workaround is to make sure the bytes are casted to a string.
    return py3compat.decode(ElementTree.tostring(h), 'utf-8')
    def _RetrievePlacemarks(self, xml_data):
        """Retrieve placemarks from xml data.

    Args:
      xml_data: Query results from the Google Places database.
    Returns:
      xmlstr: XML with placemarks.
      total_placemarks: Total no. of placemarks.
    """

        xmlstr = ""
        total_results = 0
        # Perform XML parsing using cElementTree.
        root = ET.parse(xml_data).getroot()

        for element in root:
            if element.tag == "result":
                # Rename "result" tags as "Placemark" as per KML(XML) requirements.
                element.tag = "Placemark"

                for subelement in element[:]:
                    # For the sake of simplicity, we presently look for
                    # "name", "geometry", "icon", "vicinity" tags only in the
                    # response and ignore the others.
                    if subelement.tag not in self._reqd_tags:
                        element.remove(subelement)
                        continue

                    if subelement.tag == "geometry":
                        # Extract latitude and longitude coordinates.
                        lat = subelement.find("location").find("lat").text
                        lng = subelement.find("location").find("lng").text

                        # Add "Point" and "coordinates" tags to element.
                        point = ET.SubElement(element, "Point")
                        coords = ET.SubElement(point, "coordinates")
                        coords.text = "%s, %s" % (lng, lat)
                        element.remove(subelement)

                    # Rename "vicinity" and "icon" tags to
                    # "snippet" and "description" as per naming convention
                    # being followed in existing Search Services.
                    elif subelement.tag == "vicinity":
                        subelement.tag = "snippet"
                    elif subelement.tag == "icon":
                        subelement.tag = "description"

                xmlstr += ET.tostring(element, method="xml")
                total_results += 1

        return (xmlstr, total_results)
Example #4
0
def add_anchor(html, anchor_link_text=u'¶'):
    """Add an id and an anchor-link to an html header
    
    For use on markdown headings
    """
    try:
        h = ElementTree.fromstring(py3compat.cast_bytes_py2(html, encoding='utf-8'))
    except Exception:
        # failed to parse, just return it unmodified
        return html
    link = _convert_header_id(html2text(h))
    h.set('id', link)
    a = Element("a", {"class" : "anchor-link", "href" : "#" + link})
    a.text = anchor_link_text
    h.append(a)

    # Known issue of Python3.x, ElementTree.tostring() returns a byte string
    # instead of a text string.  See issue http://bugs.python.org/issue10942
    # Workaround is to make sure the bytes are casted to a string.
    return py3compat.decode(ElementTree.tostring(h), 'utf-8')
Example #5
0
def element_to_string(element,
                      include_declaration=True,
                      encoding=DEFAULT_ENCODING,
                      method='xml'):
    """ :return: the string value of the element or element tree """

    if isinstance(element, ElementTree):
        element = element.getroot()
    elif not isinstance(element, ElementType):
        element = get_element(element)

    if element is None:
        return u''

    element_as_string = tostring(element, encoding,
                                 method).decode(encoding=encoding)
    if include_declaration:
        return element_as_string
    else:
        return strip_xml_declaration(element_as_string)