""" tag_cloud.py Creates a tagcloud to compliment the tags plugin. Simply copy to your plugins directory and enable in py['load_plugins'] if necessary. The tags plugin must be installed and properly configured. Simply add a #tags tag1[,tag2[...]] to your entry metadata section. You should then define the following classes: .smallestTag .smallTag .mediumTag .bigTag .biggestTag .hugeTag .hugestTag .mostHugeTag as well as #tagcloud The Tag classes will be used on elements to represent the number of entries with that tag. in your CSS to fit your needs. As of 1.3.1, tag_cloud supports the following config property: py[ 'ignore_tags' ] = [ ] - Use to omit tags from the tagcloud. py[ 'tag_url_display' ] = [ ] - Useful if you use mod_rewrite to redirect your tags url. Defaults to config['tag_url'] where 'ignore_tags' is a list of tags to ignore while generating the tag cloud. This way, if you have a very widely used tag, such as "general", which is overwhelming your tag and isn't necessary, you can choose to omit it. As of 1.0.2, tag_cloud obeys the ignore_directories propery in pyblosxom. Version 1.0.3 - Fixed an error choosing tag size; never used smallestTag Version 1.1.0 - Fixed an error choosing tag size; was not properly choosing mincount. Also added finer breakdown of distribution, better for smaller sites. Version 1.2.0 - Added an "untagged" tag to the cloud after Joe accepted my untagged hack to his tags plugin. Make sure you're using Tags v 2 2005/10/23 1:30:00 or later!! NOTE: The "untagged" tag does not factor into the tag weightings, and will always be shown using a .mediumTag class. Version 1.3.0 - Added a "populartagcloud" variable, which takes the full "tagcloud", removes anything which is not at least a "medium tag", and redistributes the weightings. This is great if you have a lot of tags with only one or two posts that don't pertain to the main content of your blog. Version 1.3.1 - Added defaults for config[] parameters that weren't checked for Fixed bug where mincount was always 1 instead of the actual mincount """ __author__ = 'Timothy C. Fanelli ' __version__ = '1.3.1' __url__ = 'http://www.timfanelli.com' # Variables import os, re, sys, string def cb_prepare(args): request = args['request'] config = request.getConfiguration() data = request.getData() maxcount = 1 tagcount = {} ignoretags = [] if config.has_key('ignore_tags'): config['ignore_tags'] ignoredirectories = config[ 'ignore_directories' ] for root,dirs,files in os.walk( config['datadir'] ): for file in files: if not re.compile('.*\.txt$').search(file): continue entry_location = root + "/" + file directory = os.path.dirname(entry_location) if ( os.path.split( directory )[1] in ignoredirectories ): continue contents = open(entry_location,'r').read() m = re.compile( '\n#tags\s*(.*)\n' ).search(contents) if m: tagstring = m.group(1) tags = tagstring.split(',') first = True for tag in tags: if ( tag in ignoretags ): continue count = 1 if tag in tagcount.keys(): count = tagcount[tag] + 1 tagcount[tag] = count maxcount = max( count, maxcount ) else: untaggedcount = 1 if "untagged" in tagcount.keys(): untaggedcount = tagcount["untagged"] + 1 tagcount["untagged"] = untaggedcount mincount = maxcount for tag in tagcount.keys(): mincount = min( mincount, tagcount[tag] ) data["tagcloud"] = createTagCloud( config, tagcount, mincount, maxcount ) data["populartagcloud"] = createPopularTagCloud( config, tagcount, mincount, maxcount ) def createPopularTagCloud( config, tagcount, mincount, maxcount ): distribution = ( maxcount - mincount ) / 6 popcount = {} popmin = 999999 for tag in tagcount.keys(): count = tagcount[tag] if ( count > ( mincount + distribution ) ): popcount[tag] = count popmin = min( popmin, count ) return createTagCloud( config, popcount, popmin, maxcount ) def createTagCloud( config, tagcount, mincount, maxcount ): if tagcount: tagurl = config['tag_url'] if config.has_key('tag_url_display'): tagurl = config['tag_url_display'] tagcloud = [] tagcloud.append("
") distribution = ( maxcount - mincount ) / 6 for tag in tagcount.keys(): size = "mediumTag" if tag != "untagged": if ( (int)(tagcount[tag]) == maxcount ): size = "mostHugeTag" elif ( (int)(tagcount[tag]) > ( mincount + ( distribution * 5 ) ) ): size = "hugestTag" elif ( (int)(tagcount[tag]) > ( mincount + ( distribution * 4 ) ) ): size = "hugeTag" elif ( (int)(tagcount[tag]) > ( mincount + ( distribution * 3 ) ) ): size = "biggestTag" elif ( (int)(tagcount[tag]) > ( mincount + ( distribution * 2 ) ) ): size = "bigTag" elif ( (int)(tagcount[tag]) > ( mincount + distribution ) ): size = "mediumTag" elif ( (int)(tagcount[tag]) > mincount ): size = "smallTag" elif ( (int)(tagcount[tag]) == mincount ): size = "smallestTag" tagcloud.append( "%s\n" % ( '%s%s' % ( tagurl,tag ), size, str(tagcount[tag]), tag, tag ) ) tagcloud.append("
") result = "".join(tagcloud) return result