Edgewall Software

MacroBazaar: Blog.ByCreationDate.py

File Blog.ByCreationDate.py, 4.7 KB (added by trac@…, 6 years ago)

Adaptation of Blog.py macro to sort blog entries by page creation date instead of last edit date.

Line 
1"""
2Blog macro 0.2-alpha [[BR]]
3Usage sample 1: {{{[[Blog(blog/%)]]}}} [[BR]]
4Usage sample 2: {{{[[Blog(2004%,blog/%)]]}}} [[BR]]
5
6"""
7
8from StringIO import StringIO
9import re,time
10import string
11from trac.wiki import wiki_to_html
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( 'wiki.page_name', '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
29    years = {}
30    # years = {
31    #    2004: {
32    #       1: 1,
33    #       2: 3,
34    #    },
35    #    2005: {
36    #       2: 4,
37    #       3: 3,
38    #    },
39    # }
40
41    toc  = []  # the TOC
42    body = []  # the body
43    bIndex = 0
44
45    toc.append( '</ul>' )  # remember: TOC and body are reversed before joining
46
47    for blogPattern in args.split( ',' ):
48        blogPattern = blogPattern.strip()
49        sql = "SELECT DISTINCT name from wiki where name like '%s' order by time" % blogPattern
50        csName = db.cursor()   # the matched wiki name
51        csName.execute( sql )
52        while 1:
53            rowName = csName.fetchone()
54            if rowName == None:
55                break
56
57            name = rowName[0]
58
59            sql = "SELECT text,time,author from wiki where name = '%s' order by version desc limit 1" % name
60            csText = db.cursor()    # the wiki text and time and author
61            csText.execute( sql )
62            while 1:
63                rowText = csText.fetchone()
64                if rowText == None:
65                    break
66
67                timeParts = time.localtime(rowText[1])
68                if not timeParts.tm_year in years.keys():
69                    years[timeParts.tm_year] = {timeParts.tm_mon: 1}
70                else:
71                    if not timeParts.tm_mon in years[timeParts.tm_year].keys():
72                        years[timeParts.tm_year][timeParts.tm_mon] = 1
73                    else:
74                        years[timeParts.tm_year][timeParts.tm_mon] += 1
75                   
76                if timeParts.tm_year != year or timeParts.tm_mon != month:
77                    break
78
79                #~ env.log.debug( 'rendering %s' % pagename )
80                text    = wiki_to_html( rowText[0], env, None, db, 0 )
81                timeStr = time.ctime(rowText[1])
82                author  = rowText[2]
83
84                # get the wiki title from its output
85                matchedTitle = re.search( r"<(h\d+).*?>(.*?)</\1>", text )
86                title = name
87                if matchedTitle != None:
88                    title = string.strip( matchedTitle.group(2) )
89
90                bIndex+=1
91                toc.append( '<li><a name="t%d"></a><a href="#b%d">%s</a></li>' 
92                                % (bIndex, bIndex, title) )
93
94                body.append( text )
95                body.append( '<a name="b%d"></a>'
96                             '<div class="nav">'
97                               '<ul>'
98                                 '<li><a href="%s">%s</a></li>'
99                                 '<li>%s</li>'
100                                 '<li>%s</li>'
101                                 '<li><a href="#t%d">top</a></li>'
102                               '</ul>'
103                             '</div>' 
104                             % (bIndex, env.href.wiki(name), title, author, timeStr, bIndex) )
105
106    toc.append( '<a name="toc"></a><ul>' )
107    #~ toc.append( '<p>Blog of %d/%d</p>' % (year, month) )
108
109    # Reverse TOC and body to put the most recent items at the top
110    toc.reverse()
111    body.reverse()
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( '&nbsp;%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( '&nbsp;%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( ''.join(toc) )
130    toc = None
131    out.write( ''.join(body) )
132    body = None
133    return out.getvalue()
134