#format python

"""

    --------------------------
    Modified MonthCalendar Top List MACRO to allow for multiple MonthCalendars
    to be included in the 'Top List'.

    example  [[MonthCalendarTopListMulti(30, /MyCalendar, /OtherCalendar/Calendar1)]]

    modified by ScottSheffield
    --------------------------



    MoinMoin - MonthCalendar Top List MACRO
		
		by Mark Stier
		
		May be used to create a Blog off of a MonthCalendar data structure.
		(You'd need the MonthCalendar macro and the MonthCalendarRSS action
		for that.)
		
		Based on:
		    MonthCalendar macro from MoinMoin 1.5.2

    Call this macro on a base page for a MonthCalendar.
		It will pack the contents of all tips you get when moving a mouse
		over some day entry into a simple, clickable list.

    This is an addition to the MonthCalendar macro, so you should make
		yourself familiar with THAT one before trying to understand what this action
	  can do for you.
"""

Dependencies = ['namespace','time']

import re, calendar, time, StringIO, os
from datetime import date, timedelta
from MoinMoin import config, wikiutil, util
from MoinMoin.Page import Page

def execute(macro, text):
    request = macro.request
    formatter = macro.formatter
    thispage = formatter.page.page_name

    max_days = 365
    n_max_def = 10
    
    now = date.today()
    result = []
    n_cnt = 0
    
    if text:
        arguments = text.split(',')
        arguments = [argument.strip() for argument in arguments]
        if len(arguments) == 1:
                try:
                        n_max = int(arguments)
                except ValueError:
                        n_max = n_max_def
                        cals = arguments
        else:
                try:
                        n_max = int(arguments[0])
                        cals = arguments[1:]
                except ValueError:
                        n_max = n_max_def
                        cals = arguments
    else:
        n_max = n_max_def
        cals = [formatter.page.page_name]
        
    for i in range(-1,max_days): # include next day because of different timezones
            dte = now - timedelta(i)
            datestr = "%4d-%02d-%02d" % (dte.year, dte.month, dte.day)
            for cal in cals:
                    link = "%s/%s" % (cal, datestr)
                    daypage = Page(request, link)
                                    
                    if daypage.exists() and request.user.may.read(daypage.page_name):
                            daycontent = daypage.get_raw_body()
                            header1_re = re.compile(r'^\s*=\s(.*)\s=$', re.MULTILINE) # re.UNICODE
                            for match in header1_re.finditer(daycontent):
                                    if match:
                                            title = match.group(1)
                                            result.append("<a href=\"%s/%s\">%s (%s)</a>" % (request.getBaseURL(),link,title,datestr))
                                            n_cnt = n_cnt + 1
                                            if n_cnt == n_max:
                                                    break
                            if n_cnt == n_max:
                                    break
                    if n_cnt == n_max:
                        break
    if len(result) == 0:
        result = ["sorry, there are no entries within the last %d days." % max_days]
    return formatter.rawHTML('<br>'.join(result))
