clean.rst.file.py

Kelley-Hoskins Nathan, 06/30/2015 12:41 PM

Download (1.02 KB)

 
1
#!/usr/bin/python
2
# Nathan Kelley-Hoskins
3
# 2015Jun30
4
import sys, re
5

    
6
if len(sys.argv) != 3 :
7
  print """
8
Clean up input_filename RST file for easier human readability, and write to output_filename.'
9

10
usage: $ %s input_filename output_filename' % sys.argv[0]
11
"""
12
  sys.exit(1)
13
  
14
inputfile  = sys.argv[1]
15
outputfile = sys.argv[2]
16

    
17
with open( outputfile, 'w') as fout :
18
  with open( inputfile, 'rU') as fin : # the 'U' is for reading newlines (\n) or carriage returns (\r or ^M)
19
    for line in fin :
20
      repline = line
21
      
22
      # don't write line to output if its not needed
23
      if re.search('^.. _', repline ) : continue
24
      
25
      if re.search('\:ref\:`', repline ) : 
26
        
27
        # get rid of reference notation
28
        repline = repline.replace( ':ref:', '' ) 
29
        repline = repline.replace( '`'    , '' )
30

    
31
      repline = repline.replace( '``'       , ''             ) # remove ``
32
      repline = repline.replace( '.. note::', 'Note\n----\n' ) # replace note tag with cleaner Note title
33
      
34
      fout.write( repline )
35