Skip to content

Commit fc6530b

Browse files
committed
support html5 as an output html_type
1 parent 875d0f5 commit fc6530b

3 files changed

Lines changed: 23 additions & 20 deletions

File tree

textile/functions.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Textile(object):
197197
doctype_whitelist = ['html', 'xhtml', 'html5']
198198

199199
def __init__(self, restricted=False, lite=False, noimage=False,
200-
auto_link=False, get_sizes=False):
200+
auto_link=False, get_sizes=False, html_type='xhtml'):
201201
"""Textile properties that are common to regular textile and
202202
textile_restricted"""
203203
self.restricted = restricted
@@ -209,22 +209,18 @@ def __init__(self, restricted=False, lite=False, noimage=False,
209209
self.urlrefs = {}
210210
self.shelf = {}
211211
self.rel = ''
212-
self.html_type = 'xhtml'
212+
self.html_type = html_type
213213
self.max_span_depth = 5
214214

215215
if self.html_type == 'html5':
216216
self.glyph_replace[19] = r'<abbr title="\2">\1</abbr>'
217217

218-
def textile(self, text, rel=None, head_offset=0, html_type='xhtml',
219-
sanitize=False):
218+
def textile(self, text, rel=None, head_offset=0, sanitize=False):
220219
"""
221220
>>> import textile
222221
>>> textile.textile('some textile')
223222
'\\t<p>some textile</p>'
224223
"""
225-
html_type = html_type.lower()
226-
self.html_type = (html_type in self.doctype_whitelist and html_type or
227-
'xhtml')
228224
self.notes = OrderedDict()
229225
self.unreferencedNotes = OrderedDict()
230226
self.notelist_cache = OrderedDict()
@@ -289,7 +285,7 @@ def textile(self, text, rel=None, head_offset=0, html_type='xhtml',
289285
if sanitize:
290286
text = sanitizer.sanitize(text)
291287

292-
breaktag = {'html': '<br>', 'xhtml': '<br />'}
288+
breaktag = {'html': '<br>', 'xhtml': '<br />', 'html5': '<br />'}
293289

294290
text = text.replace(breaktag[self.html_type], '%s\n'
295291
% breaktag[self.html_type])
@@ -1679,8 +1675,8 @@ def textile(text, head_offset=0, html_type='xhtml', auto_link=False,
16791675
html_type - 'xhtml' or 'html' style tags (default: 'xhtml')
16801676
16811677
"""
1682-
return Textile(auto_link=auto_link).textile(text, head_offset=head_offset,
1683-
html_type=html_type)
1678+
return Textile(auto_link=auto_link, html_type=html_type).textile(text,
1679+
head_offset=head_offset)
16841680

16851681

16861682
def textile_restricted(text, lite=True, noimage=True, html_type='xhtml',
@@ -1698,6 +1694,6 @@ def textile_restricted(text, lite=True, noimage=True, html_type='xhtml',
16981694
noimage - disable image tags (default: True)
16991695
17001696
"""
1701-
return Textile(restricted=True, lite=lite,
1702-
noimage=noimage, auto_link=auto_link).textile(
1703-
text, rel='nofollow', html_type=html_type)
1697+
return Textile(restricted=True, lite=lite, noimage=noimage,
1698+
auto_link=auto_link, html_type=html_type).textile( text,
1699+
rel='nofollow')

textile/tests/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,7 @@ def testSanitize(self):
481481

482482
test = """<p>a paragraph of benign text<br />and more text</p>"""
483483
result = '<p>a paragraph of benign text<br>\nand more text</p>'
484-
expect = textile.Textile().textile(test, sanitize=True,
485-
html_type='html')
484+
expect = textile.Textile(html_type='html').textile(test, sanitize=True)
486485
eq_(result, expect)
487486

488487
def testImageSize(self):
@@ -586,3 +585,11 @@ def testSquareBrackets(self):
586585
html = textile.textile("""1[^st^], 2[^nd^], 3[^rd^]. 2 log[~n~]\n\nA close[!http://textpattern.com/favicon.ico!]image.\nA tight["text":http://textpattern.com/]link.\nA ["footnoted link":http://textpattern.com/][182].""")
587586
searchstring = r'^\t<p>1<sup>st</sup>, 2<sup>nd</sup>, 3<sup>rd</sup>. 2 log<sub>n</sub></p>\n\n\t<p>A close<img alt="" src="http://textpattern.com/favicon.ico" />image.<br />\nA tight<a href="http://textpattern.com/">text</a>link.<br />\nA <a href="http://textpattern.com/">footnoted link</a><sup class="footnote" id="fnrev([a-f0-9]{32})"><a href="#fn\1">182</a></sup>.</p>'
588587
assert_true(re.compile(searchstring).search(html))
588+
589+
def testHTML5(self):
590+
"""docstring for testHTML5"""
591+
592+
test = 'We use CSS(Cascading Style Sheets).'
593+
result = '\t<p>We use <abbr title="Cascading Style Sheets"><span class="caps">CSS</span></abbr>.</p>'
594+
expect = textile.textile(test, html_type="html5")
595+
eq_(result, expect)

textile/textilefactory.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class TextileFactory(object):
2626
...
2727
ValueError: head_offset must be 0-6
2828
29-
>>> f = TextileFactory(html_type='html5')
29+
>>> f = TextileFactory(html_type='invalid')
3030
Traceback (most recent call last):
3131
...
32-
ValueError: html_type must be 'html' or 'xhtml'
32+
ValueError: html_type must be 'html', 'xhtml', or 'html5'
3333
3434
3535
"""
@@ -65,10 +65,10 @@ def __init__(self, restricted=False, lite=False, sanitize=False,
6565
else:
6666
self.method_parms['head_offset'] = head_offset
6767

68-
if html_type not in ['html', 'xhtml']:
69-
raise ValueError("html_type must be 'html' or 'xhtml'")
68+
if html_type not in ['html', 'xhtml', 'html5']:
69+
raise ValueError("html_type must be 'html', 'xhtml', or 'html5'")
7070
else:
71-
self.method_parms['html_type'] = html_type
71+
self.class_parms['html_type'] = html_type
7272

7373
def process(self, text):
7474
return Textile(**self.class_parms).textile(text, **self.method_parms)

0 commit comments

Comments
 (0)