Skip to content
Snippets Groups Projects
titlemd.py 1.44 KiB
Newer Older
  • Learn to ignore specific revisions
  • #!/usr/bin/python
    
    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()
    
          with open(location, 'w') as f:
              # Loop through the list of lines and titlecase
              # any line beginning with '#'.
              prev_line = lines.pop(0)
              disabled = 0
              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:
    
                      line = titlecase(line[:(line.find("]"))], callback=abbreviations)+line[(line.find("]")):] 
    
                  if line.startswith('---') or line.startswith('==='):
    
                      prev_line = titlecase(prev_line[:(prev_line.find("]"))], callback=abbreviations)+prev_line[(prev_line.find("]")):]
    
                  f.write(prev_line)
                  prev_line = line
              f.write(prev_line)
    
    if __name__ == "__main__":
      try:
          main(sys.argv[1])
      except IndexError:
          main('.')