titlemd_test.py 2.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/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):
14 15 16 17 18 19 20 21
      # Spelling exceptions
      with open('.spelling') as f:
          spelling = f.readlines()

      def abbreviations(word, **kwargs):
          if word+"\n" in spelling:
              return word

22 23 24 25 26 27 28 29 30
      # 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
31
      echo_filename = False
Lubomir Prda's avatar
Lubomir Prda committed
32 33
      if location.find("mkdocs.yml") != -1:
          disabled = 1
34
      for line in lines:
Lubomir Prda's avatar
Lubomir Prda committed
35
          if (line.startswith("``") or line.startswith("extra:")) and disabled == 0:
36 37
              disabled = 1
          else:
Lubomir Prda's avatar
Lubomir Prda committed
38
              if (line.startswith("``") or prev_line.startswith("pages:")) and disabled == 1:
39
                  disabled = 0
Lubomir Prda's avatar
Lubomir Prda committed
40
          if line.startswith('#') and disabled == 0 and (location.find("mkdocs.yml") == -1):
41
            if line != titlecase(line[:(line.find("]"))], callback=abbreviations)+line[(line.find("]")):]:
David Hrbáč's avatar
David Hrbáč committed
42 43 44
              if return_value == 0 and echo_filename == False:
                print("%s" % location)
                echo_filename = True
45
              print("-"+line,end="") 
David Hrbáč's avatar
David Hrbáč committed
46 47
              print("+"+titlecase(line[:(line.find("]"))], callback=abbreviations)+line[(line.find("]")):],end="")
              print()
48
              return_value = 1
Lubomir Prda's avatar
Lubomir Prda committed
49
          if (line.startswith('---') or line.startswith('===')) and disabled == 0:
50
            if prev_line != titlecase(prev_line[:(prev_line.find("]"))], callback=abbreviations)+prev_line[(prev_line.find("]")):]:
David Hrbáč's avatar
David Hrbáč committed
51 52 53
              if return_value == 0 and echo_filename == False:
                print("%s" % location)
                echo_filename = True
54
              print("-"+prev_line,end="")
David Hrbáč's avatar
David Hrbáč committed
55 56
              print("+"+titlecase(prev_line[:(prev_line.find("]"))], callback=abbreviations)+prev_line[(prev_line.find("]")):],end="")
              print()
57
              return_value = 1
Lubomir Prda's avatar
Lubomir Prda committed
58 59 60 61 62 63 64 65 66
          if ((location.find("mkdocs.yml") != -1) and not line.startswith('#') and disabled == 0):
            if line != titlecase(line[:(line.find(":"))], callback=abbreviations)+line[(line.find(":")):]:
              if return_value == 0 and echo_filename == False:
                print("%s" % location)
                echo_filename = True
              print("-"+line,end="")
              print("+"+titlecase(line[:(line.find(":"))], callback=abbreviations)+line[(line.find(":")):],end="")
              print()
              return_value = 1
67 68 69 70 71 72 73
          prev_line = line
      exit(return_value)
if __name__ == "__main__":
  try:
      main(sys.argv[1])
  except IndexError:
      main('.')