"image/bmp": 'bmp', "image/gif": 'gif', "image/jpeg": 'jpeg', "image/pjpeg": 'pjpeg', "image/png": 'png', "image/x-png": 'png', "text/html": 'html', "text/java": 'java', "text/plain": 'txt', "text/x-java": 'java', "text/x-java-source": 'java', "text/x-log": 'log', "text/xml": 'xml' } jive = JiveConverter() def parse_date(date_str): return datetime.strptime(date_str[:-8], '%Y/%m/%d %H:%M:%S') def fix_internal_links_in_post(post): """will replace old internal urls with the new ones.""" def link_is_naked(match): """naked link either starts at the beginning of string or is not inside the jive link construct: [...]""" pos = match.start() # the second test is rather naive as it assumes that a # | will be preceded by something like [some link # which we don't test here return pos < 2 or post.text[pos-2] not in ('[', '|')
def setUp(self): self.converter = JiveConverter()
class JiveTests(unittest.TestCase): def setUp(self): self.converter = JiveConverter() def convert(self, text): return self.converter.convert(text) def test_headings(self): text = """ h1. Heading1 blah blah h2. Heading 2 blah blah h3. Heading 3 blah blah """ expected = """<h1>Heading1</h1> <p>blah blah</p> <h2>Heading 2</h2> <p>blah blah</p> <h3>Heading 3</h3> <p>blah blah</p>""" output = self.convert(text) self.assertEqual(output, expected) def test_horizontal_rules(self): text = """ ----- some text """ expected = "<hr/>\n<p>some text</p>" self.assertEqual(self.convert(text), expected) def test_list1(self): text = """ * one * two * three """ expected = """<ul> <li>one</li> <li>two</li> <li>three</li> </ul>""" self.assertEqual(self.convert(text), expected) def test_list2(self): text = """ # one # two # three """ expected = """<ol> <li>one</li> <li>two</li> <li>three</li> </ol>""" self.assertEqual(self.convert(text), expected) def test_list3(self): text = """ * one ** two * three """ expected = """<ul> <li>one</li> <li> <ul> <li>two</li> </ul> </li> <li>three</li> </ul>""" self.assertEqual(self.convert(text), expected) def test_bq1(self): text = """ bq. two plus two equals four """ expected = """<blockquote><p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq2(self): text = """{quote} two plus two equals four {quote} """ expected = """<blockquote><p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq3(self): text = """[quote=alex] two plus two equals four {quote} """ expected = """<blockquote><span class="quote-header">alex wrote:</span><br/> <p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq4(self): text = """[quote=alex] two plus two equals four two plus two equals four {quote} """ expected = """<blockquote><span class="quote-header">alex wrote:</span><br/> <p>two plus two equals four</p> <p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq5(self): text = """> {quote:title=alex wrote:}{quote} > two plus two equals four > > two plus two equals four """ expected = """<blockquote><span class="quote-header">alex wrote:</span><br/> <p>two plus two equals four</p> <p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_code0(self): text = """something {code}#comment _haha_ http://example.com {code}""" expected = """<p>something</p> <pre><code>#comment _haha_ http://example.com </code></pre>""" self.assertEqual(self.convert(text), expected) def test_code1(self): text = """something {code:html}#comment _haha_ http://example.com {code}""" expected = """<p>something</p> <pre><code>#comment _haha_ http://example.com </code></pre>""" self.assertEqual(self.convert(text), expected) def test_links1(self): text = """[url]http://example.com/2[/url] blah http://example.com/1 blah [link text3|http://example.com/3|tooltip text3] blah2 [link text4|http://example.com/4|tooltip text4] !http://example.com/img.png! [[email protected]] [/some/file/] """ expected = """<p><a href="http://example.com/2">http://example.com/2</a> blah<br/> <a href="http://example.com/1">http://example.com/1</a> blah<br/> <a href="http://example.com/3" title="tooltip text3">link text3</a> blah2 <br/> <a href="http://example.com/4" title="tooltip text4">link text4</a><br/> <img src="http://example.com/img.png"/><br/> <a href="mailto:[email protected]">[email protected]</a><br/> <a href="/some/file/">/some/file/</a></p>""" self.assertEqual(self.convert(text), expected) def test_bold(self): text = "*some text*" self.assertEqual(self.convert(text), "<p><strong>some text</strong></p>") def test_italics(self): text = "+some text+" self.assertEqual(self.convert(text), "<p><em>some text</em></p>") def test_underline(self): text = "_some text_" self.assertEqual(self.convert(text), '<p><span class="underline">some text</span></p>') def test_super(self): text = "e = mc^2^" self.assertEqual(self.convert(text), "<p>e = mc<sup>2</sup></p>") def test_sub(self): text = "e~1~" self.assertEqual(self.convert(text), "<p>e<sub>1</sub></p>") def test_strike(self): text = "--A--" self.assertEqual(self.convert(text), "<p><strike>A</strike></p>") def test_leading_spaces(self): """test lazy copy-pasted code""" text = """ function() { alert('hi'); } """ expected = """<p>function() {<br/> alert('hi');<br/> }</p>""" self.assertEqual(self.convert(text), expected) def test_fancy(self): text = """ h1. Once [upon|http://example.com] a *time* There was a queen who said: {quote} I _find_ *this* interesting {code}e = mc^2;{code} As you said: # one # two # *three* # [four|http://example.com/four|item four] # *five*, --six--, +seven+, _eight_ {quote} h2. Another time Nothing happened. """ expected = """<h1>Once <a href="http://example.com">upon</a> a <strong>time</strong></h1> <p>There was a queen who said:</p> <blockquote><p>I <span class="underline">find</span> <strong>this</strong> interesting</p> <pre><code>e = mc^2;</code></pre> <p>As you said:</p> <ol> <li>one</li> <li>two</li> <li><strong>three</strong></li> <li><a href="http://example.com/four" title="item four">four</a></li> <li><strong>five</strong>, <strike>six</strike>, <em>seven</em>, <span class="underline">eight</span></li> </ol></blockquote> <h2>Another time</h2> <p>Nothing happened.</p>""" self.assertEqual(self.convert(text), expected) def test_internal_link_re(self): self.assertTrue(link_re.search("soasao /message.jspa?messageID=8477 a")) self.assertTrue(link_re.search("/thread.jspa?messageID=10175&#10175")) self.assertTrue(link_re.search("/thread.jspa?messageID=10662#10662")) self.assertTrue(link_re.search("/thread.jspa?messageID=11058")) self.assertTrue(link_re.search("/thread.jspa?threadID=1888&tstart=210")) self.assertTrue(link_re.search("/thread.jspa?threadID=3087&tstart=-258"))
class JiveTests(unittest.TestCase): def setUp(self): self.converter = JiveConverter() def convert(self, text): return self.converter.convert(text) def test_headings(self): text = """ h1. Heading1 blah blah h2. Heading 2 blah blah h3. Heading 3 blah blah """ expected = """<h1>Heading1</h1> <p>blah blah</p> <h2>Heading 2</h2> <p>blah blah</p> <h3>Heading 3</h3> <p>blah blah</p>""" output = self.convert(text) self.assertEqual(output, expected) def test_horizontal_rules(self): text = """ ----- some text """ expected = '<hr/>\n<p>some text</p>' self.assertEqual(self.convert(text), expected) def test_list1(self): text = """ * one * two * three """ expected = """<ul> <li>one</li> <li>two</li> <li>three</li> </ul>""" self.assertEqual(self.convert(text), expected) def test_list2(self): text = """ # one # two # three """ expected = """<ol> <li>one</li> <li>two</li> <li>three</li> </ol>""" self.assertEqual(self.convert(text), expected) def test_list3(self): text = """ * one ** two * three """ expected = """<ul> <li>one</li> <li> <ul> <li>two</li> </ul> </li> <li>three</li> </ul>""" self.assertEqual(self.convert(text), expected) def test_bq1(self): text = """ bq. two plus two equals four """ expected = """<blockquote><p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq2(self): text = """{quote} two plus two equals four {quote} """ expected = """<blockquote><p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq3(self): text = """[quote=alex] two plus two equals four {quote} """ expected = """<blockquote><span class="quote-header">alex wrote:</span><br/> <p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq4(self): text = """[quote=alex] two plus two equals four two plus two equals four {quote} """ expected = """<blockquote><span class="quote-header">alex wrote:</span><br/> <p>two plus two equals four</p> <p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_bq5(self): text = """> {quote:title=alex wrote:}{quote} > two plus two equals four > > two plus two equals four """ expected = """<blockquote><span class="quote-header">alex wrote:</span><br/> <p>two plus two equals four</p> <p>two plus two equals four</p></blockquote>""" self.assertEqual(self.convert(text), expected) def test_code0(self): text = """something {code}#comment _haha_ http://example.com {code}""" expected = """<p>something</p> <pre><code>#comment _haha_ http://example.com </code></pre>""" self.assertEqual(self.convert(text), expected) def test_code1(self): text = """something {code:html}#comment _haha_ http://example.com {code}""" expected = """<p>something</p> <pre><code>#comment _haha_ http://example.com </code></pre>""" self.assertEqual(self.convert(text), expected) def test_links1(self): text = """[url]http://example.com/2[/url] blah http://example.com/1 blah [link text3|http://example.com/3|tooltip text3] blah2 [link text4|http://example.com/4|tooltip text4] !http://example.com/img.png! [[email protected]] [/some/file/] """ expected = """<p><a href="http://example.com/2">http://example.com/2</a> blah<br/> <a href="http://example.com/1">http://example.com/1</a> blah<br/> <a href="http://example.com/3" title="tooltip text3">link text3</a> blah2 <br/> <a href="http://example.com/4" title="tooltip text4">link text4</a><br/> <img src="http://example.com/img.png"/><br/> <a href="mailto:[email protected]">[email protected]</a><br/> <a href="/some/file/">/some/file/</a></p>""" self.assertEqual(self.convert(text), expected) def test_bold(self): text = "*some text*" self.assertEqual(self.convert(text), '<p><strong>some text</strong></p>') def test_italics(self): text = "+some text+" self.assertEqual(self.convert(text), '<p><em>some text</em></p>') def test_underline(self): text = "_some text_" self.assertEqual(self.convert(text), '<p><span class="underline">some text</span></p>') def test_super(self): text = "e = mc^2^" self.assertEqual(self.convert(text), '<p>e = mc<sup>2</sup></p>') def test_sub(self): text = "e~1~" self.assertEqual(self.convert(text), '<p>e<sub>1</sub></p>') def test_strike(self): text = "--A--" self.assertEqual(self.convert(text), '<p><strike>A</strike></p>') def test_leading_spaces(self): """test lazy copy-pasted code""" text = """ function() { alert('hi'); } """ expected = """<p>function() {<br/> alert('hi');<br/> }</p>""" self.assertEqual(self.convert(text), expected) def test_fancy(self): text = """ h1. Once [upon|http://example.com] a *time* There was a queen who said: {quote} I _find_ *this* interesting {code}e = mc^2;{code} As you said: # one # two # *three* # [four|http://example.com/four|item four] # *five*, --six--, +seven+, _eight_ {quote} h2. Another time Nothing happened. """ expected = """<h1>Once <a href="http://example.com">upon</a> a <strong>time</strong></h1> <p>There was a queen who said:</p> <blockquote><p>I <span class="underline">find</span> <strong>this</strong> interesting</p> <pre><code>e = mc^2;</code></pre> <p>As you said:</p> <ol> <li>one</li> <li>two</li> <li><strong>three</strong></li> <li><a href="http://example.com/four" title="item four">four</a></li> <li><strong>five</strong>, <strike>six</strike>, <em>seven</em>, <span class="underline">eight</span></li> </ol></blockquote> <h2>Another time</h2> <p>Nothing happened.</p>""" self.assertEqual(self.convert(text), expected) def test_internal_link_re(self): self.assertTrue( link_re.search('soasao /message.jspa?messageID=8477 a')) self.assertTrue( link_re.search('/thread.jspa?messageID=10175&#10175')) self.assertTrue(link_re.search('/thread.jspa?messageID=10662#10662')) self.assertTrue(link_re.search('/thread.jspa?messageID=11058')) self.assertTrue( link_re.search('/thread.jspa?threadID=1888&tstart=210')) self.assertTrue( link_re.search('/thread.jspa?threadID=3087&tstart=-258'))