"""
    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!

	Now supports anniversary calendars!

	Now includes a new optional parameter, anniversary.
        if the 4th paramater is set to 1, the year is left off of the
        pattern of pages to get... so

	[[IncludeCalendarPage(Calendar,2,0,1)]]

    includes the contents of page Calendar/03-16 down to level 2 headings.

	if the 4th parameter is set to 2, the year becomes optional
	meaning if you had an anniversary calendar called Yearly and
	a normal day calendar called myCalendar you could roll them both up with
	the following.
	
	[[IncludeCalendarPage(^(myCalendar|Yearly),2,0,2)]]

 includes the contents of page myCalendar/2003-03-16 AND Yearly/03-16 down to level 2 headings.

    $Id$ 
"""

import re
import calendar, cgi, time, re, string
#from MoinMoin import user
from MoinMoin import config
from MoinMoin import wikiutil
#from MoinMoin.i18n import _
import MoinMoin.macro.IncludePages

_arg_dayoffset = r',\s*(?P<dayoffset>-?\d+)'
_arg_level = r',\s*(?P<level>\d+)'
_arg_anniversary = r'(,\s*(?P<anniversary>\d+))?'
_args_re_pattern = r'^(?P<pattern>[^,]+)(%s)(%s)(%s)$' % (_arg_level, _arg_dayoffset, _arg_anniversary)

def execute(macro, text, args_re=re.compile(_args_re_pattern)):

    # parse and check arguments
    args = args_re.match(text)
    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')
    level = int(args.group('level'))
    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)


    if args.group('anniversary') is None:
	params = '%s/%d-%02d-%02d, %d' % (pattern, year, month, day, level)
    else:
	if int(args.group('anniversary')) == 1:
		params = '%s/%02d-%02d, %d' % (pattern, month, day, level)
    	elif  int(args.group('anniversary')) == 2:
		params = '%s/(%d-)?%02d-%02d, %d' % (pattern, year, month, day, level)	
	else:
		params = '%s/%d-%02d-%02d, %d' % (pattern, year, month, day, level)
    return MoinMoin.macro.IncludePages.execute(macro, params) 


