4 # blitiri - A single-file blog engine.
5 # Alberto Bertogli (albertito@gmail.com)
8 # Configuration section
10 # You can edit these values, or create a file named "config.py" and put them
11 # there to make updating easier. The ones in config.py take precedence.
14 # Directory where entries are stored
15 data_path = "/tmp/blog/data"
17 # Are comments allowed? (if False, comments_path option is not used)
18 enable_comments = False
20 # Directory where comments are stored (must be writeable by the web server)
21 comments_path = "/tmp/blog/comments"
23 # Path where templates are stored. Use an empty string for the built-in
24 # default templates. If they're not found, the built-in ones will be used.
25 templates_path = "/tmp/blog/templates"
27 # Path where the cache is stored (must be writeable by the web server);
28 # set to None to disable. When enabled, you must take care of cleaning it up
29 # every once in a while.
30 #cache_path = "/tmp/blog/cache"
33 # URL to the blog, including the name. Can be a full URL or just the path.
34 blog_url = "/blog/blitiri.cgi"
36 # Style sheet (CSS) URL. Can be relative or absolute. To use the built-in
37 # default, set it to blog_url + "/style".
38 css_url = blog_url + "/style"
41 title = "I don't like blogs"
44 author = "Hartmut Kegan"
50 class Captcha (object):
51 def __init__(self, article):
52 self.article = article
53 words = article.title.split()
54 self.nword = hash(article.title) % len(words) % 5
55 self.answer = words[self.nword]
56 self.help = 'gotcha, damn spam bot!'
59 nword = self.nword + 1
68 return "enter the %s word of the article's title" % n
69 puzzle = property(fget = get_puzzle)
71 def validate(self, form_data):
72 if form_data.captcha.lower() == self.answer.lower():
78 # End of configuration
79 # DO *NOT* EDIT ANYTHING PAST HERE
93 from docutils.core import publish_parts
94 from docutils.utils import SystemMessage
96 # Before importing the config, add our cwd to the Python path
97 sys.path.append(os.getcwd())
99 # Load the config file, if there is one
106 # Pimp *_path config variables to support relative paths
107 data_path = os.path.realpath(data_path)
108 templates_path = os.path.realpath(templates_path)
112 default_main_header = """\
113 <?xml version="1.0" encoding="utf-8"?>
114 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
115 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
117 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
119 <link rel="alternate" title="%(title)s" href="%(fullurl)s/atom"
120 type="application/atom+xml" />
121 <link href="%(css_url)s" rel="stylesheet" type="text/css" />
122 <title>%(title)s</title>
127 <h1><a href="%(url)s">%(title)s</a></h1>
129 <div class="content">
132 default_main_footer = """
135 %(showyear)s: %(monthlinks)s<br/>
136 years: %(yearlinks)s<br/>
137 subscribe: <a href="%(url)s/atom">atom</a><br/>
138 views: <a href="%(url)s/">blog</a> <a href="%(url)s/list">list</a><br/>
145 default_article_header = """
146 <div class="article">
147 <h2><a href="%(url)s/post/%(uuid)s">%(arttitle)s</a></h2>
148 <span class="artinfo">
149 by %(author)s on <span class="date">
151 <a class="date" href="%(url)s/%(cyear)d/">%(cyear)04d</a>-\
152 <a class="date" href="%(url)s/%(cyear)d/%(cmonth)d/">%(cmonth)02d</a>-\
153 <a class="date" href="%(url)s/%(cyear)d/%(cmonth)d/%(cday)d/">%(cday)02d</a>\
154 %(chour)02d:%(cminute)02d</span>
155 (updated on <span class="date">
156 <a class="date" href="%(url)s/%(uyear)d/">%(uyear)04d</a>-\
157 <a class="date" href="%(url)s/%(uyear)d/%(umonth)d/">%(umonth)02d</a>-\
158 <a class="date" href="%(url)s/%(uyear)d/%(umonth)d/%(uday)d/">%(uday)02d</a>\
159 %(uhour)02d:%(uminute)02d)</span><br/>
160 <span class="tags">tagged %(tags)s</span> -
161 <span class="comments">with %(comments)s
162 <a href="%(url)s/post/%(uuid)s#comments">comment(s)</a></span>
165 <div class="artbody">
168 default_article_footer = """
174 default_comment_header = """
175 <div class="comment">
176 <a name="comment-%(number)d" />
177 <h3><a href="#comment-%(number)d">Comment #%(number)d</a></h3>
178 <span class="cominfo">by %(linked_author)s
179 on %(year)04d-%(month)02d-%(day)02d %(hour)02d:%(minute)02d</span>
181 <div class="combody">
184 default_comment_footer = """
190 default_comment_form = """
191 <div class="comform">
193 <h3 class="comform"><a href="#comment">Your comment</a></h3>
194 <div class="comforminner">
195 <form method="%(form_method)s" action="%(form_action)s">
196 <div class="comformauthor">
197 <label for="comformauthor">Your name %(form_author_error)s</label>
198 <input type="text" class="comformauthor" id="comformauthor"
199 name="comformauthor" value="%(form_author)s" />
201 <div class="comformlink">
202 <label for="comformlink">Your link
203 <span class="comformoptional">(optional, will be published)</span>
204 %(form_link_error)s</label>
205 <input type="text" class="comformlink" id="comformlink"
206 name="comformlink" value="%(form_link)s" />
207 <div class="comformhelp">
208 like <span class="formurlexample">http://www.example.com/</span>
209 or <span class="formurlexample">mailto:you@example.com</span>
212 <div class="comformcaptcha">
213 <label for="comformcaptcha">Your humanity proof %(form_captcha_error)s</label>
214 <input type="text" class="comformcaptcha" id="comformcaptcha"
215 name="comformcaptcha" value="%(form_captcha)s" />
216 <div class="comformhelp">%(captcha_puzzle)s</div>
218 <div class="comformbody">
219 <label for="comformbody" class="comformbody">The comment
220 %(form_body_error)s</label>
221 <textarea class="comformbody" id="comformbody" name="comformbody" rows="15"
222 cols="80">%(form_body)s</textarea>
223 <div class="comformhelp">
225 <a href="http://docutils.sourceforge.net/docs/user/rst/quickref.html">\
226 RestructuredText</a> format, please
229 <div class="comformsend">
230 <button type="submit" class="comformsend" id="comformsend" name="comformsend">
239 default_comment_error = '<span class="comformerror">(%(error)s)</span>'
245 font-family: sans-serif;
256 border-bottom: 2px solid #99F;
265 border-bottom: 1px solid #99C;
272 border-bottom: 1px solid #99C;
276 text-decoration: none;
285 text-decoration: none;
289 span.artinfo a:hover {
290 text-decoration: none;
307 text-decoration: none;
311 span.cominfo a:hover {
312 text-decoration: none;
336 border-bottom: 1px solid #99C;
341 div.comform span.comformoptional {
357 span.formurlexample {
359 background-color: #EEF;
360 font-family: monospace;
362 padding-right: 0.2em;
365 textarea.comformbody {
366 font-family: monospace;
386 background-color: #99F;
394 border-top: 2px solid #99F;
399 text-decoration: none;
402 /* Articles are enclosed in <div class="section"> */
408 border-bottom: 1px dotted #99C;
415 def decorate(obj, *args, **kwargs):
416 if cache_path is None: # cache disabled
417 s = f(obj, *args, **kwargs)
419 cache_file = os.path.join(cache_path,
420 'blitiri.cache.%s.html' % hash(obj))
422 s = open(cache_file).read()
424 s = f(obj, *args, **kwargs)
425 open(cache_file, 'w').write(s)
430 def rst_to_html(rst, secure = True):
432 'input_encoding': encoding,
433 'output_encoding': 'utf8',
436 'file_insertion_enabled': secure,
437 'raw_enabled': secure,
439 parts = publish_parts(rst, settings_overrides = settings,
440 writer_name = "html")
441 return parts['body'].encode('utf8')
442 rst_to_html = cached(rst_to_html)
444 def validate_rst(rst, secure = True):
446 rst_to_html(rst, secure)
448 except SystemMessage, e:
449 desc = e.args[0].encode('utf-8') # the error string
450 desc = desc[9:] # remove "<string>:"
451 line = int(desc[:desc.find(':')] or 0) # get the line number
452 desc = desc[desc.find(')')+2:-1] # remove (LEVEL/N)
454 desc, context = desc.split('\n', 1)
457 if desc.endswith('.'):
459 return (line, desc, context)
461 def valid_link(link):
463 mail_re = r"^[^ \t\n\r@<>()]+@[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$"
464 scheme_re = r'^[a-zA-Z]+:'
465 url_re = r'^(?:[a-z0-9\-]+|[a-z0-9][a-z0-9\-\.\_]*\.[a-z]+)' \
466 r'(?::[0-9]+)?(?:/.*)?$'
469 if re.match(scheme_re, link, re.I):
470 scheme, rest = link.split(':', 1)
471 if (not scheme or scheme == 'mailto') and re.match(mail_re, rest, re.I):
472 return 'mailto:' + link
473 if not scheme and re.match(url_re, rest, re.I):
474 return 'http://' + rest
480 if isinstance(obj, basestring):
481 return cgi.escape(obj, True)
485 # find out our URL, needed for syndication
487 n = os.environ['SERVER_NAME']
488 p = os.environ['SERVER_PORT']
489 s = os.environ['SCRIPT_NAME']
492 full_url = 'http://%s%s%s' % (n, p, s)
494 full_url = 'Not needed'
497 class Templates (object):
498 def __init__(self, tpath, db, showyear = None):
501 now = datetime.datetime.now()
513 'showyear': showyear,
514 'monthlinks': ' '.join(db.get_month_links(showyear)),
515 'yearlinks': ' '.join(db.get_year_links()),
518 def get_template(self, page_name, default_template, extra_vars = None):
519 if extra_vars is None:
522 vars = self.vars.copy()
523 vars.update(extra_vars)
525 p = '%s/%s.html' % (self.tpath, page_name)
526 if os.path.isfile(p):
527 return open(p).read() % vars
528 return default_template % vars
530 def get_main_header(self):
531 return self.get_template('header', default_main_header)
533 def get_main_footer(self):
534 return self.get_template('footer', default_main_footer)
536 def get_article_header(self, article):
537 return self.get_template(
538 'art_header', default_article_header, article.to_vars())
540 def get_article_footer(self, article):
541 return self.get_template(
542 'art_footer', default_article_footer, article.to_vars())
544 def get_comment_header(self, comment):
545 vars = comment.to_vars()
547 vars['linked_author'] = '<a href="%s">%s</a>' \
548 % (comment.link, comment.author)
550 vars['linked_author'] = comment.author
551 return self.get_template(
552 'com_header', default_comment_header, vars)
554 def get_comment_footer(self, comment):
555 return self.get_template(
556 'com_footer', default_comment_footer, comment.to_vars())
558 def get_comment_form(self, article, form_data, captcha_puzzle):
559 vars = article.to_vars()
560 vars.update(form_data.to_vars(self))
561 vars['captcha_puzzle'] = captcha_puzzle
562 return self.get_template(
563 'com_form', default_comment_form, vars)
565 def get_comment_error(self, error):
566 return self.get_template(
567 'com_error', default_comment_error, dict(error=error))
570 class CommentFormData (object):
571 def __init__(self, author = '', link = '', captcha = '', body = ''):
574 self.captcha = captcha
576 self.author_error = ''
578 self.captcha_error = ''
583 def to_vars(self, template):
584 render_error = template.get_comment_error
585 a_error = self.author_error and render_error(self.author_error)
586 l_error = self.link_error and render_error(self.link_error)
587 c_error = self.captcha_error \
588 and render_error(self.captcha_error)
589 b_error = self.body_error and render_error(self.body_error)
591 'form_author': sanitize(self.author),
592 'form_link': sanitize(self.link),
593 'form_captcha': sanitize(self.captcha),
594 'form_body': sanitize(self.body),
596 'form_author_error': a_error,
597 'form_link_error': l_error,
598 'form_captcha_error': c_error,
599 'form_body_error': b_error,
601 'form_action': self.action,
602 'form_method': self.method,
606 class Comment (object):
607 def __init__(self, article, number, created = None):
608 self.article = article
611 self.created = datetime.datetime.now()
613 self.created = created
618 self._author = author
620 self._raw_content = 'Removed comment'
623 def get_author(self):
627 author = property(fget = get_author)
633 link = property(fget = get_link)
635 def get_raw_content(self):
638 return self._raw_content
639 raw_content = property(fget = get_raw_content)
642 def set(self, author, raw_content, link = '', created = None):
644 self._author = author
645 self._raw_content = raw_content
647 self.created = created or datetime.datetime.now()
651 filename = os.path.join(comments_path, self.article.uuid,
654 raw = open(filename).readlines()
661 name, value = l.split(':', 1)
662 if name.lower() == 'author':
663 self._author = value.strip()
664 elif name.lower() == 'link':
665 self._link = value.strip()
670 self._raw_content = ''.join(raw[count + 1:])
674 filename = os.path.join(comments_path, self.article.uuid,
677 f = open(filename, 'w')
678 f.write('Author: %s\n' % self.author)
679 f.write('Link: %s\n' % self.link)
681 f.write(self.raw_content)
687 return rst_to_html(self.raw_content)
691 'number': self.number,
692 'author': sanitize(self.author),
693 'link': sanitize(self.link),
694 'date': self.created.isoformat(' '),
695 'created': self.created.isoformat(' '),
697 'year': self.created.year,
698 'month': self.created.month,
699 'day': self.created.day,
700 'hour': self.created.hour,
701 'minute': self.created.minute,
702 'second': self.created.second,
705 class CommentDB (object):
706 def __init__(self, article):
707 self.path = os.path.join(comments_path, article.uuid)
711 def load(self, article):
713 f = open(os.path.join(self.path, 'db'))
718 # Each line has the following comma separated format:
719 # number, created (epoch)
720 # Empty lines are meaningful and represent removed
721 # comments (so we can preserve the comment number)
725 d = datetime.datetime.fromtimestamp(float(l[1]))
727 # Removed/invalid comment
728 self.comments.append(None)
730 self.comments.append(Comment(article, n, d))
733 old_db = os.path.join(self.path, 'db')
734 new_db = os.path.join(self.path, 'db.tmp')
735 f = open(new_db, 'w')
736 for c in self.comments:
740 s += str(c.number) + ', '
741 s += str(time.mktime(c.created.timetuple()))
745 os.rename(new_db, old_db)
748 class Article (object):
749 def __init__(self, path, created = None, updated = None):
751 self.created = created
752 self.updated = updated
753 self.uuid = "%08x" % zlib.crc32(self.path)
758 self._title = 'Removed post'
759 self._author = author
761 self._raw_content = ''
769 title = property(fget = get_title)
771 def get_author(self):
775 author = property(fget = get_author)
781 tags = property(fget = get_tags)
783 def get_raw_content(self):
786 return self._raw_content
787 raw_content = property(fget = get_raw_content)
789 def get_comments(self):
792 return self._comments
793 comments = property(fget = get_comments)
796 def __cmp__(self, other):
797 if self.path == other.path:
801 if not other.created:
803 if self.created < other.created:
807 def title_cmp(self, other):
808 return cmp(self.title, other.title)
811 def add_comment(self, author, raw_content, link = ''):
812 c = Comment(self, len(self.comments))
813 c.set(author, raw_content, link)
814 self.comments.append(c)
819 # XXX this tweak is only needed for old DB format, where
820 # article's paths started with a slash
822 if path.startswith('/'):
824 filename = os.path.join(data_path, path)
826 raw = open(filename).readlines()
833 name, value = l.split(':', 1)
834 if name.lower() == 'title':
835 self._title = value.strip()
836 elif name.lower() == 'author':
837 self._author = value.strip()
838 elif name.lower() == 'tags':
839 ts = value.split(',')
840 ts = [t.strip() for t in ts]
846 self._raw_content = ''.join(raw[count + 1:])
848 self._comments = db.comments
852 return rst_to_html(self.raw_content)
856 'arttitle': sanitize(self.title),
857 'author': sanitize(self.author),
858 'date': self.created.isoformat(' '),
860 'tags': self.get_tags_links(),
861 'comments': len(self.comments),
863 'created': self.created.isoformat(' '),
864 'ciso': self.created.isoformat(),
865 'cyear': self.created.year,
866 'cmonth': self.created.month,
867 'cday': self.created.day,
868 'chour': self.created.hour,
869 'cminute': self.created.minute,
870 'csecond': self.created.second,
872 'updated': self.updated.isoformat(' '),
873 'uiso': self.updated.isoformat(),
874 'uyear': self.updated.year,
875 'umonth': self.updated.month,
876 'uday': self.updated.day,
877 'uhour': self.updated.hour,
878 'uminute': self.updated.minute,
879 'usecond': self.updated.second,
882 def get_tags_links(self):
884 tags = list(self.tags)
887 l.append('<a class="tag" href="%s/tag/%s">%s</a>' % \
888 (blog_url, urllib.quote(t), sanitize(t) ))
892 class ArticleDB (object):
893 def __init__(self, dbpath):
897 self.actyears = set()
898 self.actmonths = set()
901 def get_articles(self, year = 0, month = 0, day = 0, tags = None):
903 for a in self.articles:
904 if year and a.created.year != year: continue
905 if month and a.created.month != month: continue
906 if day and a.created.day != day: continue
907 if tags and not tags.issubset(a.tags): continue
913 def get_article(self, uuid):
914 return self.uuids[uuid]
918 f = open(self.dbpath)
923 # Each line has the following comma separated format:
924 # path (relative to data_path), \
933 datetime.datetime.fromtimestamp(float(l[1])),
934 datetime.datetime.fromtimestamp(float(l[2])))
935 self.uuids[a.uuid] = a
936 self.actyears.add(a.created.year)
937 self.actmonths.add((a.created.year, a.created.month))
938 self.articles.append(a)
941 f = open(self.dbpath + '.tmp', 'w')
942 for a in self.articles:
945 s += str(time.mktime(a.created.timetuple())) + ', '
946 s += str(time.mktime(a.updated.timetuple())) + '\n'
949 os.rename(self.dbpath + '.tmp', self.dbpath)
951 def get_year_links(self):
952 yl = list(self.actyears)
953 yl.sort(reverse = True)
954 return [ '<a href="%s/%d/">%d</a>' % (blog_url, y, y)
957 def get_month_links(self, year):
958 am = [ i[1] for i in self.actmonths if i[0] == year ]
960 for i in range(1, 13):
961 name = calendar.month_name[i][:3]
963 s = '<a href="%s/%d/%d/">%s</a>' % \
964 ( blog_url, year, i, name )
974 def render_comments(article, template, form_data):
975 print '<a name="comments" />'
976 for c in article.comments:
979 print template.get_comment_header(c)
981 print template.get_comment_footer(c)
983 form_data = CommentFormData()
984 form_data.action = blog_url + '/comment/' + article.uuid + '#comment'
985 captcha = Captcha(article)
986 print template.get_comment_form(article, form_data, captcha.puzzle)
988 def render_html(articles, db, actyear = None, show_comments = False,
989 redirect = None, form_data = None):
991 print 'Status: 303 See Other\r\n',
992 print 'Location: %s\r\n' % redirect,
993 print 'Content-type: text/html; charset=utf-8\r\n',
995 template = Templates(templates_path, db, actyear)
996 print template.get_main_header()
998 print template.get_article_header(a)
1000 print template.get_article_footer(a)
1002 render_comments(a, template, form_data)
1003 print template.get_main_footer()
1005 def render_artlist(articles, db, actyear = None):
1006 template = Templates(templates_path, db, actyear)
1007 print 'Content-type: text/html; charset=utf-8\n'
1008 print template.get_main_header()
1009 print '<h2>Articles</h2>'
1011 print '<li><a href="%(url)s/uuid/%(uuid)s">%(title)s</a></li>' \
1012 % { 'url': blog_url,
1017 print template.get_main_footer()
1019 def render_atom(articles):
1020 if len(articles) > 0:
1021 updated = articles[0].updated.isoformat()
1023 updated = datetime.datetime.now().isoformat()
1025 print 'Content-type: application/atom+xml; charset=utf-8\n'
1026 print """<?xml version="1.0" encoding="utf-8"?>
1028 <feed xmlns="http://www.w3.org/2005/Atom">
1029 <title>%(title)s</title>
1030 <link rel="alternate" type="text/html" href="%(url)s"/>
1031 <link rel="self" type="application/atom+xml" href="%(url)s/atom"/>
1032 <id>%(url)s</id> <!-- TODO: find a better <id>, see RFC 4151 -->
1033 <updated>%(updated)sZ</updated>
1045 'contents': a.to_html(),
1049 <title>%(arttitle)s</title>
1050 <author><name>%(author)s</name></author>
1051 <link href="%(url)s/post/%(uuid)s" />
1052 <id>%(url)s/post/%(uuid)s</id>
1053 <summary>%(arttitle)s</summary>
1054 <published>%(ciso)sZ</published>
1055 <updated>%(uiso)sZ</updated>
1056 <content type="xhtml">
1057 <div xmlns="http://www.w3.org/1999/xhtml"><p>
1067 print 'Content-type: text/css\r\n\r\n',
1071 import cgitb; cgitb.enable()
1073 form = cgi.FieldStorage()
1074 year = int(form.getfirst("year", 0))
1075 month = int(form.getfirst("month", 0))
1076 day = int(form.getfirst("day", 0))
1077 tags = set(form.getlist("tag"))
1082 post_preview = False
1086 if os.environ.has_key('PATH_INFO'):
1087 path_info = os.environ['PATH_INFO']
1088 style = path_info == '/style'
1089 atom = path_info == '/atom'
1090 tag = path_info.startswith('/tag/')
1091 post = path_info.startswith('/post/')
1092 post_preview = path_info.startswith('/preview/post/')
1093 artlist = path_info.startswith('/list')
1094 comment = path_info.startswith('/comment/') and enable_comments
1095 if not style and not atom and not post and not post_preview \
1096 and not tag and not comment and not artlist:
1097 date = path_info.split('/')[1:]
1099 if len(date) > 1 and date[0]:
1101 if len(date) > 2 and date[1]:
1102 month = int(date[1])
1103 if len(date) > 3 and date[2]:
1108 uuid = path_info.replace('/post/', '')
1109 uuid = uuid.replace('/', '')
1111 art_path = path_info.replace('/preview/post/', '')
1112 art_path = urllib.unquote_plus(art_path)
1113 art_path = os.path.join(data_path, art_path)
1114 art_path = os.path.realpath(art_path)
1115 common = os.path.commonprefix([data_path, art_path])
1116 if common != data_path: # something nasty happened
1117 post_preview = False
1118 art_path = art_path[len(data_path)+1:]
1120 t = path_info.replace('/tag/', '')
1121 t = t.replace('/', '')
1122 t = urllib.unquote_plus(t)
1125 uuid = path_info.replace('/comment/', '')
1126 uuid = uuid.replace('#comment', '')
1127 uuid = uuid.replace('/', '')
1128 author = form.getfirst('comformauthor', '')
1129 link = form.getfirst('comformlink', '')
1130 captcha = form.getfirst('comformcaptcha', '')
1131 body = form.getfirst('comformbody', '')
1133 db = ArticleDB(os.path.join(data_path, 'db'))
1135 articles = db.get_articles(tags = tags)
1136 articles.sort(reverse = True)
1137 render_atom(articles[:10])
1141 render_html( [db.get_article(uuid)], db, year, enable_comments )
1143 article = Article(art_path, datetime.datetime.now(),
1144 datetime.datetime.now())
1145 render_html( [article], db, year, enable_comments )
1147 articles = db.get_articles()
1148 articles.sort(cmp = Article.title_cmp)
1149 render_artlist(articles, db)
1151 form_data = CommentFormData(author.strip().replace('\n', ' '),
1152 link.strip().replace('\n', ' '), captcha,
1153 body.replace('\r', ''))
1154 article = db.get_article(uuid)
1155 captcha = Captcha(article)
1158 if not form_data.author:
1159 form_data.author_error = 'please, enter your name'
1162 link = valid_link(form_data.link)
1164 form_data.link = link
1166 form_data.link_error = 'please, enter a ' \
1169 if not captcha.validate(form_data):
1170 form_data.captcha_error = captcha.help
1172 if not form_data.body:
1173 form_data.body_error = 'please, write a comment'
1176 error = validate_rst(form_data.body, secure=False)
1177 if error is not None:
1178 (line, desc, ctx) = error
1181 at = ' at line %d' % line
1182 form_data.body_error = 'error%s: %s' \
1186 c = article.add_comment(form_data.author,
1187 form_data.body, form_data.link)
1189 cdb = CommentDB(article)
1190 cdb.comments = article.comments
1192 redirect = blog_url + '/post/' + uuid + '#comment-' \
1194 render_html( [article], db, year, enable_comments, redirect,
1197 articles = db.get_articles(year, month, day, tags)
1198 articles.sort(reverse = True)
1199 if not year and not month and not day and not tags:
1200 articles = articles[:10]
1201 render_html(articles, db, year)
1205 print 'Usage: %s {add|rm|update} article_path' % sys.argv[0]
1208 if len(sys.argv) != 3:
1213 art_path = os.path.realpath(sys.argv[2])
1215 if os.path.commonprefix([data_path, art_path]) != data_path:
1216 print "Error: article (%s) must be inside data_path (%s)" % \
1217 (art_path, data_path)
1219 art_path = art_path[len(data_path)+1:]
1221 db_filename = os.path.join(data_path, 'db')
1222 if not os.path.isfile(db_filename):
1223 open(db_filename, 'w').write('')
1224 db = ArticleDB(db_filename)
1227 article = Article(art_path, datetime.datetime.now(),
1228 datetime.datetime.now())
1229 for a in db.articles:
1231 print 'Error: article already exists'
1233 db.articles.append(article)
1236 comment_dir = os.path.join(comments_path, article.uuid)
1238 os.mkdir(comment_dir, 0775)
1240 if e.errno != errno.EEXIST:
1241 print "Error: can't create comments " \
1242 "directory %s (%s)" \
1244 # otherwise is probably a removed and re-added
1247 article = Article(art_path)
1248 for a in db.articles:
1252 print "Error: no such article"
1255 r = raw_input('Remove comments [y/N]? ')
1256 db.articles.remove(a)
1258 if enable_comments and r.lower() == 'y':
1259 shutil.rmtree(os.path.join(comments_path, a.uuid))
1260 elif cmd == 'update':
1261 article = Article(art_path)
1262 for a in db.articles:
1266 print "Error: no such article"
1268 a.updated = datetime.datetime.now()
1277 if os.environ.has_key('GATEWAY_INTERFACE'):
1278 i = datetime.datetime.now()
1280 f = datetime.datetime.now()
1281 print '<!-- render time: %s -->' % (f-i)
1283 sys.exit(handle_cmd())