generate_posts.py
1 """Generate blog posts from check docstrings.""" 2 3 from builtins import str 4 from builtins import range 5 import os 6 import ast 7 import datetime 8 9 10 grandparent = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 11 checks_dir = os.path.join(grandparent, "proselint", "checks") 12 listing = os.walk(checks_dir) 13 14 15 def is_check(fn): 16 """Check whether a file contains a check.""" 17 if not fn[-3:] == ".py": 18 return False 19 20 if fn[-11:] == "__init__.py": 21 return False 22 23 if "inprogress" in fn: 24 return False 25 26 return True 27 28 29 for root, subdirs, files in listing: 30 for file in files: 31 fn = os.path.join(root, file) 32 if is_check(fn): 33 M = ast.parse(''.join(open(os.path.join(checks_dir, fn)))) 34 docstring = ast.get_docstring(M) 35 head, sep, tail = docstring.partition("title: ") 36 docstring = head + sep + " :" + tail[4:] 37 38 post_filename = os.path.join( 39 os.path.join(grandparent, "site", "_posts"), 40 str(datetime.date.today()) + "-" + docstring[0:6] + ".md") 41 42 # Chop off the first two lines 43 for i in range(2): 44 docstring = '\n'.join(docstring.split('\n')[1:]) 45 46 # Create a new post in the blog. 47 with open(post_filename, 'w') as f: 48 f.write(docstring.encode('utf8'))