| 1 | """ |
|---|
| 2 | Blog macro 0.1-alpha |
|---|
| 3 | Usage sample 1: [[Blog(blog/%)]] |
|---|
| 4 | Usage sample 2: [[Blog(2004%,blog/%)]] |
|---|
| 5 | |
|---|
| 6 | """ |
|---|
| 7 | |
|---|
| 8 | from StringIO import StringIO |
|---|
| 9 | import re,time |
|---|
| 10 | import string |
|---|
| 11 | #from trac.WikiFormatter import * |
|---|
| 12 | from trac.wiki import wiki_to_html |
|---|
| 13 | |
|---|
| 14 | month_abbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
|---|
| 15 | |
|---|
| 16 | def execute(hdf, args, env): |
|---|
| 17 | if hdf.getValue('macro.blog.rendering', '0') == '1': |
|---|
| 18 | return "" |
|---|
| 19 | hdf.setValue('macro.blog.rendering', '1') |
|---|
| 20 | |
|---|
| 21 | pagename = hdf.getValue("args.page", "WikiStart") |
|---|
| 22 | |
|---|
| 23 | # we can accept two optional parameters: year and month, if not provided, will use today's |
|---|
| 24 | timeToday = time.localtime() |
|---|
| 25 | year = int(hdf.getValue("args.year", str(timeToday.tm_year))) |
|---|
| 26 | month = int(hdf.getValue("args.month", str(timeToday.tm_mon))) |
|---|
| 27 | |
|---|
| 28 | db = env.get_db_cnx() |
|---|
| 29 | #formatter = Formatter(hdf, env, db, 0) |
|---|
| 30 | |
|---|
| 31 | years = {} |
|---|
| 32 | # years = { |
|---|
| 33 | # 2004: { |
|---|
| 34 | # 1: 1, |
|---|
| 35 | # 2: 3, |
|---|
| 36 | # }, |
|---|
| 37 | # 2005: { |
|---|
| 38 | # 2: 4, |
|---|
| 39 | # 3: 3, |
|---|
| 40 | # }, |
|---|
| 41 | # } |
|---|
| 42 | |
|---|
| 43 | out1 = StringIO() # the TOC |
|---|
| 44 | #~ out1.write('<p>Blog of %d/%d</p>' % (year, month)) |
|---|
| 45 | out1.write('<a name="toc"></a><ul>') |
|---|
| 46 | |
|---|
| 47 | out2 = StringIO() # the body |
|---|
| 48 | bIndex = 0 |
|---|
| 49 | |
|---|
| 50 | for blogPattern in args.split(','): |
|---|
| 51 | blogPattern = blogPattern.strip() |
|---|
| 52 | sql = "SELECT DISTINCT name from wiki where name like '%s' order by time desc" % blogPattern |
|---|
| 53 | csName = db.cursor() # the matched wiki name |
|---|
| 54 | csName.execute(sql) |
|---|
| 55 | while 1: |
|---|
| 56 | rowName = csName.fetchone() |
|---|
| 57 | if rowName == None: |
|---|
| 58 | break |
|---|
| 59 | |
|---|
| 60 | name = rowName[0] |
|---|
| 61 | |
|---|
| 62 | sql = "SELECT text,time,author from wiki where name = '%s' order by version desc limit 1" % name |
|---|
| 63 | csText = db.cursor() # the wiki text and time and author |
|---|
| 64 | csText.execute(sql) |
|---|
| 65 | while 1: |
|---|
| 66 | rowText = csText.fetchone() |
|---|
| 67 | if rowText == None: |
|---|
| 68 | break |
|---|
| 69 | |
|---|
| 70 | timeParts = time.localtime(rowText[1]) |
|---|
| 71 | if not timeParts.tm_year in years.keys(): |
|---|
| 72 | years[timeParts.tm_year] = {timeParts.tm_mon: 1} |
|---|
| 73 | else: |
|---|
| 74 | if not timeParts.tm_mon in years[timeParts.tm_year].keys(): |
|---|
| 75 | years[timeParts.tm_year][timeParts.tm_mon] = 1 |
|---|
| 76 | else: |
|---|
| 77 | years[timeParts.tm_year][timeParts.tm_mon] += 1 |
|---|
| 78 | |
|---|
| 79 | if timeParts.tm_year != year or timeParts.tm_mon != month: |
|---|
| 80 | break |
|---|
| 81 | |
|---|
| 82 | text = rowText[0] |
|---|
| 83 | timeStr = time.ctime(rowText[1]) |
|---|
| 84 | author = rowText[2] |
|---|
| 85 | out3 = StringIO() # the rendered output of current wiki |
|---|
| 86 | #~ env.log.debug('rendering %s' % pagename) |
|---|
| 87 | #formatter.format(text, out3) |
|---|
| 88 | out3.write(wiki_to_html(text,env,hdf,db,0)) |
|---|
| 89 | text = out3.getvalue() |
|---|
| 90 | out3 = None |
|---|
| 91 | |
|---|
| 92 | # get the wiki title from its output |
|---|
| 93 | matchedTitle = re.search(r"<(h\d+).*?>(.*?)</\1>", text) |
|---|
| 94 | title = name |
|---|
| 95 | if matchedTitle != None: |
|---|
| 96 | title = string.strip(matchedTitle.group(2)) |
|---|
| 97 | |
|---|
| 98 | bIndex+=1 |
|---|
| 99 | out1.write('<li><a name="t%d"></a><a href="#b%d">%s</a></li>' % (bIndex, bIndex, title)) |
|---|
| 100 | |
|---|
| 101 | out2.write('<a name="b%d"></a>' % bIndex) |
|---|
| 102 | out2.write('<div class="nav"><ul>') |
|---|
| 103 | out2.write('<li><a href="%s">%s</a></li>' % (env.href.wiki(name), title)) |
|---|
| 104 | out2.write('<li>%s</li>' % author) |
|---|
| 105 | out2.write('<li>%s</li>' % timeStr) |
|---|
| 106 | out2.write('<li><a href="#t%d">top</a></li>' % bIndex) |
|---|
| 107 | out2.write('</ul></div>') |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | out2.write(text) |
|---|
| 111 | out1.write('</ul>') |
|---|
| 112 | |
|---|
| 113 | out = StringIO() |
|---|
| 114 | #~ out.write('<p>Blog by Date:</p>') |
|---|
| 115 | out.write('<ul>') |
|---|
| 116 | ykeys = years.keys() |
|---|
| 117 | ykeys.sort() |
|---|
| 118 | ykeys.reverse() |
|---|
| 119 | for y in ykeys: |
|---|
| 120 | out.write('<li>%d:' % y) |
|---|
| 121 | for m in years[y].keys(): |
|---|
| 122 | if y == year and m == month: |
|---|
| 123 | out.write(' %s(%d)' % (month_abbr[m-1], years[y][m])) |
|---|
| 124 | else: |
|---|
| 125 | shref = '<a href="%s?year=%d&month=%d">' % (env.href.wiki(pagename), y, m) |
|---|
| 126 | out.write(' %s%s</a>(%s%d</a>)' % (shref, month_abbr[m-1], shref, years[y][m])) |
|---|
| 127 | out.write('</li>') |
|---|
| 128 | out.write('</ul>') |
|---|
| 129 | out.write(out1.getvalue()) |
|---|
| 130 | out.write(out2.getvalue()) |
|---|
| 131 | return out.getvalue() |
|---|