forked from makandra/spreewald
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumentation_generator.rb
119 lines (96 loc) · 2.65 KB
/
documentation_generator.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
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
module DocumentationGenerator
module CommentExtractor
def parse_and_format_comment(comment)
comment.gsub!(/.*coding:.*UTF-8.*/, '')
comment.strip!
comment_lines = comment.split("\n").take_while { |line| line =~ /^\s*#/ }
comment_lines && comment_lines.join("\n").gsub(/^\s*# ?/, '')
end
end
class StepDefinition
extend CommentExtractor
def initialize(definition, comment = nil)
@definition = definition
@comment = comment
end
def self.try_and_parse(code)
definition = code[/^\s*((When|Then|Given|AfterStep).*)do/, 1]
return unless definition
comment = parse_and_format_comment(code)
return if comment =~ /\bnodoc\b/
new(definition.strip, comment)
end
def format
<<-TEXT
* **#{format_definition}**
#{@comment.gsub(/^/, ' ')}
TEXT
end
def format_definition
if @definition =~ /AfterStep/
@definition[/@\w*/]
else
capture_groups = %w[([^\"]*) ([^"]*) (.*) (.*?) [^"]+ ([^\"]+) ([^']*) ([^/]*) (.+) (.*[^:])]
capture_groups.map! &Regexp.method(:escape)
@definition.
gsub('/^', '').
gsub('$/', '').
gsub(' ?', ' ').
gsub('(?:|I )', 'I ').
gsub('?:', '').
gsub(Regexp.new(capture_groups.join '|'), '...').
gsub(/\\\//, '/')
end
end
end
class StepDefinitionFile
FILE_COMMENT_END = 'FILE_COMMENT_END'
include CommentExtractor
def initialize(filename)
@filename = filename
@code = File.read(filename)
@steps = []
extract_comment
add_steps
end
def extract_comment
if @code.include?(FILE_COMMENT_END)
file_comment = @code.split(FILE_COMMENT_END).first
@comment = parse_and_format_comment(file_comment)
end
end
def add_steps
@code.split("\n\n").each do |block|
step = StepDefinition.try_and_parse(block)
if step
@steps << step
end
end
end
def format
<<-TEXT
### #{format_filename}
#{@comment}
#{format_steps}
TEXT
end
def format_filename
@filename.split('/').last
end
def format_steps
@steps.collect { |step| step.format }.join("\n\n")
end
end
class StepDefinitionsDirectory
def initialize(directory)
@step_definition_files = []
Dir["#{directory}/*.rb"].to_a.sort.each do |filename|
next if filename =~ /all_steps/
@step_definition_files << StepDefinitionFile.new(filename)
end
end
def format
@step_definition_files.collect { |step_definition_file| step_definition_file.format }.join("\n\n")
end
end
end