hail2u / blosxom-plugins

various plugin for blosxom 2.x

This URL has Read+Write access

blosxom-plugins / categories
100644 204 lines (151 sloc) 4.522 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Blosxom Plugin: categories
# Author(s): Kyo Nagashima <kyo@hail2u.net>
# Version: 2005-09-11
# Blosxom Home/Docs/Licensing: http://www.blosxom.com/
 
# This plugin is simplified version of "categories" plugin.
# "categories" plugin maybe found at:
# http://www.blosxom.com/plugins/category/categories.htm
 
package categories;
 
use strict;
use vars qw($categories $title $name);
 
# --- Configuration Variables ----------
 
# should the story-count add up?
my $add_up_story_count = 1;
 
# should the story-count display?
my $display_story_count = 0;
 
# directories to include, but not children of -- full name under $datadir
my @prune_dirs = qw!/draft /old /freeze!;
 
# friendly name of directories
my $friendly_name = {
  'alt-shell' => 'Alternative Shell',
  'blog' => 'Blog',
  'blosxom' => 'Blosxom',
  'coding' => 'Coding',
  'foaf' => 'FOAF',
  'gadget' => 'Gadget',
  'game' => 'Game',
  'internet' => 'Internet',
  'media' => 'Media',
  'misc' => 'Miscellaneous',
  'rss' => 'RSS',
  'software' => 'Software',
  'sports' => 'Sports',
  'webdesign' => 'Web Design',
};
 
# separator string between $blog_title and $categories::name
my $title_sep = " - ";
 
# separater string between each name of category
my $name_sep = " &#187; ";
 
# --- Plug-in package variables --------
 
my %children;
my %stories;
my %seen;
 
# --------------------------------------
 
sub start {
  return 1;
}
 
sub filter {
  my($pkg, $files) = @_;
 
  foreach (keys %$files) {
    my($dir, $file) = m!(.*)/(.*)!;
    my $child;
    $stories{$dir}++;
 
    while ($dir ne $blosxom::datadir) {
      ($dir, $child) = ($dir =~ m!(.*)/(.*)!);
      $stories{$dir}++ if $add_up_story_count;
 
      if (!$seen{"$dir/$child"}++) {
        push @{$children{$dir}}, $child;
      }
    }
  }
 
  $categories = report_root() if defined($children{$blosxom::datadir});
 
  return 1;
}
 
sub head {
  if (
    !$blosxom::path_info_yr and
    $blosxom::path_info and
    ($blosxom::path_info !~ m|.*?/?\w+\.\w+$|)
  ) {
    $title = '';
    my @path_info = split(/\//, $blosxom::path_info);
 
    foreach (@path_info) {
      next if !$_;
      $_ = $friendly_name->{$_} if $friendly_name->{$_};
      $title .= qq!$name_sep$_!;
    }
  }
 
  $title =~ s!^$name_sep!$title_sep!;
 
  return 1;
}
 
sub story {
  my($pkg, $path, $fn, $story_ref, $title_ref, $body_ref) = @_;
 
  $name = '';
  my @name = split(/\//, $path);
 
  foreach (@name) {
    next if !$_;
    $_ = $friendly_name->{$_} if $friendly_name->{$_};
    $name .= qq!$name_sep$_!;
  }
 
  $name =~ s!^$name_sep!!;
 
  return 1;
}
 
sub report_root {
  my $results = report_categories_start();
  # $results .= report_dir_start('', 'all entries', $stories{$blosxom::datadir});
 
  foreach (sort @{$children{$blosxom::datadir}}) {
    $results .= report_dir('/', $_);
  }
 
  # $results .= report_dir_end();
  $results .= report_categories_end();
 
  return $results;
}
 
sub report_categories_start {
  return qq!<ul>\n!;
}
 
sub report_dir_start {
  my($fulldir, $thisdir, $numstories) = @_;
 
  $numstories ||= 0;
  $thisdir = $friendly_name->{$thisdir} if $friendly_name->{$thisdir};
 
  return qq!<li><a href="$blosxom::url${fulldir}" title="$thisdir">$thisdir</a> ($numstories)\n<ul>\n! if $display_story_count;
  return qq!<li><a href="$blosxom::url${fulldir}" title="$thisdir">$thisdir</a>\n<ul>\n!;
}
 
sub report_dir {
  my($parent, $dir) = @_;
 
  my $results;
  local $_;
 
  if (
    !defined($children{"$blosxom::datadir$parent$dir"}) or
    is_prune_dir("$parent$dir")
  ) {
    $results = report_dir_leaf("$parent$dir/", "$dir", $stories{"$blosxom::datadir$parent$dir"});
  } else {
    $results = report_dir_start("$parent$dir/", "$dir", $stories{"$blosxom::datadir$parent$dir"});
 
    foreach (sort @{$children{"$blosxom::datadir$parent$dir"}}) {
      $results .= report_dir("$parent$dir/", $_);
    }
 
    $results .= report_dir_end();
  }
 
  return $results;
}
 
sub report_dir_leaf {
  my($fulldir, $thisdir, $numstories) = @_;
 
  $numstories ||= 0;
  $thisdir = $friendly_name->{$thisdir} if $friendly_name->{$thisdir};
 
  return qq!<li><a href="$blosxom::url${fulldir}" title="$thisdir">$thisdir</a> ($numstories)</li>\n! if $display_story_count;
  return qq!<li><a href="$blosxom::url${fulldir}" title="$thisdir">$thisdir</a></li>\n!;
}
 
sub report_dir_end {
  return qq!</ul>\n</li>\n!;
}
 
sub report_categories_end {
  return qq!</ul>!;
}
 
sub is_prune_dir {
  my($dir) = @_;
 
  foreach (@prune_dirs) {
    return 1 if $dir eq $_;
  }
 
  return 0;
}
 
1;