def test_sanitize(): test = "a paragraph of benign text" result = "\t<p>a paragraph of benign text</p>" expect = textile.Textile().parse(test, sanitize=True) assert result == expect test = """<p style="width: expression(alert('evil'));">a paragraph of evil text</p>""" result = '<p style="">a paragraph of evil text</p>' expect = textile.Textile().parse(test, sanitize=True) assert result == expect test = """<p>a paragraph of benign text<br />and more text</p>""" result = '<p>a paragraph of benign text<br />\nand more text</p>' expect = textile.Textile(html_type='html5').parse(test, sanitize=True) assert result == expect
def test_block_tags_false(): t = textile.Textile(block_tags=False) assert t.block_tags is False result = t.parse('test') expect = 'test' assert result == expect
def test_block(): t = textile.Textile() result = t.block('h1. foobar baby') expect = '\t<h1>foobar baby</h1>' assert result == expect b = Block(t, "bq", "", None, "", "Hello BlockQuote") expect = ('blockquote', OrderedDict(), 'p', OrderedDict(), 'Hello BlockQuote') result = (b.outer_tag, b.outer_atts, b.inner_tag, b.inner_atts, b.content) assert result == expect b = Block(t, "bq", "", None, "http://google.com", "Hello BlockQuote") citation = '{0}1:url'.format(t.uid) expect = ('blockquote', OrderedDict([('cite', '{0.uid}{0.refIndex}:url'.format(t))]), 'p', OrderedDict(), 'Hello BlockQuote') result = (b.outer_tag, b.outer_atts, b.inner_tag, b.inner_atts, b.content) assert result == expect b = Block(t, "bc", "", None, "", 'printf "Hello, World";') # the content of text will be turned shelved, so we'll asert only the # deterministic portions of the expected values, below expect = ('pre', OrderedDict(), 'code', OrderedDict()) result = (b.outer_tag, b.outer_atts, b.inner_tag, b.inner_atts) assert result == expect b = Block(t, "h1", "", None, "", "foobar") expect = ('h1', OrderedDict(), '', OrderedDict(), 'foobar') result = (b.outer_tag, b.outer_atts, b.inner_tag, b.inner_atts, b.content) assert result == expect
def test_encode_url(): # I tried adding these as doctests, but the unicode tests weren't # returning the correct results. t = textile.Textile() url = 'http://www.example.local' result = 'http://www.example.local' eurl = t.encode_url(url) assert eurl == result url = 'http://[email protected]' result = 'http://[email protected]' eurl = t.encode_url(url) assert eurl == result url = 'http://*****:*****@www.example.local' result = 'http://*****:*****@www.example.local' eurl = t.encode_url(url) assert eurl == result url = 'http://*****:*****@www.example.local/Ubermensch' result = 'http://*****:*****@www.example.local/Ubermensch' eurl = t.encode_url(url) assert eurl == result url = "http://*****:*****@www.example.local/Übermensch" result = "http://*****:*****@www.example.local/%C3%9Cbermensch" eurl = t.encode_url(url) assert eurl == result url = 'http://*****:*****@www.example.local:8080/Übermensch' result = 'http://*****:*****@www.example.local:8080/%C3%9Cbermensch' eurl = t.encode_url(url) assert eurl == result
def test_imagesize(): PIL = pytest.importorskip('PIL') test = "!http://www.google.com/intl/en_ALL/images/srpr/logo1w.png!" result = '\t<p><img alt="" height="95" src="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" width="275" /></p>' expect = textile.Textile(get_sizes=True).parse(test) assert result == expect
def test_github_pull_61(): """Fixed code block multiline encoding on quotes/span""" test = '''bc.. This is some TEXT inside a "Code BLOCK" { if (JSON) { return {"JSON":"value"} } } Back to 10-4 CAPS p.. Some multiline Paragragh Here is some output!!! "Some" CAPS''' expect = '''<pre><code>This is some TEXT inside a "Code BLOCK" { if (JSON) { return {"JSON":"value"} } } Back to 10-4 CAPS </code></pre> <p>Some multiline Paragragh</p> <p>Here is some output!!! “Some” <span class="caps">CAPS</span></p>''' t = textile.Textile() result = t.parse(test) assert result == expect
def testImageSize(self): try: from PIL import ImageFile except ImportError: raise SkipTest() test = "!http://www.google.com/intl/en_ALL/images/srpr/logo1w.png!" result = '\t<p><img src="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" alt="" width="275" height="95" /></p>' expect = textile.Textile(get_sizes=True).textile(test) eq_(result, expect)
def testSanitize(self): try: import html5lib except ImportError: raise SkipTest() test = "a paragraph of benign text" result = "\t<p>a paragraph of benign text</p>" expect = textile.Textile().textile(test, sanitize=True) eq_(result, expect) test = """<p style="width: expression(alert('evil'));">a paragraph of evil text</p>""" result = '<p style="">a paragraph of evil text</p>' expect = textile.Textile().textile(test, sanitize=True) eq_(result, expect) test = """<p>a paragraph of benign text<br />and more text</p>""" result = '<p>a paragraph of benign text<br>and more text</p>' expect = textile.Textile().textile(test, sanitize=True, html_type='html') eq_(result, expect)
def test_github_pull_62(): """Fix for paragraph multiline, only last paragraph is rendered correctly""" test = '''p.. First one 'is' ESCAPED "bad" p.. Second one 'is' ESCAPED "bad" p.. Third one 'is' ESCAPED "bad" p.. Last one 'is' ESCAPED "good" test''' expect = '''<p>First one ‘is’</p> <p><span class="caps">ESCAPED</span> “bad”</p> <p>Second one ‘is’</p> <p><span class="caps">ESCAPED</span> “bad”</p> <p>Third one ‘is’</p> <p><span class="caps">ESCAPED</span> “bad”</p> <p>Last one ‘is’</p> <p><span class="caps">ESCAPED</span> “good” test</p>''' t = textile.Textile() result = t.parse(test) assert result == expect
def test_blockcode_comment(): input = '###.. block comment\nanother line\n\np. New line' expect = '\t<p>New line</p>' t = textile.Textile() result = t.parse(input) assert result == expect
def test_blockcode_extended(): input = 'bc.. text\nmoretext\n\nevenmoretext\n\nmoremoretext\n\np. test' expect = '<pre><code>text\nmoretext\n\nevenmoretext\n\nmoremoretext</code></pre>\n\n\t<p>test</p>' t = textile.Textile() result = t.parse(input) assert result == expect
def testPBAColspan(self): eq_(textile.Textile().pba(r'\3', element='td'), ' colspan="3"')
from django import template from django.template.defaultfilters import stringfilter from django.utils.html import escape from django.utils.text import wrap from django.utils.safestring import mark_safe from django.core.urlresolvers import reverse from discipline.models import STEP_DESC, STEP_VIEW, PRE_LETTER_STEPS import textile Textile = textile.Textile(restricted=True) register = template.Library() @register.filter() def format_field(case, field): """ Format long-form text as required by the discipline module, making substitutions as appropriate. """ text = eval("case." + field) if text is None or text.strip() == "": return mark_safe('<p class="empty">None</p>') if field == 'contact_email_text': # special case: contact email is plain text return mark_safe( "<pre>" + escape(wrap(case.substitite_values(unicode(text)), 78)) + "</pre>") else: return mark_safe(Textile.textile(case.substitite_values( unicode(text))))
def test_relURL(): t = textile.Textile() t.restricted = True assert t.relURL("gopher://gopher.com/") == '#'
def test_github_pull_63(): """Forgot to set multiline_para to False""" test = '''p.. First one 'is' ESCAPED "bad" bc.. { First code BLOCK {"JSON":'value'} } p.. Second one 'is' ESCAPED "bad" p.. Third one 'is' ESCAPED "bad" bc.. { Last code BLOCK {"JSON":'value'} } p.. Last one 'is' ESCAPED "good" test''' expect = '''<p>First one ‘is’</p> <p><span class="caps">ESCAPED</span> “bad”</p> <pre><code>{ First code BLOCK {"JSON":'value'} }</code></pre> <p>Second one ‘is’</p> <p><span class="caps">ESCAPED</span> “bad”</p> <p>Third one ‘is’</p> <p><span class="caps">ESCAPED</span> “bad”</p> <pre><code>{ Last code BLOCK {"JSON":'value'} }</code></pre> <p>Last one ‘is’</p> <p><span class="caps">ESCAPED</span> “good” test</p>''' t = textile.Textile() result = t.parse(test) assert result == expect