Basic Usage
>>> from creoleparser import text2html
Simply call the text2html() function with one argument (the text to be parsed):
>>> print text2html("Some real **simple** mark-up"),
<p>Some real <strong>simple</strong> mark-up</p>
To customize things a little, create your own dialect and parser:
>>> from creoleparser.dialects import Creole10
>>> from creoleparser.core import Parser
>>> my_dialect=Creole10(wiki_links_base_url='http://www.mysite.net/',
... interwiki_links_base_urls=dict(wikicreole='http://wikicreole.org/wiki/'))
>>> my_parser = Parser(dialect=my_dialect)
>>> print my_parser("[[Home]] and [[wikicreole:Home]]"),
<p><a href="http://www.mysite.net/Home">Home</a> and <a
href="http://wikicreole.org/wiki/Home">wikicreole:Home</a></p>
If you want pure Creole 1.0 (i.e., no additions), use creole2html() instead of text2html().
API Documentation
Example implementation in a wiki engine (note that this document was updated for creoleparser version 0.5 )
Below is most of the code that was used to integrate Creoleparser into the demo wiki. In a nutshell, the text2xhtml function replaced the native PikiPiki PageFormatter.print_html method. Please also see the constructor for Creole10 objects in the generated documentation.
Note that class_func, path_func, and macro_func are not needed for basic integration. Also, my macro_func is particularly crude...
import creoleparser as cp
def class_func(page_name):
if not Page(page_name).exists():
return 'nonexistent'
def path_func(page_name):
if page_name == 'Home':
return 'FrontPage'
else:
return page_name
def macro_func(name,arg_string,body,isblock):
if name=='include':
return include(arg_string)
elif name=='include-source':
return include_source(arg_string)
elif name=='source':
return source(body)
elif name=='pre':
return pre(body)
elif name=='include-raw':
return include_raw(arg_string)
else:
return '**' + arg_string + '**'
dialect = cp.dialects.Creole10(
wiki_links_base_url='http://creoleparser.x10hosting.com/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_func)
text2xhtml = cp.Parser(dialect)
The Macros
These macros return Genshi Stream or Element objects, as is required.
import genshi.builder as bldr
def include(page_name):
page = Page(page_name.strip())
return text2xhtml.generate(page.get_raw_body())
def include_raw(page_name):
page = Page(page_name.strip())
return bldr.tag.pre(page.get_raw_body(),class_='plain')
def include_source(page_name):
page = Page(page_name.strip())
return bldr.tag.pre(text2xhtml.render(page.get_raw_body()))
def source(string):
if string is None:
return 'None'
return bldr.tag.pre(text2xhtml.render(string))
def pre(string):
return bldr.tag.pre(string)
Hacking of Page.send_page()
before
def send_page(self, msg=None):
link = get_scriptname() + '?fullsearch=' + self.page_name
send_title(self.split_title(), link, msg)
PageFormatter(self.get_raw_body()).print_html()
print_footer(self.page_name, 1, self._last_modified())
after
Note that certain pages still use the native parser, all others use text2xhtml.
def send_page(self, msg=None):
link = get_scriptname() + '?fullsearch=' + self.page_name
send_title(self.split_title(), link, msg)
if self.page_name in ('TitleSearch','FullSearch','WordIndex',
'TitleIndex','RecentChanges','GoTo'):
PageFormatter(self.get_raw_body()).print_html()
else:
print text2xhtml(self.get_raw_body())
print_footer(self.page_name, 1, self._last_modified())