From 0901b28bac3104b8176a000c8d6a0d6e75f046b8 Mon Sep 17 00:00:00 2001 From: Kurt Raschke Date: Wed, 14 Jul 2010 13:21:27 -0400 Subject: [PATCH 1/3] First pass at TextileFactory class. --- textile/textilefactory.py | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 textile/textilefactory.py diff --git a/textile/textilefactory.py b/textile/textilefactory.py new file mode 100644 index 00000000..83cb3913 --- /dev/null +++ b/textile/textilefactory.py @@ -0,0 +1,73 @@ +from functions import Textile + + +class TextileFactory(object): + """ + Use TextileFactory to create a Textile object which can be re-used + to process multiple strings with the same settings. + + >>> f = TextileFactory() + >>> f.process("some text here") + '\\t

some text here

' + + >>> f = TextileFactory(restricted=True) + >>> f.process("more text here") + '\\t

more text here

' + + Certain parameter values are not permitted because they are illogical: + + >>> f = TextileFactory(lite=True) + Traceback (most recent call last): + ... + ValueError: lite can only be enabled in restricted mode + + >>> f = TextileFactory(head_offset=7) + Traceback (most recent call last): + ... + ValueError: head_offset must be 0-6 + + >>> f = TextileFactory(html_type='html5') + Traceback (most recent call last): + ... + ValueError: html_type must be 'html' or 'xhtml' + + + """ + + def __init__(self, restricted=False, lite=False, noimage=None, + auto_link=False, get_sizes=False, head_offset=0, + html_type='xhtml'): + + self.class_parms = {} + self.method_parms = {} + + if lite and not restricted: + raise ValueError("lite can only be enabled in restricted mode") + + if restricted: + self.class_parms['restricted'] = True + self.class_parms['lite'] = lite + self.method_parms['rel'] = 'nofollow' + + if noimage is None: + if restricted: + noimage = True + else: + noimage = False + + self.class_parms['noimage'] = noimage + self.class_parms['auto_link'] = auto_link + self.class_parms['get_sizes'] = get_sizes + + if int(head_offset) not in range(0, 6): + raise ValueError("head_offset must be 0-6") + else: + self.method_parms['head_offset'] = head_offset + + if html_type not in ['html', 'xhtml']: + raise ValueError("html_type must be 'html' or 'xhtml'") + else: + self.method_parms['html_type'] = html_type + + def process(self, text): + return Textile(**self.class_parms).textile(text, **self.method_parms) From 44b06c76c001b09faf767968d172cdb8bea5cf06 Mon Sep 17 00:00:00 2001 From: Kurt Raschke Date: Wed, 14 Jul 2010 13:23:02 -0400 Subject: [PATCH 2/3] Add parameter for get_sizes. --- textile/functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/textile/functions.py b/textile/functions.py index 8b85839b..b64cd3f5 100644 --- a/textile/functions.py +++ b/textile/functions.py @@ -109,12 +109,12 @@ class Textile(object): ) def __init__(self, restricted=False, lite=False, noimage=False, - auto_link=False): + auto_link=False, get_sizes=False): """docstring for __init__""" self.restricted = restricted self.lite = lite self.noimage = noimage - self.get_sizes = False + self.get_sizes = get_sizes self.auto_link = auto_link self.fn = {} self.urlrefs = {} From 3d828bb2ca975172191225dd20ddc22b33578c38 Mon Sep 17 00:00:00 2001 From: Kurt Raschke Date: Wed, 14 Jul 2010 21:13:43 -0400 Subject: [PATCH 3/3] Add support for sanitization to TextileFactory. --- textile/textilefactory.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/textile/textilefactory.py b/textile/textilefactory.py index 83cb3913..c8c3bf34 100644 --- a/textile/textilefactory.py +++ b/textile/textilefactory.py @@ -12,7 +12,7 @@ class TextileFactory(object): >>> f = TextileFactory(restricted=True) >>> f.process("more text here") - '\\t

more text here

' + u'\\t

more text here

' Certain parameter values are not permitted because they are illogical: @@ -34,9 +34,9 @@ class TextileFactory(object): """ - def __init__(self, restricted=False, lite=False, noimage=None, - auto_link=False, get_sizes=False, head_offset=0, - html_type='xhtml'): + def __init__(self, restricted=False, lite=False, sanitize=None, + noimage=None, auto_link=False, get_sizes=False, + head_offset=0, html_type='xhtml'): self.class_parms = {} self.method_parms = {} @@ -55,7 +55,14 @@ def __init__(self, restricted=False, lite=False, noimage=None, else: noimage = False + if sanitize is None: + if restricted: + sanitize = True + else: + sanitize = False + self.class_parms['noimage'] = noimage + self.method_parms['sanitize'] = sanitize self.class_parms['auto_link'] = auto_link self.class_parms['get_sizes'] = get_sizes