yll 4 years ago
parent
commit
40cb42d27f
2 changed files with 96 additions and 0 deletions
  1. 8 0
      README.md
  2. 88 0
      githublog.py

+ 8 - 0
README.md

@@ -0,0 +1,8 @@
+## posts
+
+- ["mustache 模板引擎中文文档"](http://git.wanbits.io/joe/blog\content\mustache.md)
+- ["好网站 一辈子"](http://git.wanbits.io/joe/blog\content\best-sites.md)
+- ["mysql commands"](http://git.wanbits.io/joe/blog\content\daily-mysql.md)
+- ["环境变量配置"](http://git.wanbits.io/joe/blog\content\env.md)
+- ["git in real world"](http://git.wanbits.io/joe/blog\content\git-in-real-world.md)
+

+ 88 - 0
githublog.py

@@ -0,0 +1,88 @@
+#!/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
+
+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'), os.path.join(remote, POSTS_ROOT, metas.get('file'))))
+        h.write('\n')
+print('done.')