#vim: tabstop=4 shiftwidth=4 expandtab """ tagger.py Create your own tagging markup Inside your stories, you can use markup like so: "B00082IJ08"[amazon] In your flavour_dir or datadir or wherever you keep your templates, create a file called format.content_type (replace content type with whatever content_type (html, rss, etc)) Inside format.content_type, have this: #amazon text = '' % (text, text) The tagging plugin will exec the code and replace the information inside the quotes. You can specify exec code on more than one line. For instance, an image link: "/images/cover.jpg;http://www.yahoo.com"[imagelink] And inside the format file: #imagelink (imagesrc,linksrc) = text.split(';') text = '' % (linksrc, imagesrc) You can also group tags. They will be executed from left to right. So you can have: "Text that will be bold and italic"[b,i] format.content_type: #b text = "%s" % text #i text = "$s" % text The tag idea was originally used in the magiclink plugin for blosxom. I made it a bit more flexible and converted it to python. Questions, comments and fixes can be sent to joe@terrarum.net """ __author__ = 'Joe Topjian ' __version__ = "$Id: tagger.py, v 1 2005/0/29 joe Exp $" __url__ = "http://joe.terrarum.net" PREFORMATTER_ID = 'tagger' import re, os.path formats = { } def replace(text,format): if ',' in format: for f in format.split(','): if formats.has_key(f): exec(formats[f]) else: if formats.has_key(format): exec(formats[format]) return text def cb_start(args): code = [] r = args["request"] config = r.getConfiguration() data = r.getData() form = r.getForm() data['flavour'] = (form.has_key('flav') and form['flav'].value or config.get('default_flavour', 'html')) if config.has_key("flavourdir"): flavourpath = config["flavourdir"] else: flavourpath = config["datadir"] filelocation = os.path.join(flavourpath, "format.%s" % data["flavour"]) if not os.path.isfile(filelocation): return 0 file = open(filelocation) for line in file: if line[0] == '#': if code: formats[curr] = "\n".join(code) code = [] curr = line.replace('#','').rstrip() else: code.append(line) formats[curr] = "\n".join(code) def cb_preformat(args): if args['parser'] == PREFORMATTER_ID: return parse(''.join(args['story'])) def parse(text): formatre = re.compile(r'"([\w\s\-\.\+\:\/\;\~\=\?\&\# ]+)"\[([\,\w]+)\]') m = formatre.search(text) while m: tmp = replace(m.group(1), m.group(2)) text = formatre.sub(tmp, text, count=1) m = formatre.search(text) return text