#!/usr/bin/perl use strict; use warnings; use File::Find; use HTML::TreeBuilder; use Getopt::Long; my $octopress_root; my $options_read = GetOptions("dir=s", \$octopress_root); ############################################################ unless ($options_read && $octopress_root) { print "\n"; print "\n"; print "usage: tagify.pl --dir d\n"; print "\n"; print "where d is the root octopress directory\n"; print " - the parent of source, public, etc.\n"; print "\n"; exit 1; } # The tag cloud HTML gets saved into this file. # This file is included by two others: # a) The file used for the sidebar aside # b) The page used to display all tags # (accessible as /tags/index.html) # my $custom_file = "$octopress_root/source/_includes". "/custom/tag_cloud.html"; # This is the data structure that contains all the tag # data parsed by the HTML files. # It's key is the tag name (not case-normalized). # The value is another hash. That hash has 2 keys: # count - number of pages with that tag # range_num - a number from 1 - 10 indicating # popularity (see below) # pages - an array of hashes # # Each hash in the pages array has 3 keys: # title - the HTML title of the post # file - the full file name of the HTML file # categories - yet another hash # # The categories hash has two keys: # href - the url to the category page (as determined # by OctoPress) # text - the name of the category (as displayed by # Octopress) # my $tag_data = { }; # This function populates the tag_data data structure # find(\&getTags, "$octopress_root/public/blog"); # Find the number of times the most popular tag is used # my $max = 1; # start with 1, not 0 to prevent a # divide-by-zero error later # if none of the posts have tags foreach my $tag (keys %$tag_data) { $tag_data->{$tag}->{count} = scalar(@{$tag_data->{$tag}->{pages}}); if ($tag_data->{$tag}->{count} > $max) { $max = $tag_data->{$tag}->{count}; } } # Assign each tag a range number from 1 - 10 # based on popularity. This range number will # be used along with CSS to print tags with # the appropriate size. # foreach my $tag (keys %$tag_data) { $tag_data->{$tag}->{range_num} = int(($tag_data->{$tag}->{count} / $max) * 10 + 0.5); # nearest whole number if ($tag_data->{$tag}->{range_num} == 0) { $tag_data->{$tag}->{range_num} = 1; # we want 1-10, not 0-10 } } # Write the tag cloud file # open (O, ">$custom_file") || die; print O "