Skip to content

Instantly share code, notes, and snippets.

@kshimo69
Created August 18, 2009 07:21
Show Gist options
  • Save kshimo69/169590 to your computer and use it in GitHub Desktop.
Save kshimo69/169590 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf-8
"""Convert howm formated document to ChangeLog formated document.
"""
import re
import sys
import os
import glob
import calendar
import datetime
class HowmEntry:
re_date = re.compile(r'\d{4}-\d{2}-\d{2}')
re_title = re.compile(r'^=\s*(.+)$')
re_tag = re.compile(r'^#tags\s+(?:(.+),?)+$')
def __init__(self, filename):
self._parse_entry(filename)
#print self.lines
def _parse_entry(self, filename):
self.filename = filename
self.date = self._get_date()
self._read_file()
self._do_parse()
#self.title = self._get_title()
#self.tags = self._get_tags()
#self.tag = self._get_tag_str()
#self.body = self._get_body_str()
#print "TITLE: %s%s" % (self.tag,self.title)
#print "BODY:\n%s" % self.body
def _get_date(self):
date = self.re_date.search(self.filename)
if date:
return date.group()
def _read_file(self):
f = open(self.filename)
self.lines = f.readlines()
f.close()
def _get_title(self):
self.title = ''
title = self.re_title.match(self.lines[0])
if title:
self.lines.pop(0)
self.title = title.group(1)
def _do_parse(self):
self._get_title()
self.tags = []
self.bodys = []
i = 0
for line in self.lines:
tags = self.re_tag.match(line)
if tags:
#print tags.group()
#print tags.group(1).split(',')
self.lines.pop(i)
self.tags = tags.group(1).split(',')
i += 1
self._get_tag_str()
self.title = self.tag + self.title
self.bodys = self.lines
self._get_body_str()
def _get_tag_str(self):
self.tag = ''
if self.tags:
tag = ''
for t in self.tags:
tag += "[%s]" % t
self.tag = tag
def _get_body_str(self):
body = ''
for b in markdown_to_hatena(self.bodys):
body += " " + b
self.body = body
class ChangeLog:
def __init__(self):
self.changelogfile = open('ChangeLog.txt', 'w')
self.entries = {}
def add_entry(self, date, title, body):
if not self.entries.has_key(date):
self.entries[date] = []
self.entries[date].append({'title':title,'body':body})
def flash(self):
buf = ''
dates = self.entries.keys()
dates.sort()
dates.reverse()
for date in dates:
buf += "%s (%s) Kimihiko Shimomura <kshimo69@gmail.com>\n\n" % (date, get_day_name(date))
for entry in self.entries[date]:
buf += " * %s:\n" % entry['title']
buf += "%s" % entry['body']
buf += " \n"
self.changelogfile.write(buf)
def close(self):
self.changelogfile.close()
def get_day_name(date):
weeklist = calendar.weekheader(3).split(' ')
d = datetime.datetime.strptime(date, '%Y-%m-%d')
return weeklist[calendar.weekday(d.year,d.month,d.day)]
def markdown_to_hatena(list):
body = []
re_markdown_link = re.compile(r'\[(.*)\]\((.*)\)')
re_code_str = re.compile(r'^\s{4}(.*\n)')
re_block_str = re.compile(r'^>\s?(.*\n)')
f_in_code = None
f_in_block = None
for line in list:
line = re_markdown_link.sub(r"[\2:title=\1]", line)
if f_in_code:
code_str = re_code_str.match(line)
if code_str:
body.append(code_str.group(1))
else:
body.append('||<\n')
body.append(line)
f_in_code = False
else:
code_str = re_code_str.match(line)
if code_str:
body.append('>||\n')
body.append(code_str.group(1))
f_in_code = True
elif f_in_block:
block_str = re_block_str.match(line)
if block_str:
body.append(block_str.group(1))
else:
body.append('<<\n')
body.append(line)
f_in_block = False
else:
block_str = re_block_str.match(line)
if block_str:
body.append('>>\n')
body.append(block_str.group(1))
f_in_block = True
else:
body.append(line)
return body
if __name__ == '__main__':
try:
howm_dir = sys.argv[1]
except:
print "Usage: python " + sys.argv[0] + " 'your howm files directory'"
exit(1)
changelog = ChangeLog()
howmfiles = sorted(glob.glob(howm_dir + '/*.howm'))
for howmfile in howmfiles:
howm = HowmEntry(howmfile)
changelog.add_entry(howm.date, howm.title, howm.body)
changelog.flash()
changelog.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment