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.

87 lines
2.7 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. File.foreach(filename).with_index do | line, line_num |
  28. line_num += 1
  29. # Skip non-markdown table lines and table schema lines
  30. if !line.start_with?('|') || line.eql?("|---|---|---|---|---|\n")
  31. next
  32. end
  33. values = line.split("|")
  34. ################### GLOBAL ###################
  35. values.each.with_index do |val, val_index|
  36. msg = ""
  37. case val_index
  38. when INDEX_TITLE..INDEX_LINK
  39. # every line segment should start and end with exactly 1 space
  40. if val[/\A */].size != 1 || val[/ *\z/].size != 1
  41. add_error(line_num, val_index, "string should start and end with exactly 1 space")
  42. end
  43. end
  44. end
  45. ################# DESCRIPTION ################
  46. # First character should be capitalized
  47. desc_val = values[INDEX_DESCRIPTION].lstrip.chop
  48. if !/[[:upper:]]/.match(desc_val[0])
  49. add_error(line_num, INDEX_DESCRIPTION, "first char not uppercase")
  50. end
  51. # value should not be punctuated
  52. last_char = desc_val[desc_val.length-1]
  53. if punctuation.include?(last_char)
  54. add_error(line_num, INDEX_DESCRIPTION, "description should not end with \"#{last_char}\"")
  55. end
  56. #################### AUTH ####################
  57. # Values should conform to valid options only
  58. auth_val = values[INDEX_AUTH].lstrip.chop.tr('``', '')
  59. if !auth_keys.include?(auth_val)
  60. add_error(line_num, INDEX_AUTH, "not a valid option: #{auth_val}")
  61. end
  62. #################### HTTPS ###################
  63. # Values should be either "Yes" or "No"
  64. https_val = values[INDEX_HTTPS].lstrip.chop
  65. if !https_keys.include?(https_val)
  66. add_error(line_num, INDEX_HTTPS, "must use \"Yes\" or \"No\": #{https_val}")
  67. end
  68. #################### LINK ####################
  69. # Url should be wrapped in "[Go!]" view
  70. link_val = values[INDEX_LINK].lstrip.chop
  71. if !link_val.start_with?("[Go!](") || !link_val.end_with?(')')
  72. add_error(line_num, INDEX_LINK, "format should be \"[Go!](<LINK>)\": #{link_val}")
  73. end
  74. end
  75. $errors.each do | e |
  76. puts e
  77. end
  78. exit($errors.length)