strings.py
152 lines
| 3.7 KiB
| text/x-python
|
PythonLexer
MinRK
|
r11302 | # coding: utf-8 | ||
Jonathan Frederic
|
r10676 | """String filters. | ||
Jonathan Frederic
|
r10433 | |||
Jonathan Frederic
|
r10676 | Contains a collection of useful string manipulation filters for use in Jinja | ||
templates. | ||||
Jonathan Frederic
|
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 | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r11202 | import re | ||
Jonathan Frederic
|
r10676 | import textwrap | ||
MinRK
|
r11302 | from xml.etree import ElementTree | ||
from IPython.utils import py3compat | ||||
Brian E. Granger
|
r11089 | |||
Jonathan Frederic
|
r10433 | #----------------------------------------------------------------------------- | ||
# Functions | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10676 | |||
Brian E. Granger
|
r11088 | __all__ = [ | ||
Jonathan Frederic
|
r11685 | 'wrap_text', | ||
'html2text', | ||||
MinRK
|
r11302 | 'add_anchor', | ||
Brian E. Granger
|
r11088 | 'strip_dollars', | ||
Jonathan Frederic
|
r11685 | 'strip_files_prefix', | ||
'comment_lines', | ||||
Brian E. Granger
|
r11088 | 'get_lines' | ||
] | ||||
Jonathan Frederic
|
r11685 | def wrap_text(text, width=100): | ||
Jonathan Frederic
|
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. | ||||
""" | ||||
Jonathan Frederic
|
r10433 | |||
Jonathan Frederic
|
r10676 | split_text = text.split('\n') | ||
wrp = map(lambda x:textwrap.wrap(x,width), split_text) | ||||
Jonathan Frederic
|
r10433 | wrpd = map('\n'.join, wrp) | ||
Jonathan Frederic
|
r10434 | return '\n'.join(wrpd) | ||
MinRK
|
r11302 | |||
Jonathan Frederic
|
r11685 | def html2text(element): | ||
MinRK
|
r11302 | """extract inner text from html | ||
Analog of jQuery's $(element).text() | ||||
""" | ||||
Jonathan Frederic
|
r11547 | if isinstance(element, py3compat.string_types): | ||
MinRK
|
r11302 | element = ElementTree.fromstring(element) | ||
text = element.text or "" | ||||
for child in element: | ||||
Jonathan Frederic
|
r11685 | text += html2text(child) | ||
MinRK
|
r11302 | text += (element.tail or "") | ||
return text | ||||
def add_anchor(html): | ||||
"""Add an anchor-link to an html header tag | ||||
MinRK
|
r11293 | |||
MinRK
|
r11302 | For use in heading cells | ||
MinRK
|
r11293 | """ | ||
MinRK
|
r11302 | h = ElementTree.fromstring(py3compat.cast_bytes_py2(html)) | ||
Jonathan Frederic
|
r11685 | link = html2text(h).replace(' ', '-') | ||
MinRK
|
r11302 | h.set('id', link) | ||
a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) | ||||
a.text = u'¶' | ||||
h.append(a) | ||||
return ElementTree.tostring(h) | ||||
MinRK
|
r11293 | |||
Jonathan Frederic
|
r10434 | |||
def strip_dollars(text): | ||||
Jonathan Frederic
|
r10676 | """ | ||
Remove all dollar symbols from text | ||||
Parameters | ||||
---------- | ||||
text : str | ||||
Text to remove dollars from | ||||
""" | ||||
Jonathan Frederic
|
r10434 | |||
Jonathan Frederic
|
r10485 | return text.strip('$') | ||
jakobgager
|
r10882 | |||
MinRK
|
r11202 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') | ||
Jonathan Frederic
|
r11685 | def strip_files_prefix(text): | ||
Jonathan Frederic
|
r10676 | """ | ||
MinRK
|
r11202 | Fix all fake URLs that start with `files/`, | ||
stripping out the `files/` prefix. | ||||
Jonathan Frederic
|
r10676 | |||
Parameters | ||||
---------- | ||||
text : str | ||||
MinRK
|
r11202 | Text in which to replace 'src="files/real...' with 'src="real...' | ||
Jonathan Frederic
|
r10676 | """ | ||
MinRK
|
r11202 | return files_url_pattern.sub(r"\1=\2", text) | ||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r11685 | def comment_lines(text, prefix='# '): | ||
Jonathan Frederic
|
r10676 | """ | ||
Build a Python comment line from input text. | ||||
Parameters | ||||
---------- | ||||
text : str | ||||
Text to comment out. | ||||
Jonathan Frederic
|
r11685 | prefix : str | ||
Character to append to the start of each line. | ||||
Jonathan Frederic
|
r10676 | """ | ||
#Replace line breaks with line breaks and comment symbols. | ||||
#Also add a comment symbol at the beginning to comment out | ||||
#the first line. | ||||
Jonathan Frederic
|
r11685 | return prefix + ('\n'+prefix).join(text.split('\n')) | ||
Jonathan Frederic
|
r10676 | |||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r10676 | def get_lines(text, start=None,end=None): | ||
Jonathan Frederic
|
r10485 | """ | ||
Split the input text into separate lines and then return the | ||||
lines that the caller is interested in. | ||||
Jonathan Frederic
|
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. | ||||
Jonathan Frederic
|
r10485 | """ | ||
# Split the input into lines. | ||||
Jonathan Frederic
|
r10676 | lines = text.split("\n") | ||
Jonathan Frederic
|
r10485 | |||
# Return the right lines. | ||||
return "\n".join(lines[start:end]) #re-join | ||||