1 # Copyright (c) 2008, 2009, Simon Morgan <sjm@spamcop.net>
3 # Permission to use, copy, modify, and/or distribute this software for any
4 # purpose with or without fee is hereby granted, provided that the above
5 # copyright notice and this permission notice appear in all copies.
7 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 conn
= sqlite3
.connect(config
.DBPATH
)
26 conn
.execute("CREATE TABLE IF NOT EXISTS entries (id INTEGER \
27 PRIMARY KEY, date INTEGER, title TEXT, text TEXT)")
28 except sqlite3
.OperationalError
:
29 print_error("Failed to connect to database. Check file and \
30 directory permissions.")
34 def getnumposts(conn
, postid
=None):
35 """Enumerate the number of posts in the database. If an ID is
36 specified then enumerate the number of posts with that ID. The
37 result of the latter should be 0 or 1 as essentially this is a check
38 for whether the specified post exists.
41 numposts
= conn
.execute("SELECT count(id) FROM entries WHERE id = ?",
44 numposts
= conn
.execute("SELECT count(id) FROM entries").fetchone()
45 return int(numposts
[0])
47 def print_headers(title
):
49 print '<title>' + title
+ '</title>'
50 print '<link href="default.css" rel="stylesheet" type="text/css">'
53 def print_class(msg
, class_
):
54 print '<div class="%s">' % class_
55 print '<p>%s</p>' % msg
58 def print_id(msg
, id_
):
59 print '<div id="%s">' % id_
60 print '<p>%s</p>' % msg
64 print_class(msg
, "error")
67 print_class(msg
, "message")
69 def print_post(title
, body
, date
=None):
70 """Formats and prints a post"""
71 print '<div class="blogpost">'
72 print '<h1>%s</h1>' % title
74 print '<h3>%s</h3>' % time
.ctime(date
)
75 print markdown2
.markdown(body
)
79 print_id(config
.HEADER
, "header")
82 print_id(config
.FOOTER
, "footer")