-
Notifications
You must be signed in to change notification settings - Fork 0
/
gendocs.rb
83 lines (69 loc) · 2.85 KB
/
gendocs.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
82
83
require_relative "lib/rbedi.rb"
segments_list_heading = <<EOF
# Segments
Docs generated at #{Time.now.strftime("%A, %B %e, %Y %l:%M %p %Z")}
EOF
def segments_docs_path
"docs/segments.md"
end
def segments_docs_readme_path
"docs/segments/readme.md"
end
def segment_docs_path(segment_code, relative=true)
relative ? "segments/#{segment_code}.md" : "docs/segments/#{segment_code}.md"
end
def element_codes_path(segment_code, element_position, relative=true)
relative ? "../elements/#{segment_code}_#{element_position}.md" : "docs/elements/#{segment_code}_#{element_position}.md"
end
def segment_list_item(segment_code, segment_symbol)
<<EOF
* `#{segment_code}` - [`:#{segment_symbol}`](#{segment_docs_path(segment_code)})
EOF
end
def segment_elements_list_heading(segment_code, segment_symbol)
<<EOF
# Elements for #{segment_code} - #{segment_symbol}
[Segments](../segments.md) > #{segment_code}
EOF
end
def segment_element_list_item(element_position, element_symbol, segment_code, codes_count)
unless codes_count.nil?
<<EOF
* #{element_position}: `:#{element_symbol}` - [#{codes_count} codes](#{element_codes_path(segment_code, element_position)})
EOF
else
<<EOF
* #{element_position}: `:#{element_symbol}` - no codes
EOF
end
end
def element_codes_list_heading(segment_code, element_position, element_symbol)
element_formatted = "#{segment_code}#{"%02d" % element_position}"
<<EOF
# Codes for #{element_formatted} - #{element_symbol}
[Segments](../segments.md) > [#{segment_code}](../segments/#{segment_code}.md) > #{element_formatted}
EOF
end
def element_codes_list_item(code, symbol)
<<EOF
* `#{code.upcase}` - `#{symbol.is_a?(Symbol) ? ":#{symbol}" : symbol}`
EOF
end
File.write(segments_docs_path, segments_list_heading)
RBEDI::Codes::SegmentNames::SEGMENT_NAMES.each do |segment_code, segment_symbol|
File.write(segments_docs_path, segment_list_item(segment_code, segment_symbol), mode: ?a)
File.write(segment_docs_path(segment_code, false), segment_elements_list_heading(segment_code, segment_symbol))
codes_class = RBEDI::Codes.const_get(segment_code)
codes_class::SEGMENT_POSITIONS.each do |element_position, element_symbol|
codes_hash = codes_class::CODES[element_position]
unless codes_hash.nil?
File.write(segment_docs_path(segment_code, false), segment_element_list_item(element_position, element_symbol, segment_code, codes_hash.length), mode: ?a)
File.write(element_codes_path(segment_code, element_position, false), element_codes_list_heading(segment_code, element_position, element_symbol))
codes_hash.each do |code, symbol|
File.write(element_codes_path(segment_code, element_position, false), element_codes_list_item(code, symbol), mode: ?a)
end
else
File.write(segment_docs_path(segment_code, false), segment_element_list_item(element_position, element_symbol, segment_code, nil), mode: ?a)
end
end
end