summaryrefslogtreecommitdiff
blob: 4b583baaf6379f848dc4cfbe1d42b9a942dc70d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
""" 

Displays a splitted page with different categories. I use this to
allow having a german and an english blog at the same time.

"""
__author__ = "Gunnar Wrobel <wrobel@gentoo.org>"
__version__ = "$$"
__url__ = "http://www.gunnarwrobel.de"
__description__ = "Shows several categories at the same time."
__license__ = "GPL 2"

# Pyblosxom imports
from Pyblosxom.renderers.blosxom import Renderer, NoSuchFlavourException
from Pyblosxom import tools
from Pyblosxom.entries.fileentry import FileEntry

# Variables
TRIGGER = "/blocks"
TRIGGER_KEY = "block_urltrigger"
INIT_KEY = "block_initiated"

_default_template = """
<div>
</div>
"""

def _update_config(config):
    if TRIGGER_KEY in config:
        global TRIGGER
        TRIGGER = config[TRIGGER_KEY]
    else:
        # set this explicitly so it's available in templates
        config[TRIGGER_KEY] = TRIGGER

class BlocksRenderer(Renderer):

    def _processBlockContent(self):
        """
        Processes the content for the story portion of a page.

        @returns: the content string
        @rtype: string
        """
        config = self._request.getConfiguration()
        data = self._request.getData()

        if isinstance(self._content, list):
            current_date = ''

            for block in self._content:
		if self.flavour.has_key('block_head'):
		    self._outputFlavour(parsevars,'block_head')
		outputbuffer = []
		for entry in block:
		    output, current_date = self._processEntry(entry, current_date)
		    outputbuffer.append(output)
		self.write(u"".join(outputbuffer))
		if self.flavour.has_key('block_foot'):
		    self._outputFlavour(parsevars,'block_foot')
    
    def render(self, header=1):
        """
        Figures out flavours and such and then renders the content according
        to which flavour we're using.

        @param header: whether (1) or not (0) to render the HTTP headers
        @type  header: boolean
        """
        # if we've already rendered, then we don't want to do so again
        if self.rendered == 1:
            return

        data = self._request.getData()
        config = self._request.getConfiguration()

        # FIXME
        parsevars = tools.VariableDict()
        parsevars.update(config)
        parsevars.update(data)

        try:
            self.flavour = self._getFlavour(data.get("flavour", "html"))

        except NoSuchFlavourException, nsfe:
            error_msg = nsfe._msg
            try:
                self.flavour = self._getFlavour("error")
            except NoSuchFlavourException, nsfe2:
                self.flavour = get_included_flavour("error")
                error_msg = error_msg + "And your error flavour doesn't exist."

            self._content = { "title": "Flavour error", 
                              "body": error_msg }
        
        data['content-type'] = self.flavour['content_type'].strip()
        if header:
            if self._needs_content_type and data['content-type'] !="":
                self.addHeader('Content-type', '%(content-type)s' % data)

            self.showHeaders()
        
        if self._content:
            if self.flavour.has_key('head'):
                self._outputFlavour(parsevars,'head')
	    self._processBlockContent()
            if self.flavour.has_key('date_foot'): 
                self._outputFlavour(parsevars,'date_foot')                
            if self.flavour.has_key('foot'): 
                self._outputFlavour(parsevars,'foot')                
        
        self.rendered = 1


#******************************
# Callbacks
#******************************

def cb_start(args):
    request = args['request']
    http = request.getHttp()
    config = request.getConfiguration()
    _update_config(config)

    if http['PATH_INFO'].startswith(TRIGGER):
        data = request.getData()
        data[INIT_KEY] = True

def cb_renderer(args):
    request = args['request']
    data = request.getData()

    if INIT_KEY in data:
        return BlocksRenderer(request, request.getResponse())

def cb_end(args):
    request = args['request']
    data = request.getData()
    if INIT_KEY in data:
        del data[INIT_KEY]


class Blocklist(list):

    def get(self, key, default):
	if key == 'mtime':
	    result = 0
	    for i in self:
		j = i.get('mtime', 0)
		if j > result:
		    result = j
	    if not result:
		return default
	    else:
		return result
	return default

def cb_filelist(args):
    """
    """
    request = args["request"]

    data = request.getData()
    config = request.getConfiguration()

    if not INIT_KEY in data:
	return

    blocklist = []
    for category in config['block_categories']:
        filelist = tools.Walk(request, config['datadir'] + '/' + category, int(config['depth']))

	entrylist = []
	for ourfile in filelist:
	    e = FileEntry(request, ourfile, data['root_datadir'])
	    entrylist.append((e._mtime, e))

	# this sorts entries by mtime in reverse order.  entries that have
	# no mtime get sorted to the top.
	entrylist.sort()
	entrylist.reverse()
    
	# Match dates with files if applicable
	if data['pi_yr']:
	    # This is called when a date has been requested, e.g. /some/category/2004/Sep
	    month = (data['pi_mo'] in tools.month2num.keys() and tools.month2num[data['pi_mo']] or data['pi_mo'])
	    matchstr = "^" + data["pi_yr"] + month + data["pi_da"]
	    valid_list = [x for x in entrylist if re.match(matchstr, x[1]._fulltime)]
	else:
	    valid_list = entrylist

	# This is the maximum number of entries we can show on the front page
	# (zero indicates show all entries)
	max = config.get("block_entries", 0)
	if max and not data["pi_yr"]:
	    valid_list = valid_list[:max]
	    data["debugme"] = "done"

	valid_list = [x[1] for x in valid_list]
	
	block = Blocklist()
	for i in valid_list:
	    block.append(i)
	blocklist.append(block)

    return blocklist