Skip to content

Commit dd0eef8

Browse files
authored
Merge pull request #308 from rspec/docify
Add cucumber to markdown generation
2 parents fdd8b38 + be6d3e5 commit dd0eef8

1 file changed

Lines changed: 158 additions & 18 deletions

File tree

Rakefile

Lines changed: 158 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,148 @@ def each_project(options = {})
6767
end
6868
end
6969

70+
def rdoc_for_project(project, args, doc_destination_path)
71+
FileUtils.mkdir_p doc_destination_path
72+
cmd = "bundle update && \
73+
RUBYOPT='-I#{args[:website_path]}/lib' bundle exec yard \
74+
--yardopts .yardopts \
75+
--output-dir #{doc_destination_path}"
76+
puts cmd
77+
Bundler.unbundled_system(cmd)
78+
79+
in_place =
80+
if RUBY_PLATFORM =~ /darwin/ # if this is os x then we must modify sed
81+
"-i ''"
82+
else
83+
"-i''"
84+
end
85+
86+
Bundler.unbundled_system %Q{ag -l src=\\"\\\(?:..\/\\\)*js #{doc_destination_path} | xargs -I{} sed #{in_place} 's/src="\\\(..\\\/\\\)*js/src="\\\/documentation\\\/#{args[:version]}\\\/#{project}\\\/js/' {}}
87+
Bundler.unbundled_system %Q{ag -l href=\\"\\\(?:..\/\\\)*css #{doc_destination_path} | xargs -I{} sed #{in_place} 's/href="\\\(..\\\/\\\)*css/href="\\\/documentation\\\/#{args[:version]}\\\/#{project}\\\/css/' {}}
88+
Bundler.unbundled_system %Q{ag --html -l . #{doc_destination_path} | xargs -I{} sed #{in_place} /^[[:space:]]*$/d {}}
89+
end
90+
91+
def html_filename(filename)
92+
filename.gsub(/^\/?features/, '').gsub('_', '-').gsub('README', 'index').gsub(/\.(feature|md)$/, '.html.md')
93+
end
94+
95+
def cucumber_doc_for_project(project, _args, doc_destination_path)
96+
if `which gherkin2markdown`.empty?
97+
abort <<-MSG
98+
Creating cucumber documentation requires the gherkin2makrdown tool:
99+
- Install go using your preferred run time (tested with asdf and golang 1.20)
100+
- Then run:
101+
```
102+
go install github.com/raviqqe/gherkin2markdown@latest
103+
```
104+
OR surpress this message by running with NO_CUCUMBER=true
105+
MSG
106+
end
107+
FileUtils.mkdir_p doc_destination_path
108+
109+
features = Dir['features/**/*feature']
110+
markdown = Dir['features/**/*md']
111+
112+
# Convert features to markdown files in the website with - based filenames
113+
features.each do |file|
114+
dest_file = File.join(doc_destination_path, html_filename(file))
115+
FileUtils.mkdir_p File.dirname(dest_file)
116+
Bundler.unbundled_system "gherkin2markdown #{file} > #{dest_file}"
117+
118+
result = File.read(dest_file)
119+
table_regexp = /^\s*\|.*\|$/
120+
table_header_regexp = /^\s*\|-+(\|-+)*\|\s*$/
121+
122+
# If no table skip the table generation
123+
next unless result =~ table_regexp
124+
125+
lines = result.split("\n")
126+
127+
# Find all a files tables
128+
tables =
129+
lines.each.with_index.reduce([]) do |table_ranges, (line, index)|
130+
if line =~ table_regexp
131+
(table_start, table_end) = table_ranges.pop
132+
133+
if table_end == index - 1
134+
# then this is our table
135+
table_ranges << [table_start, index]
136+
else
137+
# this is a new table
138+
table_ranges << [table_start, table_end] unless table_start.nil? && table_end.nil?
139+
table_ranges << [index, index]
140+
end
141+
else
142+
table_ranges
143+
end
144+
end
145+
146+
File.open(dest_file, 'w') do |tableised_file|
147+
written_end =
148+
tables.reduce(0) do |last_index, (table_start, table_end)|
149+
# Write file before the table
150+
tableised_file.write lines[last_index...table_start].join("\n")
151+
tableised_file.write "\n"
152+
153+
# Calculate the header
154+
(spacing, contents,) = lines[table_start].split('|', 2)
155+
table_width = contents.length - 1
156+
table_header = "|#{' ' * table_width}|\n|#{'-' * table_width}|\n"
157+
158+
# If the header is missing for this table write the header
159+
tableised_file.write table_header unless lines[table_start + 1] =~ table_header_regexp
160+
161+
# Write the rest of the table
162+
lines[table_start..table_end].each do |line|
163+
tableised_file.write line.gsub(/^#{spacing}/, '')
164+
tableised_file.write "\n"
165+
end
166+
167+
# next index is last of the table plus one
168+
table_end + 1
169+
end
170+
171+
# Write any remaining file
172+
if written_end < lines.length
173+
tableised_file.write lines[written_end..].join("\n")
174+
tableised_file.write "\n"
175+
end
176+
end
177+
end
178+
179+
# Copy markdown files in the website with - based filenames
180+
markdown.each do |file|
181+
dest_file = File.join(doc_destination_path, html_filename(file))
182+
FileUtils.mkdir_p File.dirname(dest_file)
183+
Bundler.unbundled_system "cp #{file} #{dest_file}"
184+
end
185+
186+
# For all folders check we have an index.html and add the front matter required
187+
(features + markdown).each do |filename|
188+
file_in_dest = File.join(doc_destination_path, html_filename(filename))
189+
file_we_care_about = File.join(File.dirname(file_in_dest), 'index.html.md')
190+
191+
front_matter = %Q(---\nlayout: "feature_index"\n---\n\n)
192+
193+
next if file_we_care_about =~ /#{project}\/index\.html\.md$/
194+
195+
if File.exist?(file_we_care_about)
196+
contents = File.read(file_we_care_about)
197+
File.write(file_we_care_about, front_matter + contents) unless contents.include?(front_matter)
198+
else
199+
File.write(file_we_care_about, front_matter)
200+
end
201+
end
202+
203+
# Copy .nav file to the project with the filenames converted to hyphens
204+
File.write("#{doc_destination_path}.nav", File.read('features/.nav').gsub('_', '-'))
205+
end
206+
70207
task :make_repos_directory do
71208
FileUtils.mkdir_p ReposPath
72209
end
73210

74-
desc "run an arbitrary command against all repos"
211+
desc 'run an arbitrary command against all repos'
75212
task :run, :command do |_t, args|
76213
run_command args[:command]
77214
end
@@ -92,14 +229,19 @@ task :update_docs, [:version, :website_path] do |_t, args|
92229

93230
each_project :silent => true, :except => (UnDocumentedProjects) do |project|
94231
$stdout.write "\rChecking versions... #{project}"
95-
latest_release = `git fetch --tags && git tag -l "v#{args[:version]}*" | grep v#{args[:version]} | tail -1`
232+
latest_release =
233+
if args[:version] =~ /maintenance$/
234+
args[:version]
235+
else
236+
`git fetch --tags && git tag -l "v#{args[:version]}*" | grep v#{args[:version]} | tail -1`
237+
end
96238

97239
if latest_release.empty?
98240
skipped << project
99241
else
100242
projects[project] = latest_release
101243
end
102-
$stdout.write "\rChecking versions... " + (" " * MAX_PROJECT_NAME_LENGTH)
244+
$stdout.write "\rChecking versions... #{' ' * MAX_PROJECT_NAME_LENGTH}"
103245
end
104246

105247
$stdout.write "\r\n"
@@ -108,24 +250,22 @@ task :update_docs, [:version, :website_path] do |_t, args|
108250

109251
each_project(:only => projects.keys) do |project|
110252
`git checkout #{projects[project]}`
111-
doc_destination_path = "#{output_directory}/source/documentation/#{args[:version]}/#{project}/"
112-
FileUtils.mkdir_p doc_destination_path
113-
cmd = "bundle update && \
114-
RUBYOPT='-I#{args[:website_path]}/lib' bundle exec yard \
115-
--yardopts .yardopts \
116-
--output-dir #{doc_destination_path}"
117-
puts cmd
118-
Bundler.unbundled_system(cmd)
119-
in_place =
120-
if RUBY_PLATFORM =~ /darwin/ # if this is os x then we must modify sed
121-
"-i ''"
253+
254+
(major, minor, *_patch) =
255+
case args[:version]
256+
when /^\d+\.\d+/ then args[:version].split('.')
257+
when /^\d+-\d+-maintenance/ then args[:version].split('-')
122258
else
123-
"-i''"
259+
raise ArgumentError, "Unexpected version #{args[:version]}, expected either `x.x` or `x-x-maintenance`"
124260
end
125261

126-
Bundler.unbundled_system %Q{ag -l src=\\"\\\(?:..\/\\\)*js #{doc_destination_path} | xargs -I{} sed #{in_place} 's/src="\\\(..\\\/\\\)*js/src="\\\/documentation\\\/#{args[:version]}\\\/#{project}\\\/js/' {}}
127-
Bundler.unbundled_system %Q{ag -l href=\\"\\\(?:..\/\\\)*css #{doc_destination_path} | xargs -I{} sed #{in_place} 's/href="\\\(..\\\/\\\)*css/href="\\\/documentation\\\/#{args[:version]}\\\/#{project}\\\/css/' {}}
128-
Bundler.unbundled_system %Q{ag --html -l . #{doc_destination_path} | xargs -I{} sed #{in_place} /^[[:space:]]*$/d {}}
262+
if ENV.fetch('NO_RDOC', '').empty?
263+
rdoc_for_project(project, args, "#{output_directory}/source/documentation/#{args.fetch(:version, '')}/#{project}/")
264+
end
265+
266+
if ENV.fetch('NO_CUCUMBER', '').empty?
267+
cucumber_doc_for_project(project, args, "#{output_directory}/source/features/#{major}-#{minor}/#{project}/")
268+
end
129269
end
130270

131271
puts "Skipped projects: (#{skipped.join(", ")}) due to no matching version." unless skipped.empty?

0 commit comments

Comments
 (0)