Attachment 'Blog.py'

Download

   1 """
   2     MoinMoin - Blog macro
   3 
   4     version 1.0-Beta
   5 
   6     (c) 2003 Mark Proctor, athico.com
   7     Licensed under GNU GPL - see COPYING for details.
   8 
   9     ----
  10     Overview:
  11 
  12     This is a simply Blog macro that utilises a javascript calendar and the Include macro to hack together a pseudo bwiki (blog/wiki).
  13     The calendar is used to choose the entries to show, the number of visible entries is controlled by the select control "max entries".
  14     The button to the left of "max entries" allows you to toggle between the two modes "Show All" and "Show Published".
  15      *  "Show Published" only shows those days that contain entries up to the given "max entries", from the chosen calendar date.
  16      *  "Show All" Show all the dates, previous to the chosen calendar date, up to a maximum of "max entries".
  17      This is the mode you  will need to use to enter new  blog entries
  18 
  19     Dependencies
  20      * Include
  21 
  22     To install:
  23      * Save this macro in your macros directory
  24 
  25     To Use:
  26      * <date>       : in the format of yyyy-mm-dd
  27      * <showAll>    : 1 or 0, where 1 shows all and 0 shows published
  28      * <entries>    : the maximum visible number of entries
  29      * <maxEntries> : the maximum value that <entries> can be, this is used to restrict the web gui
  30      * <startDay>   : The start Day for the calendar
  31       * values      : "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"
  32 
  33 
  34     defaults:
  35      * <date>      : today
  36      * <showAll>   : 0
  37      * <entries>   : 5
  38      * <maxEntries>: 20
  39      * <startDay>  : 'Mo'
  40 
  41     [[Blog[<date>, <showAll>, <entries>, <maxEntries>
  42     [[Blog(, 1, 7)]] - Shows all days, up to 7 days, from todays date.
  43     [[Blog(2003-05-23, 0, 5)]] - Shows upto 5 published entries from the given date.
  44     [[Blog( , , , 10)]] - Shows upto 5(default) published(default) entries from todays date(default), but does not allow the user to speficy max entries to be more than 10.
  45     [[Blog( , , , , We)]] - Shows upto 5(default) published(default) entries from todays date(default), maxEntries(20) with start calendar  day Wednesday.
  46     [[Blog(2003-05-23, 0, 5, ,Sa)]] - Shows upto 5 published entries from the given date, with start calendar day Saturday
  47 
  48     $Id$
  49 """
  50 
  51 from MoinMoin import config, wikiutil
  52 from MoinMoin.Page import Page
  53 import re, time, string
  54 import MoinMoin.macro.Include
  55 
  56 def getStyle():
  57     style = """
  58 <style type="text/css">
  59     .calendarButton {
  60         font-size:10;
  61         cursor:pointer;
  62         cursor:hand;
  63     }
  64 
  65     .calendarHeader {
  66         background-color:#C0DED1;
  67         font-size:12;
  68         text-decoration:none;
  69         cursor:pointer;
  70         cursor:hand;
  71     }
  72 
  73     .calendarValue {
  74         background-color:#FDFAD1;
  75         font-size:10;
  76         font-color:black;
  77         cursor:pointer;
  78         cursor:hand;
  79     }
  80 
  81     .calendarValueSelected {
  82         background-color:#FD0000;
  83         font-size:10;
  84         font-color:black;
  85         cursor:pointer;
  86         cursor:hand;
  87     }
  88 </style>
  89 """
  90 
  91     return style
  92 
  93 def getJavascript(page, date, entries, showAll, startDay):
  94     javascript = """
  95 <script>
  96 
  97 var global = {};
  98 global.page = "%s";
  99 global.date = "%s";
 100 global.entries = "%s";
 101 global.showAll = "%s";
 102 
 103 global.daysLookup = {"Su":0, "Mo":1, "Tu":2, "We":3, "Th":4, "Fr":5, "Sa":6};
 104 global.days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
 105 global.startDay = "%s";
 106 
 107 function gotoDate() {
 108     if (!this.date) return;
 109     global.date = this.date;
 110     updateBlog();
 111 }
 112 
 113 function updateBlog() {
 114     window.location.href = "/"+global.page+"?date="+global.date+"&entries="+global.entries+"&showAll="+global.showAll
 115 }
 116 
 117 function setToday() {
 118     var now   = new Date();
 119     var day   = now.getDate();
 120     var month = now.getMonth();
 121     var year  = now.getYear();
 122     if (year < 2000)    // Y2K Fix, Isaac Powell
 123     year = year + 1900; // http://onyx.idbsu.edu/~ipowell
 124     this.focusDay = day;
 125     document.calControl.month.selectedIndex = month;
 126     document.calControl.year.value = year;
 127     updateCalendar(month, year);
 128 }
 129 
 130 function isFourDigitYear(year) {
 131     if (year.length != 4) {
 132         alert ("Sorry, the year must be four-digits in length.");
 133         document.calControl.year.select();
 134         document.calControl.year.focus();
 135         return false;
 136     } else {
 137         return true;
 138     }
 139 }
 140 
 141 function selectDate() {
 142     var year  = document.calControl.year.value;
 143     if (isFourDigitYear(year)) {
 144         var day   = 0;
 145         var month = document.calControl.month.selectedIndex;
 146         updateCalendar(month, year);
 147     }
 148 }
 149 
 150 function setPreviousYear() {
 151     var year  = document.calControl.year.value;
 152     if (isFourDigitYear(year)) {
 153         var day   = 0;
 154         var month = document.calControl.month.selectedIndex;
 155         year--;
 156         document.calControl.year.value = year;
 157         updateCalendar(month, year);
 158    }
 159 }
 160 function setPreviousMonth() {
 161     var year  = document.calControl.year.value;
 162     if (isFourDigitYear(year)) {
 163         var day   = 0;
 164         var month = document.calControl.month.selectedIndex;
 165         if (month == 0) {
 166             month = 11;
 167             if (year > 1000) {
 168                 year--;
 169                 document.calControl.year.value = year;
 170             }
 171         } else {
 172             month--;
 173         }
 174         document.calControl.month.selectedIndex = month;
 175         updateCalendar(month, year);
 176    }
 177 }
 178 function setNextMonth() {
 179     var year  = document.calControl.year.value;
 180     if (isFourDigitYear(year)) {
 181         var day   = 0;
 182         var month = document.calControl.month.selectedIndex;
 183         if (month == 11) {
 184             month = 0;
 185             year++;
 186             document.calControl.year.value = year;
 187         } else {
 188             month++;
 189         }
 190         document.calControl.month.selectedIndex = month;
 191         updateCalendar(month, year);
 192    }
 193 }
 194 function setNextYear() {
 195     var year = document.calControl.year.value;
 196     if (isFourDigitYear(year)) {
 197         var day = 0;
 198         var month = document.calControl.month.selectedIndex;
 199         year++;
 200         document.calControl.year.value = year;
 201         updateCalendar(month, year);
 202    }
 203 }
 204 
 205 function makeCalendar() {
 206     var cal = document.getElementById("calendarTbody");
 207     if (cal) return;
 208     var i;
 209     var table = document.createElement("table");
 210     table.style.cssText = "border-left:1px solid black; border-top:1px solid black";
 211     table.cellSpacing = 0;
 212     table.cellPadding = 2;
 213     var thead = document.createElement("thead");
 214 
 215     var cell;
 216     var row = document.createElement("tr");
 217 
 218     var titleDays = [];
 219     var startDay = global.daysLookup[global.startDay];
 220 
 221     for (i=startDay;i<7;i++) {
 222         titleDays.push(global.days[i]);
 223     }
 224 
 225     for (i=0;i<startDay;i++) {
 226         titleDays.push(global.days[i]);
 227     }
 228 
 229     for  (i=0;i<titleDays.length;i++) {
 230         cell = document.createElement("td");
 231         cell.style.cssText = "border-right:1px solid black; border-bottom:1px solid black";
 232         cell.className = "calendarHeader";
 233         cell.innerHTML =  titleDays[i];
 234         row.appendChild(cell);
 235     }
 236     thead.appendChild(row);
 237     table.appendChild(thead);
 238 
 239     var tbody = document.createElement("tbody");
 240     tbody.id = "calendarTbody";
 241     row = document.createElement("tr");
 242     for (i=0; i<42; i++)  {
 243         if ( i%%7 == 0 ) { //start new line
 244             tbody.appendChild(row);
 245             row = document.createElement("tr");
 246         }
 247         cell = document.createElement("td");
 248         cell.className = "calendarValue";
 249         cell.style.cssText = "border-right:1px solid black; border-bottom:1px solid black";
 250         cell.innerHTML = "&nbsp;";
 251         cell.date = null;
 252         cell.onclick = gotoDate
 253         row.appendChild(cell);
 254     }
 255     tbody.appendChild(row);
 256     table.appendChild(tbody);
 257 
 258     var  calendar = document.getElementById("calendar");
 259     calendar.appendChild(table);
 260 }
 261 
 262 function updateCalendar(month, year) {
 263     month = parseInt(month);
 264     year = parseInt(year);
 265     var i = 0;
 266     var days = getDaysInMonth(month+1,year);
 267     var startDay = global.daysLookup[global.startDay];
 268     var firstOfMonth = new Date (year, month, 1).getDay();
 269     var startingPos =  (firstOfMonth >= startDay) ? firstOfMonth - startDay : 7 - (startDay - firstOfMonth);
 270     days += startingPos;
 271 
 272     var cal = document.getElementById("calendarTbody");
 273 
 274     var cells = cal.getElementsByTagName("td");
 275     var cell;
 276     for (i = 0; i < startingPos; i++) {
 277         cell = cells[i];
 278         cell.innerHTML = "&nbsp;";
 279         cell.date = null;
 280         cell.className = "calendarValue";
 281     }
 282 
 283     var value;
 284     month++;
 285     if (month<10) month = "0" + month;
 286     date = year+"-"+month+"-";
 287     for (i = startingPos; i < days; i++) {
 288         cell = cells[i];
 289         value = "";
 290         value = i-startingPos+1;
 291         if (value < 10) value = "0" + value;
 292         cell.date = year+'-'+(month)+'-'+value;
 293         cell.innerHTML = value;
 294         if (global.date != date+value) cell.className = "calendarValue";
 295         else cell.className = "calendarValueSelected";
 296     }
 297 
 298     for (i = days; i < 42; i++) {
 299         cell = cells[i];
 300         cell.date = null;
 301         cell.className = "calendarValue";
 302         cell.innerHTML = "&nbsp;";
 303     }
 304 }
 305 
 306 function getDaysInMonth(month,year)  {
 307     var days;
 308     if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)  days=31;
 309     else if (month==4 || month==6 || month==9 || month==11) days=30;
 310     else if (month==2)  {
 311         if (isLeapYear(year)) { days=29; }
 312         else { days=28; }
 313     }
 314     return (days);
 315 }
 316 
 317 function isLeapYear (Year) {
 318     if (((Year %% 4)==0) && ((Year %% 100)!=0) || ((Year %% 400)==0)) {
 319     return (true);
 320     } else { return (false); }
 321 }
 322 </script>
 323 """ % (page, date, entries, showAll, startDay);
 324 
 325     return javascript
 326 
 327 def getCalendar():
 328     calendar = """
 329 
 330 <FORM NAME="calControl" onsubmit="return false;" >
 331 <TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 >
 332 <TR><TD>
 333 <CENTER>
 334 <SELECT class="calendarButton" NAME="month" onchange="selectDate()">
 335 <OPTION>January
 336 <OPTION>February
 337 <OPTION>March
 338 <OPTION>April
 339 <OPTION>May
 340 <OPTION>June
 341 <OPTION>July
 342 
 343 <OPTION>August
 344 <OPTION>September
 345 <OPTION>October
 346 <OPTION>November
 347 <OPTION>December
 348 </SELECT>
 349 <INPUT NAME="year" class="calendarButton" TYPE=TEXT SIZE=4 MAXLENGTH=4>
 350 <INPUT TYPE="button" class="calendarButton" NAME="Go" value="Update Year" onClick="selectDate()">
 351 </CENTER>
 352 </TD>
 353 </TR>
 354 </FORM>
 355 <FORM NAME="calButtons">
 356 <TR><TD id="calendar" align="center" onsubmit="return false;" >
 357 
 358 </TD><TR><TD><CENTER>
 359 <INPUT class='calendarButton' TYPE=BUTTON NAME="previousYear" VALUE=" <<  "    onClick="setPreviousYear()">
 360 <INPUT class='calendarButton' TYPE=BUTTON NAME="previousYear" VALUE="  <  "    onClick="setPreviousMonth()">
 361 <INPUT class='calendarButton' TYPE=BUTTON NAME="previousYear" VALUE="Today"    onClick="setToday()">
 362 <INPUT class='calendarButton' TYPE=BUTTON NAME="previousYear" VALUE="  >  "    onClick="setNextMonth()">
 363 <INPUT class='calendarButton' TYPE=BUTTON NAME="previousYear" VALUE="  >> "    onClick="setNextYear()">
 364 </CENTER></TD></TR>
 365 </TABLE></FORM>
 366 <script>
 367  onload=function() {
 368     var date = global.date.split("-");
 369     document.calControl.month.selectedIndex = date[1]-1;
 370     document.calControl.year.value = date[0];
 371     makeCalendar();
 372     updateCalendar(date[1]-1, date[0]);
 373  };
 374 </script>
 375 """
 376     return calendar
 377 
 378 #def execute(macro, text, args_re=re.compile(_args_re_pattern)):
 379 
 380 def execute(macro, text):
 381     thisPage = macro.formatter.page.page_name
 382 
 383 
 384     #get incoming macro args, else set to []
 385     if text:
 386         args = string.split(text, ",")
 387     else:
 388         args = []
 389 
 390     #remove all leading and trailing spaces
 391     for i in range(len(args)):
 392         args[i] = string.strip(args[i])
 393 
 394     #set date
 395     if macro.form.has_key('date'):
 396         date = macro.form['date'].value
 397     elif (len(args) >= 0) and (not args[0] == ""):
 398         date = args[0]
 399     else:
 400         date = ""
 401 
 402 
 403     #set showAll
 404     if macro.form.has_key('showAll'):
 405         showAll = macro.form['showAll'].value
 406     elif (len(args) >= 1) and (not args[1] == ""):
 407         showAll = args[1]
 408     else:
 409         showAll = "0"
 410 
 411     #set entries
 412     if macro.form.has_key('entries'):
 413         entries = macro.form['entries'].value
 414     elif (len(args) >= 2) and (not args[2] == ""):
 415         entries = args[2]
 416     else:
 417         entries = -1
 418 
 419     #set max entries
 420     if (len(args) >= 3) and (not args[3] == ""):
 421         maxEntries = int(args[3])
 422         if maxEntries < 0:
 423             maxEntries = 20
 424     else:
 425         maxEntries = 20
 426 
 427     if (len(args) >= 4) and (not args[4] == ""):
 428         startDay = args[4]
 429     else:
 430         startDay = "Mo";
 431 
 432 
 433     #set the number of visible entries
 434     if not entries == -1:
 435         args_re=re.compile(r'(?P<entries>\d+)')
 436         args = args_re.match(entries)
 437         if not args:
 438             return ('<p><strong class="error">%s</strong></p>' %('Invalid entries "%s"!')) % (macro.form['beforeDate'].value)
 439         entries = int(macro.form['entries'].value)
 440         if entries > maxEntries:
 441             entries = maxEntries
 442         if entries < 0:
 443             entries = 5
 444     else:
 445         entries = 5
 446 
 447     #get the date
 448     if not date == "":
 449         args_re=re.compile(r'(?P<year>\d\d\d\d)-(?P<month>\d?\d)-(?P<day>\d?\d)')
 450         args = args_re.match(date)
 451         if not args:
 452             return ('<p><strong class="error">%s</strong></p>' %('Invalid date "%s"!')) % (macro.form['date'].value)
 453         year  = int(args.group('year'))
 454         month = int(args.group('month'))
 455         day   = int(args.group('day'))
 456     else:
 457         datetofind = time.time()
 458         (year,month,day,h,m,s,wd,yd,ds) = time.localtime(datetofind)
 459 
 460     ret  = getStyle()
 461     ret += getJavascript(thisPage, "%d-%02d-%02d" % (year, month, day), entries, showAll, startDay)
 462     ret += getCalendar()
 463 
 464     #get the entries to display
 465     if (showAll == '1'):
 466         ret += getShowAll(macro, thisPage, year, month, day, entries, maxEntries)
 467     else:
 468         ret += getShowEntered(macro, thisPage, year, month, day, entries, maxEntries)
 469 
 470     return ret
 471 
 472 def getShowAll(macro, thisPage,  year, month, day, entries, maxEntries):
 473     ret = "<input class='calendarButton' type=button value='Show Published' onclick='global.showAll=0;updateBlog();'>"
 474 
 475     ret += " max entries: <select class='calendarButton' onchange='if (this.value&&(this.value != \"\")) {global.entries=this.value; updateBlog();}'>"
 476     ret += "<option value=''>--Entries--</option>"
 477     for i in range(maxEntries):
 478         if (i==entries-1):
 479             ret += "<option selected value='"+str(i+1)+"'>"+str(i+1)+"</value>"
 480         else:
 481             ret += "<option value='"+str(i+1)+"'>"+str(i+1)+"</value>"
 482     ret += "</select>"
 483 
 484     for i in range(entries):
 485         includeParams = '%s/BlogEntry-%d-%02d-%02d, "%d-%02d-%02d", 1' % (thisPage, year, month, day, year, month, day)
 486         ret += MoinMoin.macro.Include.execute(macro, includeParams)
 487         #date = strftime("%Y-%m-$d", (year,month,day, 0, 0, 0, 0, 0, 0)
 488         day = day - 1
 489         date = time.mktime((year,month,day, 0, 0, 0, 0, 0, 0))
 490         (year,month,day,h,m,s,wd,yd,ds) = time.localtime(date)
 491     return ret
 492 
 493 def getShowEntered(macro, thisPage, year, month, day, entries, maxEntries):
 494     ret = "<input class='calendarButton' type=button value='Show All' onclick='global.showAll=1;updateBlog();'>"
 495     ret += " max entries: <select class='calendarButton' onchange='if (this.value&&(this.value != \"\")) {global.entries=this.value; updateBlog();}'>"
 496     ret += "<option value=''>--Entries--</option>"
 497     for i in range(maxEntries):
 498         if (i==entries-1):
 499             ret += "<option selected value='"+str(i+1)+"'>"+str(i+1)+"</value>"
 500         else:
 501             ret += "<option value='"+str(i+1)+"'>"+str(i+1)+"</value>"
 502     ret += "</select>"
 503     pages = wikiutil.getPageList(config.text_dir)
 504     displayPages = []
 505     for page_name in pages:
 506         if re.match('^' + thisPage + '/', page_name):
 507             displayPages.append(page_name)
 508 
 509     displayPages.sort()
 510     displayPages.reverse()
 511     selectedDate = int( "%d%02d%02d" % (year, month, day))
 512     pages = 0
 513     for pageName in displayPages:
 514         pageDate = int(pageName[-10:-6] + pageName[-5:-3] + pageName[-2:])
 515         if (pageDate <= selectedDate):
 516             pages = pages + 1
 517             includeParams = pageName + ', "%s-%s-%s", 1' % (pageName[-10:-6], pageName[-5:-3], pageName[-2:])
 518             ret += MoinMoin.macro.Include.execute(macro, includeParams)
 519             if (pages  >= entries):
 520                 break
 521     return ret

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2003-12-07 18:15:54, 6.8 KB) [[attachment:AttachmentLink.py]]
  • [get | view] (2003-12-07 18:15:54, 16.7 KB) [[attachment:Blog.py]]
  • [get | view] (2003-12-07 18:15:54, 2.8 KB) [[attachment:IncludeCalendarPage.py]]
  • [get | view] (2003-12-07 18:15:54, 3.0 KB) [[attachment:IncludePages.py]]
  • [get | view] (2005-03-14 22:41:00, 2.7 KB) [[attachment:LinkedFrom-1.3.py]]
  • [get | view] (2003-12-07 18:15:54, 2.9 KB) [[attachment:LinkedFrom.py]]
  • [get | view] (2003-12-07 18:15:54, 3.2 KB) [[attachment:PageLinks.py]]
  • [get | view] (2003-12-07 18:15:54, 3.1 KB) [[attachment:autosub.py]]
  • [get | view] (2003-12-07 18:15:54, 2.3 KB) [[attachment:cc-20030808.tgz]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.