-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhtml_step_outputter.rb
146 lines (129 loc) · 3.49 KB
/
html_step_outputter.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Outputter that generates HTML markup
# Feel free to customize the below code for your needs
require 'cgi'
require 'fileutils'
class HtmlStepOutputter
def initialize(file)
@file = File.open(file, 'w')
@previous_type = ""
@id_number = 0
end
# HTML file header - customize as needed
def header
@file.puts <<-eos
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Cucumber step documentation</title>
<style>
html, body {
font-family: Helvetica, Arial, sans-serif;
margin: 0;
padding: 0;
}
h2 {
background-color: #eee;
border-bottom: 1px solid #ccf;
box-shadow: 0 5px 10px -5px rgba(0,0,0,0.2);
padding: 5px 1rem;
position: sticky;
top: -2px;
}
h3 {
padding: 0 1rem;
}
.stepdefs {
font-size: smaller;
}
.stepdefs li {
margin-bottom: 0.5em;
list-style-type: none;
}
.stepdefs li:before {
content: "\u00BB ";
font-size: larger;
padding-right: 0.3em;
}
.stepdef {
color: #111;
text-decoration: none;
}
.type {
color: #99e;
}
.extrainfo {
display: none;
overflow: hidden; /* Fixes jumping issue in jQuery slideToggle() */
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
</head>
<body>
eos
end
def footer
@file.puts <<-eos
</ul>
<p> </p>
<p><em>Documentation generated #{Time.now}</em></p>
</body>
</html>
eos
end
def close
@file.close
end
def start_directory(dir)
@file.puts %(<section>)
@file.puts %(<h2>Step definitions in #{dir}/</h2>)
@previous_type = ""
end
def end_directory
@file.puts %(</ul>) if @previous_type != ""
@file.puts %(</section>)
end
def start_all
@file.puts %(</ul>)
@file.puts %(<p> </p>)
@file.puts %(<h2>All definitions alphabetically</h2>)
@previous_type = ""
end
def end_all
# No-op
end
def step(step)
if @previous_type != step[:type]
@file.puts %(</ul>) if @previous_type != ""
@file.puts %(<h3>#{step[:type]} definitions</h3>)
@file.puts %(<ul class="stepdefs">)
@previous_type = step[:type]
end
id = new_id
@file.puts %(<li>)
@file.puts %( <a href="#" onclick="$('##{id}').slideToggle(); return false;" class="stepdef">#{span_first_word(CGI.escapeHTML(step[:name]))}</a>)
@file.puts %( <div id="#{id}" class="extrainfo">)
# TODO: Add link to source repo or Jenkins workspace
# <p><a href=".../#{CGI.escapeHTML(step[:filename])}" style="color: #888;">#{CGI.escapeHTML(step[:filename])}:#{step[:line_number]}</a></p>
@file.puts %( <p style="color: #888;">#{CGI.escapeHTML(step[:filename])}:#{step[:line_number]}</p>)
@file.puts %( <pre style="background-color: #ddd; padding-top: 1.2em;">)
step[:code].each do |line|
@file.puts %( #{CGI.escapeHTML(line)})
end
@file.puts %( </pre>)
@file.puts %( </div>)
@file.puts %(</li>)
end
private
def new_id
@id_number += 1
"id#{@id_number}"
end
def span_first_word(str)
if (str =~ /^([A-Za-z]+)(.*)/)
return "<span class='type'>#{$1}</span>#{$2}"
else
return str
end
end
end