URI: 
       sta-log-gopher-index.py - stahg-gopher - Static Mercurial page generator for gopher
  HTML hg clone https://bitbucket.org/iamleot/stahg-gopher
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       sta-log-gopher-index.py
       ---
            1 #!/usr/bin/env python3.7
            2 
            3 
            4 import re
            5 import os
            6 
            7 
            8 def gph_escape_entry(text):
            9     """Render text entry `[...]' by escaping/translating characters"""
           10     escaped_text = text.expandtabs().replace('|', '\|')
           11 
           12     return escaped_text
           13 
           14 
           15 def shorten(text, n=80):
           16     """Shorten text to the first `n' character of first line"""
           17     s, _, _ = text.partition('\n')
           18 
           19     if len(s) > n:
           20         s = s[:n - 1] + '…'
           21 
           22     return s
           23 
           24 if __name__ == '__main__':
           25     import getopt
           26     import sys
           27 
           28     def usage():
           29         print('usage: {} [-b baseprefix] stagit-dir|stahg-dir ...'.format(
           30                sys.argv[0]))
           31         exit(1)
           32 
           33     try:
           34         opts, args = getopt.getopt(sys.argv[1:], 'b:')
           35     except:
           36         usage()
           37 
           38     if len(args) == 0:
           39         usage()
           40 
           41     base_prefix = ''
           42     limit = None
           43     for o, a in opts:
           44         if o == '-b':
           45             base_prefix = a
           46 
           47     print('{:20}  {:40}  {}'.format('Name', 'Description', 'Last commit'))
           48     for sd in args:
           49         repo = description = date = ''
           50         repo = os.path.basename(os.path.normpath(sd))
           51         with open(os.path.join(sd, 'log.gph')) as f:
           52             for l in f:
           53                 if not description and 'Log - ' in l:
           54                     description = l.split(' - ', maxsplit=2)[-1]
           55                     description = description.strip()
           56                 # XXX: simplify that
           57                 elif re.match('^\[1\|[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+', l):
           58                     r = re.match('^\[1\|([0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+)', l)
           59                     date = r.group(1)
           60                     f.close()
           61                     break
           62 
           63         if repo and description and date:
           64             print('[1|{desc}|{path}|server|port]'.format(
           65                 desc=gph_escape_entry(
           66                     '{repo:20}  {description:40}  {date}'.format(
           67                         repo=repo,
           68                         description=shorten(description, 40),
           69                         date=date)),
           70                 path='{base_path}{repo}'.format(
           71                     base_path=base_prefix,
           72                     repo=repo)))