#!/usr/bin/env python """ File: githublog.py Hugo assistance to generate a special README file, including all posts title inside as a TOC. This will make blog repo to be a blog inplace. """ import os import string import subprocess from posixpath import join as urljoin POSTS_ROOT = 'content' EXTS = ['.md', '.rst'] README = 'README.md' GITREPO = 'http://git.wanbits.io/joe/blog' def get_git_remote(): proc = subprocess.Popen(['git', 'remote', '-v'], stdout=subprocess.PIPE, shell=True) (out, err) = proc.communicate() if err: return None lines = out.decode('utf-8').split('\n') if len(lines) < 2: return None parts = lines[1].split('\t') if len(parts) < 2: return None values = parts[1].split(' ') return None if len(values) < 1 else values[0] def get_category(dir): ''' ''' parts = dir.split(os.sep) return os.sep.join(parts[1:]) remote = get_git_remote() if not GITREPO else GITREPO if not remote: print('not a git repo, quit') exit(1) posts = {} # find out all non-draft posts for r, dl, fl in os.walk(POSTS_ROOT): if fl: cate = get_category(r) posts[cate] = [] for f in fl: _, ext = os.path.splitext(f) if ext.lower() not in EXTS: continue fullname = os.path.join(r, f) comment_started = False metas = {} with open(fullname, 'r', encoding='utf-8') as h: line = h.readline().strip() while line: if line == '---': if not comment_started: comment_started = True else: break else: parts = line.split(': ') if len(parts) != 2: break metas[parts[0].strip().lower()] = parts[1].strip().lower() metas['file'] = f line = h.readline().strip() if metas.get('draft') == 'false': posts[cate].append(metas) # generate README.md with open(README, 'wt', encoding='utf-8') as h: h.write('') for k, v in posts.items(): h.write('## {}\n\n'.format(k)) v = sorted(v, key=lambda metas: metas.get('date'), reverse=True) for metas in v: h.write('- [{}]({})\n'.format(metas.get('title'), urljoin(remote, 'src/master', POSTS_ROOT, metas.get('file')))) h.write('\n') print('done.')