docs/test2rst.py
changeset 235 8469ccb9550f
child 525 a0327c78a5d3
equal deleted inserted replaced
234:d32c07269dcd 235:8469ccb9550f
       
     1 #!/usr/bin/env python
       
     2 
       
     3 import os, os.path as op, re, sys
       
     4 
       
     5 # line starts with two chars one of which is not a space (and both are not
       
     6 # newlines obviously) and ends with one or more newlines followed by two spaces
       
     7 # on a next line (indented text)
       
     8 CODEBLOCK = re.compile(r'()\n(([^ \n][^\n]|[^\n][^ \n])[^\n]*)\n+  ')
       
     9 
       
    10 INDEX = '''
       
    11 Mercurial tests
       
    12 ===============
       
    13 
       
    14 .. toctree::
       
    15    :maxdepth: 1
       
    16 '''
       
    17 
       
    18 
       
    19 def rstify(orig, name):
       
    20     header = '%s\n%s\n\n' % (name, '=' * len(name))
       
    21     content = header + orig
       
    22     content = CODEBLOCK.sub(r'\n\1\n\n::\n\n  ', content)
       
    23     return content
       
    24 
       
    25 
       
    26 def main(base):
       
    27     if os.path.isdir(base):
       
    28         one_dir(base)
       
    29     else:
       
    30         print one_file(base)
       
    31 
       
    32 
       
    33 def one_dir(base):
       
    34     index = INDEX
       
    35     #doc = lambda x: op.join(op.dirname(__file__), 'docs', x)
       
    36 
       
    37     for fn in sorted(os.listdir(base)):
       
    38         if not fn.endswith('.t'):
       
    39             continue
       
    40         print fn
       
    41         name = os.path.splitext(fn)[0]
       
    42         content = one_file(op.join(base, fn))
       
    43         target = op.join(base, name + '.rst')
       
    44         #with file(doc(name + '.rst'), 'w') as f:
       
    45         with file(target, 'w') as f:
       
    46             f.write(content)
       
    47         print f
       
    48 
       
    49         index += '\n   ' + name
       
    50 
       
    51     #with file(doc('index.rst'), 'w') as f:
       
    52     #    f.write(index)
       
    53 
       
    54 
       
    55 def one_file(path):
       
    56     name = os.path.basename(path)[:-2]
       
    57     return rstify(file(path).read(), name)
       
    58 
       
    59 
       
    60 if __name__ == '__main__':
       
    61     if len(sys.argv) != 2:
       
    62         print 'Please supply a path to tests dir as parameter'
       
    63         sys.exit()
       
    64     main(sys.argv[1])