|
1 | | -# frozen_string_literal: true |
2 | | - |
3 | | -# A UC Berkeley-specific validator for Jekyll sites |
4 | | -# This class validates the following options: |
5 | | -# 1) Ensures required attributes are present in the config file. |
6 | | -# 2) Ensures the baseurl is a consistent format based on the semester |
7 | | -# 3) Uses a `course_department` config to implement some shared styling/language. |
8 | | -# 4) Ensures the `color_scheme` is valid. |
9 | | -# Future config validations might make sense, like footer/a11y/etc. configuration, |
10 | | - |
11 | | -# Implement additional validations by registering an entry in `KEY_VALIDATIONS` |
12 | | -# This is a key_name => function_name map. |
13 | | -# function_name should be defined in the ConfigValidator class, and is called with |
14 | | -# the key, value pair in the config. |
15 | | -# Note that Jekyll parses the YAML file such that the config keys are *strings* not symbols. |
16 | | - |
17 | | -# See the inclusion_validator to add simple allow-list style validations |
18 | | - |
19 | | -# A simple class for nicer error message formatting. |
20 | | -class ConfigValidationError < StandardError |
21 | | - attr_reader :errors |
22 | | - |
23 | | - def initialize(errors) |
24 | | - super |
25 | | - @errors = errors |
26 | | - end |
27 | | - |
28 | | - def message |
29 | | - "The config file contained validation errors:\n\t#{errors.join('\n\t')}\n\n" |
30 | | - end |
31 | | -end |
32 | | - |
33 | | -module Jekyll |
34 | | - # Jekyll::ConfigValidator class definition (see docs at the top of file) |
35 | | - class ConfigValidator |
36 | | - SEMESTER_REGEXP = /(wi|sp|su|fa)\d\d$/ |
37 | | - VALID_COURSE_DEPARTMENT = %w[eecs dsus stat].freeze |
38 | | - VALID_COLOR_SCHEME = %w[light dark].freeze |
39 | | - |
40 | | - # To validate a key with an 'allow list': |
41 | | - # Use the function :inclusion_validator |
42 | | - # and definite VALID_KEY_NAME above |
43 | | - KEY_VALIDATIONS = { |
44 | | - url: :validate_clean_url, |
45 | | - baseurl: :validate_semester_format, |
46 | | - course_department: :inclusion_validator, |
47 | | - color_scheme: :inclusion_validator |
48 | | - }.freeze |
49 | | - |
50 | | - attr_accessor :config, :errors |
51 | | - |
52 | | - def initialize(config) |
53 | | - @config = config |
54 | | - @errors = [] |
55 | | - end |
56 | | - |
57 | | - def validate |
58 | | - validate_keys! |
59 | | - |
60 | | - KEY_VALIDATIONS.each do |key, validator| |
61 | | - send(validator, key, config[key.to_s]) if @config.key?(key.to_s) |
62 | | - end |
63 | | - |
64 | | - raise ConfigValidationError, errors if errors.length.positive? |
65 | | - |
66 | | - puts 'Passed Berkeley YAML Config Validations' |
67 | | - end |
68 | | - |
69 | | - def validate_keys! |
70 | | - required_keys = %i[baseurl course_department] |
71 | | - required_keys.each do |key| |
72 | | - errors << "#{key} is missing from site config" unless @config.key?(key.to_s) |
73 | | - end |
74 | | - end |
75 | | - |
76 | | - private |
77 | | - |
78 | | - def validate_clean_url(_key, url) |
79 | | - errors << '`url` should not end with a `/`' if url.end_with?('/') |
80 | | - errors << '`url` should contain a protocol' unless url.match?(%r{https?://}) |
81 | | - end |
82 | | - |
83 | | - def validate_semester_format(_key, baseurl) |
84 | | - # This is just for consistency of URL presentation. |
85 | | - errors << '`baseurl` must start with a `/`.' unless baseurl.match?(%r{^/}) |
86 | | - # skip, just for the template. |
87 | | - return if baseurl == '/berkeley-class-site' |
88 | | - |
89 | | - return if baseurl.match?(SEMESTER_REGEXP) |
90 | | - |
91 | | - errors << "`baseurl` must be a valid semester (faXX, spXX, suXX or wiXX), not #{baseurl}" |
92 | | - end |
93 | | - |
94 | | - def inclusion_validator(key, value) |
95 | | - allowed = self.class.const_get("VALID_#{key.upcase}") |
96 | | - errors << "`#{key}` must be one of #{allowed} (not '#{value}')" unless allowed.include?(value) |
97 | | - end |
98 | | - end |
99 | | -end |
100 | | - |
101 | | -Jekyll::Hooks.register [:site], :after_init do |site| |
102 | | - next if ENV['JEKYLL_ENV'] == 'production' |
103 | | - |
104 | | - Jekyll::ConfigValidator.new(site.config).validate |
105 | | -end |
| 1 | +# # frozen_string_literal: true |
| 2 | + |
| 3 | +# # A UC Berkeley-specific validator for Jekyll sites |
| 4 | +# # This class validates the following options: |
| 5 | +# # 1) Ensures required attributes are present in the config file. |
| 6 | +# # 2) Ensures the baseurl is a consistent format based on the semester |
| 7 | +# # 3) Uses a `course_department` config to implement some shared styling/language. |
| 8 | +# # 4) Ensures the `color_scheme` is valid. |
| 9 | +# # Future config validations might make sense, like footer/a11y/etc. configuration, |
| 10 | + |
| 11 | +# # Implement additional validations by registering an entry in `KEY_VALIDATIONS` |
| 12 | +# # This is a key_name => function_name map. |
| 13 | +# # function_name should be defined in the ConfigValidator class, and is called with |
| 14 | +# # the key, value pair in the config. |
| 15 | +# # Note that Jekyll parses the YAML file such that the config keys are *strings* not symbols. |
| 16 | + |
| 17 | +# # See the inclusion_validator to add simple allow-list style validations |
| 18 | + |
| 19 | +# # A simple class for nicer error message formatting. |
| 20 | +# class ConfigValidationError < StandardError |
| 21 | +# attr_reader :errors |
| 22 | + |
| 23 | +# def initialize(errors) |
| 24 | +# super |
| 25 | +# @errors = errors |
| 26 | +# end |
| 27 | + |
| 28 | +# def message |
| 29 | +# "The config file contained validation errors:\n\t#{errors.join('\n\t')}\n\n" |
| 30 | +# end |
| 31 | +# end |
| 32 | + |
| 33 | +# module Jekyll |
| 34 | +# # Jekyll::ConfigValidator class definition (see docs at the top of file) |
| 35 | +# class ConfigValidator |
| 36 | +# SEMESTER_REGEXP = /(wi|sp|su|fa)\d\d$/ |
| 37 | +# VALID_COURSE_DEPARTMENT = %w[eecs dsus stat].freeze |
| 38 | +# VALID_COLOR_SCHEME = %w[light dark].freeze |
| 39 | + |
| 40 | +# # To validate a key with an 'allow list': |
| 41 | +# # Use the function :inclusion_validator |
| 42 | +# # and definite VALID_KEY_NAME above |
| 43 | +# KEY_VALIDATIONS = { |
| 44 | +# url: :validate_clean_url, |
| 45 | +# baseurl: :validate_semester_format, |
| 46 | +# course_department: :inclusion_validator, |
| 47 | +# color_scheme: :inclusion_validator |
| 48 | +# }.freeze |
| 49 | + |
| 50 | +# attr_accessor :config, :errors |
| 51 | + |
| 52 | +# def initialize(config) |
| 53 | +# @config = config |
| 54 | +# @errors = [] |
| 55 | +# end |
| 56 | + |
| 57 | +# def validate |
| 58 | +# validate_keys! |
| 59 | + |
| 60 | +# KEY_VALIDATIONS.each do |key, validator| |
| 61 | +# send(validator, key, config[key.to_s]) if @config.key?(key.to_s) |
| 62 | +# end |
| 63 | + |
| 64 | +# raise ConfigValidationError, errors if errors.length.positive? |
| 65 | + |
| 66 | +# puts 'Passed Berkeley YAML Config Validations' |
| 67 | +# end |
| 68 | + |
| 69 | +# def validate_keys! |
| 70 | +# required_keys = %i[baseurl course_department] |
| 71 | +# required_keys.each do |key| |
| 72 | +# errors << "#{key} is missing from site config" unless @config.key?(key.to_s) |
| 73 | +# end |
| 74 | +# end |
| 75 | + |
| 76 | +# private |
| 77 | + |
| 78 | +# def validate_clean_url(_key, url) |
| 79 | +# errors << '`url` should not end with a `/`' if url.end_with?('/') |
| 80 | +# errors << '`url` should contain a protocol' unless url.match?(%r{https?://}) |
| 81 | +# end |
| 82 | + |
| 83 | +# def validate_semester_format(_key, baseurl) |
| 84 | +# # This is just for consistency of URL presentation. |
| 85 | +# errors << '`baseurl` must start with a `/`.' unless baseurl.match?(%r{^/}) |
| 86 | +# # skip, just for the template. |
| 87 | +# return if baseurl == '/berkeley-class-site' |
| 88 | + |
| 89 | +# return if baseurl.match?(SEMESTER_REGEXP) |
| 90 | + |
| 91 | +# errors << "`baseurl` must be a valid semester (faXX, spXX, suXX or wiXX), not #{baseurl}" |
| 92 | +# end |
| 93 | + |
| 94 | +# def inclusion_validator(key, value) |
| 95 | +# allowed = self.class.const_get("VALID_#{key.upcase}") |
| 96 | +# errors << "`#{key}` must be one of #{allowed} (not '#{value}')" unless allowed.include?(value) |
| 97 | +# end |
| 98 | +# end |
| 99 | +# end |
| 100 | + |
| 101 | +# Jekyll::Hooks.register [:site], :after_init do |site| |
| 102 | +# next if ENV['JEKYLL_ENV'] == 'production' |
| 103 | + |
| 104 | +# Jekyll::ConfigValidator.new(site.config).validate |
| 105 | +# end |
0 commit comments