| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #!/usr/bin/env python
- """
- File: githublog.py
- This is a quick&dirty tool helping you generating a TOC file for blog repo based on Hugo.
- Usage:
- python githublog.py
- IMPORTANT
- Before starting, you should check that the working-directory is right at the root of
- the hugo blog and managed by git (is preferred)
- """
- import os
- import string
- import subprocess
- from posixpath import join as urljoin
- INTRO_NAME = '简介'
- INTRO_DESC = '''本站所有原创、翻译、引用都会随着时间进行更新'''
- POSTS_ROOT = 'content' # hugo root dir of posts.
- EXTS = ['.md', '.rst'] # post file extensions. (should be in lowercase)
- README = 'README.md' # filename to be generated.
- GITREPO = 'https://git.wenlabs.org/joe/blog' # repo address of blog code.
- BRANCH = 'master' # publish branch
- def get_git_remote():
- """ Get git remote url
- should be executed in git directory and with git installed.
- """
- 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_git_branch():
- """ Get current git branch """
- pass
- def get_category(dir):
- '''
- Get blog category by directory strucure
- '''
- parts = dir.split(os.sep)
- return os.sep.join(parts[1:])
- # repo remote
- remote = get_git_remote() if not GITREPO else GITREPO
- if not remote:
- print('not a git repo, quit')
- exit(1)
- # current git branch
- branch = get_git_branch() if not BRANCH else BRANCH
- if not branch:
- print('no branch?')
- exit(1)
- # all posts, searching result
- # map[str][list]
- 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)
- meta_started = False
- metas = {} # hugo post meta-info map[str]str
- with open(fullname, 'r', encoding='utf-8') as h:
- line = h.readline().strip()
- while line:
- if line == '---':
- if not meta_started:
- meta_started = True
- else:
- break # from while
- else:
- parts = line.split(': ')
- if len(parts) != 2:
- break # from while
- metas[parts[0].strip().lower()] = parts[1].strip().lower()
- line = h.readline().strip()
- if metas.get('draft') == 'false':
- metas['file'] = f
- posts[cate].append(metas)
- ## generate README.md
- with open(README, 'wt', encoding='utf-8') as h:
- h.write(f'## {INTRO_NAME}\n\n')
- h.write(f'{INTRO_DESC}\n\n')
- for cate, metalist in posts.items():
- h.write('## {}\n\n'.format(cate))
- # sortint posts by published date.
- metalist = sorted(metalist, key=lambda metas: metas.get('date'), reverse=True)
- for metas in metalist:
- h.write('- [{}]({})\n'.format(metas.get('title').strip('"'), urljoin(remote, 'src', branch, POSTS_ROOT, cate, metas.get('file'))))
- h.write('\n')
- print('done.')
|