paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - EventAggregator Macro |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2008, 2009 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@0 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@0 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 9 | """ |
paul@0 | 10 | |
paul@10 | 11 | from MoinMoin import wikiutil |
paul@10 | 12 | import EventAggregatorSupport |
paul@0 | 13 | import calendar |
paul@0 | 14 | |
paul@5 | 15 | try: |
paul@5 | 16 | set |
paul@5 | 17 | except NameError: |
paul@5 | 18 | from sets import Set as set |
paul@5 | 19 | |
paul@0 | 20 | Dependencies = ['pages'] |
paul@0 | 21 | |
paul@0 | 22 | # Date labels. |
paul@0 | 23 | |
paul@0 | 24 | month_labels = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] |
paul@0 | 25 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@0 | 26 | |
paul@10 | 27 | # HTML-related functions. |
paul@2 | 28 | |
paul@2 | 29 | def getColour(s): |
paul@2 | 30 | colour = [0, 0, 0] |
paul@2 | 31 | digit = 0 |
paul@2 | 32 | for c in s: |
paul@2 | 33 | colour[digit] += ord(c) |
paul@2 | 34 | colour[digit] = colour[digit] % 256 |
paul@2 | 35 | digit += 1 |
paul@2 | 36 | digit = digit % 3 |
paul@2 | 37 | return tuple(colour) |
paul@2 | 38 | |
paul@2 | 39 | def getBlackOrWhite(colour): |
paul@2 | 40 | if sum(colour) / 3.0 > 127: |
paul@2 | 41 | return (0, 0, 0) |
paul@2 | 42 | else: |
paul@2 | 43 | return (255, 255, 255) |
paul@2 | 44 | |
paul@10 | 45 | # Macro function. |
paul@10 | 46 | |
paul@0 | 47 | def execute(macro, args): |
paul@0 | 48 | |
paul@0 | 49 | """ |
paul@0 | 50 | Execute the 'macro' with the given 'args': an optional list of selected |
paul@0 | 51 | category names (categories whose pages are to be shown), together with |
paul@5 | 52 | optional named arguments of the following forms: |
paul@5 | 53 | |
paul@5 | 54 | start=YYYY-MM shows event details starting from the specified month |
paul@5 | 55 | end=YYYY-MM shows event details ending at the specified month |
paul@5 | 56 | |
paul@5 | 57 | mode=calendar shows a calendar view of events |
paul@5 | 58 | mode=list shows a list of events by month |
paul@9 | 59 | mode=ics provides iCalendar data for the events |
paul@5 | 60 | |
paul@5 | 61 | names=daily shows the name of an event on every day of that event |
paul@5 | 62 | names=weekly shows the name of an event once per week |
paul@0 | 63 | """ |
paul@0 | 64 | |
paul@0 | 65 | request = macro.request |
paul@0 | 66 | fmt = macro.formatter |
paul@0 | 67 | page = fmt.page |
paul@0 | 68 | _ = request.getText |
paul@0 | 69 | |
paul@0 | 70 | # Interpret the arguments. |
paul@0 | 71 | |
paul@0 | 72 | try: |
paul@0 | 73 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 74 | except AttributeError: |
paul@0 | 75 | parsed_args = args.split(",") |
paul@0 | 76 | |
paul@0 | 77 | parsed_args = [arg for arg in parsed_args if arg] |
paul@0 | 78 | |
paul@0 | 79 | # Get special arguments. |
paul@0 | 80 | |
paul@0 | 81 | category_names = [] |
paul@0 | 82 | calendar_start = None |
paul@0 | 83 | calendar_end = None |
paul@0 | 84 | mode = "calendar" |
paul@5 | 85 | name_usage = "daily" |
paul@0 | 86 | |
paul@0 | 87 | for arg in parsed_args: |
paul@0 | 88 | if arg.startswith("start="): |
paul@10 | 89 | calendar_start = EventAggregatorSupport.getMonth(arg[6:]) |
paul@0 | 90 | elif arg.startswith("end="): |
paul@10 | 91 | calendar_end = EventAggregatorSupport.getMonth(arg[4:]) |
paul@0 | 92 | elif arg.startswith("mode="): |
paul@0 | 93 | mode = arg[5:] |
paul@5 | 94 | elif arg.startswith("names="): |
paul@5 | 95 | name_usage = arg[6:] |
paul@0 | 96 | else: |
paul@0 | 97 | category_names.append(arg) |
paul@0 | 98 | |
paul@10 | 99 | events, shown_events, all_shown_events, earliest, latest = \ |
paul@10 | 100 | EventAggregatorSupport.getEvents(request, category_names, calendar_start, calendar_end) |
paul@2 | 101 | |
paul@0 | 102 | # Make a calendar. |
paul@0 | 103 | |
paul@0 | 104 | output = [] |
paul@0 | 105 | |
paul@9 | 106 | # Output top-level information. |
paul@9 | 107 | |
paul@2 | 108 | if mode == "list": |
paul@2 | 109 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-listings"})) |
paul@0 | 110 | |
paul@2 | 111 | # Visit all months in the requested range, or across known events. |
paul@0 | 112 | |
paul@2 | 113 | first = calendar_start or earliest |
paul@2 | 114 | last = calendar_end or latest |
paul@1 | 115 | |
paul@10 | 116 | for year, month in EventAggregatorSupport.daterange(first, last): |
paul@2 | 117 | |
paul@2 | 118 | # Either output a calendar view... |
paul@0 | 119 | |
paul@2 | 120 | if mode == "calendar": |
paul@2 | 121 | |
paul@2 | 122 | # Output a month. |
paul@2 | 123 | |
paul@2 | 124 | output.append(fmt.table(on=1, attrs={"tableclass" : "event-month"})) |
paul@0 | 125 | |
paul@2 | 126 | output.append(fmt.table_row(on=1)) |
paul@2 | 127 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-month-heading", "colspan" : "7"})) |
paul@2 | 128 | output.append(fmt.span(on=1)) |
paul@2 | 129 | output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels |
paul@2 | 130 | output.append(fmt.span(on=0)) |
paul@2 | 131 | output.append(fmt.text(" ")) |
paul@2 | 132 | output.append(fmt.span(on=1)) |
paul@2 | 133 | output.append(fmt.text(year)) |
paul@2 | 134 | output.append(fmt.span(on=0)) |
paul@2 | 135 | output.append(fmt.table_cell(on=0)) |
paul@2 | 136 | output.append(fmt.table_row(on=0)) |
paul@0 | 137 | |
paul@2 | 138 | # Weekday headings. |
paul@0 | 139 | |
paul@2 | 140 | output.append(fmt.table_row(on=1)) |
paul@0 | 141 | |
paul@2 | 142 | for weekday in range(0, 7): |
paul@5 | 143 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-weekday-heading"})) |
paul@2 | 144 | output.append(fmt.text(_(weekday_labels[weekday]))) |
paul@0 | 145 | output.append(fmt.table_cell(on=0)) |
paul@2 | 146 | |
paul@2 | 147 | output.append(fmt.table_row(on=0)) |
paul@2 | 148 | |
paul@2 | 149 | # Process the days of the month. |
paul@2 | 150 | |
paul@2 | 151 | start_weekday, number_of_days = calendar.monthrange(year, month) |
paul@0 | 152 | |
paul@2 | 153 | # The start weekday is the weekday of day number 1. |
paul@2 | 154 | # Find the first day of the week, counting from below zero, if |
paul@2 | 155 | # necessary, in order to land on the first day of the month as |
paul@2 | 156 | # day number 1. |
paul@2 | 157 | |
paul@2 | 158 | first_day = 1 - start_weekday |
paul@2 | 159 | |
paul@2 | 160 | while first_day <= number_of_days: |
paul@2 | 161 | |
paul@5 | 162 | # Find events in this week and determine how to mark them on the |
paul@5 | 163 | # calendar. |
paul@5 | 164 | |
paul@5 | 165 | week_events = [] |
paul@5 | 166 | week_coverage = set() |
paul@5 | 167 | |
paul@5 | 168 | week_start = (year, month, max(first_day, 1)) |
paul@5 | 169 | week_end = (year, month, min(first_day + 6, number_of_days)) |
paul@5 | 170 | |
paul@5 | 171 | # Get event details. |
paul@5 | 172 | |
paul@5 | 173 | for event in shown_events.get((year, month), []): |
paul@5 | 174 | event_page, event_details = event |
paul@5 | 175 | |
paul@5 | 176 | # Test for the event in the current week. |
paul@5 | 177 | |
paul@5 | 178 | if event_details["start"] <= week_end and event_details["end"] >= week_start: |
paul@5 | 179 | |
paul@5 | 180 | # Find the coverage of this week for the event. |
paul@5 | 181 | |
paul@5 | 182 | event_start = max(event_details["start"], week_start) |
paul@5 | 183 | event_end = min(event_details["end"], week_end) |
paul@10 | 184 | event_coverage = set(EventAggregatorSupport.daterange(event_start, event_end)) |
paul@5 | 185 | |
paul@5 | 186 | # Update the overall coverage. |
paul@5 | 187 | |
paul@5 | 188 | week_coverage.update(event_coverage) |
paul@5 | 189 | |
paul@5 | 190 | # Try and fit the event into the events list. |
paul@5 | 191 | |
paul@5 | 192 | for i, (coverage, events) in enumerate(week_events): |
paul@5 | 193 | |
paul@5 | 194 | # Where the event does not overlap with the current |
paul@5 | 195 | # element, add it alongside existing events. |
paul@5 | 196 | |
paul@5 | 197 | if not coverage.intersection(event_coverage): |
paul@5 | 198 | events.append(event) |
paul@5 | 199 | week_events[i] = coverage.union(event_coverage), events |
paul@5 | 200 | break |
paul@5 | 201 | |
paul@5 | 202 | # Make a new element in the list if the event cannot be |
paul@5 | 203 | # marked alongside existing events. |
paul@5 | 204 | |
paul@5 | 205 | else: |
paul@5 | 206 | week_events.append((event_coverage, [event])) |
paul@5 | 207 | |
paul@5 | 208 | # Output a week, starting with the day numbers. |
paul@0 | 209 | |
paul@0 | 210 | output.append(fmt.table_row(on=1)) |
paul@0 | 211 | |
paul@0 | 212 | for weekday in range(0, 7): |
paul@2 | 213 | day = first_day + weekday |
paul@5 | 214 | date = (year, month, day) |
paul@2 | 215 | |
paul@2 | 216 | # Output out-of-month days. |
paul@2 | 217 | |
paul@2 | 218 | if day < 1 or day > number_of_days: |
paul@5 | 219 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading event-day-excluded"})) |
paul@2 | 220 | output.append(fmt.table_cell(on=0)) |
paul@2 | 221 | |
paul@2 | 222 | # Output normal days. |
paul@2 | 223 | |
paul@2 | 224 | else: |
paul@5 | 225 | if date in week_coverage: |
paul@5 | 226 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading event-day-busy"})) |
paul@2 | 227 | else: |
paul@5 | 228 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day-heading event-day-empty"})) |
paul@2 | 229 | |
paul@2 | 230 | output.append(fmt.div(on=1)) |
paul@2 | 231 | output.append(fmt.span(on=1, css_class="event-day-number")) |
paul@2 | 232 | output.append(fmt.text(day)) |
paul@2 | 233 | output.append(fmt.span(on=0)) |
paul@2 | 234 | output.append(fmt.div(on=0)) |
paul@2 | 235 | |
paul@5 | 236 | # End of day. |
paul@5 | 237 | |
paul@5 | 238 | output.append(fmt.table_cell(on=0)) |
paul@5 | 239 | |
paul@5 | 240 | # End of day numbers. |
paul@5 | 241 | |
paul@5 | 242 | output.append(fmt.table_row(on=0)) |
paul@5 | 243 | |
paul@7 | 244 | # Either generate empty days... |
paul@5 | 245 | |
paul@7 | 246 | if not week_events: |
paul@5 | 247 | output.append(fmt.table_row(on=1)) |
paul@5 | 248 | |
paul@5 | 249 | for weekday in range(0, 7): |
paul@5 | 250 | day = first_day + weekday |
paul@5 | 251 | date = (year, month, day) |
paul@5 | 252 | |
paul@7 | 253 | # Output out-of-month days. |
paul@5 | 254 | |
paul@5 | 255 | if day < 1 or day > number_of_days: |
paul@5 | 256 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-excluded"})) |
paul@5 | 257 | output.append(fmt.table_cell(on=0)) |
paul@2 | 258 | |
paul@7 | 259 | # Output empty days. |
paul@5 | 260 | |
paul@5 | 261 | else: |
paul@5 | 262 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-empty"})) |
paul@5 | 263 | |
paul@7 | 264 | output.append(fmt.table_row(on=0)) |
paul@7 | 265 | |
paul@7 | 266 | # Or visit each set of scheduled events... |
paul@7 | 267 | |
paul@7 | 268 | else: |
paul@7 | 269 | for coverage, events in week_events: |
paul@7 | 270 | |
paul@7 | 271 | # Output each set. |
paul@7 | 272 | |
paul@7 | 273 | output.append(fmt.table_row(on=1)) |
paul@5 | 274 | |
paul@7 | 275 | # Then, output day details. |
paul@7 | 276 | |
paul@7 | 277 | for weekday in range(0, 7): |
paul@7 | 278 | day = first_day + weekday |
paul@7 | 279 | date = (year, month, day) |
paul@7 | 280 | |
paul@7 | 281 | # Skip out-of-month days. |
paul@7 | 282 | |
paul@7 | 283 | if day < 1 or day > number_of_days: |
paul@7 | 284 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-excluded"})) |
paul@7 | 285 | output.append(fmt.table_cell(on=0)) |
paul@5 | 286 | continue |
paul@2 | 287 | |
paul@7 | 288 | # Output the day. |
paul@2 | 289 | |
paul@7 | 290 | if date in coverage: |
paul@7 | 291 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-busy"})) |
paul@7 | 292 | else: |
paul@7 | 293 | output.append(fmt.table_cell(on=1, attrs={"class" : "event-day event-day-empty"})) |
paul@2 | 294 | |
paul@7 | 295 | # Get event details for the current day. |
paul@2 | 296 | |
paul@7 | 297 | for event_page, event_details in events: |
paul@7 | 298 | if not (event_details["start"] <= date <= event_details["end"]): |
paul@7 | 299 | continue |
paul@7 | 300 | |
paul@7 | 301 | # Get a pretty version of the page name. |
paul@3 | 302 | |
paul@10 | 303 | pretty_pagename = EventAggregatorSupport.getPrettyPageName(event_page) |
paul@7 | 304 | |
paul@7 | 305 | # Generate a colour for the event. |
paul@3 | 306 | |
paul@7 | 307 | bg = getColour(event_page.page_name) |
paul@7 | 308 | fg = getBlackOrWhite(bg) |
paul@7 | 309 | |
paul@7 | 310 | css_classes = ["event-summary"] |
paul@3 | 311 | |
paul@7 | 312 | if event_details["start"] == date: |
paul@7 | 313 | css_classes.append("event-starts") |
paul@7 | 314 | start_of_event = 1 |
paul@7 | 315 | else: |
paul@7 | 316 | start_of_event = 0 |
paul@2 | 317 | |
paul@7 | 318 | if event_details["end"] == date: |
paul@7 | 319 | css_classes.append("event-ends") |
paul@7 | 320 | |
paul@7 | 321 | # Output the event. |
paul@5 | 322 | |
paul@7 | 323 | if name_usage == "daily" or start_of_event or weekday == 0 or day == 1: |
paul@7 | 324 | hide_text = 0 |
paul@7 | 325 | else: |
paul@7 | 326 | hide_text = 1 |
paul@5 | 327 | |
paul@7 | 328 | output.append(fmt.div(on=1, css_class=(" ".join(css_classes)), |
paul@7 | 329 | style=("background-color: rgb(%d, %d, %d); color: rgb(%d, %d, %d);" % (bg + fg)))) |
paul@5 | 330 | |
paul@7 | 331 | if not hide_text: |
paul@7 | 332 | output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename))) |
paul@7 | 333 | |
paul@7 | 334 | output.append(fmt.div(on=0)) |
paul@2 | 335 | |
paul@7 | 336 | # End of day. |
paul@2 | 337 | |
paul@7 | 338 | output.append(fmt.table_cell(on=0)) |
paul@0 | 339 | |
paul@7 | 340 | # End of set. |
paul@5 | 341 | |
paul@7 | 342 | output.append(fmt.table_row(on=0)) |
paul@5 | 343 | |
paul@5 | 344 | # Process the next week... |
paul@0 | 345 | |
paul@2 | 346 | first_day += 7 |
paul@0 | 347 | |
paul@2 | 348 | # End of month. |
paul@0 | 349 | |
paul@2 | 350 | output.append(fmt.table(on=0)) |
paul@0 | 351 | |
paul@2 | 352 | # Or output a summary view... |
paul@0 | 353 | |
paul@2 | 354 | elif mode == "list": |
paul@0 | 355 | |
paul@2 | 356 | output.append(fmt.listitem(on=1, attr={"class" : "event-listings-month"})) |
paul@2 | 357 | output.append(fmt.div(on=1, attr={"class" : "event-listings-month-heading"})) |
paul@2 | 358 | output.append(fmt.span(on=1)) |
paul@2 | 359 | output.append(fmt.text(_(month_labels[month - 1]))) # zero-based labels |
paul@2 | 360 | output.append(fmt.span(on=0)) |
paul@2 | 361 | output.append(fmt.text(" ")) |
paul@2 | 362 | output.append(fmt.span(on=1)) |
paul@2 | 363 | output.append(fmt.text(year)) |
paul@2 | 364 | output.append(fmt.span(on=0)) |
paul@2 | 365 | output.append(fmt.div(on=0)) |
paul@0 | 366 | |
paul@2 | 367 | output.append(fmt.bullet_list(on=1, attr={"class" : "event-month-listings"})) |
paul@0 | 368 | |
paul@2 | 369 | for event_page, event_details in shown_events.get((year, month), []): |
paul@0 | 370 | |
paul@2 | 371 | # Get a pretty version of the page name. |
paul@0 | 372 | |
paul@10 | 373 | pretty_pagename = EventAggregatorSupport.getPrettyPageName(event_page) |
paul@0 | 374 | |
paul@2 | 375 | output.append(fmt.listitem(on=1, attr={"class" : "event-listing"})) |
paul@0 | 376 | |
paul@2 | 377 | # Link to the page using the pretty name. |
paul@0 | 378 | |
paul@2 | 379 | output.append(event_page.link_to_raw(request, wikiutil.escape(pretty_pagename))) |
paul@0 | 380 | |
paul@2 | 381 | # Add the event details. |
paul@0 | 382 | |
paul@2 | 383 | output.append(fmt.definition_list(on=1, attr={"class" : "event-details"})) |
paul@0 | 384 | |
paul@2 | 385 | for key, value in event_details.items(): |
paul@2 | 386 | output.append(fmt.definition_term(on=1)) |
paul@2 | 387 | output.append(fmt.text(key)) |
paul@2 | 388 | output.append(fmt.definition_term(on=0)) |
paul@2 | 389 | output.append(fmt.definition_desc(on=1)) |
paul@2 | 390 | output.append(fmt.text(value)) |
paul@2 | 391 | output.append(fmt.definition_desc(on=0)) |
paul@0 | 392 | |
paul@2 | 393 | output.append(fmt.definition_list(on=0)) |
paul@2 | 394 | output.append(fmt.listitem(on=0)) |
paul@0 | 395 | |
paul@2 | 396 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 397 | |
paul@9 | 398 | # Output top-level information. |
paul@9 | 399 | |
paul@9 | 400 | # End of list view output. |
paul@9 | 401 | |
paul@10 | 402 | if mode == "list": |
paul@0 | 403 | output.append(fmt.bullet_list(on=0)) |
paul@0 | 404 | |
paul@0 | 405 | return ''.join(output) |
paul@0 | 406 | |
paul@0 | 407 | # vim: tabstop=4 expandtab shiftwidth=4 |