Edgewall Software

MacroBazaar: Blog.py

File Blog.py, 4.6 KB (added by wangzq@…, 7 years ago)

macro "Blog"

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