Skip to content
Snippets Groups Projects
titlemd_test.py 2.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • #!/usr/bin/env python
    from __future__ import print_function
    
    import fnmatch
    import os
    import sys
    
    try:
      from titlecase import titlecase
    except ImportError:
      print("Please install titlecase")
    
    def main(location):
    
          # Spelling exceptions
          with open('.spelling') as f:
              spelling = f.readlines()
    
          def abbreviations(word, **kwargs):
              if word+"\n" in spelling:
                  return word
    
    
          # Open the file and read the lines as a list
          with open(location) as f:
              lines = f.readlines()
    
          # Loop through the list of lines and titlecase
          # any line beginning with '#'.
          return_value = 0
          prev_line = lines.pop(0)
          disabled = 0
    
    David Hrbáč's avatar
    David Hrbáč committed
          echo_filename = False
    
          for line in lines:
              if line.startswith("``") and disabled == 0:
                  disabled = 1
              else:
                  if line.startswith("``") and disabled == 1:
                      disabled = 0
              if line.startswith('#') and disabled == 0:
    
                if line != titlecase(line[:(line.find("]"))], callback=abbreviations)+line[(line.find("]")):]:
    
    David Hrbáč's avatar
    David Hrbáč committed
                  if return_value == 0 and echo_filename == False:
                    print("%s" % location)
                    echo_filename = True
    
                  print("-"+line,end="") 
    
    David Hrbáč's avatar
    David Hrbáč committed
                  print("+"+titlecase(line[:(line.find("]"))], callback=abbreviations)+line[(line.find("]")):],end="")
                  print()
    
                  return_value = 1
    
              if (line.startswith('---') or line.startswith('===')) and disabled == 0:
    
                if prev_line != titlecase(prev_line[:(prev_line.find("]"))], callback=abbreviations)+prev_line[(prev_line.find("]")):]:
    
    David Hrbáč's avatar
    David Hrbáč committed
                  if return_value == 0 and echo_filename == False:
                    print("%s" % location)
                    echo_filename = True
    
                  print("-"+prev_line,end="")
    
    David Hrbáč's avatar
    David Hrbáč committed
                  print("+"+titlecase(prev_line[:(prev_line.find("]"))], callback=abbreviations)+prev_line[(prev_line.find("]")):],end="")
                  print()
    
                  return_value = 1
              prev_line = line
          exit(return_value)
    if __name__ == "__main__":
      try:
          main(sys.argv[1])
      except IndexError:
          main('.')