diff options
author | Jens Diemer <github.com@jensdiemer.de> | 2010-04-04 17:49:21 +0000 |
---|---|---|
committer | Jens Diemer <github.com@jensdiemer.de> | 2010-04-04 17:49:21 +0000 |
commit | ec15ef78256d12b9e99348bcae147b8b26b9b468 (patch) | |
tree | 02e21e311bcedab73cfc8054391e4419e798baaa | |
parent | fa7295da91ef2839da747f7b5d818f48684a500b (diff) | |
download | creole-ec15ef78256d12b9e99348bcae147b8b26b9b468.tar.gz |
- bugfix in setup.py
- Cleanup DocStrings
- add unittests
-rw-r--r-- | MANIFEST.in | 1 | ||||
-rw-r--r-- | README | 6 | ||||
-rw-r--r-- | creole/__init__.py | 26 | ||||
-rw-r--r-- | creole/creole2html.py | 44 | ||||
-rw-r--r-- | creole/creole_parser.py | 12 | ||||
-rw-r--r-- | creole/html2creole.py | 76 | ||||
-rwxr-xr-x | setup.py | 19 | ||||
-rw-r--r-- | tests/run_all_tests.py | 7 | ||||
-rw-r--r-- | tests/test_creole2html.py | 120 | ||||
-rw-r--r-- | tests/test_cross_compare.py | 1 |
10 files changed, 171 insertions, 141 deletions
diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..f1c47ff --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include AUTHORS @@ -32,6 +32,12 @@ See also: http://code.google.com/p/python-creole/source/browse/trunk/demo.py history ========= +- v0.2.6 + + - bugfix in setup.py + - Cleanup DocStrings + - add unittests + - v0.2.5 - creole2html: Bugfix if "--", "//" etc. stands alone, see also: http://code.google.com/p/python-creole/issues/detail?id=12 diff --git a/creole/__init__.py b/creole/__init__.py index 42a1cc1..cb78417 100644 --- a/creole/__init__.py +++ b/creole/__init__.py @@ -1,17 +1,31 @@ # -*- coding: utf-8 -*- + +__version__ = (0, 2, 6) +__api__ = (1, 0) # Creole 1.0 spec - http://wikicreole.org/ + + +import os import sys from creole_parser import Parser from creole2html import HtmlEmitter from html2creole import Html2CreoleParser, Html2CreoleEmitter -# Important for setuptools: -# - Only use . as a separator -# - No spaces: "0.8.0 RC2" -> "0.8.0RC2" -# http://peak.telecommunity.com/DevCenter/setuptools#specifying-your-project-s-version -__version__ = (0, 2, 5, "") -VERSION_STRING = "0.2.5" +try: + from django.utils.version import get_svn_revision +except ImportError: + pass +else: + path = os.path.split(os.path.abspath(__file__))[0] + svn_revision = get_svn_revision(path) + if svn_revision != u'SVN-unknown': + svn_revision = svn_revision.replace("-", "").lower() + __version__ += (svn_revision,) + + +VERSION_STRING = '.'.join(str(part) for part in __version__) +API_STRING = '.'.join(str(integer) for integer in __api__) def creole2html(markup_string, debug=False, **kwargs): diff --git a/creole/creole2html.py b/creole/creole2html.py index 326c521..a1861ab 100644 --- a/creole/creole2html.py +++ b/creole/creole2html.py @@ -1,43 +1,11 @@ -# -*- coding: utf-8 -*- +# coding: utf-8 """ -WikiCreole to HTML converter -This program is an example of how the creole.py WikiCreole parser -can be used. - -Copyright (c) 2007, Radomir Dopieralski <creole@sheep.art.pl> -:copyleft: 2008 by the PyLucid team, see AUTHORS for more details. - - PyLucid Updates by the PyLucid team: - - Bugfixes and better html code style - - Add a passthrough for all django template blocktags - - Add a passthrough for html code lines - -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions -are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + WikiCreole to HTML converter + + @copyright: 2007 MoinMoin:RadomirDopieralski, + 2008-2010 JensDiemer + @license: GNU GPL v3 or above, see LICENSE for details. """ import sys, re, traceback diff --git a/creole/creole_parser.py b/creole/creole_parser.py index 133e0d3..58b1348 100644 --- a/creole/creole_parser.py +++ b/creole/creole_parser.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# coding: utf-8 """ Creole wiki markup parser @@ -17,17 +17,11 @@ unrecognized schemes (like wtf://server/path) triggering italic rendering for the rest of the paragraph. - PyLucid Updates by the PyLucid team: - - Bugfixes and better html code style - - Make the image tag match more strict, so it doesn't clash with - django template tags - - Add a passthrough for all django template blocktags - - Add a passthrough for html code lines @copyright: 2007 MoinMoin:RadomirDopieralski (creole 0.5 implementation), 2007 MoinMoin:ThomasWaldmann (updates) - 2008 PyLucid:JensDiemer (PyLucid patches) - @license: GNU GPL, see COPYING for details. + 2008-2010 JensDiemer + @license: GNU GPL v3 or above, see LICENSE for details. """ import re diff --git a/creole/html2creole.py b/creole/html2creole.py index c555303..43a180d 100644 --- a/creole/html2creole.py +++ b/creole/html2creole.py @@ -12,7 +12,7 @@ created by Jens Diemer - :copyleft: 2009 by the python-creole team, see AUTHORS for more details. + :copyleft: 2009-2010 by the python-creole team, see AUTHORS for more details. :license: GNU GPL v3 or above, see LICENSE for more details. """ @@ -110,7 +110,7 @@ class DocNode: return u"<DocNode %s: %r>" % (self.kind, self.content) def debug(self): - print "_"*80 + print "_" * 80 print "\tDocNode - debug:" print "str(): %s" % self print "attributes:" @@ -176,10 +176,10 @@ def strip_html(html_code): u'<p>a<pre>preformated area</pre>foo</p>' """ def strip_tag(match): - block = match.group(0) - end_tag = match.group("end") in ("/", u"/") + block = match.group(0) + end_tag = match.group("end") in ("/", u"/") startend_tag = match.group("startend") in ("/", u"/") - tag = match.group("tag") + tag = match.group("tag") # print "_"*40 # print match.groupdict() @@ -288,7 +288,7 @@ class Html2CreoleParser(HTMLParser): print "append blockdata: %r" % data assert isinstance(data, unicode), "blockdata is not unicode" self.blockdata.append(data) - id = len(self.blockdata)-1 + id = len(self.blockdata) - 1 return u'<%s type="%s" id="%s" />' % (placeholder, type, id) def _pre_pre_inline_cut(self, groups): @@ -327,13 +327,13 @@ class Html2CreoleParser(HTMLParser): data = strip_html(data) if self.debugging: - print "_"*79 + print "_" * 79 print "raw data:" print repr(raw_data) - print " -"*40 + print " -" * 40 print "cleaned data:" print data - print "-"*79 + print "-" * 79 # print clean_data.replace(">", ">\n") # print "-"*79 @@ -366,14 +366,14 @@ class Html2CreoleParser(HTMLParser): def handle_starttag(self, tag, attrs): self.debug_msg("starttag", "%r atts: %s" % (tag, attrs)) - + if tag in IGNORE_TAGS: return headline = headline_tag_re.match(tag) if headline: self.cur = DocNode( - "headline", self.cur, level = int(headline.group(1)) + "headline", self.cur, level=int(headline.group(1)) ) return @@ -393,7 +393,7 @@ class Html2CreoleParser(HTMLParser): self.debug_msg("data", "%r" % data) if isinstance(data, str): data = unicode(data) - DocNode("data", self.cur, content = data) + DocNode("data", self.cur, content=data) def handle_charref(self, name): self.debug_msg("charref", "%r" % name) @@ -412,7 +412,7 @@ class Html2CreoleParser(HTMLParser): DocNode( "%s_%s" % (tag, attr_dict["type"]), self.cur, - content = self.blockdata[id], + content=self.blockdata[id], # attrs = attr_dict ) else: @@ -421,7 +421,7 @@ class Html2CreoleParser(HTMLParser): def handle_endtag(self, tag): if tag in IGNORE_TAGS: return - + self.debug_msg("endtag", "%r" % tag) self.debug_msg("starttag", "%r" % self.get_starttag_text()) @@ -444,7 +444,7 @@ class Html2CreoleParser(HTMLParser): """ Display the current document tree """ - print "_"*80 + print "_" * 80 if start_node == None: start_node = self.root @@ -452,10 +452,10 @@ class Html2CreoleParser(HTMLParser): else: print " tree from %s:" % start_node - print "="*80 + print "=" * 80 def emit(node, ident=0): for child in node.children: - txt = u"%s%s" % (u" "*ident, child.kind) + txt = u"%s%s" % (u" " * ident, child.kind) if child.content: txt += ": %r" % child.content @@ -467,9 +467,9 @@ class Html2CreoleParser(HTMLParser): txt += " - level: %r" % child.level print txt - emit(child, ident+4) + emit(child, ident + 4) emit(start_node) - print "*"*80 + print "*" * 80 @@ -592,7 +592,7 @@ class Html2CreoleEmitter(object): #node.debug() attrs = node.get_attrs_as_string() if attrs: - attrs = " "+attrs + attrs = " " + attrs tag_data = { "tag": node.kind, @@ -616,7 +616,7 @@ class Html2CreoleEmitter(object): #node.debug() attrs = node.get_attrs_as_string() if attrs: - attrs = " "+attrs + attrs = " " + attrs tag_data = { "tag": node.kind, @@ -695,7 +695,7 @@ class Html2CreoleEmitter(object): return u"\n" def headline_emit(self, node): - return u"%s %s\n" % (u"="*node.level, self.emit_children(node)) + return u"%s %s\n" % (u"=" * node.level, self.emit_children(node)) #-------------------------------------------------------------------------- @@ -740,17 +740,17 @@ class Html2CreoleEmitter(object): def img_emit(self, node): src = node.attrs["src"] - + title = node.attrs.get("title", "") alt = node.attrs.get("alt", "") - if len(alt)>len(title): # Use the longest one + if len(alt) > len(title): # Use the longest one text = alt else: text = title - - if text=="": # Use filename as picture text + + if text == "": # Use filename as picture text text = posixpath.basename(src) - + return u"{{%s|%s}}" % (src, text) #-------------------------------------------------------------------------- @@ -810,10 +810,10 @@ class Html2CreoleEmitter(object): content = u"= %s" % content self._table.add_td(content) return u"" - + def td_emit(self, node): content = self.emit_children(node) - content = self._escape_linebreaks(content) + content = self._escape_linebreaks(content) self._table.add_td(content) return u"" @@ -887,19 +887,19 @@ class CreoleTable(object): self.debug_msg = debug_msg self.rows = [] self.row_index = None - + def add_tr(self): self.debug_msg("Table.add_tr", "") self.rows.append([]) - self.row_index = len(self.rows)-1 - + self.row_index = len(self.rows) - 1 + def add_td(self, text): - if self.row_index==None: + if self.row_index == None: self.add_tr() - + self.debug_msg("Table.add_td", text) self.rows[self.row_index].append(text) - + def get_creole(self): """ return the table data in creole markup. """ # preformat every table cell @@ -924,7 +924,7 @@ class CreoleTable(object): for row in cells: cells = [cell.ljust(width) for cell, width in zip(row, widths)] lines.append("|" + "|".join(cells) + "|") - + result = "\n".join(lines) self.debug_msg("Table.get_creole", result) @@ -953,8 +953,8 @@ if __name__ == '__main__': debug=True ) content = e.emit() - print "*"*79 + print "*" * 79 print content - print "*"*79 + print "*" * 79 print content.replace(" ", ".").replace("\n", "\\n\n") @@ -16,24 +16,27 @@ """ import os -import sys from setuptools import setup, find_packages from creole import VERSION_STRING +PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__)) + + def get_authors(): authors = [] - f = file("AUTHORS", "r") + f = file(os.path.join(PACKAGE_ROOT, "AUTHORS"), "r") for line in f: if line.startswith('*'): authors.append(line[1:].strip()) f.close() return authors + def get_long_description(): - f = file("README", "r") + f = file(os.path.join(PACKAGE_ROOT, "README"), "r") long_description = f.read() f.close() long_description.strip() @@ -44,15 +47,15 @@ setup( name='python-creole', version=VERSION_STRING, description='python-creole is an open-source creole2html and html2creole converter in pure Python.', - long_description = get_long_description(), - author = get_authors(), - maintainer = "Jens Diemer", + long_description=get_long_description(), + author=get_authors(), + maintainer="Jens Diemer", url='http://code.google.com/p/python-creole/', packages=find_packages(), include_package_data=True, # include package data under svn source control zip_safe=True, - classifiers = [ - "Development Status :: 4 - Beta", + classifiers=[ +# "Development Status :: 4 - Beta", "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Intended Audience :: Developers", diff --git a/tests/run_all_tests.py b/tests/run_all_tests.py index e3eb1d9..c19716d 100644 --- a/tests/run_all_tests.py +++ b/tests/run_all_tests.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- +# coding: utf-8 """ run all unittests ~~~~~~~~~~~~~~~~~ @@ -7,8 +7,8 @@ Last commit info: ~~~~~~~~~~~~~~~~~ - $LastChangedDate:$ - $Rev:$ + $LastChangedDate$ + $Rev$ $Author$ :copyleft: 2008-2009 by python-creole team, see AUTHORS for more details. @@ -24,4 +24,3 @@ from tests.test_html2creole import TestHtml2Creole, TestHtml2CreoleMarkup if __name__ == '__main__': unittest.main() -
\ No newline at end of file diff --git a/tests/test_creole2html.py b/tests/test_creole2html.py index 0aba66e..abbb02a 100644 --- a/tests/test_creole2html.py +++ b/tests/test_creole2html.py @@ -46,7 +46,7 @@ class TestCreole2html(unittest.TestCase): verbose=2, stderr=my_stderr, debug=False ) error_msg = my_stderr.getvalue() - + # Check if we get a traceback information into our stderr handler must_have = ( "<pre>", "</pre>", @@ -60,34 +60,34 @@ class TestCreole2html(unittest.TestCase): part in error_msg, "String %r not found in:\n******\n%s******" % (part, error_msg) ) - + def test_default_macro1(self): """ Test the default "html" macro, found in ./creole/default_macros.py """ html = creole2html( markup_string=u"<<html>><p>foo</p><</html>><bar?>", - verbose=1, + verbose=1, # stderr=sys.stderr, debug=False ) self.assertEqual(html, u'<p>foo</p>\n<p><bar?></p>\n') - + def test_default_macro2(self): html = creole2html( markup_string=u"<<html>>{{{<nocode>}}}<</html>>", - verbose=1, + verbose=1, # stderr=sys.stderr, debug=False ) self.assertEqual(html, u'{{{<nocode>}}}\n') - + def test_default_macro3(self): html = creole2html( markup_string=u"<<html>>1<</html>><<html>>2<</html>>", - verbose=1, + verbose=1, # stderr=sys.stderr, debug=False ) self.assertEqual(html, u'1\n2\n') - + def test_macro_class(self): """ simple test for the "macro API" @@ -95,35 +95,35 @@ class TestCreole2html(unittest.TestCase): class TestMacro(object): def test(self, args, text): return u"XXX%s|%sXXX" % (args, text) - + html = creole2html( markup_string=u"<<test foo=1>>bar<</test>>", macros=TestMacro() ) self.assertEqual(html, u'XXXfoo=1|barXXX\n') - + def test_macro_dict(self): """ simple test for the "macro API" """ def test(args, text): - return u"XXX%s|%sXXX" % (args, text) - + return u"XXX%s|%sXXX" % (args, text) + html = creole2html( markup_string=u"<<test foo=1>>bar<</test>>", macros={"test": test} ) self.assertEqual(html, u'XXXfoo=1|barXXX\n') - + def test_macro_callable(self): """ simple test for the "macro API" """ def testmacro(macroname, args, text): - if macroname=="test": + if macroname == "test": return u"XXX%s|%sXXX" % (args, text) raise AssertionError("Wrong macro name?") - + html = creole2html( markup_string=u"<<test foo=1>>bar<</test>>", macros=testmacro @@ -134,6 +134,8 @@ class TestCreole2html(unittest.TestCase): + + class TestCreole2htmlMarkup(BaseCreoleTest): def assertCreole(self, *args, **kwargs): @@ -149,13 +151,13 @@ class TestCreole2htmlMarkup(BaseCreoleTest): """ Test all existing lineending version """ out_string = creole2html(u"first\nsecond") self.assertEqual(out_string, u"<p>first<br />\nsecond</p>\n") - + out_string = creole2html(u"first\rsecond") self.assertEqual(out_string, u"<p>first<br />\nsecond</p>\n") - + out_string = creole2html(u"first\r\nsecond") self.assertEqual(out_string, u"<p>first<br />\nsecond</p>\n") - + #-------------------------------------------------------------------------- def test_creole_linebreak(self): @@ -186,7 +188,7 @@ class TestCreole2htmlMarkup(BaseCreoleTest): <p><p>This escaped, too.</p></p> """) - + def test_escape_char(self): self.assertCreole(r""" ~#1 @@ -202,24 +204,36 @@ class TestCreole2htmlMarkup(BaseCreoleTest): [[Link]]</p> """) - def test_cross_paragraphs(self): + def test_cross_lines(self): + """ + TODO: bold/italics cross lines + see: http://code.google.com/p/python-creole/issues/detail?id=13 + """ self.assertCreole(r""" Bold and italics should //be - able// to cross lines. + able// to **cross + lines.** + """, """ + <p>Bold and italics should <i>be<br /> + able</i> to <strong>cross<br /> + lines.</strong></p> + """) - But, should //not be... + def test_cross_paragraphs(self): + self.assertCreole(r""" + Bold and italics should //not be... - ...able// to cross paragraphs. + ...able// to **cross + + paragraphs.** """, """ - <p>Bold and italics should <i>be<br /> - able</i> to cross lines.</p> + <p>Bold and italics should //not be...</p> - <p>But, should <i>not be...</i></p> + <p>...able// to **cross</p> - <p>...able<i> to cross paragraphs.</i></p> + <p>paragraphs.**</p> """) - - + def test_list_special(self): """ optional whitespace before the list @@ -250,7 +264,7 @@ class TestCreole2htmlMarkup(BaseCreoleTest): </ol></li> </ol> """) - + def test_macro_basic(self): """ Test the three diferent macro types with a "unittest macro" @@ -278,8 +292,8 @@ class TestCreole2htmlMarkup(BaseCreoleTest): [args="foo3" text: the text] <p>the end</p> - """) - + """) + def test_macro_html1(self): self.assertCreole(r""" html macro: @@ -295,7 +309,7 @@ class TestCreole2htmlMarkup(BaseCreoleTest): <p>inline: {...} code</p> """, #debug=True ) - + def test_macro_not_exist1(self): """ not existing macro with creole2html.HtmlEmitter(verbose=1): @@ -321,12 +335,12 @@ class TestCreole2htmlMarkup(BaseCreoleTest): [Error: Macro 'notexisttoo' doesn't exist] </p> """ - + self.assertCreole(source_string, should_string, verbose=1) - + #---------------------------------------------------------------------- # Test with verbose=2 ans a StringIO stderr handler - + def test_macro_not_exist2(self): """ not existing macro with creole2html.HtmlEmitter(verbose=0): @@ -349,7 +363,7 @@ class TestCreole2htmlMarkup(BaseCreoleTest): """, verbose=0 ) - + def test_image(self): """ test image tag with different picture text """ self.assertCreole(r""" @@ -363,7 +377,6 @@ class TestCreole2htmlMarkup(BaseCreoleTest): """) def test_links(self): - self.assertCreole(r""" [[/foobar/Creole_(Markup)]] [[http://de.wikipedia.org/wiki/Creole_(Markup)|Creole@wikipedia]] @@ -372,6 +385,37 @@ class TestCreole2htmlMarkup(BaseCreoleTest): <a href="http://de.wikipedia.org/wiki/Creole_(Markup)">Creole@wikipedia</a></p> """) + def test_macro_get_raw_content(self): + """ + A macro should get the complete content without any modifications. + """ + def testmacro(macroname, args, text): + self.failUnlessEqual(macroname, "code") + text = text.replace("{", "{").replace("}", "}") + return text + + html = creole2html( + markup_string=self._prepare_text(u""" + intro + <<code>> + a {{ django }} variable, not a image ;) + Foo {% blocktag %} bar {% endblocktag %}! + **bold** //italics// + <</code>> + outro + """), + macros=testmacro + ) + self.assertEqual(html, self._prepare_text(u""" + <p>intro</p> + a {{ django }} variable, not a image ;) + Foo {% blocktag %} bar {% endblocktag %}! + **bold** //italics// + <p>outro</p> + + """)) + + if __name__ == '__main__': unittest.main() #if __name__ == '__main__': diff --git a/tests/test_cross_compare.py b/tests/test_cross_compare.py index ffa4843..5345657 100644 --- a/tests/test_cross_compare.py +++ b/tests/test_cross_compare.py @@ -480,6 +480,7 @@ class CrossCompareTests(BaseCreoleTest): greater-than sign: ></p> """) + # def test_macro_html1(self): # self.assertCreole(r""" # <<a_not_existing_macro>> |