"""
readmore.py - break a long story at B R E A K (with no spaces) in the top page.
the readmore message is formated by py['readmore_template'] in config.py.
In this variable(a string or a list of two string), you can the following
designators:

%(url)s		the full path to the story
%(base_url)s	base_url
%(flavour)s	the flavour selected now
%(file_path)s	path to the story (without extension)

It default value is: 

   '<br /><br />::<a href="%(url)s">READ MORE</a>'

Using the default value, after an empty line, '::READ MORE' is placed 
to navigate to the story.

Note from Will Guaraldi (October 25, 2005):

I'm assuming IWS doesn't care about this anymore so I'm going to "fork"
the plugin and take over development and hosting for it.  I've made minor
adjustments to it.


NOTE: this plugin doesn't work with the rss2renderer plugin.


SUBVERSION VERSION: $Id$

Revisions:
2005-11-11 - Pulled into another new version control system.
1.5 - (26 October, 2005) pulled into new version control system
0.5 - (October 25, 2005) Changed the ^L to B R E A K, fixed some instructrions
      and took over hosting.
"""

# ORIGINAL AUTHOR
# __author__ = "IWS - iws@iws.dyndns.org"

# CURRENT MAINTAINER
__author__ = "original author: IWS; maintainer: Will Guaraldi - willg at bluesock dot org"
__version__ = "$Date$"
__licence__ = "python or GNU"
__url__ = "http://www.bluesock.org/~willg/pyblosxom/"
__description__ = "Breaks a long story at B R E A K (no spaces)."

import re

def cb_story(args):
    pagedelimiter = '\x0c'
    entry = args['entry']
    if not entry.has_key('body'):
        return
    continue_template = '<a href="%(url)s">continue reading &quot;' + entry['title'] + '&quot;....</a>'
    continued_template = '<a name="contd">&nbsp;</a>'

    match = re.search(pagedelimiter, entry['body'])

    httpinfo = args['request'].getHttp()
    pathinfo = httpinfo['PATH_INFO']
    
    sigindex = entry['body'].rfind('\x0c\n--')
    if(sigindex != -1):
        sig = '<br />' + entry['body'][sigindex+1:]
    else:
        sig = ''

    if match and sigindex != match.start():
        if args['entry'].has_key('readmore_template'):
            readmore_template = args['entry']['readmore_template']
            if isinstance(readmore_template, basestring):
                continue_template = readmore_template
                continued_template = ''
            elif isinstance(readmore_template, list):
                continue_template = readmore_template[0]
                if len(readmore_template) > 1:
                    continued_template = readmore_template[1]
        if entry['bl_type' ] == 'file':
            entry['body'] = re.sub(pagedelimiter,
                                   continued_template,
                                   entry['body'])
        elif pathinfo[0:7] == '/index.':
            base_url = entry['base_url']
            file_path = entry['file_path']
            flavour = entry['flavour']
            tuple = {'url':'%s/%s.html#contd' % (base_url, file_path),
                     'base_url':base_url,
                     'file_path':file_path,
                     'flavour':flavour}
            entry['body'] = entry['body'][:match.start()]
            entry['body'] += continue_template % tuple
            entry['body'] += sig
        else:
            entry['body'] = entry['body'].replace(pagedelimiter, '')
    else:
        entry['body'] = entry['body'].replace(pagedelimiter, '')
