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.

112 lines
3.6 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. ################### CHECK ALPHABETICAL ORDER ###################
  28. section = ''
  29. sections = []
  30. section_to_line_num = {}
  31. section_to_entries = Hash.new {|h,k| h[k] = Array.new }
  32. File.foreach(filename).with_index do | line, line_num |
  33. if line.start_with?('###')
  34. section = line.sub('###', '').lstrip.chop
  35. sections.push(section)
  36. section_to_line_num[section] = line_num + 1
  37. end
  38. # Skip non-markdown table lines and table schema lines
  39. if !line.start_with?('|') || line.eql?("|---|---|---|---|---|\n")
  40. next
  41. end
  42. # char to check is the first column
  43. check_char = line.split("|")[1].strip.upcase
  44. section_to_entries[section].push(check_char)
  45. end
  46. sections.each do | sect |
  47. if section_to_entries[sect] != section_to_entries[sect].sort
  48. add_error(section_to_line_num[sect], INDEX_TITLE, "#{sect} section is not in alphabetical order")
  49. end
  50. end
  51. #################### CHECK LINE ENTRIES ########################
  52. File.foreach(filename).with_index do | line, line_num |
  53. line_num += 1
  54. # Skip non-markdown table lines and table schema lines
  55. if !line.start_with?('|') || line.eql?("|---|---|---|---|---|\n")
  56. next
  57. end
  58. values = line.split("|")
  59. ################### GLOBAL ###################
  60. values.each.with_index do |val, val_index|
  61. msg = ""
  62. case val_index
  63. when INDEX_TITLE..INDEX_LINK
  64. # every line segment should start and end with exactly 1 space
  65. if val[/\A */].size != 1 || val[/ *\z/].size != 1
  66. add_error(line_num, val_index, "string should start and end with exactly 1 space")
  67. end
  68. end
  69. end
  70. ################# DESCRIPTION ################
  71. # First character should be capitalized
  72. desc_val = values[INDEX_DESCRIPTION].lstrip.chop
  73. if !/[[:upper:]]/.match(desc_val[0])
  74. add_error(line_num, INDEX_DESCRIPTION, "first char not uppercase")
  75. end
  76. # value should not be punctuated
  77. last_char = desc_val[desc_val.length-1]
  78. if punctuation.include?(last_char)
  79. add_error(line_num, INDEX_DESCRIPTION, "description should not end with \"#{last_char}\"")
  80. end
  81. #################### AUTH ####################
  82. # Values should conform to valid options only
  83. auth_val = values[INDEX_AUTH].lstrip.chop.tr('``', '')
  84. if !auth_keys.include?(auth_val)
  85. add_error(line_num, INDEX_AUTH, "not a valid option: #{auth_val}")
  86. end
  87. #################### HTTPS ###################
  88. # Values should be either "Yes" or "No"
  89. https_val = values[INDEX_HTTPS].lstrip.chop
  90. if !https_keys.include?(https_val)
  91. add_error(line_num, INDEX_HTTPS, "must use \"Yes\" or \"No\": #{https_val}")
  92. end
  93. #################### LINK ####################
  94. # Url should be wrapped in "[Go!]" view
  95. link_val = values[INDEX_LINK].lstrip.chop
  96. if !link_val.start_with?("[Go!](") || !link_val.end_with?(')')
  97. add_error(line_num, INDEX_LINK, "format should be \"[Go!](<LINK>)\": #{link_val}")
  98. end
  99. end
  100. $errors.each do | e |
  101. puts e
  102. end
  103. exit($errors.length)