AmiFormat 2.0

AmiFormat - the format system that is used for formatting pages in Skeletonz is updated. The new version is much cleaner, faster and offers some powerful features.

Facts of AmiFormat 2.0:

  • Textile like, but simpler and faster.
  • Links, headings, code, lists, text style, images, paragraphs and line breaks are supported.
  • Support for plugins.

I have used some things I learned in last semesters Compilation course to build a really nice parser. For example, the new parser runs in linear time, while the old one ran in O(n*m) where n is input size and m is number of plugins.

Let me show you an example. You write some text in a special AmiFormat syntax, the text could look like this:

h2. Hello World

%{color: red} A red handed paragraph%.

When you run the above text through AmiFormat, it will convert the text to XHTML.
The output will be:

<h2>Hello World</h2>

<p>
<span style="color: red">A red handed paragraph</span>.
</p>

See AmiFormat reference for more examples.

Support for plugins

The really nobel feature of AmiFormat is that it supports plugins. I have taken the plugin system from Skeletonz and integrated it into AmiFormat.

A really useful Skeletonz plugin is the GreyBox plugin, sample syntax looks like this:

[gb_image=[image=Night valley small], link=[image=Night valley big]] 

When the above text is scanned by AmiFormat it will find 2 plugins, and it will call the appropriate handlers. Those can then render the plugin. The end result looks like this:

Night valley small

It's really a powerful one-liner :)

How plugins work

Extending AmiFormat with plugins is really easy. Here is an example:

from amiformat import amiformat
import random

def rndColorsHandler(args):
    colors = ['red', 'green', 'blue', 'cyan', 'yellow']
    text = args['random_colors']
    rnd_clr = []
    for c in text:
        color = colors[random.randint(0, 4)]
        rnd_clr.append('<span style="color: %s">%s</span>' % (color, c))

    return False, ''.join(rnd_clr)

parser = amiformat.AmiFormat()
parser.registerSLPlugin('random_colors', rndColorsHandler)
text = """Here are some [random_colors=random colors]!"""
print parser.htmlFormat(text)

The above example will (approximately) output:

Here are some random colors!

Download today

You can download the new version from the official site.

Code · Python · Skeletonz 23. Dec 2006
© Amir Salihefendic. Powered by Skeletonz.