def test_add_heading_ids(self): dialect = create_dialect(creole10_base, add_heading_ids=True) parse = Parser(dialect) self.assertEquals( parse("== Level 2"), '<h2 id="!level-2">Level 2</h2>\n') self.assertEquals( parse("= Level 1\n= Level 1"), '<h1 id="!level-1">Level 1</h1>\n<h1 id="!level-1_1">Level 1</h1>\n') self.assertEquals( parse("= [[http://www.google.com|Google]]\n"), """<h1 id="!google"><a href="http://www.google.com">Google</a></h1>\n""") self.assertEquals( parse("== http://www.google.com\n"), """<h2 id="!http-www-google-com"><a href="http://www.google.com">http://www.google.com</a></h2>\n""") self.assertEquals( parse("== ~http://www.google.com\n"), '<h2 id="!http-www-google-com">http://www.google.com</h2>\n') self.assertEquals( parse("[[foo bar#!a-heading_1]]"), wrap_result("""<a href="foo_bar#!a-heading_1">foo bar#!a-heading_1</a>""")) self.assertEquals( parse("[[#!a-heading]]"), wrap_result("""<a href="#!a-heading">#!a-heading</a>""")) self.assertEquals( parse("[[foo bar#1]]"), wrap_result("""<a href="foo_bar%231">foo bar#1</a>""")) self.assertEquals( parse("[[#1]]"), wrap_result("""<a href="%231">#1</a>""")) dialect = create_dialect(creole10_base, add_heading_ids='') parse = Parser(dialect) self.assertEquals( parse("== Level 2"), '<h2 id="level-2">Level 2</h2>\n')
def test_add_heading_ids(self): dialect = create_dialect(creole10_base, add_heading_ids=True) parse = Parser(dialect) self.assertEquals(parse("== Level 2"), '<h2 id="!level-2">Level 2</h2>\n') self.assertEquals( parse("= Level 1\n= Level 1"), '<h1 id="!level-1">Level 1</h1>\n<h1 id="!level-1_1">Level 1</h1>\n' ) self.assertEquals( parse("= [[http://www.google.com|Google]]\n"), """<h1 id="!google"><a href="http://www.google.com">Google</a></h1>\n""", ) self.assertEquals( parse("== http://www.google.com\n"), """<h2 id="!http-www-google-com"><a href="http://www.google.com">http://www.google.com</a></h2>\n""", ) self.assertEquals( parse("== ~http://www.google.com\n"), '<h2 id="!http-www-google-com">http://www.google.com</h2>\n' ) self.assertEquals( parse("[[foo bar#!a-heading_1]]"), wrap_result("""<a href="foo_bar#!a-heading_1">foo bar#!a-heading_1</a>"""), ) self.assertEquals(parse("[[#!a-heading]]"), wrap_result("""<a href="#!a-heading">#!a-heading</a>""")) self.assertEquals(parse("[[foo bar#1]]"), wrap_result("""<a href="foo_bar%231">foo bar#1</a>""")) self.assertEquals(parse("[[#1]]"), wrap_result("""<a href="%231">#1</a>""")) dialect = create_dialect(creole10_base, add_heading_ids="") parse = Parser(dialect) self.assertEquals(parse("== Level 2"), '<h2 id="level-2">Level 2</h2>\n')
def setUp(self): dialect = create_dialect(creole11_base, wiki_links_base_url='http://creoleparser.x10hosting.com/cgi-bin/creolepiki/', wiki_links_space_char='', no_wiki_monospace=False, macro_func=self.macroFactory) self.parse = Parser(dialect)
def setUp(self): def inter_wiki_link_maker(name): return name[::-1] def simple_class_maker(name): return name.lower() functions = { 'moo':inter_wiki_link_maker, 'goo':inter_wiki_link_maker, } base_urls = { 'goo': 'http://example.org', 'poo': 'http://example.org', 'Ohana': inter_wiki_url, } space_characters = { 'goo': '+', 'poo': '+', } class_functions = { 'moo':simple_class_maker, 'goo':simple_class_maker, } dialect = create_dialect(creole10_base, interwiki_links_path_funcs=functions, interwiki_links_class_funcs=class_functions, interwiki_links_base_urls=base_urls, interwiki_links_space_chars=space_characters ) self.parse = Parser(dialect)
def test_bodied_macros_option(self): def red(macro, e, *args, **kw): return builder.tag.__getattr__(macro.isblock and "div" or "span")(macro.parsed_body(), style="color:red") red.parse_body = True # return {'style':'color:red'} def blockquote(macro, e, *args, **kw): return builder.tag.blockquote(macro.parsed_body()) blockquote.parse_body = True # return {'tag':'blockquote'} MyDialect = create_dialect(creole11_base, bodied_macros=dict(red=red, blockquote=blockquote)) parse = Parser(MyDialect) self.assertEquals( parse("This block of <<red>>text **should** be monospace<</red>> now"), wrap_result('This block of <span style="color:red">text <strong>should</strong> be monospace</span> now'), ) self.assertEquals( parse("<<red>>\ntext **should** be monospace\n<</red>>"), '<div style="color:red"><p>text <strong>should</strong> be monospace</p>\n</div>', ) self.assertEquals( parse("This block of <<blockquote>>text **should** be monospace<</blockquote>> now"), wrap_result("This block of </p><blockquote>text <strong>should</strong> be monospace</blockquote><p> now"), ) self.assertEquals( parse("<<blockquote>>\ntext **should** be monospace\n<</blockquote>>"), "<blockquote><p>text <strong>should</strong> be monospace</p>\n</blockquote>", )
def test_simple_markup_option(self): MyDialect = create_dialect(creole10_base, simple_markup=[("*", "strong"), ("#", "code")]) parse = Parser(MyDialect) self.assertEquals( parse("This block of #text *should* be monospace# now"), wrap_result("This block of <code>text <strong>should</strong> be monospace</code> now"), )
def test_use_additions_option(self): dialect = create_dialect(creole11_base) # , use_additions=True) parse = Parser(dialect) self.assertEquals( parse("This block of ##text **should** be monospace## now"), wrap_result("This block of <code>text <strong>should</strong> be monospace</code> now"), )
def test_no_wiki_monospace_option(self): dialect = create_dialect(creole10_base, no_wiki_monospace=True) parse = Parser(dialect) self.assertEquals( parse("This block of {{{no_wiki **shouldn't** be monospace}}} now"), wrap_result("This block of <code>no_wiki **shouldn't** be monospace</code> now"), )
def setUp(self): dialect = create_dialect(creole11_base, wiki_links_base_url='', wiki_links_space_char='_', interwiki_links_base_urls={'Ohana': inter_wiki_url}, no_wiki_monospace=False, macro_func=self.macroFactory) self.parse = Parser(dialect)
def setUp(self): self.parse = Parser( dialect=create_dialect(creole11_base, wiki_links_base_url='', interwiki_links_base_urls={'Ohana': inter_wiki_url}, #use_additions=True, no_wiki_monospace=False ) )
def test_blog_style_endings_option(self): dialect = create_dialect(creole10_base, blog_style_endings=True) parse = Parser(dialect) self.assertEquals( parse("The first line\nthis text **should** be on the second line\n now third"), wrap_result("The first line<br />this text <strong>should</strong> be on the second line<br /> now third")) self.assertEquals( parse("The first line\\\\\nthis text **should** be on the second line\\\\\n now third"), wrap_result("The first line<br />this text <strong>should</strong> be on the second line<br /> now third"))
def setUp(self): creole2html = Parser( dialect=create_dialect(creole10_base, wiki_links_base_url=base_url, interwiki_links_base_urls={'Ohana': inter_wiki_url}, #use_additions=False, no_wiki_monospace=True, ) ) self.parse = creole2html
def setUp(self): creole2html = Parser( dialect=create_dialect(creole10_base, wiki_links_base_url=base_url, interwiki_links_base_urls={'Ohana': inter_wiki_url}, #use_additions=False, no_wiki_monospace=True ) ) self.parse = creole2html
def setUp(self): dialect = create_dialect(creole11_base, wiki_links_base_url='', wiki_links_space_char='_', interwiki_links_base_urls={'Ohana': inter_wiki_url}, no_wiki_monospace=False, macro_func=self.macroFactory, bodied_macros=dict(span=self.span, div=self.div), non_bodied_macros=dict(luca=self.luca), ) self.parse = Parser(dialect)
def test_external_links_class_option(self): dialect = create_dialect(creole10_base, external_links_class='external') parse = Parser(dialect) self.assertEquals( parse("[[campfire.jpg]]"), wrap_result("""<a href="campfire.jpg">campfire.jpg</a>""")) self.assertEquals( parse("[[/campfire.jpg]]"), wrap_result("""<a class="external" href="/campfire.jpg">/campfire.jpg</a>""")) self.assertEquals( parse("[[http://www.somesite.com/campfire.jpg]]"), wrap_result("""<a class="external" href="http://www.somesite.com/campfire.jpg">http://www.somesite.com/campfire.jpg</a>"""))
def test_image_link_options(self): dialect = create_dialect( creole10_base, wiki_links_base_url=["/pages/", "/files/"], wiki_links_space_char=["_", " "], wiki_links_path_func=[lambda s: s.upper(), lambda s: s.capitalize()], ) parse = Parser(dialect) self.assertEquals(parse("[[foo bar]]"), wrap_result("""<a href="/pages/FOO_BAR">foo bar</a>""")) self.assertEquals( parse("{{foo bar}}"), wrap_result("""<img src="/files/Foo bar" alt="foo bar" title="foo bar" />""") )
def test_interwiki_image_link_options(self): dialect = create_dialect( creole10_base, interwiki_links_base_urls={"a": ["/pages/", "/files/"]}, interwiki_links_space_chars={"a": ["_", " "]}, interwiki_links_path_funcs={"a": [lambda s: s.upper(), lambda s: s.capitalize()]}, ) parse = Parser(dialect) self.assertEquals(parse("[[a:foo bar|Foo]]"), wrap_result("""<a href="/pages/FOO_BAR">Foo</a>""")) self.assertEquals( parse("{{a:foo bar|Foo}}"), wrap_result("""<img src="/files/Foo bar" alt="Foo" title="Foo" />""") )
def test_disable_external_content(self): dialect = create_dialect(creole10_base, disable_external_content=True) parse = Parser(dialect) self.assertEquals( parse("{{campfire.jpg}}"), wrap_result("""<img src="campfire.jpg" alt="campfire.jpg" title="campfire.jpg" />""")) self.assertEquals( parse("{{/campfire.jpg}}"), wrap_result("""<span class="external_image">External images are disabled</span>""")) self.assertEquals( parse("{{http://www.somesite.com/campfire.jpg}}"), wrap_result("""<span class="external_image">External images are disabled</span>"""))
def setUp(self): noSpaces = Parser( dialect=create_dialect(creole11_base, wiki_links_base_url=base_url, wiki_links_space_char='', interwiki_links_base_urls={'Ohana': inter_wiki_url}, no_wiki_monospace=False, wiki_links_class_func=class_name_function, wiki_links_path_func=path_name_function ) ) self.parse = noSpaces
def setUp(self): noSpaces = Parser( dialect=create_dialect( wiki_links_base_url=base_url, wiki_links_space_char='', use_additions=True, no_wiki_monospace=False, wiki_links_class_func=class_name_function, wiki_links_path_func=path_name_function ) ) self.parse = noSpaces
def test_image_link_options(self): dialect = create_dialect(creole10_base, wiki_links_base_url=['/pages/', '/files/'], wiki_links_space_char=['_',' '], wiki_links_path_func=[lambda s:s.upper(), lambda s:s.capitalize()]) parse = Parser(dialect) self.assertEquals( parse("[[foo bar]]"), wrap_result("""<a href="/pages/FOO_BAR">foo bar</a>""")) self.assertEquals( parse("{{foo bar}}"), wrap_result("""<img src="/files/Foo bar" alt="foo bar" title="foo bar" />"""))
def test_custom_markup_option(self): def wikiword(mo, e): return builder.tag.a(mo.group(1),href=mo.group(1)) dialect = create_dialect(creole10_base, custom_markup=[('(c)','©'), (re.compile(esc_neg_look + r'\b([A-Z]\w+[A-Z]+\w+)'),wikiword)]) parse = Parser(dialect) self.assertEquals( parse("The copyright symbol (c), escaped ~(c)"), wrap_result("The copyright symbol ©, escaped (c)")) self.assertEquals( parse("A WikiPage name that is a ~WikiWord"), wrap_result('A <a href="WikiPage">WikiPage</a> name that is a WikiWord'))
def test_interwiki_image_link_options(self): dialect = create_dialect(creole10_base, interwiki_links_base_urls={'a': ['/pages/','/files/']}, interwiki_links_space_chars={'a': ['_',' ']}, interwiki_links_path_funcs={'a': [lambda s:s.upper(), lambda s:s.capitalize()]}) parse = Parser(dialect) self.assertEquals( parse("[[a:foo bar|Foo]]"), wrap_result("""<a href="/pages/FOO_BAR">Foo</a>""")) self.assertEquals( parse("{{a:foo bar|Foo}}"), wrap_result("""<img src="/files/Foo bar" alt="Foo" title="Foo" />"""))
def setUp(self): def inter_wiki_link_maker(name): return name[::-1] def simple_class_maker(name): return name.lower() functions = {"moo": inter_wiki_link_maker, "goo": inter_wiki_link_maker} base_urls = {"goo": "http://example.org", "poo": "http://example.org", "Ohana": inter_wiki_url} space_characters = {"goo": "+", "poo": "+"} class_functions = {"moo": simple_class_maker, "goo": simple_class_maker} dialect = create_dialect( creole10_base, interwiki_links_path_funcs=functions, interwiki_links_class_funcs=class_functions, interwiki_links_base_urls=base_urls, interwiki_links_space_chars=space_characters, ) self.parse = Parser(dialect)
def setUp(self): def inter_wiki_link_maker(name): return name[::-1] functions = { 'moo':inter_wiki_link_maker, 'goo':inter_wiki_link_maker, } base_urls = { 'goo': 'http://example.org', 'poo': 'http://example.org', } space_characters = { 'goo': '+', 'poo': '+', } dialect = create_dialect( interwiki_links_funcs=functions, interwiki_links_base_urls=base_urls, interwiki_links_space_chars=space_characters ) self.parse = Parser(dialect)
def test_bodied_macros_option(self): def red(macro,e,*args,**kw): return builder.tag.__getattr__(macro.isblock and 'div' or 'span')( macro.parsed_body(),style='color:red') red.parse_body = True #return {'style':'color:red'} def blockquote(macro,e,*args,**kw): return builder.tag.blockquote(macro.parsed_body()) blockquote.parse_body = True #return {'tag':'blockquote'} MyDialect = create_dialect(creole11_base, bodied_macros=dict(red=red, blockquote=blockquote)) parse = Parser(MyDialect) self.assertEquals( parse("This block of <<red>>text **should** be monospace<</red>> now"), wrap_result('This block of <span style="color:red">text <strong>should</strong> be monospace</span> now')) self.assertEquals( parse("<<red>>\ntext **should** be monospace\n<</red>>"), '<div style="color:red"><p>text <strong>should</strong> be monospace</p>\n</div>') self.assertEquals( parse("This block of <<blockquote>>text **should** be monospace<</blockquote>> now"), wrap_result('This block of </p><blockquote>text <strong>should</strong> be monospace</blockquote><p> now')) self.assertEquals( parse("<<blockquote>>\ntext **should** be monospace\n<</blockquote>>"), '<blockquote><p>text <strong>should</strong> be monospace</p>\n</blockquote>')
# # This module is part of Creoleparser and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php # import string import keyword from core import Parser, ArgParser from dialects import (creole11_base, creole10_base, creepy10_base, create_dialect) __docformat__ = 'restructuredtext en' __version__ = '0.7.2' creole2html = Parser(dialect=create_dialect(creole10_base), method='html') """This is a pure Creole 1.0 parser created for convenience""" text2html = Parser(dialect=create_dialect(creole11_base), method='html') """This is a Creole 1.0 parser (+ additions) created for convenience""" parse_args = ArgParser( dialect=creepy10_base(), key_func=string.lower, illegal_keys=keyword.kwlist + ['macro_name', 'arg_string', 'body', 'isblock', 'environ', 'macro']) """Function for parsing macro arg_strings using a relaxed xml style""" def _test(): import doctest
def test_wiki_links_base_url_option(self): dialect = create_dialect(creole10_base, wiki_links_base_url='http://www.example.com') parse = Parser(dialect) self.assertEquals( parse("[[foobar]]"), wrap_result("""<a href="http://www.example.com/foobar">foobar</a>"""))
macros = {'include':include, 'include-raw':include_raw, 'include-source':include_source, 'source':source, 'pre':pre } def macro_dispatcher(macro_name,arg_string,body,isblock,environ): if macro_name in macros: return macros[macro_name](arg_string,body,isblock) dialect = dialects.create_dialect(dialects.creole11_base, wiki_links_base_url='http://creoleparser.srcom.org/cgi-bin/creolepiki/', wiki_links_space_char='', # use_additions=True, no_wiki_monospace=False, wiki_links_class_func=class_func, wiki_links_path_func=path_func, macro_func=macro_dispatcher) text2html = core.Parser(dialect) if __name__ == '__main__': text = Page('CheatSheetPlus').get_raw_body() f = open(os.path.join('test_pages','CheatSheetPlus.html'),'r') rendered = f.read() f.close() f = open(os.path.join('test_pages','template.html'),'r') template = f.read()
def test_no_wiki_monospace_option(self): dialect = create_dialect(creole10_base, no_wiki_monospace=True) parse = Parser(dialect) self.assertEquals( parse("This block of {{{no_wiki **shouldn't** be monospace}}} now"), wrap_result("This block of <code>no_wiki **shouldn't** be monospace</code> now"))
def test_use_additions_option(self): dialect = create_dialect(creole11_base) #, use_additions=True) parse = Parser(dialect) self.assertEquals( parse("This block of ##text **should** be monospace## now"), wrap_result("This block of <code>text <strong>should</strong> be monospace</code> now"))
def test_simple_markup_option(self): MyDialect = create_dialect(creole10_base, simple_markup=[('*','strong'),('#','code')]) parse = Parser(MyDialect) self.assertEquals( parse("This block of #text *should* be monospace# now"), wrap_result("This block of <code>text <strong>should</strong> be monospace</code> now"))
return 'nonexistent' def path_name_function(page_name): if page_name == 'ThisPageHere': path = 'Special/ThisPageHere' else: path = urllib.quote(page_name.encode('utf-8')) return path text2html = Parser( dialect=create_dialect( wiki_links_base_url=base_url, interwiki_links_base_urls={'Ohana': inter_wiki_url}, use_additions=True, no_wiki_monospace=False ) ) def wrap_result(expected): return "<p>%s</p>\n" % expected class BaseTest(object): """ """ parse = lambda x: None
return 'nonexistent' def path_name_function(page_name): if page_name == 'ThisPageHere': path = 'Special/ThisPageHere' else: path = urllib.quote(page_name.encode('utf-8')) return path text2html = Parser( dialect=create_dialect(creole11_base, wiki_links_base_url=base_url, interwiki_links_base_urls={'Ohana': inter_wiki_url}, no_wiki_monospace=False ) ) def wrap_result(expected): return "<p>%s</p>\n" % expected class BaseTest(object): """ """ #parse = lambda x: None
macros = {'include':include, 'include-raw':include_raw, 'include-source':include_source, 'source':source, 'pre':pre } def macro_dispatcher(macro_name,arg_string,body,isblock,environ): if macro_name in macros: return macros[macro_name](arg_string,body,isblock) dialect = dialects.create_dialect(dialects.creole11_base, wiki_links_base_url='', wiki_links_space_char='', # use_additions=True, no_wiki_monospace=False, wiki_links_class_func=class_func, wiki_links_path_func=path_func, macro_func=macro_dispatcher) text2html = core.Parser(dialect) if __name__ == '__main__': text = Page('CheatSheetPlus').get_raw_body() f = open(os.path.join('test_pages','CheatSheetPlus.html'),'r') rendered = f.read() f.close() f = open(os.path.join('test_pages','template.html'),'r') template = f.read()
# # This module is part of Creoleparser and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php # import string import keyword from core import Parser, ArgParser from dialects import (creole11_base, creole10_base, creepy10_base, create_dialect, parse_args) __docformat__ = 'restructuredtext en' __version__ = '0.7.4' creole2html = Parser(dialect=create_dialect(creole10_base), method='html') """This is a pure Creole 1.0 parser created for convenience""" text2html = Parser(dialect=create_dialect(creole11_base), method='html') """This is a Creole 1.0 parser (+ additions) created for convenience""" #parse_args = ArgParser(dialect=creepy10_base(),key_func=string.lower, # illegal_keys=keyword.kwlist + ['macro_name', # 'arg_string', 'body', 'isblock', 'environ', 'macro']) #"""Function for parsing macro arg_strings using a relaxed xml style""" def _test(): import doctest doctest.testmod() if __name__ == "__main__":