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!'
60 nword = self.nword + 1
69 return "enter the %s word of the article's title" % n
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 # It only works if the function is pure (that is, its return value depends
416 # only on its arguments), and if all the arguments are hash()eable.
418 # do not decorate if the cache is disabled
419 if cache_path is None:
422 def decorate(*args, **kwargs):
423 hashes = '-'.join( str(hash(x)) for x in args +
424 tuple(kwargs.items()) )
425 fname = 'blitiri.%s.%s.cache' % (f.__name__, hashes)
426 cache_file = os.path.join(cache_path, fname)
428 s = open(cache_file).read()
430 s = f(*args, **kwargs)
431 open(cache_file, 'w').write(s)
439 def rst_to_html(rst, secure = True):
441 'input_encoding': encoding,
442 'output_encoding': 'utf8',
445 'file_insertion_enabled': secure,
446 'raw_enabled': secure,
448 parts = publish_parts(rst, settings_overrides = settings,
449 writer_name = "html")
450 return parts['body'].encode('utf8')
452 def validate_rst(rst, secure = True):
454 rst_to_html(rst, secure)
456 except SystemMessage, e:
457 desc = e.args[0].encode('utf-8') # the error string
458 desc = desc[9:] # remove "<string>:"
459 line = int(desc[:desc.find(':')] or 0) # get the line number
460 desc = desc[desc.find(')')+2:-1] # remove (LEVEL/N)
462 desc, context = desc.split('\n', 1)
465 if desc.endswith('.'):
467 return (line, desc, context)
469 def valid_link(link):
471 mail_re = r"^[^ \t\n\r@<>()]+@[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$"
472 scheme_re = r'^[a-zA-Z]+:'
473 url_re = r'^(?:[a-z0-9\-]+|[a-z0-9][a-z0-9\-\.\_]*\.[a-z]+)' \
474 r'(?::[0-9]+)?(?:/.*)?$'
477 if re.match(scheme_re, link, re.I):
478 scheme, rest = link.split(':', 1)
479 if (not scheme or scheme == 'mailto') and re.match(mail_re, rest, re.I):
480 return 'mailto:' + link
481 if not scheme and re.match(url_re, rest, re.I):
482 return 'http://' + rest
488 if isinstance(obj, basestring):
489 return cgi.escape(obj, True)
493 # find out our URL, needed for syndication
495 n = os.environ['SERVER_NAME']
496 p = os.environ['SERVER_PORT']
497 s = os.environ['SCRIPT_NAME']
500 full_url = 'http://%s%s%s' % (n, p, s)
502 full_url = 'Not needed'
505 class Templates (object):
506 def __init__(self, tpath, db, showyear = None):
509 now = datetime.datetime.now()
521 'showyear': showyear,
522 'monthlinks': ' '.join(db.get_month_links(showyear)),
523 'yearlinks': ' '.join(db.get_year_links()),
526 def get_template(self, page_name, default_template, extra_vars = None):
527 if extra_vars is None:
530 vars = self.vars.copy()
531 vars.update(extra_vars)
533 p = '%s/%s.html' % (self.tpath, page_name)
534 if os.path.isfile(p):
535 return open(p).read() % vars
536 return default_template % vars
538 def get_main_header(self):
539 return self.get_template('header', default_main_header)
541 def get_main_footer(self):
542 return self.get_template('footer', default_main_footer)
544 def get_article_header(self, article):
545 return self.get_template(
546 'art_header', default_article_header, article.to_vars())
548 def get_article_footer(self, article):
549 return self.get_template(
550 'art_footer', default_article_footer, article.to_vars())
552 def get_comment_header(self, comment):
553 vars = comment.to_vars()
555 vars['linked_author'] = '<a href="%s">%s</a>' \
556 % (comment.link, comment.author)
558 vars['linked_author'] = comment.author
559 return self.get_template(
560 'com_header', default_comment_header, vars)
562 def get_comment_footer(self, comment):
563 return self.get_template(
564 'com_footer', default_comment_footer, comment.to_vars())
566 def get_comment_form(self, article, form_data, captcha_puzzle):
567 vars = article.to_vars()
568 vars.update(form_data.to_vars(self))
569 vars['captcha_puzzle'] = captcha_puzzle
570 return self.get_template(
571 'com_form', default_comment_form, vars)
573 def get_comment_error(self, error):
574 return self.get_template(
575 'com_error', default_comment_error, dict(error=error))
578 class CommentFormData (object):
579 def __init__(self, author = '', link = '', captcha = '', body = ''):
582 self.captcha = captcha
584 self.author_error = ''
586 self.captcha_error = ''
591 def to_vars(self, template):
592 render_error = template.get_comment_error
593 a_error = self.author_error and render_error(self.author_error)
594 l_error = self.link_error and render_error(self.link_error)
595 c_error = self.captcha_error \
596 and render_error(self.captcha_error)
597 b_error = self.body_error and render_error(self.body_error)
599 'form_author': sanitize(self.author),
600 'form_link': sanitize(self.link),
601 'form_captcha': sanitize(self.captcha),
602 'form_body': sanitize(self.body),
604 'form_author_error': a_error,
605 'form_link_error': l_error,
606 'form_captcha_error': c_error,
607 'form_body_error': b_error,
609 'form_action': self.action,
610 'form_method': self.method,
614 class Comment (object):
615 def __init__(self, article, number, created = None):
616 self.article = article
619 self.created = datetime.datetime.now()
621 self.created = created
626 self._author = author
628 self._raw_content = 'Removed comment'
643 def raw_content(self):
646 return self._raw_content
648 def set(self, author, raw_content, link = '', created = None):
650 self._author = author
651 self._raw_content = raw_content
653 self.created = created or datetime.datetime.now()
657 filename = os.path.join(comments_path, self.article.uuid,
660 raw = open(filename).readlines()
667 name, value = l.split(':', 1)
668 if name.lower() == 'author':
669 self._author = value.strip()
670 elif name.lower() == 'link':
671 self._link = value.strip()
676 self._raw_content = ''.join(raw[count + 1:])
680 filename = os.path.join(comments_path, self.article.uuid,
683 f = open(filename, 'w')
684 f.write('Author: %s\n' % self.author)
685 f.write('Link: %s\n' % self.link)
687 f.write(self.raw_content)
693 return rst_to_html(self.raw_content)
697 'number': self.number,
698 'author': sanitize(self.author),
699 'link': sanitize(self.link),
700 'date': self.created.isoformat(' '),
701 'created': self.created.isoformat(' '),
703 'year': self.created.year,
704 'month': self.created.month,
705 'day': self.created.day,
706 'hour': self.created.hour,
707 'minute': self.created.minute,
708 'second': self.created.second,
711 class CommentDB (object):
712 def __init__(self, article):
713 self.path = os.path.join(comments_path, article.uuid)
717 def load(self, article):
719 f = open(os.path.join(self.path, 'db'))
724 # Each line has the following comma separated format:
725 # number, created (epoch)
726 # Empty lines are meaningful and represent removed
727 # comments (so we can preserve the comment number)
731 d = datetime.datetime.fromtimestamp(float(l[1]))
733 # Removed/invalid comment
734 self.comments.append(None)
736 self.comments.append(Comment(article, n, d))
739 old_db = os.path.join(self.path, 'db')
740 new_db = os.path.join(self.path, 'db.tmp')
741 f = open(new_db, 'w')
742 for c in self.comments:
746 s += str(c.number) + ', '
747 s += str(time.mktime(c.created.timetuple()))
751 os.rename(new_db, old_db)
754 class Article (object):
755 def __init__(self, path, created = None, updated = None):
757 self.created = created
758 self.updated = updated
759 self.uuid = "%08x" % zlib.crc32(self.path)
764 self._title = 'Removed post'
765 self._author = author
767 self._raw_content = ''
789 def raw_content(self):
792 return self._raw_content
798 return self._comments
800 def __cmp__(self, other):
801 if self.path == other.path:
805 if not other.created:
807 if self.created < other.created:
811 def title_cmp(self, other):
812 return cmp(self.title, other.title)
815 def add_comment(self, author, raw_content, link = ''):
816 c = Comment(self, len(self.comments))
817 c.set(author, raw_content, link)
818 self.comments.append(c)
823 # XXX this tweak is only needed for old DB format, where
824 # article's paths started with a slash
826 if path.startswith('/'):
828 filename = os.path.join(data_path, path)
830 raw = open(filename).readlines()
837 name, value = l.split(':', 1)
838 if name.lower() == 'title':
839 self._title = value.strip()
840 elif name.lower() == 'author':
841 self._author = value.strip()
842 elif name.lower() == 'tags':
843 ts = value.split(',')
844 ts = [t.strip() for t in ts]
850 self._raw_content = ''.join(raw[count + 1:])
852 self._comments = db.comments
856 return rst_to_html(self.raw_content)
860 'arttitle': sanitize(self.title),
861 'author': sanitize(self.author),
862 'date': self.created.isoformat(' '),
864 'tags': self.get_tags_links(),
865 'comments': len(self.comments),
867 'created': self.created.isoformat(' '),
868 'ciso': self.created.isoformat(),
869 'cyear': self.created.year,
870 'cmonth': self.created.month,
871 'cday': self.created.day,
872 'chour': self.created.hour,
873 'cminute': self.created.minute,
874 'csecond': self.created.second,
876 'updated': self.updated.isoformat(' '),
877 'uiso': self.updated.isoformat(),
878 'uyear': self.updated.year,
879 'umonth': self.updated.month,
880 'uday': self.updated.day,
881 'uhour': self.updated.hour,
882 'uminute': self.updated.minute,
883 'usecond': self.updated.second,
886 def get_tags_links(self):
888 tags = list(self.tags)
891 l.append('<a class="tag" href="%s/tag/%s">%s</a>' % \
892 (blog_url, urllib.quote(t), sanitize(t) ))
896 class ArticleDB (object):
897 def __init__(self, dbpath):
901 self.actyears = set()
902 self.actmonths = set()
905 def get_articles(self, year = 0, month = 0, day = 0, tags = None):
907 for a in self.articles:
908 if year and a.created.year != year: continue
909 if month and a.created.month != month: continue
910 if day and a.created.day != day: continue
911 if tags and not tags.issubset(a.tags): continue
917 def get_article(self, uuid):
918 return self.uuids[uuid]
922 f = open(self.dbpath)
927 # Each line has the following comma separated format:
928 # path (relative to data_path), \
937 datetime.datetime.fromtimestamp(float(l[1])),
938 datetime.datetime.fromtimestamp(float(l[2])))
939 self.uuids[a.uuid] = a
940 self.actyears.add(a.created.year)
941 self.actmonths.add((a.created.year, a.created.month))
942 self.articles.append(a)
945 f = open(self.dbpath + '.tmp', 'w')
946 for a in self.articles:
949 s += str(time.mktime(a.created.timetuple())) + ', '
950 s += str(time.mktime(a.updated.timetuple())) + '\n'
953 os.rename(self.dbpath + '.tmp', self.dbpath)
955 def get_year_links(self):
956 yl = list(self.actyears)
957 yl.sort(reverse = True)
958 return [ '<a href="%s/%d/">%d</a>' % (blog_url, y, y)
961 def get_month_links(self, year):
962 am = [ i[1] for i in self.actmonths if i[0] == year ]
964 for i in range(1, 13):
965 name = calendar.month_name[i][:3]
967 s = '<a href="%s/%d/%d/">%s</a>' % \
968 ( blog_url, year, i, name )
978 def render_comments(article, template, form_data):
979 print '<a name="comments" />'
980 for c in article.comments:
983 print template.get_comment_header(c)
985 print template.get_comment_footer(c)
987 form_data = CommentFormData()
988 form_data.action = blog_url + '/comment/' + article.uuid + '#comment'
989 captcha = Captcha(article)
990 print template.get_comment_form(article, form_data, captcha.puzzle)
992 def render_html(articles, db, actyear = None, show_comments = False,
993 redirect = None, form_data = None):
995 print 'Status: 303 See Other\r\n',
996 print 'Location: %s\r\n' % redirect,
997 print 'Content-type: text/html; charset=utf-8\r\n',
999 template = Templates(templates_path, db, actyear)
1000 print template.get_main_header()
1002 print template.get_article_header(a)
1004 print template.get_article_footer(a)
1006 render_comments(a, template, form_data)
1007 print template.get_main_footer()
1009 def render_artlist(articles, db, actyear = None):
1010 template = Templates(templates_path, db, actyear)
1011 print 'Content-type: text/html; charset=utf-8\n'
1012 print template.get_main_header()
1013 print '<h2>Articles</h2>'
1015 print '<li><a href="%(url)s/uuid/%(uuid)s">%(title)s</a></li>' \
1016 % { 'url': blog_url,
1021 print template.get_main_footer()
1023 def render_atom(articles):
1024 if len(articles) > 0:
1025 updated = articles[0].updated.isoformat()
1027 updated = datetime.datetime.now().isoformat()
1029 print 'Content-type: application/atom+xml; charset=utf-8\n'
1030 print """<?xml version="1.0" encoding="utf-8"?>
1032 <feed xmlns="http://www.w3.org/2005/Atom">
1033 <title>%(title)s</title>
1034 <link rel="alternate" type="text/html" href="%(url)s"/>
1035 <link rel="self" type="application/atom+xml" href="%(url)s/atom"/>
1036 <id>%(url)s</id> <!-- TODO: find a better <id>, see RFC 4151 -->
1037 <updated>%(updated)sZ</updated>
1049 'contents': a.to_html(),
1053 <title>%(arttitle)s</title>
1054 <author><name>%(author)s</name></author>
1055 <link href="%(url)s/post/%(uuid)s" />
1056 <id>%(url)s/post/%(uuid)s</id>
1057 <summary>%(arttitle)s</summary>
1058 <published>%(ciso)sZ</published>
1059 <updated>%(uiso)sZ</updated>
1060 <content type="xhtml">
1061 <div xmlns="http://www.w3.org/1999/xhtml"><p>
1071 print 'Content-type: text/css\r\n\r\n',
1075 import cgitb; cgitb.enable()
1077 form = cgi.FieldStorage()
1078 year = int(form.getfirst("year", 0))
1079 month = int(form.getfirst("month", 0))
1080 day = int(form.getfirst("day", 0))
1081 tags = set(form.getlist("tag"))
1086 post_preview = False
1090 if os.environ.has_key('PATH_INFO'):
1091 path_info = os.environ['PATH_INFO']
1092 style = path_info == '/style'
1093 atom = path_info == '/atom'
1094 tag = path_info.startswith('/tag/')
1095 post = path_info.startswith('/post/')
1096 post_preview = path_info.startswith('/preview/post/')
1097 artlist = path_info.startswith('/list')
1098 comment = path_info.startswith('/comment/') and enable_comments
1099 if not style and not atom and not post and not post_preview \
1100 and not tag and not comment and not artlist:
1101 date = path_info.split('/')[1:]
1103 if len(date) > 1 and date[0]:
1105 if len(date) > 2 and date[1]:
1106 month = int(date[1])
1107 if len(date) > 3 and date[2]:
1112 uuid = path_info.replace('/post/', '')
1113 uuid = uuid.replace('/', '')
1115 art_path = path_info.replace('/preview/post/', '')
1116 art_path = urllib.unquote_plus(art_path)
1117 art_path = os.path.join(data_path, art_path)
1118 art_path = os.path.realpath(art_path)
1119 common = os.path.commonprefix([data_path, art_path])
1120 if common != data_path: # something nasty happened
1121 post_preview = False
1122 art_path = art_path[len(data_path)+1:]
1124 t = path_info.replace('/tag/', '')
1125 t = t.replace('/', '')
1126 t = urllib.unquote_plus(t)
1129 uuid = path_info.replace('/comment/', '')
1130 uuid = uuid.replace('#comment', '')
1131 uuid = uuid.replace('/', '')
1132 author = form.getfirst('comformauthor', '')
1133 link = form.getfirst('comformlink', '')
1134 captcha = form.getfirst('comformcaptcha', '')
1135 body = form.getfirst('comformbody', '')
1137 db = ArticleDB(os.path.join(data_path, 'db'))
1139 articles = db.get_articles(tags = tags)
1140 articles.sort(reverse = True)
1141 render_atom(articles[:10])
1145 render_html( [db.get_article(uuid)], db, year, enable_comments )
1147 article = Article(art_path, datetime.datetime.now(),
1148 datetime.datetime.now())
1149 render_html( [article], db, year, enable_comments )
1151 articles = db.get_articles()
1152 articles.sort(cmp = Article.title_cmp)
1153 render_artlist(articles, db)
1155 form_data = CommentFormData(author.strip().replace('\n', ' '),
1156 link.strip().replace('\n', ' '), captcha,
1157 body.replace('\r', ''))
1158 article = db.get_article(uuid)
1159 captcha = Captcha(article)
1162 if not form_data.author:
1163 form_data.author_error = 'please, enter your name'
1166 link = valid_link(form_data.link)
1168 form_data.link = link
1170 form_data.link_error = 'please, enter a ' \
1173 if not captcha.validate(form_data):
1174 form_data.captcha_error = captcha.help
1176 if not form_data.body:
1177 form_data.body_error = 'please, write a comment'
1180 error = validate_rst(form_data.body, secure=False)
1181 if error is not None:
1182 (line, desc, ctx) = error
1185 at = ' at line %d' % line
1186 form_data.body_error = 'error%s: %s' \
1190 c = article.add_comment(form_data.author,
1191 form_data.body, form_data.link)
1193 cdb = CommentDB(article)
1194 cdb.comments = article.comments
1196 redirect = blog_url + '/post/' + uuid + '#comment-' \
1198 render_html( [article], db, year, enable_comments, redirect,
1201 articles = db.get_articles(year, month, day, tags)
1202 articles.sort(reverse = True)
1203 if not year and not month and not day and not tags:
1204 articles = articles[:10]
1205 render_html(articles, db, year)
1209 print 'Usage: %s {add|rm|update} article_path' % sys.argv[0]
1212 if len(sys.argv) != 3:
1217 art_path = os.path.realpath(sys.argv[2])
1219 if os.path.commonprefix([data_path, art_path]) != data_path:
1220 print "Error: article (%s) must be inside data_path (%s)" % \
1221 (art_path, data_path)
1223 art_path = art_path[len(data_path)+1:]
1225 db_filename = os.path.join(data_path, 'db')
1226 if not os.path.isfile(db_filename):
1227 open(db_filename, 'w').write('')
1228 db = ArticleDB(db_filename)
1231 article = Article(art_path, datetime.datetime.now(),
1232 datetime.datetime.now())
1233 for a in db.articles:
1235 print 'Error: article already exists'
1237 db.articles.append(article)
1240 comment_dir = os.path.join(comments_path, article.uuid)
1242 os.mkdir(comment_dir, 0775)
1244 if e.errno != errno.EEXIST:
1245 print "Error: can't create comments " \
1246 "directory %s (%s)" \
1248 # otherwise is probably a removed and re-added
1251 article = Article(art_path)
1252 for a in db.articles:
1256 print "Error: no such article"
1259 r = raw_input('Remove comments [y/N]? ')
1260 db.articles.remove(a)
1262 if enable_comments and r.lower() == 'y':
1263 shutil.rmtree(os.path.join(comments_path, a.uuid))
1264 elif cmd == 'update':
1265 article = Article(art_path)
1266 for a in db.articles:
1270 print "Error: no such article"
1272 a.updated = datetime.datetime.now()
1281 if os.environ.has_key('GATEWAY_INTERFACE'):
1282 i = datetime.datetime.now()
1284 f = datetime.datetime.now()
1285 print '<!-- render time: %s -->' % (f-i)
1287 sys.exit(handle_cmd())