Latest Version: 0.9.6.2

text

Text Helpers

Provides a set of methods for filtering, formatting and transforming strings.


Functions

f auto_link(text, link='all', **href_options) ...

Turns all urls and email addresses into clickable links.

link
Used to determine what to link. Options are "all", "email_addresses", or "urls"

Example:

>>> auto_link("Go to http://www.planetpython.com and say hello to guido@python.org")
'Go to <a href="http://www.planetpython.com">http://www.planetpython.com</a> and say hello to <a href="mailto:guido@python.org">guido@python.org</a>'

f counter(name='default', start=1, step=1) ...

Return the next cardinal in a sequence.

Every time counter is called, the value returned will be the next counting number in that sequence. This is reset to start on every request, but can also be reset by calling reset_counter().

You can optionally specify the number you want to start at by passing in the start argument (defaults to 1).

You can also optionally specify the step size you want by passing in the step argument (defaults to 1).

Sequences will increase monotonically by step each time it is called, until the heat death of the universe or python explodes.

This can be used to count rows in a table:

# In Myghty
% for item in items:
<tr>
    <td><% h.counter() %></td>
</tr>
% #endfor

You can use named counters to prevent clashes in nested loops. You'll have to reset the inner cycle manually though. See the documentation for webhelpers.text.cycle() for a similar example.

f cycle(*args, **kargs) ...

Returns the next cycle of the given list

Everytime cycle is called, the value returned will be the next item in the list passed to it. This list is reset on every request, but can also be reset by calling reset_cycle().

You may specify the list as either arguments, or as a single list argument.

This can be used to alternate classes for table rows:

# In Myghty...
% for item in items:
<tr class="<% cycle("even", "odd") %>">
    ... use item ...
</tr>
% #endfor

You can use named cycles to prevent clashes in nested loops. You'll have to reset the inner cycle, manually:

% for item in items:
<tr class="<% cycle("even", "odd", name="row_class") %>
    <td>
%     for value in item.values:
        <span style="color:'<% cycle("red", "green", "blue",
                                     name="colors") %>'">
                    item
        </span>
%     #endfor
    <% reset_cycle("colors") %>
    </td>
</tr>
% #endfor

f excerpt(text, phrase, radius=100, excerpt_string='...') ...

Extracts an excerpt from the text. Returns an empty string if the phrase isn't found.

phrase
Phrase to excerpt from text
radius
How many surrounding characters to include
excerpt_string
Characters surrounding entire excerpt

Example:

>>> excerpt("hello my world", "my", 3)
'...lo my wo...'

f highlight(text, phrase, highlighter='<strong class="highlight">\\1</strong>', hilighter=None) ...

Highlights the phrase where it is found in the text

The highlighted phrase will be surrounded by the highlighter, by default:

<strong class="highlight">I'm a highlight phrase</strong>
highlighter
Defines the highlighting phrase. This argument should be a single-quoted string with \1 where the phrase is supposed to be inserted.

Note: The phrase is sanitized to include only letters, digits, and spaces before use.

Example:

>>> highlight('You searched for: Pylons', 'Pylons')
'You searched for: <strong class="highlight">Pylons</strong>'

f markdown(text, **kwargs) ...

Format the text with MarkDown formatting

This function uses the Python MarkDown library which is included with WebHelpers.

f reset_counter(name='default') ...

Resets a counter.

Resets the counter so that it starts from the start cardinal in the sequence next time it is used.

f reset_cycle(name='default') ...

Resets a cycle

Resets the cycle so that it starts from the first element in the array the next time it is used.

f simple_format(text) ...

Returns text transformed into HTML using very simple formatting rules

Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered a linebreak and a <br /> tag is appended. This method does not remove the newlines from the text.

f strip_links(text) ...

Strips link tags from text leaving just the link label.

Example:

>>> strip_links('<a href="something">else</a>')
'else'

f textilize(text, sanitize=False) ...

Format the text with Textile formatting

This function uses the PyTextile library which is included with WebHelpers.

Additionally, the output can be sanitized which will fix tags like <img />, <br /> and <hr /> for proper XHTML output.

f truncate(text, length=30, truncate_string='...') ...

Truncates text with replacement characters

length
The maximum length of text before replacement
truncate_string
If text exceeds the length, this string will replace the end of the string

Example:

>>> truncate('Once upon a time in a world far far away', 14)
'Once upon a...'

f word_wrap(text, line_width=80) ...

Wraps text into lines no longer than line_width width. This function breaks on the first whitespace character that does not exceed line_width.

Deprecated: Use python's builtin textwrap.fill instead.

See the source for more information.

Top