You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

  1. #!/usr/bin/env ruby
  2. auth_keys = ['apiKey', 'OAuth', 'X-Mashape-Key', 'No']
  3. punctuation = ['.', '?', '!']
  4. https_keys = ['Yes', 'No']
  5. INDEX_TITLE = 1
  6. INDEX_DESCRIPTION = 2
  7. INDEX_AUTH = 3
  8. INDEX_HTTPS = 4
  9. INDEX_LINK = 5
  10. filename = ARGV[0]
  11. $errors = []
  12. def add_error(line_num, val_index, message)
  13. case val_index
  14. when INDEX_TITLE
  15. segment = "Title"
  16. when INDEX_DESCRIPTION
  17. segment = "Description"
  18. when INDEX_AUTH
  19. segment = "Auth"
  20. when INDEX_HTTPS
  21. segment = "HTTPS"
  22. when INDEX_LINK
  23. segment = "Link"
  24. end
  25. $errors.push("(L%03d) %-14.14s #{message}" % [line_num, segment])
  26. end
  27. section = ''
  28. sections = []
  29. section_to_line_num = {}
  30. section_to_entries = Hash.new {|h,k| h[k] = Array.new }
  31. File.foreach(filename).with_index do | line, line_num |
  32. if line.start_with?('###')
  33. section = line.sub('###', '').lstrip.chop
  34. sections.push(section)
  35. section_to_line_num[section] = line_num + 1
  36. end
  37. # Skip non-markdown table lines and table schema lines
  38. if !line.start_with?('|') || line.eql?("|---|---|---|---|---|\n")
  39. next
  40. end
  41. # char to check is the first char in the first column
  42. check_char = line.split("|")[1].strip[0]
  43. section_to_entries[section].push(check_char)
  44. end
  45. sections.each do | sect |
  46. if section_to_entries[sect] != section_to_entries[sect].sort
  47. add_error(section_to_line_num[sect], INDEX_TITLE, "#{sect} section is not in alphabetical order")
  48. end
  49. end
  50. File.foreach(filename).with_index do | line, line_num |
  51. line_num += 1
  52. # Skip non-markdown table lines and table schema lines
  53. if !line.start_with?('|') || line.eql?("|---|---|---|---|---|\n")
  54. next
  55. end
  56. values = line.split("|")
  57. ################### GLOBAL ###################
  58. values.each.with_index do |val, val_index|
  59. msg = ""
  60. case val_index
  61. when INDEX_TITLE..INDEX_LINK
  62. # every line segment should start and end with exactly 1 space
  63. if val[/\A */].size != 1 || val[/ *\z/].size != 1
  64. add_error(line_num, val_index, "string should start and end with exactly 1 space")
  65. end
  66. end
  67. end
  68. ################# DESCRIPTION ################
  69. # First character should be capitalized
  70. desc_val = values[INDEX_DESCRIPTION].lstrip.chop
  71. if !/[[:upper:]]/.match(desc_val[0])
  72. add_error(line_num, INDEX_DESCRIPTION, "first char not uppercase")
  73. end
  74. # value should not be punctuated
  75. last_char = desc_val[desc_val.length-1]
  76. if punctuation.include?(last_char)
  77. add_error(line_num, INDEX_DESCRIPTION, "description should not end with \"#{last_char}\"")
  78. end
  79. #################### AUTH ####################
  80. # Values should conform to valid options only
  81. auth_val = values[INDEX_AUTH].lstrip.chop.tr('``', '')
  82. if !auth_keys.include?(auth_val)
  83. add_error(line_num, INDEX_AUTH, "not a valid option: #{auth_val}")
  84. end
  85. #################### HTTPS ###################
  86. # Values should be either "Yes" or "No"
  87. https_val = values[INDEX_HTTPS].lstrip.chop
  88. if !https_keys.include?(https_val)
  89. add_error(line_num, INDEX_HTTPS, "must use \"Yes\" or \"No\": #{https_val}")
  90. end
  91. #################### LINK ####################
  92. # Url should be wrapped in "[Go!]" view
  93. link_val = values[INDEX_LINK].lstrip.chop
  94. if !link_val.start_with?("[Go!](") || !link_val.end_with?(')')
  95. add_error(line_num, INDEX_LINK, "format should be \"[Go!](<LINK>)\": #{link_val}")
  96. end
  97. end
  98. $errors.each do | e |
  99. puts e
  100. end
  101. exit($errors.length)