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"
49 # Captcha method to use. At the moment only "title" is supported, but if you
50 # are keen with Python you can provide your own captcha implementation, see
52 captcha_method = "title"
56 # End of configuration
57 # DO *NOT* EDIT ANYTHING PAST HERE
71 from docutils.core import publish_parts
72 from docutils.utils import SystemMessage
74 # Before importing the config, add our cwd to the Python path
75 sys.path.append(os.getcwd())
77 # Load the config file, if there is one
84 # Pimp *_path config variables to support relative paths
85 data_path = os.path.realpath(data_path)
86 templates_path = os.path.realpath(templates_path)
92 # They must follow the interface described below.
95 # Captcha(article) -> constructor, takes an article[1] as argument
97 # puzzle -> a string with the puzzle the user must solve to prove he is
98 # not a bot (can be raw HTML)
99 # help -> a string with extra instructions, shown only when the user
100 # failed to solve the puzzle
102 # validate(form_data) -> based on the form data[2], returns True if
103 # the user has solved the puzzle uccessfully
106 # Note you must ensure that the puzzle attribute and validate() method can
107 # "communicate" because they are executed in different requests. You can pass a
108 # cookie or just calculate the answer based on the article's data, for example.
110 # [1] article is an object with all the article's information:
112 # created -> datetime
113 # updated -> datetime
114 # uuid -> string (unique ID)
117 # tags -> list of strings
118 # raw_contents -> string in rst format
119 # comments -> list of Comment objects (not too relevant here)
120 # [2] form_data is an object with the form fields (all strings):
121 # author, author_error
123 # catpcha, captcha_error
127 class TitleCaptcha (object):
128 "Captcha that uses the article's title for the puzzle"
129 def __init__(self, article):
130 self.article = article
131 words = article.title.split()
132 self.nword = hash(article.title) % len(words) % 5
133 self.answer = words[self.nword]
134 self.help = 'gotcha, damn spam bot!'
138 nword = self.nword + 1
146 n = str(nword) + 'th'
147 return "enter the %s word of the article's title" % n
149 def validate(self, form_data):
150 if form_data.captcha.lower() == self.answer.lower():
154 known_captcha_methods = {
155 'title': TitleCaptcha,
158 # If the configured captcha method was a known string, replace it by the
159 # matching class; otherwise assume it's already a class and leave it
160 # alone. This way the user can either use one of our methods, or provide one
162 if captcha_method in known_captcha_methods:
163 captcha_method = known_captcha_methods[captcha_method]
168 default_main_header = """\
169 <?xml version="1.0" encoding="utf-8"?>
170 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
171 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
173 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
175 <link rel="alternate" title="%(title)s" href="%(fullurl)s/atom"
176 type="application/atom+xml" />
177 <link href="%(css_url)s" rel="stylesheet" type="text/css" />
178 <title>%(title)s</title>
183 <h1><a href="%(url)s">%(title)s</a></h1>
185 <div class="content">
188 default_main_footer = """
191 %(showyear)s: %(monthlinks)s<br/>
192 years: %(yearlinks)s<br/>
193 subscribe: <a href="%(url)s/atom">atom</a><br/>
194 views: <a href="%(url)s/">blog</a> <a href="%(url)s/list">list</a><br/>
195 tags: %(taglinks)s<br/>
202 default_article_header = """
203 <div class="article">
204 <h2><a href="%(url)s/post/%(uuid)s">%(arttitle)s</a></h2>
205 <span class="artinfo">
206 by %(author)s on <span class="date">
208 <a class="date" href="%(url)s/%(cyear)d/">%(cyear)04d</a>-\
209 <a class="date" href="%(url)s/%(cyear)d/%(cmonth)d/">%(cmonth)02d</a>-\
210 <a class="date" href="%(url)s/%(cyear)d/%(cmonth)d/%(cday)d/">%(cday)02d</a>\
211 %(chour)02d:%(cminute)02d</span>
212 (updated on <span class="date">
213 <a class="date" href="%(url)s/%(uyear)d/">%(uyear)04d</a>-\
214 <a class="date" href="%(url)s/%(uyear)d/%(umonth)d/">%(umonth)02d</a>-\
215 <a class="date" href="%(url)s/%(uyear)d/%(umonth)d/%(uday)d/">%(uday)02d</a>\
216 %(uhour)02d:%(uminute)02d)</span><br/>
217 <span class="tags">tagged %(tags)s</span> -
218 <span class="comments">with %(comments)s
219 <a href="%(url)s/post/%(uuid)s#comments">comment(s)</a></span>
222 <div class="artbody">
225 default_article_footer = """
231 default_comment_header = """
232 <div class="comment">
233 <a name="comment-%(number)d" />
234 <h3><a href="#comment-%(number)d">Comment #%(number)d</a></h3>
235 <span class="cominfo">by %(linked_author)s
236 on %(year)04d-%(month)02d-%(day)02d %(hour)02d:%(minute)02d</span>
238 <div class="combody">
241 default_comment_footer = """
247 default_comment_form = """
248 <div class="comform">
250 <h3 class="comform"><a href="#comment">Your comment</a></h3>
251 <div class="comforminner">
252 <form method="%(form_method)s" action="%(form_action)s">
253 <div class="comformauthor">
254 <label for="comformauthor">Your name %(form_author_error)s</label>
255 <input type="text" class="comformauthor" id="comformauthor"
256 name="comformauthor" value="%(form_author)s" />
258 <div class="comformlink">
259 <label for="comformlink">Your link
260 <span class="comformoptional">(optional, will be published)</span>
261 %(form_link_error)s</label>
262 <input type="text" class="comformlink" id="comformlink"
263 name="comformlink" value="%(form_link)s" />
264 <div class="comformhelp">
265 like <span class="formurlexample">http://www.example.com/</span>
266 or <span class="formurlexample">mailto:you@example.com</span>
269 <div class="comformcaptcha">
270 <label for="comformcaptcha">Your humanity proof %(form_captcha_error)s</label>
271 <input type="text" class="comformcaptcha" id="comformcaptcha"
272 name="comformcaptcha" value="%(form_captcha)s" />
273 <div class="comformhelp">%(captcha_puzzle)s</div>
275 <div class="comformbody">
276 <label for="comformbody" class="comformbody">The comment
277 %(form_body_error)s</label>
278 <textarea class="comformbody" id="comformbody" name="comformbody" rows="15"
279 cols="80">%(form_body)s</textarea>
280 <div class="comformhelp">
282 <a href="http://docutils.sourceforge.net/docs/user/rst/quickref.html">\
283 RestructuredText</a> format, please
286 <div class="comformsend">
287 <button type="submit" class="comformsend" id="comformsend" name="comformsend">
296 default_comment_error = '<span class="comformerror">(%(error)s)</span>'
302 font-family: sans-serif;
313 border-bottom: 2px solid #99F;
322 border-bottom: 1px solid #99C;
329 border-bottom: 1px solid #99C;
333 text-decoration: none;
342 text-decoration: none;
346 span.artinfo a:hover {
347 text-decoration: none;
364 text-decoration: none;
368 span.cominfo a:hover {
369 text-decoration: none;
393 border-bottom: 1px solid #99C;
398 div.comform span.comformoptional {
414 span.formurlexample {
416 background-color: #EEF;
417 font-family: monospace;
419 padding-right: 0.2em;
422 textarea.comformbody {
423 font-family: monospace;
443 background-color: #99F;
451 border-top: 2px solid #99F;
456 text-decoration: none;
459 /* Articles are enclosed in <div class="section"> */
465 border-bottom: 1px dotted #99C;
472 # It only works if the function is pure (that is, its return value depends
473 # only on its arguments), and if all the arguments are hash()eable.
475 # do not decorate if the cache is disabled
476 if cache_path is None:
479 def decorate(*args, **kwargs):
480 hashes = '-'.join( str(hash(x)) for x in args +
481 tuple(kwargs.items()) )
482 fname = 'blitiri.%s.%s.cache' % (f.__name__, hashes)
483 cache_file = os.path.join(cache_path, fname)
485 s = open(cache_file).read()
487 s = f(*args, **kwargs)
488 open(cache_file, 'w').write(s)
496 def rst_to_html(rst, secure = True):
498 'input_encoding': encoding,
499 'output_encoding': 'utf8',
502 'file_insertion_enabled': secure,
503 'raw_enabled': secure,
505 parts = publish_parts(rst, settings_overrides = settings,
506 writer_name = "html")
507 return parts['body'].encode('utf8')
509 def validate_rst(rst, secure = True):
511 rst_to_html(rst, secure)
513 except SystemMessage, e:
514 desc = e.args[0].encode('utf-8') # the error string
515 desc = desc[9:] # remove "<string>:"
516 line = int(desc[:desc.find(':')] or 0) # get the line number
517 desc = desc[desc.find(')')+2:-1] # remove (LEVEL/N)
519 desc, context = desc.split('\n', 1)
522 if desc.endswith('.'):
524 return (line, desc, context)
526 def valid_link(link):
528 scheme_re = r'^[a-zA-Z]+:'
529 mail_re = r"^[^ \t\n\r@<>()]+@[a-z0-9][a-z0-9\.\-_]*\.[a-z]+$"
530 url_re = r'^(?:[a-z0-9\-]+|[a-z0-9][a-z0-9\-\.\_]*\.[a-z]+)' \
531 r'(?::[0-9]+)?(?:/.*)?$'
533 if re.match(scheme_re, link, re.I):
534 scheme, rest = link.split(':', 1)
535 # if we have an scheme and a rest, assume the link is valid
536 # and return it as-is; otherwise (having just the scheme) is
542 # at this point, we don't have a scheme; we will try to recognize some
543 # common addresses (mail and http at the moment) and complete them to
544 # form a valid link, if we fail we will just claim it's invalid
545 if re.match(mail_re, link, re.I):
546 return 'mailto:' + link
547 elif re.match(url_re, link, re.I):
548 return 'http://' + link
553 return cgi.escape(obj, quote = True)
556 # find out our URL, needed for syndication
558 n = os.environ['SERVER_NAME']
559 p = os.environ['SERVER_PORT']
560 s = os.environ['SCRIPT_NAME']
563 full_url = 'http://%s%s%s' % (n, p, s)
565 full_url = 'Not needed'
568 class Templates (object):
569 def __init__(self, tpath, db, showyear = None):
572 now = datetime.datetime.now()
584 'showyear': showyear,
585 'monthlinks': ' '.join(db.get_month_links(showyear)),
586 'yearlinks': ' '.join(db.get_year_links()),
587 'taglinks': ' '.join(db.get_tag_links()),
590 def get_template(self, page_name, default_template, extra_vars = None):
591 if extra_vars is None:
594 vars = self.vars.copy()
595 vars.update(extra_vars)
597 p = '%s/%s.html' % (self.tpath, page_name)
598 if os.path.isfile(p):
599 return open(p).read() % vars
600 return default_template % vars
602 def get_main_header(self):
603 return self.get_template('header', default_main_header)
605 def get_main_footer(self):
606 return self.get_template('footer', default_main_footer)
608 def get_article_header(self, article):
609 return self.get_template(
610 'art_header', default_article_header, article.to_vars())
612 def get_article_footer(self, article):
613 return self.get_template(
614 'art_footer', default_article_footer, article.to_vars())
616 def get_comment_header(self, comment):
617 vars = comment.to_vars()
619 vars['linked_author'] = '<a href="%s">%s</a>' \
620 % (vars['link'], vars['author'])
622 vars['linked_author'] = vars['author']
623 return self.get_template(
624 'com_header', default_comment_header, vars)
626 def get_comment_footer(self, comment):
627 return self.get_template(
628 'com_footer', default_comment_footer, comment.to_vars())
630 def get_comment_form(self, article, form_data, captcha_puzzle):
631 vars = article.to_vars()
632 vars.update(form_data.to_vars(self))
633 vars['captcha_puzzle'] = captcha_puzzle
634 return self.get_template(
635 'com_form', default_comment_form, vars)
637 def get_comment_error(self, error):
638 return self.get_template(
639 'com_error', default_comment_error, dict(error=error))
642 class CommentFormData (object):
643 def __init__(self, author = '', link = '', captcha = '', body = ''):
646 self.captcha = captcha
648 self.author_error = ''
650 self.captcha_error = ''
655 def to_vars(self, template):
656 render_error = template.get_comment_error
657 a_error = self.author_error and render_error(self.author_error)
658 l_error = self.link_error and render_error(self.link_error)
659 c_error = self.captcha_error \
660 and render_error(self.captcha_error)
661 b_error = self.body_error and render_error(self.body_error)
663 'form_author': sanitize(self.author),
664 'form_link': sanitize(self.link),
665 'form_captcha': sanitize(self.captcha),
666 'form_body': sanitize(self.body),
668 'form_author_error': a_error,
669 'form_link_error': l_error,
670 'form_captcha_error': c_error,
671 'form_body_error': b_error,
673 'form_action': self.action,
674 'form_method': self.method,
678 class Comment (object):
679 def __init__(self, article, number, created = None):
680 self.article = article
683 self.created = datetime.datetime.now()
685 self.created = created
690 self._author = author
692 self._raw_content = 'Removed comment'
707 def raw_content(self):
710 return self._raw_content
712 def set(self, author, raw_content, link = '', created = None):
714 self._author = author
715 self._raw_content = raw_content
717 self.created = created or datetime.datetime.now()
721 filename = os.path.join(comments_path, self.article.uuid,
724 raw = open(filename).readlines()
731 name, value = l.split(':', 1)
732 if name.lower() == 'author':
733 self._author = value.strip()
734 elif name.lower() == 'link':
735 self._link = value.strip()
740 self._raw_content = ''.join(raw[count + 1:])
744 filename = os.path.join(comments_path, self.article.uuid,
747 f = open(filename, 'w')
748 f.write('Author: %s\n' % self.author)
749 f.write('Link: %s\n' % self.link)
751 f.write(self.raw_content)
757 return rst_to_html(self.raw_content)
761 'number': self.number,
762 'author': sanitize(self.author),
763 'link': sanitize(self.link),
764 'date': self.created.isoformat(' '),
765 'created': self.created.isoformat(' '),
767 'year': self.created.year,
768 'month': self.created.month,
769 'day': self.created.day,
770 'hour': self.created.hour,
771 'minute': self.created.minute,
772 'second': self.created.second,
775 class CommentDB (object):
776 def __init__(self, article):
777 self.path = os.path.join(comments_path, article.uuid)
778 # if comments were enabled after the article was added, we
779 # will need to create the directory
780 if not os.path.exists(self.path):
781 os.mkdir(self.path, 0755)
786 def load(self, article):
788 f = open(os.path.join(self.path, 'db'))
793 # Each line has the following comma separated format:
794 # number, created (epoch)
795 # Empty lines are meaningful and represent removed
796 # comments (so we can preserve the comment number)
800 d = datetime.datetime.fromtimestamp(float(l[1]))
802 # Removed/invalid comment
803 self.comments.append(None)
805 self.comments.append(Comment(article, n, d))
808 old_db = os.path.join(self.path, 'db')
809 new_db = os.path.join(self.path, 'db.tmp')
810 f = open(new_db, 'w')
811 for c in self.comments:
815 s += str(c.number) + ', '
816 s += str(time.mktime(c.created.timetuple()))
820 os.rename(new_db, old_db)
823 class Article (object):
824 def __init__(self, path, created = None, updated = None):
826 self.created = created
827 self.updated = updated
828 self.uuid = "%08x" % zlib.crc32(self.path)
833 self._title = 'Removed post'
834 self._author = author
836 self._raw_content = ''
858 def raw_content(self):
861 return self._raw_content
867 return self._comments
869 def __cmp__(self, other):
870 if self.path == other.path:
874 if not other.created:
876 if self.created < other.created:
880 def title_cmp(self, other):
881 return cmp(self.title, other.title)
884 def add_comment(self, author, raw_content, link = ''):
885 c = Comment(self, len(self.comments))
886 c.set(author, raw_content, link)
887 self.comments.append(c)
892 # XXX this tweak is only needed for old DB format, where
893 # article's paths started with a slash
895 if path.startswith('/'):
897 filename = os.path.join(data_path, path)
899 raw = open(filename).readlines()
906 name, value = l.split(':', 1)
907 if name.lower() == 'title':
908 self._title = value.strip()
909 elif name.lower() == 'author':
910 self._author = value.strip()
911 elif name.lower() == 'tags':
912 ts = value.split(',')
913 ts = [t.strip() for t in ts]
919 self._raw_content = ''.join(raw[count + 1:])
921 self._comments = db.comments
925 return rst_to_html(self.raw_content)
929 'arttitle': sanitize(self.title),
930 'author': sanitize(self.author),
931 'date': self.created.isoformat(' '),
933 'tags': self.get_tags_links(),
934 'comments': len(self.comments),
936 'created': self.created.isoformat(' '),
937 'ciso': self.created.isoformat(),
938 'cyear': self.created.year,
939 'cmonth': self.created.month,
940 'cday': self.created.day,
941 'chour': self.created.hour,
942 'cminute': self.created.minute,
943 'csecond': self.created.second,
945 'updated': self.updated.isoformat(' '),
946 'uiso': self.updated.isoformat(),
947 'uyear': self.updated.year,
948 'umonth': self.updated.month,
949 'uday': self.updated.day,
950 'uhour': self.updated.hour,
951 'uminute': self.updated.minute,
952 'usecond': self.updated.second,
955 def get_tags_links(self):
957 tags = list(self.tags)
960 l.append('<a class="tag" href="%s/tag/%s">%s</a>' % \
961 (blog_url, urllib.quote(t), sanitize(t) ))
965 class ArticleDB (object):
966 def __init__(self, dbpath):
970 self.actyears = set()
971 self.actmonths = set()
975 def get_articles(self, year = 0, month = 0, day = 0, tags = None):
977 for a in self.articles:
978 if year and a.created.year != year: continue
979 if month and a.created.month != month: continue
980 if day and a.created.day != day: continue
981 if tags and not tags.issubset(a.tags): continue
987 def get_article(self, uuid):
988 return self.uuids[uuid]
992 f = open(self.dbpath)
997 # Each line has the following comma separated format:
998 # path (relative to data_path), \
1007 datetime.datetime.fromtimestamp(float(l[1])),
1008 datetime.datetime.fromtimestamp(float(l[2])))
1009 self.uuids[a.uuid] = a
1010 self.acttags.update(a.tags)
1011 self.actyears.add(a.created.year)
1012 self.actmonths.add((a.created.year, a.created.month))
1013 self.articles.append(a)
1016 f = open(self.dbpath + '.tmp', 'w')
1017 for a in self.articles:
1020 s += str(time.mktime(a.created.timetuple())) + ', '
1021 s += str(time.mktime(a.updated.timetuple())) + '\n'
1024 os.rename(self.dbpath + '.tmp', self.dbpath)
1026 def get_year_links(self):
1027 yl = list(self.actyears)
1028 yl.sort(reverse = True)
1029 return [ '<a href="%s/%d/">%d</a>' % (blog_url, y, y)
1032 def get_month_links(self, year):
1033 am = [ i[1] for i in self.actmonths if i[0] == year ]
1035 for i in range(1, 13):
1036 name = calendar.month_name[i][:3]
1038 s = '<a href="%s/%d/%d/">%s</a>' % \
1039 ( blog_url, year, i, name )
1045 def get_tag_links(self):
1046 tl = list(self.acttags)
1048 return [ '<a href="%s/tag/%s">%s</a>' % (blog_url,
1049 sanitize(t), sanitize(t)) for t in tl ]
1055 def render_comments(article, template, form_data):
1056 print '<a name="comments" />'
1057 for c in article.comments:
1060 print template.get_comment_header(c)
1062 print template.get_comment_footer(c)
1064 form_data = CommentFormData()
1065 form_data.action = blog_url + '/comment/' + article.uuid + '#comment'
1066 captcha = captcha_method(article)
1067 print template.get_comment_form(article, form_data, captcha.puzzle)
1069 def render_html(articles, db, actyear = None, show_comments = False,
1070 redirect = None, form_data = None):
1072 print 'Status: 303 See Other\r\n',
1073 print 'Location: %s\r\n' % redirect,
1074 print 'Content-type: text/html; charset=utf-8\r\n',
1076 template = Templates(templates_path, db, actyear)
1077 print template.get_main_header()
1079 print template.get_article_header(a)
1081 print template.get_article_footer(a)
1083 render_comments(a, template, form_data)
1084 print template.get_main_footer()
1086 def render_artlist(articles, db, actyear = None):
1087 template = Templates(templates_path, db, actyear)
1088 print 'Content-type: text/html; charset=utf-8\n'
1089 print template.get_main_header()
1090 print '<h2>Articles</h2>'
1092 print '<li><a href="%(url)s/post/%(uuid)s">%(title)s</a></li>' \
1093 % { 'url': blog_url,
1098 print template.get_main_footer()
1100 def render_atom(articles):
1101 if len(articles) > 0:
1102 updated = articles[0].updated.isoformat()
1104 updated = datetime.datetime.now().isoformat()
1106 print 'Content-type: application/atom+xml; charset=utf-8\n'
1107 print """<?xml version="1.0" encoding="utf-8"?>
1109 <feed xmlns="http://www.w3.org/2005/Atom">
1110 <title>%(title)s</title>
1111 <link rel="alternate" type="text/html" href="%(url)s"/>
1112 <link rel="self" type="application/atom+xml" href="%(url)s/atom"/>
1113 <id>%(url)s</id> <!-- TODO: find a better <id>, see RFC 4151 -->
1114 <updated>%(updated)sZ</updated>
1126 'contents': a.to_html(),
1130 <title>%(arttitle)s</title>
1131 <author><name>%(author)s</name></author>
1132 <link href="%(url)s/post/%(uuid)s" />
1133 <id>%(url)s/post/%(uuid)s</id>
1134 <summary>%(arttitle)s</summary>
1135 <published>%(ciso)sZ</published>
1136 <updated>%(uiso)sZ</updated>
1137 <content type="xhtml">
1138 <div xmlns="http://www.w3.org/1999/xhtml">
1148 print 'Content-type: text/css\r\n\r\n',
1152 import cgitb; cgitb.enable()
1154 form = cgi.FieldStorage()
1155 year = int(form.getfirst("year", 0))
1156 month = int(form.getfirst("month", 0))
1157 day = int(form.getfirst("day", 0))
1158 tags = set(form.getlist("tag"))
1163 post_preview = False
1167 if os.environ.has_key('PATH_INFO'):
1168 path_info = os.environ['PATH_INFO']
1169 style = path_info == '/style'
1170 atom = path_info == '/atom'
1171 tag = path_info.startswith('/tag/')
1172 post = path_info.startswith('/post/')
1173 post_preview = path_info.startswith('/preview/post/')
1174 artlist = path_info.startswith('/list')
1175 comment = path_info.startswith('/comment/') and enable_comments
1176 if not style and not atom and not post and not post_preview \
1177 and not tag and not comment and not artlist:
1178 date = path_info.split('/')[1:]
1180 if len(date) > 1 and date[0]:
1182 if len(date) > 2 and date[1]:
1183 month = int(date[1])
1184 if len(date) > 3 and date[2]:
1189 uuid = path_info.replace('/post/', '')
1190 uuid = uuid.replace('/', '')
1192 art_path = path_info.replace('/preview/post/', '')
1193 art_path = urllib.unquote_plus(art_path)
1194 art_path = os.path.join(data_path, art_path)
1195 art_path = os.path.realpath(art_path)
1196 common = os.path.commonprefix([data_path, art_path])
1197 if common != data_path: # something nasty happened
1198 post_preview = False
1199 art_path = art_path[len(data_path)+1:]
1201 t = path_info.replace('/tag/', '')
1202 t = t.replace('/', '')
1203 t = urllib.unquote_plus(t)
1206 uuid = path_info.replace('/comment/', '')
1207 uuid = uuid.replace('#comment', '')
1208 uuid = uuid.replace('/', '')
1209 author = form.getfirst('comformauthor', '')
1210 link = form.getfirst('comformlink', '')
1211 captcha = form.getfirst('comformcaptcha', '')
1212 body = form.getfirst('comformbody', '')
1214 db = ArticleDB(os.path.join(data_path, 'db'))
1216 articles = db.get_articles(tags = tags)
1217 articles.sort(reverse = True)
1218 render_atom(articles[:10])
1222 render_html( [db.get_article(uuid)], db, year, enable_comments )
1224 article = Article(art_path, datetime.datetime.now(),
1225 datetime.datetime.now())
1226 render_html( [article], db, year, enable_comments )
1228 articles = db.get_articles()
1229 articles.sort(cmp = Article.title_cmp)
1230 render_artlist(articles, db)
1231 elif comment and enable_comments:
1232 form_data = CommentFormData(author.strip().replace('\n', ' '),
1233 link.strip().replace('\n', ' '), captcha,
1234 body.replace('\r', ''))
1235 article = db.get_article(uuid)
1236 captcha = captcha_method(article)
1239 if not form_data.author:
1240 form_data.author_error = 'please, enter your name'
1243 link = valid_link(form_data.link)
1245 form_data.link = link
1247 form_data.link_error = 'please, enter a ' \
1250 if not captcha.validate(form_data):
1251 form_data.captcha_error = captcha.help
1253 if not form_data.body:
1254 form_data.body_error = 'please, write a comment'
1257 error = validate_rst(form_data.body, secure=False)
1258 if error is not None:
1259 (line, desc, ctx) = error
1262 at = ' at line %d' % line
1263 form_data.body_error = 'error%s: %s' \
1267 c = article.add_comment(form_data.author,
1268 form_data.body, form_data.link)
1270 cdb = CommentDB(article)
1271 cdb.comments = article.comments
1273 redirect = blog_url + '/post/' + uuid + '#comment-' \
1275 render_html( [article], db, year, enable_comments, redirect,
1278 articles = db.get_articles(year, month, day, tags)
1279 articles.sort(reverse = True)
1280 if not year and not month and not day and not tags:
1281 articles = articles[:10]
1282 render_html(articles, db, year)
1286 print 'Usage: %s {add|rm|update} article_path' % sys.argv[0]
1289 if len(sys.argv) != 3:
1294 art_path = os.path.realpath(sys.argv[2])
1296 if os.path.commonprefix([data_path, art_path]) != data_path:
1297 print "Error: article (%s) must be inside data_path (%s)" % \
1298 (art_path, data_path)
1300 art_path = art_path[len(data_path)+1:]
1302 db_filename = os.path.join(data_path, 'db')
1303 if not os.path.isfile(db_filename):
1304 open(db_filename, 'w').write('')
1305 db = ArticleDB(db_filename)
1308 article = Article(art_path, datetime.datetime.now(),
1309 datetime.datetime.now())
1310 for a in db.articles:
1312 print 'Error: article already exists'
1314 db.articles.append(article)
1317 comment_dir = os.path.join(comments_path, article.uuid)
1319 os.mkdir(comment_dir, 0775)
1321 if e.errno != errno.EEXIST:
1322 print "Error: can't create comments " \
1323 "directory %s (%s)" \
1325 # otherwise is probably a removed and re-added
1328 article = Article(art_path)
1329 for a in db.articles:
1333 print "Error: no such article"
1336 r = raw_input('Remove comments [y/N]? ')
1337 db.articles.remove(a)
1339 if enable_comments and r.lower() == 'y':
1340 shutil.rmtree(os.path.join(comments_path, a.uuid))
1341 elif cmd == 'update':
1342 article = Article(art_path)
1343 for a in db.articles:
1347 print "Error: no such article"
1349 a.updated = datetime.datetime.now()
1358 if os.environ.has_key('GATEWAY_INTERFACE'):
1359 i = datetime.datetime.now()
1361 f = datetime.datetime.now()
1362 print '<!-- render time: %s -->' % (f-i)
1364 sys.exit(handle_cmd())