"""
    MoinMoin - IncludeCalendarPage macro

    version 1.0

    (c) 2003 Charles Crichton, Oxford University Computing Laboratory
        Charles.Crichton (in the domain) comlab.ox.ac.uk
    Licensed under GNU GPL - see COPYING for details.


    The date code is modified from code in MonthCalendar.py by Thomas Waldmann
    Thanks! :-) 

    The argument matching code is modified from IncludePages.py by Michael Reinsch
    Cheers!

    I spotted the name Richard Jones in Include.py on which this ultimately relies.
    Thankyou.

    Obviously credit should also go to Jürgen Hermann for making this all possible!

    ----

    This simple macro allows the contents of a calendar page created
    using the MonthCalendar.py macro. To be displayed on the current page.

    I use it to display a week of calendar entries on my front page.

    To install:
     * Install the MonthCalendar.py and the IncludePages.py macros
     * Save this macro in your macros directory

    To use:

     [[IncludeCalendarPage(pagepattern,level,0)]] - include todays page
     [[IncludeCalendarPage(pagepattern,level,-1)]] - include yesterdays page
     [[IncludeCalendarPage(pagepattern,level,1)]] - include tomorrows page
     [[IncludeCalendarPage(pagepattern,level,365)]] - include next years page

     * pagepattern: (A page pattern to which a date will be appended
     * level: has the same meaning as in IncludePages.py macro.

    Todays date is 2003-03-16. So the macro

     [[IncludeCalendarPage(Calendar,2,0)]]

    includes the contents of page Calendar/2003-03-16 down to level 2 headings.

     [[IncludeCalendarPage(CharlieDiary,2,-1)]] - include CharlieDiary/2003-03-15

    You get the idea!

    $Id$ 
"""

import calendar, cgi, time, re, string
from MoinMoin import config
from MoinMoin import wikiutil
import MoinMoin.macro.Include

_arg_dayoffset = r',\s*(?P<dayoffset>-?\d+)'
_arg_level = r',\s*(?P<level>\d+)'
_args_re_pattern = r'^(?P<pattern>[^,]+)(%s)(%s)$' % (_arg_level, _arg_dayoffset)
#_args_re_pattern = '^(?P<pattern>[^,]+)$'

def execute(macro, text, args_re=re.compile(_args_re_pattern)):
     # parse and check arguments
     args = args_re.match(text)
     #return ('<p><string class="error">%s</string></p>') % (_args_re_pattern,)
     if not args:
        return ('<p><strong class="error">%s</strong></p>' %
            ('Invalid include calendar page arguments "%s"!')) % (text,)

    # get the arguments
     pattern = args.group('pattern')
     dayoffset = int(args.group('dayoffset'))

     # There are 86400 seconds in a day.
     datetofind = time.time() + dayoffset * 86400
     (year,month,day,h,m,s,wd,yd,ds) = time.localtime(datetofind)

     params = '%s/%d-%02d-%02d' % (pattern, year, month, day)
    
     return MoinMoin.macro.Include.execute(macro,params) 

