Cook.py :  » Web-Frameworks » Python-Publishing-Accessories » PPA-0.2.7 » PPA » Template » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Python Publishing Accessories 
Python Publishing Accessories » PPA 0.2.7 » PPA » Template » Cook.py
# $Id: Cook.py,v 1.3 2005/10/29 21:39:37 corva Exp $

from __future__ import generators
import re


def quoteHTML(text, nl2br=0, pre=0):
    for char, repl in [('&', '&'),
                       ('"', '"'),
                       ('<', '&lt;'),
                       ('>', '&gt;')]:
        text = text.replace(char, repl)
    if nl2br or pre:
        if type(nl2br) not in (str, unicode):
            nl2br = '<br>'
        text = text.replace('\n', nl2br)
        if pre:
            text = text.replace('\t', ' '*8).replace('  ', '&nbsp; ')
            text = '<tt>%s</tt>' % text
    return text


def quoteURLPath(text):
    return re.sub(r"[^\w\d_.!~*'()/+-]",
                  lambda m: '%%%02X' % ord(m.group()), text)


def quoteFormField(text):
    text = re.sub(r"[^\w\d_.!~*'() -]",
                  lambda m: '%%%02X' % ord(m.group()), text)
    return text.replace(' ', '+')


def quoteJS(text):
    text = text.replace('\r\n', '\\n');
    text = text.replace('\'', '&#39;')
    return text
    

class Repeat:

    class _Item:
        def __init__(self, value, index, has_next):
            self.value = value
            self.index = index
            self.isLast = not has_next
            self.isFirst = not index
        def isOdd(self):
            return self.index % 2
        isOdd = property(isOdd)
        def isEven(self):
            return not self.index % 2
        isEven = property(isEven)
        def alter(self, *args):
            return args[self.index % len(args)]

    def __init__(self, sequence):
        self._iter = iter(sequence)

    def _shift(self):
        try:
            self._next = self._iter.next()
        except StopIteration:
            self._has_next = 0
        else:
            self._has_next = 1

    def __iter__(self):
        self._shift()
        index = 0
        while self._has_next:
            value = self._next
            self._shift()
            yield self._Item(value, index, self._has_next)
            index += 1


def pare(string, size, etc='...'):
    '''Pare string to have maximum size and add etc to the end if it's
    changed'''
    size = int(size)
    string = string.strip()
    if len(string)>size:
        string = string[:size]
        half = size//2
        last = None
        import re
        whitespace = re.compile('\s+')
        for mo in whitespace.finditer(string[half:]):
            if mo is not None:
                last = mo
        if last is not None:
            string = string[:half+last.start()+1]
        return string+etc
    else:
        return string
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.