#!/usr/bin/env python
"""
notes.py - Get Whits Notes
Author: Sean B. Palmer, inamidst.com
"""
import sys, os, re, glob
months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'
r_spaces = re.compile(r'(?m)(^ | +)')
r_link = re.compile(r'(?s)\{([^}]+)[ \t\r\n]([A-Za-z]+:[^}]+)\}')
def filenames():
for year in sorted(glob.glob('2*'), reverse=True):
for month in reversed(months.split(' ')):
for fn in sorted(glob.glob(year + '/' + month + '*'), reverse=True):
yield fn
def hypertext(bytes):
bytes = bytes.rstrip('\r\n')
bytes = bytes.replace('\r\n', '\n')
bytes = bytes.replace('\r', '\n')
bytes = bytes.replace('<', '<')
bytes = bytes.replace('>', '>')
bytes = r_link.sub(r'\g<1>', bytes)
bytes = bytes.replace('\n', '
\n')
bytes = bytes.replace('
\n
\n', '
') bytes = r_spaces.sub(lambda m: ' ' * len(m.group(1)), bytes) return bytes class Note: def __init__(note, fn): note.path = '/'.join(fn.split('/')[-2:]) note.bytes = open(fn, 'rb').read() note.content = hypertext(note.bytes) year, month, day = note.path[:4], num[note.path[5:8]], note.path[8:10] hour, minute, second = note.path[11:13], note.path[13:15], '00' note.updated = (year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':' + second + 'Z') note.cite = (ordinal(day) + ' ' + note.path[5:8] + ' ' + year + ' ' + hour + ':' + minute) num = { 'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12' } def ordinal(num): num = num.lstrip('0') if num.endswith('1') and (num != '11'): return num + 'st' elif num.endswith('2') and (num != '12'): return num + 'nd' elif num.endswith('3') and (num != '13'): return num + 'rd' else: return num + 'th' def notes(): for i, fn in enumerate(filenames()): if i >= 15: break note = Note(fn) yield note if __name__ == '__main__': main()