-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathgenerate_adopters.rb
More file actions
131 lines (106 loc) · 3.84 KB
/
generate_adopters.rb
File metadata and controls
131 lines (106 loc) · 3.84 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require 'pp'
def collect_section_nodes(file, section_title)
markdown = open(file).readlines().join
doc = CommonMarker.render_doc(markdown)
title_found = false
section_nodes = []
doc.walk do |node|
if node.type == :header
if title_found == false
node.each do |subnode|
if subnode.type == :text and subnode.string_content == section_title
title_found = true
end
end
# stop the recursion once the next header is reached
# TODO: is this correct, or should we check if this is another `##` header, rather than any header?
else
break
end
# once the title has been found, collect all nodes up to the next header
elsif title_found == true
section_nodes << node
end
end
return section_nodes
end
# Extracts the value of a specific section from a markdown file
def extract_section(file, section_title)
markdown = open(file).readlines().join
doc = CommonMarker.render_doc(markdown)
title_found = false
section_nodes = []
# once the header in question is found, extract all the text nodes from the
# subsequent paragaph
doc.walk do |node|
if node.type == :header
node.each do |subnode|
if subnode.type == :text and subnode.string_content == section_title
title_found = true
end
end
elsif node.type == :paragraph && title_found == true
node.each do |subnode|
section_nodes += extract_text(subnode)
end
break # stop the recursion once the paragraph has been processed
end
end
section_content = section_nodes.join("")
return section_content
end
# extracts the pure text content from this CommonMarker node, and its children.
# returns an array of strings
def extract_text(node)
section_nodes = []
if node.type == :softbreak
section_nodes << " "
elsif node.type == :text or node.type == :code
section_nodes << node.string_content
else
node.each do |subnode|
section_nodes += extract_text(subnode)
end
end
return section_nodes
end
def generate_markdown_filename(org_name)
markdown_filename = org_name.downcase.tr(" ", "-").gsub(/[^\w.-]/, "")
markdown_filename = "#{markdown_filename}.md"
return markdown_filename
end
PATTERNS = Dir["../patterns/2-structured/*.md","../patterns/2-structured/project-setup/*.md", "../patterns/3-validated/*.md"]
ORGS = File.readlines('adopters.txt').map(&:chomp)
adopter_patterns = Hash.new()
pattern_titles = Hash.new()
# init with empty lists
ORGS.each do |org|
adopter_patterns[org] = []
end
PATTERNS.each do |file|
title = extract_section(file, "Title")
pattern_titles[file] = title
known_instances = collect_section_nodes(file, "Known Instances")
known_instances = known_instances.map{|n| extract_text(n)}.join(" ")
ORGS.each do |org|
if known_instances.include? org
adopter_patterns[org] << file
end
end
end
adopter_patterns.each do |org, patterns_files|
markdown_filename = generate_markdown_filename(org)
file_content = "# #{org}\n\n"
file_content += "These are the InnerSource Patterns that **#{org}** has adopted:\n\n"
file_content += patterns_files.map{|f| "* [#{pattern_titles[f]}](#{f})"}.join("\n")
file_content += "\n\n"
file_content += "If you work at **#{org}** and want to modify the list above, please follow [these instructions](./README.md).\n"
File.write(markdown_filename, file_content)
# print in format required for inclusion in the ToC
puts "* [#{org}](../../adopters/#{markdown_filename})"
end
# pp adopter_patterns
# puts adopter_patterns.map{|patterns| patterns.count}.reduce(&:+)