ContentBBcode examples

Check out the new site at https://rkblog.dev.

ContentBBcode is a pluggable set of BBcode-like tags that can perform various operations - from inserting some data from the database to JS wrappers.
The code is located in /diamandas/cbcplugins:
  • cbcparser.py - the parser
  • tags.py - file with tags code, this file you may want to hack
There are two tag types - single tags (with no closing tag) and double tags (with closing tag). The skeleton of a tag looks like this:
def tagname(dic, text):
	for i in dic:
		text = text.replace(i['tag'], 'the thing plugin makes')
	return text
And the tag syntax looks like this:
[ rk : tagname someparam="somevalue" ]
[ rk : tagname someparam="somevalue" ] some text [ /rk : tagname ]
The dic argument contains data parsed from the thags in a dictionary:
  • i['tag'] - full code of the tag (for replacing)
  • i['attributes']['ATRIBUTE_NAME'] - value of an attribute
  • i['code'] - for double tags - text between tags
Here is a rk:h plugin that makes H1-4 HTML tags with labels etc.
def h(dic, text):
	"""
	display h1-4 tags with labes/links
	"""
	s = 1
	for i in dic:
		text = text.replace(i['tag'], '<a name="' + str(s) +'" class="' +  i['attributes']['id'] + '" title="' + i['code'] + '"></a><h' + i['attributes']['id'] + ' class="pageh"><a href="#' + str(s) +'">' + i['code'] + '</a></h' + i['attributes']['id'] + '>')
		s = s+1
	return text
And here a rk:thumb which shows a graphic file from site_media with a thumb made on-the-fly (if doesn't exist) by PIL:
def thumb(dic, text):
	"""
	display an image with a thumbnail and lightbox like script
	"""
	THUMB = '<a href="/site_media/resources/%s/images/%s" rel="facebox"><img src="/site_media/resources/%s/images/%s" alt="%s" /></a>'
	for i in dic:
		img = i['attributes']['src'].split('/')
		thumb = 'thumb_' + img[-1]
		im = img[-1]
		domain = img[0]
		if isfile(settings.MEDIA_ROOT + '/resources/' + domain + '/images/' + im):
			if not isfile(settings.MEDIA_ROOT + '/resources/' + domain + '/images/' + thumb):
				imi = Image.open(settings.MEDIA_ROOT + '/resources/' + domain + '/images/' + im)
				imi.thumbnail((145, 145), Image.ANTIALIAS)
				sharpener = ImageEnhance.Sharpness(imi)
				imi = sharpener.enhance(2.4)
				imi.save(settings.MEDIA_ROOT + '/resources/' + domain + '/images/' + thumb)
			thm = THUMB % (domain, im, domain, thumb, im)
			text = text.replace(i['tag'], thm)
	return text
This article is powered by rk:syntax and pygments ;)
RkBlog

Diamanda Applications Set, 13 November 2008


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact