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 # If None is specified, cache is disabled
29 cache_path = "/tmp/blog/cache"
31 # URL to the blog, including the name. Can be a full URL or just the path.
32 blog_url = "/blog/blitiri.cgi"
34 # Style sheet (CSS) URL. Can be relative or absolute. To use the built-in
35 # default, set it to blog_url + "/style".
36 css_url = blog_url + "/style"
39 title = "I don't like blogs"
42 author = "Hartmut Kegan"
48 class Captcha (object):
49 def __init__(self, article):
50 self.article = article
51 words = article.title.split()
52 self.nword = hash(article.title) % len(words) % 5
53 self.answer = words[self.nword]
54 self.help = 'gotcha, damn spam bot!'
57 nword = self.nword + 1
66 return "enter the %s word of the article's title" % n
67 puzzle = property(fget = get_puzzle)
69 def validate(self, form_data):
70 if form_data.captcha.lower() == self.answer.lower():
76 # End of configuration
77 # DO *NOT* EDIT ANYTHING PAST HERE
91 from docutils.core import publish_parts
92 from docutils.utils import SystemMessage
94 # Before importing the config, add our cwd to the Python path
95 sys.path.append(os.getcwd())
97 # Load the config file, if there is one
104 # Pimp *_path config variables to support relative paths
105 data_path = os.path.realpath(data_path)
106 templates_path = os.path.realpath(templates_path)
110 default_main_header = """\
111 <?xml version="1.0" encoding="utf-8"?>
112 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
113 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
115 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
117 <link rel="alternate" title="%(title)s" href="%(fullurl)s/atom"
118 type="application/atom+xml" />
119 <link href="%(css_url)s" rel="stylesheet" type="text/css" />
120 <title>%(title)s</title>
125 <h1><a href="%(url)s">%(title)s</a></h1>
127 <div class="content">
130 default_main_footer = """
133 %(showyear)s: %(monthlinks)s<br/>
134 years: %(yearlinks)s<br/>
135 subscribe: <a href="%(url)s/atom">atom</a><br/>
136 views: <a href="%(url)s/">blog</a> <a href="%(url)s/list">list</a><br/>
143 default_article_header = """
144 <div class="article">
145 <h2><a href="%(url)s/post/%(uuid)s">%(arttitle)s</a></h2>
146 <span class="artinfo">
147 by %(author)s on <span class="date">
149 <a class="date" href="%(url)s/%(cyear)d/">%(cyear)04d</a>-\
150 <a class="date" href="%(url)s/%(cyear)d/%(cmonth)d/">%(cmonth)02d</a>-\
151 <a class="date" href="%(url)s/%(cyear)d/%(cmonth)d/%(cday)d/">%(cday)02d</a>\
152 %(chour)02d:%(cminute)02d</span>
153 (updated on <span class="date">
154 <a class="date" href="%(url)s/%(uyear)d/">%(uyear)04d</a>-\
155 <a class="date" href="%(url)s/%(uyear)d/%(umonth)d/">%(umonth)02d</a>-\
156 <a class="date" href="%(url)s/%(uyear)d/%(umonth)d/%(uday)d/">%(uday)02d</a>\
157 %(uhour)02d:%(uminute)02d)</span><br/>
158 <span class="tags">tagged %(tags)s</span> -
159 <span class="comments">with %(comments)s
160 <a href="%(url)s/post/%(uuid)s#comments">comment(s)</a></span>
163 <div class="artbody">
166 default_article_footer = """
172 default_comment_header = """
173 <div class="comment">
174 <a name="comment-%(number)d" />
175 <h3><a href="#comment-%(number)d">Comment #%(number)d</a></h3>
176 <span class="cominfo">by %(linked_author)s
177 on %(year)04d-%(month)02d-%(day)02d %(hour)02d:%(minute)02d</span>
179 <div class="combody">
182 default_comment_footer = """
188 default_comment_form = """
189 <div class="comform">
191 <h3 class="comform"><a href="#comment">Your comment</a></h3>
192 <div class="comforminner">
193 <form method="%(form_method)s" action="%(form_action)s">
194 <div class="comformauthor">
195 <label for="comformauthor">Your name %(form_author_error)s</label>
196 <input type="text" class="comformauthor" id="comformauthor"
197 name="comformauthor" value="%(form_author)s" />
199 <div class="comformlink">
200 <label for="comformlink">Your link
201 <span class="comformoptional">(optional, will be published)</span>
202 %(form_link_error)s</label>
203 <input type="text" class="comformlink" id="comformlink"
204 name="comformlink" value="%(form_link)s" />
205 <div class="comformhelp">
206 like <span class="formurlexample">http://www.example.com/</span>
207 or <span class="formurlexample">mailto:you@example.com</span>
210 <div class="comformcaptcha">
211 <label for="comformcaptcha">Your humanity proof %(form_captcha_error)s</label>
212 <input type="text" class="comformcaptcha" id="comformcaptcha"
213 name="comformcaptcha" value="%(form_captcha)s" />
214 <div class="comformhelp">%(captcha_puzzle)s</div>
216 <div class="comformbody">
217 <label for="comformbody" class="comformbody">The comment
218 %(form_body_error)s</label>
219 <textarea class="comformbody" id="comformbody" name="comformbody" rows="15"
220 cols="80">%(form_body)s</textarea>
221 <div class="comformhelp">
223 <a href="http://docutils.sourceforge.net/docs/user/rst/quickref.html">\
224 RestructuredText</a> format, please
227 <div class="comformsend">
228 <button type="submit" class="comformsend" id="comformsend" name="comformsend">
237 default_comment_error = '<span class="comformerror">(%(error)s)</span>'
243 font-family: sans-serif;
254 border-bottom: 2px solid #99F;
263 border-bottom: 1px solid #99C;
270 border-bottom: 1px solid #99C;
274 text-decoration: none;
283 text-decoration: none;
287 span.artinfo a:hover {
288 text-decoration: none;
305 text-decoration: none;
309 span.cominfo a:hover {
310 text-decoration: none;
334 border-bottom: 1px solid #99C;
339 div.comform span.comformoptional {
355 span.formurlexample {
357 background-color: #EEF;
358 font-family: monospace;
360 padding-right: 0.2em;
363 textarea.comformbody {
364 font-family: monospace;
384 background-color: #99F;
392 border-top: 2px solid #99F;
397 text-decoration: none;
400 /* Articles are enclosed in <div class="section"> */
406 border-bottom: 1px dotted #99C;
413 def decorate(obj, *args, **kwargs):
414 if cache_path is None: # cache disabled
415 s = f(obj, *args, **kwargs)
417 cache_file = os.path.join(cache_path,
418 'blitiri.cache.%s.html' % hash(obj))
420 s = open(cache_file).read()
422 s = f(obj, *args, **kwargs)
423 open(cache_file, 'w').write(s)
428 def rst_to_html(rst, secure = True):
430 'input_encoding': encoding,
431 'output_encoding': 'utf8',
434 'file_insertion_enabled': secure,
435 'raw_enabled': secure,
437 parts = publish_parts(rst, settings_overrides = settings,
438 writer_name = "html")
439 return parts['body'].encode('utf8')
440 rst_to_html = cached(rst_to_html)
442 def validate_rst(rst, secure = True):
444 rst_to_html(rst, secure)
446 except SystemMessage, e:
447 desc = e.args[0].encode('utf-8') # the error string
448 desc = desc[9:] # remove "<string>:"
449 line = int(desc[:desc.find(':')] or 0) # get the line number
450 desc = desc[desc.find(')')+2:-1] # remove (LEVEL/N)
452 desc, context = desc.split('\n', 1)
455 if desc.endswith('.'):
457 return (line, desc, context)
459 def valid_link(link):
461 mail_re = r"^[^ \t\n\r@<>()]+@[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$"
462 scheme_re = r'^[a-zA-Z]+:'
463 url_re = r'^(?:[a-z0-9\-]+|[a-z0-9][a-z0-9\-\.\_]*\.[a-z]+)' \
464 r'(?::[0-9]+)?(?:/.*)?$'
467 if re.match(scheme_re, link, re.I):
468 scheme, rest = link.split(':', 1)
469 if (not scheme or scheme == 'mailto') and re.match(mail_re, rest, re.I):
470 return 'mailto:' + link
471 if not scheme and re.match(url_re, rest, re.I):
472 return 'http://' + rest
478 if isinstance(obj, basestring):
479 return cgi.escape(obj, True)
483 # find out our URL, needed for syndication
485 n = os.environ['SERVER_NAME']
486 p = os.environ['SERVER_PORT']
487 s = os.environ['SCRIPT_NAME']
490 full_url = 'http://%s%s%s' % (n, p, s)
492 full_url = 'Not needed'
495 class Templates (object):
496 def __init__(self, tpath, db, showyear = None):
499 now = datetime.datetime.now()
511 'showyear': showyear,
512 'monthlinks': ' '.join(db.get_month_links(showyear)),
513 'yearlinks': ' '.join(db.get_year_links()),
516 def get_template(self, page_name, default_template, extra_vars = None):
517 if extra_vars is None:
520 vars = self.vars.copy()
521 vars.update(extra_vars)
523 p = '%s/%s.html' % (self.tpath, page_name)
524 if os.path.isfile(p):
525 return open(p).read() % vars
526 return default_template % vars
528 def get_main_header(self):
529 return self.get_template('header', default_main_header)
531 def get_main_footer(self):
532 return self.get_template('footer', default_main_footer)
534 def get_article_header(self, article):
535 return self.get_template(
536 'art_header', default_article_header, article.to_vars())
538 def get_article_footer(self, article):
539 return self.get_template(
540 'art_footer', default_article_footer, article.to_vars())
542 def get_comment_header(self, comment):
543 vars = comment.to_vars()
545 vars['linked_author'] = '<a href="%s">%s</a>' \
546 % (comment.link, comment.author)
548 vars['linked_author'] = comment.author
549 return self.get_template(
550 'com_header', default_comment_header, vars)
552 def get_comment_footer(self, comment):
553 return self.get_template(
554 'com_footer', default_comment_footer, comment.to_vars())
556 def get_comment_form(self, article, form_data, captcha_puzzle):
557 vars = article.to_vars()
558 vars.update(form_data.to_vars(self))
559 vars['captcha_puzzle'] = captcha_puzzle
560 return self.get_template(
561 'com_form', default_comment_form, vars)
563 def get_comment_error(self, error):
564 return self.get_template(
565 'com_error', default_comment_error, dict(error=error))
568 class CommentFormData (object):
569 def __init__(self, author = '', link = '', captcha = '', body = ''):
572 self.captcha = captcha
574 self.author_error = ''
576 self.captcha_error = ''
581 def to_vars(self, template):
582 render_error = template.get_comment_error
583 a_error = self.author_error and render_error(self.author_error)
584 l_error = self.link_error and render_error(self.link_error)
585 c_error = self.captcha_error \
586 and render_error(self.captcha_error)
587 b_error = self.body_error and render_error(self.body_error)
589 'form_author': sanitize(self.author),
590 'form_link': sanitize(self.link),
591 'form_captcha': sanitize(self.captcha),
592 'form_body': sanitize(self.body),
594 'form_author_error': a_error,
595 'form_link_error': l_error,
596 'form_captcha_error': c_error,
597 'form_body_error': b_error,
599 'form_action': self.action,
600 'form_method': self.method,
604 class Comment (object):
605 def __init__(self, article, number, created = None):
606 self.article = article
609 self.created = datetime.datetime.now()
611 self.created = created
616 self._author = author
618 self._raw_content = 'Removed comment'
621 def get_author(self):
625 author = property(fget = get_author)
631 link = property(fget = get_link)
633 def get_raw_content(self):
636 return self._raw_content
637 raw_content = property(fget = get_raw_content)
640 def set(self, author, raw_content, link = '', created = None):
642 self._author = author
643 self._raw_content = raw_content
645 self.created = created or datetime.datetime.now()
649 filename = os.path.join(comments_path, self.article.uuid,
652 raw = open(filename).readlines()
659 name, value = l.split(':', 1)
660 if name.lower() == 'author':
661 self._author = value.strip()
662 elif name.lower() == 'link':
663 self._link = value.strip()
668 self._raw_content = ''.join(raw[count + 1:])
672 filename = os.path.join(comments_path, self.article.uuid,
675 f = open(filename, 'w')
676 f.write('Author: %s\n' % self.author)
677 f.write('Link: %s\n' % self.link)
679 f.write(self.raw_content)
685 return rst_to_html(self.raw_content)
689 'number': self.number,
690 'author': sanitize(self.author),
691 'link': sanitize(self.link),
692 'date': self.created.isoformat(' '),
693 'created': self.created.isoformat(' '),
695 'year': self.created.year,
696 'month': self.created.month,
697 'day': self.created.day,
698 'hour': self.created.hour,
699 'minute': self.created.minute,
700 'second': self.created.second,
703 class CommentDB (object):
704 def __init__(self, article):
705 self.path = os.path.join(comments_path, article.uuid)
709 def load(self, article):
711 f = open(os.path.join(self.path, 'db'))
716 # Each line has the following comma separated format:
717 # number, created (epoch)
718 # Empty lines are meaningful and represent removed
719 # comments (so we can preserve the comment number)
723 d = datetime.datetime.fromtimestamp(float(l[1]))
725 # Removed/invalid comment
726 self.comments.append(None)
728 self.comments.append(Comment(article, n, d))
731 old_db = os.path.join(self.path, 'db')
732 new_db = os.path.join(self.path, 'db.tmp')
733 f = open(new_db, 'w')
734 for c in self.comments:
738 s += str(c.number) + ', '
739 s += str(time.mktime(c.created.timetuple()))
743 os.rename(new_db, old_db)
746 class Article (object):
747 def __init__(self, path, created = None, updated = None):
749 self.created = created
750 self.updated = updated
751 self.uuid = "%08x" % zlib.crc32(self.path)
756 self._title = 'Removed post'
757 self._author = author
759 self._raw_content = ''
767 title = property(fget = get_title)
769 def get_author(self):
773 author = property(fget = get_author)
779 tags = property(fget = get_tags)
781 def get_raw_content(self):
784 return self._raw_content
785 raw_content = property(fget = get_raw_content)
787 def get_comments(self):
790 return self._comments
791 comments = property(fget = get_comments)
794 def __cmp__(self, other):
795 if self.path == other.path:
799 if not other.created:
801 if self.created < other.created:
805 def title_cmp(self, other):
806 return cmp(self.title, other.title)
809 def add_comment(self, author, raw_content, link = ''):
810 c = Comment(self, len(self.comments))
811 c.set(author, raw_content, link)
812 self.comments.append(c)
817 # XXX this tweak is only needed for old DB format, where
818 # article's paths started with a slash
820 if path.startswith('/'):
822 filename = os.path.join(data_path, path)
824 raw = open(filename).readlines()
831 name, value = l.split(':', 1)
832 if name.lower() == 'title':
833 self._title = value.strip()
834 elif name.lower() == 'author':
835 self._author = value.strip()
836 elif name.lower() == 'tags':
837 ts = value.split(',')
838 ts = [t.strip() for t in ts]
844 self._raw_content = ''.join(raw[count + 1:])
846 self._comments = db.comments
850 return rst_to_html(self.raw_content)
854 'arttitle': sanitize(self.title),
855 'author': sanitize(self.author),
856 'date': self.created.isoformat(' '),
858 'tags': self.get_tags_links(),
859 'comments': len(self.comments),
861 'created': self.created.isoformat(' '),
862 'ciso': self.created.isoformat(),
863 'cyear': self.created.year,
864 'cmonth': self.created.month,
865 'cday': self.created.day,
866 'chour': self.created.hour,
867 'cminute': self.created.minute,
868 'csecond': self.created.second,
870 'updated': self.updated.isoformat(' '),
871 'uiso': self.updated.isoformat(),
872 'uyear': self.updated.year,
873 'umonth': self.updated.month,
874 'uday': self.updated.day,
875 'uhour': self.updated.hour,
876 'uminute': self.updated.minute,
877 'usecond': self.updated.second,
880 def get_tags_links(self):
882 tags = list(self.tags)
885 l.append('<a class="tag" href="%s/tag/%s">%s</a>' % \
886 (blog_url, urllib.quote(t), sanitize(t) ))
890 class ArticleDB (object):
891 def __init__(self, dbpath):
895 self.actyears = set()
896 self.actmonths = set()
899 def get_articles(self, year = 0, month = 0, day = 0, tags = None):
901 for a in self.articles:
902 if year and a.created.year != year: continue
903 if month and a.created.month != month: continue
904 if day and a.created.day != day: continue
905 if tags and not tags.issubset(a.tags): continue
911 def get_article(self, uuid):
912 return self.uuids[uuid]
916 f = open(self.dbpath)
921 # Each line has the following comma separated format:
922 # path (relative to data_path), \
931 datetime.datetime.fromtimestamp(float(l[1])),
932 datetime.datetime.fromtimestamp(float(l[2])))
933 self.uuids[a.uuid] = a
934 self.actyears.add(a.created.year)
935 self.actmonths.add((a.created.year, a.created.month))
936 self.articles.append(a)
939 f = open(self.dbpath + '.tmp', 'w')
940 for a in self.articles:
943 s += str(time.mktime(a.created.timetuple())) + ', '
944 s += str(time.mktime(a.updated.timetuple())) + '\n'
947 os.rename(self.dbpath + '.tmp', self.dbpath)
949 def get_year_links(self):
950 yl = list(self.actyears)
951 yl.sort(reverse = True)
952 return [ '<a href="%s/%d/">%d</a>' % (blog_url, y, y)
955 def get_month_links(self, year):
956 am = [ i[1] for i in self.actmonths if i[0] == year ]
958 for i in range(1, 13):
959 name = calendar.month_name[i][:3]
961 s = '<a href="%s/%d/%d/">%s</a>' % \
962 ( blog_url, year, i, name )
972 def render_comments(article, template, form_data):
973 print '<a name="comments" />'
974 for c in article.comments:
977 print template.get_comment_header(c)
979 print template.get_comment_footer(c)
981 form_data = CommentFormData()
982 form_data.action = blog_url + '/comment/' + article.uuid + '#comment'
983 captcha = Captcha(article)
984 print template.get_comment_form(article, form_data, captcha.puzzle)
986 def render_html(articles, db, actyear = None, show_comments = False,
987 redirect = None, form_data = None):
989 print 'Status: 303 See Other\r\n',
990 print 'Location: %s\r\n' % redirect,
991 print 'Content-type: text/html; charset=utf-8\r\n',
993 template = Templates(templates_path, db, actyear)
994 print template.get_main_header()
996 print template.get_article_header(a)
998 print template.get_article_footer(a)
1000 render_comments(a, template, form_data)
1001 print template.get_main_footer()
1003 def render_artlist(articles, db, actyear = None):
1004 template = Templates(templates_path, db, actyear)
1005 print 'Content-type: text/html; charset=utf-8\n'
1006 print template.get_main_header()
1007 print '<h2>Articles</h2>'
1009 print '<li><a href="%(url)s/uuid/%(uuid)s">%(title)s</a></li>' \
1010 % { 'url': blog_url,
1015 print template.get_main_footer()
1017 def render_atom(articles):
1018 if len(articles) > 0:
1019 updated = articles[0].updated.isoformat()
1021 updated = datetime.datetime.now().isoformat()
1023 print 'Content-type: application/atom+xml; charset=utf-8\n'
1024 print """<?xml version="1.0" encoding="utf-8"?>
1026 <feed xmlns="http://www.w3.org/2005/Atom">
1027 <title>%(title)s</title>
1028 <link rel="alternate" type="text/html" href="%(url)s"/>
1029 <link rel="self" type="application/atom+xml" href="%(url)s/atom"/>
1030 <id>%(url)s</id> <!-- TODO: find a better <id>, see RFC 4151 -->
1031 <updated>%(updated)sZ</updated>
1043 'contents': a.to_html(),
1047 <title>%(arttitle)s</title>
1048 <author><name>%(author)s</name></author>
1049 <link href="%(url)s/post/%(uuid)s" />
1050 <id>%(url)s/post/%(uuid)s</id>
1051 <summary>%(arttitle)s</summary>
1052 <published>%(ciso)sZ</published>
1053 <updated>%(uiso)sZ</updated>
1054 <content type="xhtml">
1055 <div xmlns="http://www.w3.org/1999/xhtml"><p>
1065 print 'Content-type: text/css\r\n\r\n',
1069 import cgitb; cgitb.enable()
1071 form = cgi.FieldStorage()
1072 year = int(form.getfirst("year", 0))
1073 month = int(form.getfirst("month", 0))
1074 day = int(form.getfirst("day", 0))
1075 tags = set(form.getlist("tag"))
1080 post_preview = False
1084 if os.environ.has_key('PATH_INFO'):
1085 path_info = os.environ['PATH_INFO']
1086 style = path_info == '/style'
1087 atom = path_info == '/atom'
1088 tag = path_info.startswith('/tag/')
1089 post = path_info.startswith('/post/')
1090 post_preview = path_info.startswith('/preview/post/')
1091 artlist = path_info.startswith('/list')
1092 comment = path_info.startswith('/comment/') and enable_comments
1093 if not style and not atom and not post and not post_preview \
1094 and not tag and not comment and not artlist:
1095 date = path_info.split('/')[1:]
1097 if len(date) > 1 and date[0]:
1099 if len(date) > 2 and date[1]:
1100 month = int(date[1])
1101 if len(date) > 3 and date[2]:
1106 uuid = path_info.replace('/post/', '')
1107 uuid = uuid.replace('/', '')
1109 art_path = path_info.replace('/preview/post/', '')
1110 art_path = urllib.unquote_plus(art_path)
1111 art_path = os.path.join(data_path, art_path)
1112 art_path = os.path.realpath(art_path)
1113 common = os.path.commonprefix([data_path, art_path])
1114 if common != data_path: # something nasty happened
1115 post_preview = False
1116 art_path = art_path[len(data_path)+1:]
1118 t = path_info.replace('/tag/', '')
1119 t = t.replace('/', '')
1120 t = urllib.unquote_plus(t)
1123 uuid = path_info.replace('/comment/', '')
1124 uuid = uuid.replace('#comment', '')
1125 uuid = uuid.replace('/', '')
1126 author = form.getfirst('comformauthor', '')
1127 link = form.getfirst('comformlink', '')
1128 captcha = form.getfirst('comformcaptcha', '')
1129 body = form.getfirst('comformbody', '')
1131 db = ArticleDB(os.path.join(data_path, 'db'))
1133 articles = db.get_articles(tags = tags)
1134 articles.sort(reverse = True)
1135 render_atom(articles[:10])
1139 render_html( [db.get_article(uuid)], db, year, enable_comments )
1141 article = Article(art_path, datetime.datetime.now(),
1142 datetime.datetime.now())
1143 render_html( [article], db, year, enable_comments )
1145 articles = db.get_articles()
1146 articles.sort(cmp = Article.title_cmp)
1147 render_artlist(articles, db)
1149 form_data = CommentFormData(author.strip().replace('\n', ' '),
1150 link.strip().replace('\n', ' '), captcha,
1151 body.replace('\r', ''))
1152 article = db.get_article(uuid)
1153 captcha = Captcha(article)
1156 if not form_data.author:
1157 form_data.author_error = 'please, enter your name'
1160 link = valid_link(form_data.link)
1162 form_data.link = link
1164 form_data.link_error = 'please, enter a ' \
1167 if not captcha.validate(form_data):
1168 form_data.captcha_error = captcha.help
1170 if not form_data.body:
1171 form_data.body_error = 'please, write a comment'
1174 error = validate_rst(form_data.body, secure=False)
1175 if error is not None:
1176 (line, desc, ctx) = error
1179 at = ' at line %d' % line
1180 form_data.body_error = 'error%s: %s' % (at, desc)
1183 c = article.add_comment(form_data.author,
1184 form_data.body, form_data.link)
1186 cdb = CommentDB(article)
1187 cdb.comments = article.comments
1189 redirect = blog_url + '/post/' + uuid + '#comment-' \
1191 render_html( [article], db, year, enable_comments, redirect,
1194 articles = db.get_articles(year, month, day, tags)
1195 articles.sort(reverse = True)
1196 if not year and not month and not day and not tags:
1197 articles = articles[:10]
1198 render_html(articles, db, year)
1202 print 'Usage: %s {add|rm|update} article_path' % sys.argv[0]
1205 if len(sys.argv) != 3:
1210 art_path = os.path.realpath(sys.argv[2])
1212 if os.path.commonprefix([data_path, art_path]) != data_path:
1213 print "Error: article (%s) must be inside data_path (%s)" % \
1214 (art_path, data_path)
1216 art_path = art_path[len(data_path)+1:]
1218 db_filename = os.path.join(data_path, 'db')
1219 if not os.path.isfile(db_filename):
1220 open(db_filename, 'w').write('')
1221 db = ArticleDB(db_filename)
1224 article = Article(art_path, datetime.datetime.now(),
1225 datetime.datetime.now())
1226 for a in db.articles:
1228 print 'Error: article already exists'
1230 db.articles.append(article)
1233 comment_dir = os.path.join(comments_path, article.uuid)
1235 os.mkdir(comment_dir, 0775)
1237 if e.errno != errno.EEXIST:
1238 print "Error: can't create comments " \
1239 "directory %s (%s)" \
1241 # otherwise is probably a removed and re-added
1244 article = Article(art_path)
1245 for a in db.articles:
1249 print "Error: no such article"
1252 r = raw_input('Remove comments [y/N]? ')
1253 db.articles.remove(a)
1255 if enable_comments and r.lower() == 'y':
1256 shutil.rmtree(os.path.join(comments_path, a.uuid))
1257 elif cmd == 'update':
1258 article = Article(art_path)
1259 for a in db.articles:
1263 print "Error: no such article"
1265 a.updated = datetime.datetime.now()
1274 if os.environ.has_key('GATEWAY_INTERFACE'):
1275 i = datetime.datetime.now()
1277 f = datetime.datetime.now()
1278 print '<!-- render time: %s -->' % (f-i)
1280 sys.exit(handle_cmd())