¶ index_static.py
2010-05-31 11:10
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 | # -*- coding: utf-8 -*- """ index_static.py - just fixed for pyblosxom-cmd staticrender export all .html links base index.py from: Ryan Barrett <[email protected]> This plugin displays an alphabetical index of all entries. It uses these optional config variables from config.py, shown here with their defaults: py['index_trigger'] = '/site-index' py['index_num_columns'] = 2 py['index_letters_first'] = True py['index_title'] = 'index' py['index_use_story_template'] = True VERSION: 0.2 TODO: - use a template instead of hard-coded HTML Copyright 2006 Ryan Barrett This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. """ import math import os.path import time from Pyblosxom import tools import Pyblosxom.entries __author__ = 'Ryan Barrett' __version__ = '0.2' __description__ = 'Displays an alphabetical index of all entries.' def verify_installation(request): return 1 def cb_filelist(args): request = args[ 'request' ] http = request.getHttp() data = request.getData() config = request.getConfiguration() trigger = config.get( 'index_trigger' , 'site-index' ) if http[ 'PATH_INFO' ] ! = trigger: return # get the entries datadir = config[ 'datadir' ] files = tools.Walk(request, datadir) files.sort() # sort into sections, one for each letter. the dictionary is # letter => (entry name, path) where path is the relative to datadir. sections = {} entry_extensions = data[ 'extensions' ].keys() for file in files: assert file .startswith(datadir) path, ext = os.path.splitext( file [ len (datadir):]) if ext[ 1 :] in entry_extensions: # strip the leading period from ext entry_name = os.path.basename(path) sections.setdefault(entry_name[ 0 ].upper(), []).append((entry_name, path)) # extract the first letters. sort as usual, except that numbers and other # non-letters go *after* letters. def letters_before_symbols(a, b): if a.isalpha() and not b.isalpha(): return - 1 elif not a.isalpha() and b.isalpha(): return 1 else : return cmp (a, b) letters = sections.keys() if config.get( 'index_letters_first' , 1 ): letters.sort(letters_before_symbols) else : letters.sort() # add the header with links to each section body = '<p class="index-header">' letter_links = [ '<a href="#%s">%s</a>' % (l, l) for l in letters] body + = ' |\n' .join(letter_links) body + = '</p>\n<hr class="index">\n\n' # add the sections themselves, with one link per entry, in a table. the # number of columns is taken from the index_num_columns config variable. # entries are ordered down each column, in order. num_cols = config.get( 'index_num_columns' , 2 ) for l in letters: body + = '<h3 class="index">%s</h3> <a name="%s"></a>\n' % (l, l) body + = '\n' entries = sections[l] entries.sort() num_rows = int (math.ceil( float ( len (entries)) / num_cols)) for row in range ( 0 , num_rows): # alternate the tags' class between index-row-stripe-0 and # index-row-stripe-1, so you can use CSS to alternate their color for # readability, if you want. body + = '\n' % (row % 2 ) for col in range ( 0 , num_cols): entry_index = col * num_rows + row if entry_index < len (entries): entry_name, path = entries[entry_index] else : entry_name = path = '' body + = '\n' % (path[ 1 :], entry_name) body + = '\n' body + = '<table class="index"><tbody><tr></tr><tr class="index-row-stripe-%d"><td><a href="%s.html">%s</a></td></tr></tbody></table>\n<hr class="index">\n\n' data = { 'title' : config.get( 'index_title' , 'index' )} # use the epoch for mtime. otherwise, pyblosxom uses the current time, which # makes other plugins (like weblogsping) think this is a new entry. epoch = time.localtime( 0 ) fe = Pyblosxom.entries.base.generate_entry(request, data, body, epoch) return [fe] def cb_story(args): request = args[ 'request' ] http = request.getHttp() config = request.getConfiguration() trigger = config.get( 'index_trigger' , 'site-index' ) if (http[ 'PATH_INFO' ] = = trigger and not config.get( 'index_use_story_template' , 1 )): title = config.get( 'index_title' , 'index' ) args[ 'template' ] = '<h1 class="index">%s</h1>\n<hr>\n$body' % title return args |