pyentrynavi.py

"""
Create navigation to the previous/next entry for each entry,
like this.

  < previous entry title | MAIN | next entry title >


To install pyentrynavi, do the following

  1. add "pyentrynavi" to your load_plugins list variable
     in your config.py.
  2. add the $entry_navi variable to your date_head and/or
     comment-story templates.
  3. if your file extension other than "txt", set the following on your
     config.py.

     ex) py["entry_extension"] = "rst"


version 0.4: modified regular expression '(.*\.txt)' to '(.*\.txt$)',
             to prevent hit backup files or swap files.
             Thanks Bill Powell!
version 0.5: modified system depend path handling and enable to use file
             extension other than .txt.
             Thanks Andrey Bondarenko!

----

Copyright (C) 2006-2009 shunuhs

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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

"""

__author__      = "shunuhs"
__version__     = "0.5 2009/10/24"
__url1__        = "http://sh1.2-d.jp/b/static/pyentrynavi.html"
__url2__        = "http://sh1.2-d.jp/h/pyblosxom_plugin/file/tip/pyentrynavi.py"
__description__ = "show entry navigator"

from Pyblosxom import tools, entries
import time
import re
import os.path

def verify_installation(request):
    config = request.getConfiguration()
    if not config.has_key('datadir'):
        print "missing datadir variable"
        return 0
    return 1

def cb_prepare(args):
    request = args['request']
    data = request.getData()
    if data['bl_type'] != 'file':
        return

    entry = data['entry_list']
    flavour = data['flavour']
    filename = os.path.normpath(entry[0]['filename']) # normpath is for windows.

    config = request.getConfiguration()
    datadir = config['datadir']
    base_url = config['base_url']

    extension = config.get('entry_extension','txt')

    r = re.compile('(.*\.'+extension+'$)')
    allentries = tools.Walk(request, datadir, pattern=r)

#    cur_time = time.localtime()

    entrylist = []
    for e in allentries:
        timetuple = tools.filestat(request, e)
#        if cur_time < timetuple:
#            continue

        entrylist.append((timetuple, e))

    entrylist.sort()
    entrylist.reverse()

    entrylist = [x[1] for x in entrylist]
    try:
        num = entrylist.index(filename)
    except ValueError:
        data['entry_navi'] = '| <a href="%s">MAIN</a> |'%base_url
        return

    def _entry_url(file_entry):
      return "%s/%s.%s" % (base_url, file_entry["file_path"], flavour)

    navi_str = ''
    if num != (len(entrylist)-1):
        fname = entrylist[num+1]
        e = entries.fileentry.FileEntry(request, fname, datadir)
        prev_link = _entry_url(e)
        navi_str += '&lt; <a href="%s">%s</a> '%(prev_link,e['title'])

    navi_str += '| <a href="%s">MAIN</a> |'%base_url

    if num:
        fname = entrylist[num-1]
        e = entries.fileentry.FileEntry(request, fname, datadir)
        next_link = _entry_url(e)
        navi_str += ' <a href="%s">%s</a> &gt;'%(next_link,e['title'])

    data['entry_navi'] = navi_str

| MAIN |

Trackback

Comments


Great plugin! I dropped it in, and it worked right away. But I wanted to point a funny little bug; I use Vim, and if any swap files accidentally get left in the directory, this plugin tries to open them. Granted, I should keep my directory clean. But it still might be good to filter out hidden files, if possible.

Also, another option for placement is "date_foot.html".

Really great plugin! Thanks!

Posted by Bill Powell at Sun Jun 8 01:37:13 2008



Update: I think this seems to fix it:

Change this:
  r = re.compile('(.*\.txt)')

To this:
  r = re.compile('(.*\.txt$)')

Now you'll only find files than end in txt. I think. The swap files end in txt.swp.

Otherwise, you can't edit a text file, then switch over and see how it looks in the browser, because when you refresh that post, it tries to use the open text file's swap file as the next post for pyentrynavi, and everything breaks.

Of course, I know I'm not supposed to be seeing how the post looks while I'm writing it. :)

Posted by Bill Powell at Sun Jun 8 02:08:47 2008



Thanks your comments!

I have not noticed at all because I write the entry
on my  local PC, then send to the server by ftp.

Your proposal was very useful, so I updated pyentrynavi.py.

Thanks a lot!

Posted by shunuhs at Tue Jun 10 03:14:12 2008



Hi,

Seems like a great plugin, but I can't seem to make it work. For some reason, not output is given instead of the variable, no matter where I put it. Running latest stable.

Posted by Tiago Faria at Fri Sep 5 10:39:09 2008



Hi,

Seems like a great plugin, but I can't seem to make it work. For some reason, not output is given instead of the variable, no matter where I put it. Running latest stable.

Posted by Tiago Faria at Fri Sep 5 10:55:23 2008



Update to version 0.5:

modified system depend path handling and enable to use file extension other than .txt.
Thanks Andrey Bondarenko!

Posted by shunuhs at Sat Oct 24 09:43:59 2009




Name:


E-mail: (option)


URL: (option)


The Magic Word: please enter the '0730'.


Comment: