Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

validate_format.py 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  2. import json
  3. import string
  4. import sys
  5. anchor = '###'
  6. auth_keys = ['apiKey', 'OAuth', 'X-Mashape-Key', 'No']
  7. punctuation = ['.', '?', '!']
  8. https_keys = ['Yes', 'No']
  9. index_title = 0
  10. index_desc = 1
  11. index_auth = 2
  12. index_https = 3
  13. index_link = 4
  14. errors = []
  15. def add_error(line_num, message):
  16. """adds an error to the dynamic error list"""
  17. err = '(L{:03d}) {}'.format(line_num+1, message)
  18. errors.append(err)
  19. def check_format(filename):
  20. """
  21. validates that each line is formatted correctly,
  22. appending to error list as needed
  23. """
  24. with open(filename) as fp:
  25. lines = list(line.rstrip() for line in fp)
  26. # START Alphabetical Order
  27. category = ""
  28. sections = {}
  29. section_line_num = {}
  30. for line_num, line in enumerate(lines):
  31. if line.startswith(anchor):
  32. category = line.split(anchor)[1].strip()
  33. sections[category] = []
  34. section_line_num[category] = line_num
  35. continue
  36. if not line.startswith('|') or line.startswith('|---'):
  37. continue
  38. title = [x.strip() for x in line.split('|')[1:-1]][0].upper()
  39. sections[category].append(title)
  40. for category, entries in sections.items():
  41. if sorted(entries) != entries:
  42. add_error(section_line_num[category], '{} section is not in alphabetical order'.format(category))
  43. # END Alphabetical Order
  44. # START Check Entries
  45. for line_num, line in enumerate(lines):
  46. if not line.startswith('|') or line.startswith('|---'):
  47. continue
  48. segments = line.split('|')[1:-1]
  49. # START Global
  50. for segment in segments:
  51. # every line segment should start and end with exactly 1 space
  52. if len(segment) - len(segment.lstrip()) != 1 or len(segment) - len(segment.rstrip()) != 1:
  53. add_error(line_num, "each segment must start and end with exactly 1 space")
  54. # END Global
  55. segments = [seg.strip() for seg in segments]
  56. # START Description
  57. # first character should be capitalized
  58. char = segments[index_desc][0]
  59. if char.upper() != char:
  60. add_error(line_num, "first char of Description is not capitalized")
  61. # last character should not punctuation
  62. char = segments[index_desc][-1]
  63. if char in punctuation:
  64. add_error(line_num, "description should not end with {}".format(char))
  65. # END Description
  66. # START Auth
  67. # values should conform to valid options only
  68. auth = segments[index_auth].replace('`', '')
  69. if auth not in auth_keys:
  70. add_error(line_num, "{} is not a valid Auth option".format(auth))
  71. # END Auth
  72. # START HTTPS
  73. # values should conform to valid options only
  74. https = segments[index_https]
  75. if https not in https_keys:
  76. add_error(line_num, "{} is not a valid HTTPS option".format(https))
  77. # END HTTPS
  78. # START Link
  79. # url should be wrapped in '[Go!]()' Markdown syntax
  80. link = segments[index_link]
  81. if not link.startswith('[Go!](http') or not link.endswith(')'):
  82. add_error(line_num, 'link format should be "[Go!](LINK)"')
  83. # END Link
  84. # END Check Entries
  85. def main():
  86. num_args = len(sys.argv)
  87. if num_args < 2:
  88. print("No .md file passed")
  89. sys.exit(1)
  90. check_format(sys.argv[1])
  91. if len(errors) > 0:
  92. for err in errors:
  93. print(err)
  94. sys.exit(1)
  95. if __name__ == "__main__":
  96. main()