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 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): """Add 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 = html2text(h).replace(' ', '-') h.set('id', link) a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) a.text = u'¶' 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')