# coding: utf-8 """String filters. Contains a collection of useful string manipulation filters for use in Jinja templates. """ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. import os import re import textwrap import warnings import base64 from urllib.parse import quote # defusedxml does safe(r) parsing of untrusted XML data from defusedxml import ElementTree from xml.etree.ElementTree import Element __all__ = [ 'wrap_text', 'html2text', 'add_anchor', 'strip_dollars', 'strip_files_prefix', 'comment_lines', 'get_lines', 'ipython2python', 'posix_path', 'path2url', 'add_prompts', 'ascii_only', 'prevent_list_blocks', 'strip_trailing_newline', 'text_base64', ] def wrap_text(text, width=100): """ Intelligently wrap text. Wrap text without breaking words if possible. Parameters ---------- text : str Text to wrap. width : int, optional Number of characters to wrap to, default 100. """ split_text = text.split('\n') wrp = map(lambda x:textwrap.wrap(x,width), split_text) wrpd = map('\n'.join, wrp) return '\n'.join(wrpd) def html2text(element): """extract inner text from html Analog of jQuery's $(element).text() """ if isinstance(element, (str,)): try: element = ElementTree.fromstring(element) except Exception: # failed to parse, just return it unmodified return element text = element.text or "" for child in element: text += html2text(child) text += (element.tail or "") return text def _convert_header_id(header_contents): """Convert header contents to valid id value. Takes string as input, returns string. Note: this may be subject to change in the case of changes to how we wish to generate ids. For use on markdown headings. """ # Valid IDs need to be non-empty and contain no space characters, but are otherwise arbitrary. # However, these IDs are also used in URL fragments, which are more restrictive, so we URL # encode any characters that are not valid in URL fragments. return quote(header_contents.replace(' ', '-'), safe="?/:@!$&'()*+,;=") 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(html) 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) return ElementTree.tostring(h).decode(encoding='utf-8') def add_prompts(code, first='>>> ', cont='... '): """Add prompts to code snippets""" new_code = [] code_list = code.split('\n') new_code.append(first + code_list[0]) for line in code_list[1:]: new_code.append(cont + line) return '\n'.join(new_code) def strip_dollars(text): """ Remove all dollar symbols from text Parameters ---------- text : str Text to remove dollars from """ return text.strip('$') files_url_pattern = re.compile(r'(src|href)\=([\'"]?)/?files/') markdown_url_pattern = re.compile(r'(!?)\[(?P