strings.py
151 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
|
r11302 | # coding: utf-8 | ||
|
r10676 | """String filters. | ||
|
r10433 | |||
|
r10676 | Contains a collection of useful string manipulation filters for use in Jinja | ||
templates. | ||||
|
r10433 | """ | ||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r11202 | import re | ||
|
r10676 | import textwrap | ||
|
r11302 | from xml.etree import ElementTree | ||
from IPython.utils import py3compat | ||||
|
r11089 | |||
|
r10433 | #----------------------------------------------------------------------------- | ||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
|
r10676 | |||
|
r11088 | __all__ = [ | ||
'wrap', | ||||
|
r11302 | 'html_text', | ||
'add_anchor', | ||||
|
r11088 | 'strip_dollars', | ||
'rm_fake', | ||||
'python_comment', | ||||
'get_lines' | ||||
] | ||||
|
r10433 | def wrap(text, width=100): | ||
|
r10676 | """ | ||
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. | ||||
""" | ||||
|
r10433 | |||
|
r10676 | split_text = text.split('\n') | ||
wrp = map(lambda x:textwrap.wrap(x,width), split_text) | ||||
|
r10433 | wrpd = map('\n'.join, wrp) | ||
|
r10434 | return '\n'.join(wrpd) | ||
|
r11302 | |||
def html_text(element): | ||||
"""extract inner text from html | ||||
Analog of jQuery's $(element).text() | ||||
""" | ||||
if not isinstance(element, (ElementTree.ElementTree, ElementTree.Element)): | ||||
element = ElementTree.fromstring(element) | ||||
text = element.text or "" | ||||
for child in element: | ||||
text += html_text(child) | ||||
text += (element.tail or "") | ||||
return text | ||||
def add_anchor(html): | ||||
"""Add an anchor-link to an html header tag | ||||
|
r11293 | |||
|
r11302 | For use in heading cells | ||
|
r11293 | """ | ||
|
r11302 | h = ElementTree.fromstring(py3compat.cast_bytes_py2(html)) | ||
link = html_text(h).replace(' ', '-') | ||||
h.set('id', link) | ||||
a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) | ||||
a.text = u'¶' | ||||
h.append(a) | ||||
return ElementTree.tostring(h) | ||||
|
r11293 | |||
|
r10434 | |||
def strip_dollars(text): | ||||
|
r10676 | """ | ||
Remove all dollar symbols from text | ||||
Parameters | ||||
---------- | ||||
text : str | ||||
Text to remove dollars from | ||||
""" | ||||
|
r10434 | |||
|
r10485 | return text.strip('$') | ||
|
r10882 | |||
|
r11202 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') | ||
|
r10676 | def rm_fake(text): | ||
""" | ||||
|
r11202 | Fix all fake URLs that start with `files/`, | ||
stripping out the `files/` prefix. | ||||
|
r10676 | |||
Parameters | ||||
---------- | ||||
text : str | ||||
|
r11202 | Text in which to replace 'src="files/real...' with 'src="real...' | ||
|
r10676 | """ | ||
|
r11202 | return files_url_pattern.sub(r"\1=\2", text) | ||
|
r10485 | |||
|
r10676 | def python_comment(text): | ||
""" | ||||
Build a Python comment line from input text. | ||||
Parameters | ||||
---------- | ||||
text : str | ||||
Text to comment out. | ||||
""" | ||||
#Replace line breaks with line breaks and comment symbols. | ||||
#Also add a comment symbol at the beginning to comment out | ||||
#the first line. | ||||
return '# '+'\n# '.join(text.split('\n')) | ||||
|
r10485 | |||
|
r10676 | def get_lines(text, start=None,end=None): | ||
|
r10485 | """ | ||
Split the input text into separate lines and then return the | ||||
lines that the caller is interested in. | ||||
|
r10676 | |||
Parameters | ||||
---------- | ||||
text : str | ||||
Text to parse lines from. | ||||
start : int, optional | ||||
First line to grab from. | ||||
end : int, optional | ||||
Last line to grab from. | ||||
|
r10485 | """ | ||
# Split the input into lines. | ||||
|
r10676 | lines = text.split("\n") | ||
|
r10485 | |||
# Return the right lines. | ||||
return "\n".join(lines[start:end]) #re-join | ||||