forked from coreinfrastructure/best-practices-badge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_markdown.rb
executable file
·81 lines (71 loc) · 2.63 KB
/
gen_markdown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env ruby
# Read criteria.yml and generate markdown with embedded HTML.
# frozen_string_literal: true
# It mostly generates HTML, so that any later reformat for line length
# is unaffected (markdown is primarily intended for human editing;
# its sensitivity to newlines can sometimes make it a little more work
# when it's generated.)
# Use the YAML library. It generates keys of type string, NOT keyword
require 'yaml'
# Load in entire criteria.yml, which keys off the major/minor groups
FullCriteriaHash = YAML.load_file('criteria/criteria.yml')
CriteriaText = YAML.load_file('config/locales/en.yml')['en']['criteria']['0']
def print_file(filename)
File.open(filename, 'r') do |file|
while (line = file.gets)
puts line
end
end
end
def show_details(key)
return unless CriteriaText[key].key?('details')
print "<dt><i>Details</i>:<dt> <dd>#{CriteriaText[key]['details']}</dd>"
end
def show_extra(criterion)
return unless criterion.key?('rationale')
print "<dt><i>Rationale</i>:<dt> <dd>#{criterion['rationale']}</dd>"
end
# rubocop:disable Metrics/AbcSize,Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def puts_criterion(key, criterion)
print "\n<li><a name=\"#{key}\"></a>"
print '(Future criterion) ' if criterion.key?('future')
print CriteriaText[key]['description']
# print " (N/A #{criterion.key?('na_allowed') ? '' : 'not '}allowed.)"
print ' (N/A allowed.)' if criterion.key?('na_allowed')
if criterion.key('met_justification_required')
print ' (Justification required for "Met".)'
end
if criterion.key?('na_justification_required')
print ' (Justification required for "N/A".)'
end
print ' (URL required for "met".)' if criterion.key?('met_url_required')
print " <sup>[<a href=\"\##{key}\">#{key}</a>]</sup>"
if CriteriaText[key].key?('details') || criterion.key?('rationale')
print '<dl>' # Put details and rationale in a detail list
show_details(key)
show_extra(criterion)
print '</dl>'
end
puts '</li>'
end
# rubocop:enable Metrics/AbcSize,Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
# Generate results
$stdout.reopen('doc/criteria.md', 'w') || abort('Cannot write')
print_file('doc/criteria-header.markdown')
FullCriteriaHash['0'].each do |major, major_value|
puts ''
puts "### #{major}"
major_value.each do |minor, criteria|
puts ''
puts "<b><i>#{minor}</i></b>" # Force HTML interpretation
puts ''
puts '<ul>'
criteria.each do |key, criterion|
puts_criterion(key, criterion)
end
puts '</ul>'
end
end
print_file('doc/criteria-footer.markdown')