githublog.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. """
  3. File: githublog.py
  4. This is a quick&dirty tool helping you generating a TOC file for blog repo based on Hugo.
  5. Usage:
  6. python githublog.py
  7. IMPORTANT
  8. Before starting, you should check that the working-directory is right at the root of
  9. the hugo blog and managed by git (is preferred)
  10. """
  11. import os
  12. import string
  13. import subprocess
  14. from posixpath import join as urljoin
  15. POSTS_ROOT = 'content' # hugo root dir of posts.
  16. EXTS = ['.md', '.rst'] # post file extensions. (should be in lowercase)
  17. README = 'README.md' # filename to be generated.
  18. GITREPO = 'http://git.wanbits.io/joe/blog' # repo address of blog code.
  19. BRANCH = 'master' # publish branch
  20. def get_git_remote():
  21. """ Get git remote url
  22. should be executed in git directory and with git installed.
  23. """
  24. proc = subprocess.Popen(['git', 'remote', '-v'], stdout=subprocess.PIPE, shell=True)
  25. (out, err) = proc.communicate()
  26. if err:
  27. return None
  28. lines = out.decode('utf-8').split('\n')
  29. if len(lines) < 2:
  30. return None
  31. parts = lines[1].split('\t')
  32. if len(parts) < 2:
  33. return None
  34. values = parts[1].split(' ')
  35. return None if len(values) < 1 else values[0]
  36. def get_git_branch():
  37. """ Get current git branch """
  38. pass
  39. def get_category(dir):
  40. '''
  41. Get blog category by dir(ectory)
  42. '''
  43. parts = dir.split(os.sep)
  44. return os.sep.join(parts[1:])
  45. # repo remote
  46. remote = get_git_remote() if not GITREPO else GITREPO
  47. if not remote:
  48. print('not a git repo, quit')
  49. exit(1)
  50. # current git branch
  51. branch = get_git_branch() if not BRANCH else BRANCH
  52. if not branch:
  53. print('no branch?')
  54. exit(1)
  55. # all posts, searching result
  56. # map[str][list]
  57. posts = {}
  58. ## find out all non-draft posts
  59. for r, dl, fl in os.walk(POSTS_ROOT):
  60. if fl:
  61. cate = get_category(r)
  62. posts[cate] = []
  63. for f in fl:
  64. _, ext = os.path.splitext(f)
  65. if ext.lower() not in EXTS:
  66. continue
  67. fullname = os.path.join(r, f)
  68. meta_started = False
  69. metas = {} # hugo post meta-info map[str]str
  70. with open(fullname, 'r', encoding='utf-8') as h:
  71. line = h.readline().strip()
  72. while line:
  73. if line == '---':
  74. if not meta_started:
  75. meta_started = True
  76. else:
  77. break # from while
  78. else:
  79. parts = line.split(': ')
  80. if len(parts) != 2:
  81. break # from while
  82. metas[parts[0].strip().lower()] = parts[1].strip().lower()
  83. line = h.readline().strip()
  84. if metas.get('draft') == 'false':
  85. metas['file'] = f
  86. posts[cate].append(metas)
  87. ## generate README.md
  88. with open(README, 'wt', encoding='utf-8') as h:
  89. for cate, metalist in posts.items():
  90. h.write('## {}\n\n'.format(cate))
  91. # sortint posts by published date.
  92. metalist = sorted(metalist, key=lambda metas: metas.get('date'), reverse=True)
  93. for metas in metalist:
  94. h.write('- [{}]({})\n'.format(metas.get('title').strip('"'), urljoin(remote, 'src', branch, POSTS_ROOT, cate, metas.get('file'))))
  95. h.write('\n')
  96. print('done.')