##// END OF EJS Templates
Small latex mods: Escapes, Headings, Equations...
Small latex mods: Escapes, Headings, Equations * remove console escapes * use different heading levels * allow explicit definition of latex environments

File last commit:

r10882:15702d63
r10882:15702d63
Show More
strings.py
117 lines | 2.7 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor of filters
r10676 """String filters.
Jonathan Frederic
Moved wrap code into Strings utility file.
r10433
Jonathan Frederic
Cleanup and refactor of filters
r10676 Contains a collection of useful string manipulation filters for use in Jinja
templates.
Jonathan Frederic
Moved wrap code into Strings utility file.
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
#-----------------------------------------------------------------------------
jakobgager
Small latex mods: Escapes, Headings, Equations...
r10882 import re
Jonathan Frederic
Moved wrap code into Strings utility file.
r10433
# Our own imports
Jonathan Frederic
Cleanup and refactor of filters
r10676 import textwrap
Jonathan Frederic
Moved wrap code into Strings utility file.
r10433
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Cleanup and refactor of filters
r10676
Jonathan Frederic
Moved wrap code into Strings utility file.
r10433 def wrap(text, width=100):
Jonathan Frederic
Cleanup and refactor of filters
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
Moved wrap code into Strings utility file.
r10433
Jonathan Frederic
Cleanup and refactor of filters
r10676 split_text = text.split('\n')
wrp = map(lambda x:textwrap.wrap(x,width), split_text)
Jonathan Frederic
Moved wrap code into Strings utility file.
r10433 wrpd = map('\n'.join, wrp)
Jonathan Frederic
Moved more code to Strings utilities file
r10434 return '\n'.join(wrpd)
def strip_dollars(text):
Jonathan Frederic
Cleanup and refactor of filters
r10676 """
Remove all dollar symbols from text
Parameters
----------
text : str
Text to remove dollars from
"""
Jonathan Frederic
Moved more code to Strings utilities file
r10434
Jonathan Frederic
Post code-review, extended refactor.
r10485 return text.strip('$')
jakobgager
Small latex mods: Escapes, Headings, Equations...
r10882 def rm_consoleesc(text):
"""
Remove console escapes from text
Parameters
----------
text : str
Text to remove '/files/' from
"""
r= re.compile("\033\[[0-9;]+m")
return r.sub('', text)
Jonathan Frederic
Cleanup and refactor of filters
r10676 def rm_fake(text):
"""
Remove all occurrences of '/files/' from text
Parameters
----------
text : str
Text to remove '/files/' from
"""
return text.replace('/files/', '')
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
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'))
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor of filters
r10676 def get_lines(text, start=None,end=None):
Jonathan Frederic
Post code-review, extended refactor.
r10485 """
Split the input text into separate lines and then return the
lines that the caller is interested in.
Jonathan Frederic
Cleanup and refactor of filters
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
Post code-review, extended refactor.
r10485 """
# Split the input into lines.
Jonathan Frederic
Cleanup and refactor of filters
r10676 lines = text.split("\n")
Jonathan Frederic
Post code-review, extended refactor.
r10485
# Return the right lines.
return "\n".join(lines[start:end]) #re-join