Andy's insights

Opinions, thoughts, archivements

Wed, 01 Mar 2006

PyBlosxom generic Wiki markup

It’s nice to format the blog entries in HTML. Of course HTML is somewhat complicated. I like Wiki markup: It is straightforward and simple.

To add Wiki markup support to PyBlosxom, there is a plugin in entryparsers subdirectory of the contrib package. Not bad, but I’m missing a code block option. Once again I was able to enhance the source code.

Apropos: This entry is formated with the enhanced genericwiki parser. Here the prove:


“”“
Generic wiki markup PreFormatter 2002-11-18, for pyblosxom
CHANGE wikibaseurl to point to your wiki, & wikinamepattern to yours
Bug reports, comments, presents, etc. to John Abbe at johnca@ourpla.net
ToDo: Lists; code; InterWiki links; other wikinamepatterns

You can configure this as your default preformatter by configuring it in your
L{config} file as follows::

py['parser’] = 'genericwiki’

or in your blosxom entries, place a C{#parser wiki} line after the title of
your blog::

My Little Blog Entry #parser genericwiki This is a text in '’'wiki’'’ format

This preformatter also supports WikiWirds, you need to point out where your
Wiki site is. This is configured with a new variable in your config.py file,
'genericwiki_baseurl’::

py['genericwiki_baseurl’] = 'http://www.google.com/search?q=’

The above example would expand 'WikiWord’ to
http://www.google.com/search?q=WikiWord

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright 2004, 2005 John Abbe
“”“
author = 'John Abbe '
version = “$Id: genericwiki.py,v 1.1.2.1 2005/06/21 18:22:33 willhelm Exp $”
PREFORMATTER_ID = 'genericwiki’
import re

  1. caching compiled regular expressions

re_cache = None

def cb_preformat(args): “”“ Preformat callback chain looks for this.

param args: a dict with 'parser' string and a list 'story' type args: dict “”“ if args['parser’] == PREFORMATTER_ID: config = args['request’].getConfiguration() baseurl = config.get('genericwiki_baseurl’, None) return parse(’'.join(args['story’]), baseurl)

def _build_cache(): “”“ Compile regular expression pattern and store them in global cache “”“ global re_cache # build the cache first time called (speedup later calls!) re_cache = {}

# WikiName pattern used in your wiki wikinamepattern = r’\b(([A-Z]+[a-z]+){2,})\b’ # original mailurlpattern = r’mailto\:[\”\-\_\.\w]+\@[\-\_\.\w]+\w’ newsurlpattern = r’news\:(?:\w+\.){1,}\w+’ fileurlpattern = r’(?:http|https|file|ftp):[/-_.\w-]+[\/\w][?&+=%\w/-_.#]*’ # Turn '[xxx:address label]’ into labeled link re_cache['bracket_links’] = re.compile(r’\[(’ + fileurlpattern + '|’ + mailurlpattern + '|’ + newsurlpattern + ')\s+(.+?)\]’) # Convert naked URLs into links — skip ones with a “ before re_cache['nacked_url’] = re.compile(r’(?def _parse_markup(text, wikibaseurl): “”“ Parse wiki markup for text

param text: Text for conversion type text: string “”“ if not len(text.strip()): return “” # Turn '[xxx:address label]’ into labeled link text = re_cache['bracket_links’].sub(r’\2', text) # Convert naked URLs into links — skip ones with a “ before text = re_cache['nacked_url’].sub(r’\1', text) # Convert WikiNames into links if wikibaseurl: text = re_cache['wiki_names’].sub(’\1', text) # '’ for emphasis, '’' for strong, —— for a horizontal rule text = re_cache['strong’].sub(r”\1“, text) text = re_cache['emphasis’].sub(r”\1“, text) text = re_cache['ruler’].sub(”
“, text) # Convert two or more newlines into

text = re_cache['para’].sub(r’

\n

', text) return “

“ + text + “

def _parse_code(text): if text.strip().startswith(’#!python’): keywords = “(and|del|for|is|raise|assert|elif|from|lambda|return|break”\ “|else|global|not|try|class|except|if|or|while|continue”\ “|exec|import|pass|yield|def|finally|in|print)” text = re.sub(keywords, r’\1', text) return text

def parse(text, wikibaseurl): “”“ The main workhorse that convert wiki text into html markup

param text: Text for conversion type text: string @return: string “”“ global re_cache if not re_cache: _build_cache() # replace entities text = text.replace(’&’, '&’).replace(’<', '<').replace('>', '>’) # convert preformated text and wiki markup text fragments = [ x.split(’} }}’, 1) for x in text.split(’{ {{’) ] result = [] for fragment in fragments: if len(fragment) > 1: result.append(”<" + "pre" + ">“ + _parse_code(fragment0) + ““) result.append(_parse_markup(fragment1, wikibaseurl)) else: result.append(_parse_markup(fragment0, wikibaseurl)) return “”.join(result)

if name == 'main': text = “”“This is a test To test the wiki

[http://roughingit.subtlehints.net/pyblosxom?blah=duh#spam A link] news:roughingit.subtlehints.net/pyblosxom/ – no, ''I’' do '’'not’'’ have a news server. mailto:wari@example should go link to an email. WikiWiki is a wiki Keyword A text for preformated text: {{{ #!python import this print “Yes! don’t replace ''source’' '’'code’'’ here: http://don.t.do.this.ch/” }}} That’s it, folks! “”“ print parse(text, 'http://wiki.subtlehints.net/moin/’)

Note: You should remove the space in curly brackets.

posted at: 23:36 | path: /python | permanent link to this entry