/[theodore]/bunnyblog/blog/blog.py


UCC Code Repository

Contents of /bunnyblog/blog/blog.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (show annotations) (download) (as text)
Fri Feb 22 07:16:31 2008 UTC (12 years, 1 month ago) by theodore
File MIME type: text/x-python
File size: 11733 byte(s)
Lots o' edits


1 import re
2 import time
3 from os import sys
4
5 """
6 Import modules from modules directory
7 """
8 sys.path.insert(0,'./modules')
9 import yaml
10 from logintools import isloggedin
11 del sys.path[0]
12
13 class Blog:
14 def __init__(self, loadblog = False):
15 """
16 Setting loadblog to true will load all of the articles in the blog.
17 Setting it to false will not saving time when accessing the files isn't neccessary.
18 """
19 self.config = yaml.load(load('data/config.yaml'))
20 if loadblog:
21 self.articles = yaml.load(load('data/blog.yaml'))
22 if not self.articles:
23 self.articles = []
24 self.stylename = self.config['template']
25 def addarticle(self, title, content, index):
26 newarticle = Article(title, content, index)
27 self.articles.append(newarticle)
28 return self.saveblog()
29 def delarticle(self, index):
30 article = self.findbyindex(index)
31 if article:
32 self.articles.remove(article)
33 return self.saveblog() and article.title
34 else:
35 return False
36 def findbyindex(self, index):
37 for article in self.articles:
38 if str(article.index) == str(index):
39 return article
40 return False
41 def findbytitle(self, title):
42 for article in self.articles:
43 if article.title == title:
44 return article
45 return False
46 def saveblog(self):
47 self.generaterss()
48 return save(yaml.dump(self.articles), 'data/blog.yaml')
49 def display(self,title,header,main,sidebar, meta=""):
50 style = "themes/"+self.stylename+"/"
51 page = gettemplate(style+"index", 'data')
52 page = page.replace("<@meta-data@>" , meta)
53 page = page.replace("<@style_dir@>" , 'data/'+style)
54 page = page.replace("<@page_title@>" , title)
55 page = page.replace("<@header@>" , header)
56 page = page.replace("<@main@>" , main)
57 page = page.replace("<@menu@>" , gettemplate("mainmenu","data"))
58 page = page.replace("<@menu_noul@>" , gettemplate("mainmenu","data").strip()[4:-5])
59 page = page.replace("<@sidebar@>" , sidebar)
60 page = page.replace("<@sidebar_noul@>" , sidebar.strip()[4:-5])
61 page = page.replace("<@footer@>" , "<@footer@><br />Powered by <a href='http://theodore.ucc.asn.au/blog/'>Bunnyblog</a>")
62 page = fixinternallinks(page)
63 for (key,value) in self.config.iteritems():
64 page = page.replace("<@"+key+"@>", value)
65 return page
66 def saveconfig(self):
67 save(yaml.dump(self.config),'data/config.yaml')
68 def generaterss(self):
69 rss = """Content-Type: text/xml
70
71 <?xml version="1.0" ?>
72 <rss version="2.0">
73
74 <channel>
75 """
76 rss += "<title>%s RSS Feed</title>" % self.config['site_name']
77 rss += "<description>%s</description>" % self.config['site_blurb']
78 rss += "<link>%s</link>" % self.config['site_url']
79
80 if len(self.articles) > 10:
81 articles = self.articles[-10:]
82 else:
83 articles = self.articles
84 for article in articles[::-1]:
85 if len(article.content) > 500:
86 content = article.content[:400] + " ... " + article.content[-100:]
87 else:
88 content = article.content
89 content = "%s\n<a href=\"%sindex.cgi?article=%d\">Read the full article and comments here</a>" % (tagify(content),self.config['site_url'], article.index)
90 rss += """
91 <item>
92 <title>%s</title>
93 <description>%s</description>
94 <link>%sindex.cgi?article=%d</link>
95 <pubDate>%s</pubDate>
96 <lastBuildDate>%s</lastBuildDate>
97 </item>
98 """ % (article.title, content, self.config['site_url'], article.index, article.date, time.asctime())
99
100 rss += """
101 </channel>
102
103 </rss>
104 """
105
106 save(rss,"blog.rss")
107
108 class Article(yaml.YAMLObject):
109 def __init__(self, title, content, index, comments = []):
110 self.comments = comments
111 self.content = cleanse(content)
112 self.date = time.asctime()
113 self.title = cleanse(title)
114 self.index = index
115
116 def __rep__(self):
117 return self.htmlize()
118
119 def htmlize(self):
120 numcomments = len(self.comments)
121 html = "<div class = 'article'>\n"
122 html += "<a href='index.cgi?article=%s' class='articletitle'>%s</a> (%s Comment%s)" % (self.index,self.title, numcomments,(numcomments - 1 != 0 and 's' or ''))
123 if isadmin():
124 html += " - <a href='<@site_url@>admin.cgi?action=deletearticle&amp;index=%s'>Delete this article</a>" % (self.index)
125 html += "<p class='date'>Posted on: %s</p>" % (self.date)
126 html += tagify(self.content)
127 html += "</div>\n"
128 return html
129
130 def showcomments(self):
131 deletecomment = ""
132 html = "<div class='commentarea'><h3>Comments:</h3>"
133 if isadmin():
134 deletecomment = " - <a href='<@site_url@>admin.cgi?deletecomment=%s&amp;articleindex=%s'>Delete this comment</a>" % ('<@commentindex@>',self.index)
135 if self.comments:
136 i = 0
137 for [author,comment, time] in self.comments:
138 comment = "<span class='commentauthor'>%s</span> had this to say: %s<br /><blockquote class='comment'>%s</blockquote><p class='date'>%s</p>" % (author,deletecomment, tagify(comment),time)
139 comment = comment.replace('<@commentindex@>',str(i))
140 html += comment
141 i += 1
142 else:
143 html += "No Comments"
144 html += "</div>"
145 html += gettemplate('blog/addcomment').replace('<@article_index@>',str(self.index))
146 return html
147
148 def addcomment(self,author,comment):
149 if author != "" and comment != "":
150 self.comments.append([cleanse(author), cleanse(comment),time.asctime()])
151
152 def cleanse(str):
153 str = str.replace('&', '&amp;')
154 str = str.replace('<', '&lt;')
155 str = str.replace('>', '&gt;')
156 return str
157
158 def tagify(str, preserve_newlines = True):
159 if preserve_newlines:
160 str = str.replace('\n', '<br />')
161 """
162 Find all codeblocks, and replace them with a marker,
163 """
164 tags = re.compile(r"\[code\](.*?)\[/code\]")
165 codeblocks = tags.findall(str)
166 str = tags.sub("<@CODEBLOCK@>", str)
167
168 """ Simple url tags"""
169
170 tags = re.compile(r"\[url\](http://)*(.*?)\[/url\]")
171 tags.findall(str)
172 str = tags.sub('<a href="http://\g<2>"><span>\g<2></span></a>', str)
173
174 """Complex url tags"""
175 tags = re.compile(r"\[url=['|\"](.*?)['|\"]\](.*?)\[/url\]")
176 tags.findall(str)
177 str = tags.sub('<a href="\g<1>"><span>\g<2></span></a>', str)
178
179 """Internal Links"""
180 tags = re.compile(r"\[blog=(page|title|index):(.*?)\](.*?)\[/blog\]")
181 tags.findall(str)
182 str = tags.sub('<a href = "<@site_url@>index.cgi?\g<1>=\g<2>"><span>\g<3></span></a>', str)
183
184 """Rss feed"""
185 num_rss_tags = min(str.count('[rss]'),str.count('[/rss]'))
186 str = str.replace("[rss]", '<a href = "<@site_url@>blog.rss"><span><img src="templates/blog/rss.png" class="rss_link" alt="" /> ', num_rss_tags)
187 str = str.replace("[/rss]", '</span></a>', num_rss_tags)
188 str = str.replace("[rss]", '\n<!-- Unmatched [rss] tag -->\n')
189 str = str.replace("[/rss]", '\n<!-- Unmatched [/rss] tag -->\n')
190 str = str.replace("[rss/]", '<a href = "<@site_url@>blog.rss"><span><img src="templates/blog/rss.png" class="rss_link" alt="" /> Link to <@site_name@> RSS Feed</span></a>')
191
192 """ Image Tags """
193 str = str.replace('[img]','<img src="')
194 str = str.replace('[/img]','" alt="" />')
195
196 """ Internal Image Tags """
197 # This links to an image in viewer mode
198 tags = re.compile(r"\[blog=image:(.*?)\](.*?)\[/blog\]")
199 str = tags.sub('<a href = "<@site_url@>gallery.cgi?image=\g<1>"><span>\g<2></span></a>', str)
200
201 # These insert images from the gallery into normal blog posts.
202 str = str.replace('[bimg]', '<img src="<@site_url@>data/images/')
203 str = str.replace('[bimg=preview]', '<img src="<@site_url@>data/images/thumbs/')
204 str = str.replace('[/bimg]', '" alt = "" >')
205
206
207 tags = re.compile(r"\[(/*[biu])\]")
208 tags.findall(str)
209 str = tags.sub("<\g<1>>", str)
210
211 for code in codeblocks:
212 code = '<div class="blog_code_div"><pre class="blog_code" >Code:<hr/>%s<br /></pre></div><br />' % code
213 str = str.replace("<@CODEBLOCK@>", code, 1)
214
215 if preserve_newlines:
216 return "<p>%s</p>" % str
217 else:
218 return str
219
220 def save(db, filetoload="data/blog.yaml"):
221 try:
222 file = open(filetoload, 'w')
223 file.write(db)
224 file.truncate()
225 file.close()
226 return True
227 except:
228 return zFalse
229
230 def load(filetoload="data/blog.yaml"):
231 file = open(filetoload)
232 str = file.read()
233 return str
234
235 def gettemplate(template="index", directory = "templates", tags=False):
236 try:
237 page = load(directory + "/" + template + ".pyhtml")
238 except:
239 return "Template Not Found - " + directory + "/" + template
240 if tags:
241 page = cleanse(page)
242 page = tagify(page)
243 return page
244
245 def error(errormessage):
246 return "<h1 style='color:red;'>" + errormessage + "</h1>"
247
248 def isadmin():
249 if isloggedin('data/users/'):
250 return True
251 else:
252 return False
253
254 def savenewpage(filename, content, pagetitle, filetype):
255 content = "--!pageconfig!--"+yaml.dump({'pagetitle':pagetitle,'filetype':filetype,'lastedit':time.asctime()}) + "\n--!pagecontent!--\n" + content
256 return save(content, "data/cms/%s.pyhtml" % filename)
257
258 def getcmspage(template="index"):
259 cmspage = loadcmspage(template)
260 try:
261 (page, pagetitle, filetype, lastedited) = cmspage
262 except:
263 return (error(cmspage), 'Page not found')
264 if filetype == "bbcode":
265 page = cleanse(page)
266 page = tagify(page)
267 if isadmin():
268 editthispage = "Admin options for this page | <a href='<@site_url@>admin.cgi?delete=delete&amp;filename=%s'>Delete this page</a> | <a href='<@site_url@>admin.cgi?page=editcontent&filename=%s'>Edit this page</a>" % (template, template)
269 else:
270 editthispage = ""
271 page += "<br />%s<tt style='float:right;text-size:80%%;'>Last edited on %s</tt>" % (editthispage,lastedited)
272 return (page, pagetitle)
273
274 def loadcmspage(template="index"):
275 filetype = "html"
276 lastedited = "Unknown"
277 try:
278 page = load('data/cms/'+ template + '.pyhtml')
279 except:
280 return "404 Page Not Found - " + template
281 if page.startswith('--!pageconfig!--'):
282 (pageconfig,page) = page.split('--!pagecontent!--')
283 pageconfig = pageconfig.replace("--!pageconfig!--","")
284 pageconfig = yaml.load(pageconfig)
285
286 filetype = pageconfig['filetype']
287 lastedited = pageconfig['lastedit']
288 pagetitle = pageconfig['pagetitle']
289 else:
290 pagetitle = template.replace('_',' ').title()
291 return (page, pagetitle, filetype, lastedited)
292
293 def fixinternallinks(page):
294 tags = re.compile(r"<@(page|title|index):(.*?)@>")
295 links = tags.findall(page)
296 page = tags.sub("<@site_url@>index.cgi?\g<1>=\g<2>", page)
297 return page
298
299 def savemenu(menu, file = "mainmenu.pyhtml"):
300 htmlmenu = ""
301 for line in menu.splitlines():
302 htmlmenu += line + '\n'
303 htmlmenu = htmlmenu.replace('\n\n','\n')
304 htmlmenu = htmlmenu.replace('\n', '</li>\n<li>')
305 htmlmenu = tagify(htmlmenu, False)
306 htmlmenu = htmlmenu[0:-4]
307
308 htmlmenu = "<ul>\n<li>%s</ul>" % htmlmenu
309 return save(htmlmenu, 'data/' + file)

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26