Skip to content

Commit fce1cd5

Browse files
authored
Merge pull request #287 from rpocklin/feature/slate-generator-tweaks
Feature/slate generator tweaks
2 parents 7478cfd + 2228570 commit fce1cd5

File tree

8 files changed

+79
-105
lines changed

8 files changed

+79
-105
lines changed

features/slate_documentation.feature

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ Feature: Generate Slate documentation from test examples
144144
And the exit status should be 0
145145

146146
Scenario: Example 'Getting a list of orders' docs should look like we expect
147-
Then the file "doc/api/_generated_examples.markdown" should contain:
147+
Then the file "doc/api/index.html.md" should contain:
148148
"""
149149
## Getting a list of orders
150150
151+
151152
### Request
152153
153154
#### Endpoint
@@ -159,7 +160,6 @@ Feature: Generate Slate documentation from test examples
159160
160161
`GET /orders`
161162
162-
163163
#### Parameters
164164
165165
@@ -202,17 +202,20 @@ Feature: Generate Slate documentation from test examples
202202
| page | Current page |
203203
204204
205-
206-
### cURL
207-
208-
<code>curl&nbsp;"http://localhost:3000/orders"&nbsp;-X&nbsp;GET&nbsp;&#92;<br>&nbsp;&nbsp;-H&nbsp;"Host:&nbsp;example.org"&nbsp;&#92;<br>&nbsp;&nbsp;-H&nbsp;"Cookie:&nbsp;"</code>
205+
```shell
206+
curl "http://localhost:3000/orders" -X GET \
207+
-H "Host: example.org" \
208+
-H "Cookie: "
209209
"""
210210

211211
Scenario: Example 'Creating an order' docs should look like we expect
212-
Then the file "doc/api/_generated_examples.markdown" should contain:
212+
Then the file "doc/api/index.html.md" should contain:
213213
"""
214+
# Orders
215+
214216
## Creating an order
215217
218+
216219
### Request
217220
218221
#### Endpoint
@@ -225,7 +228,6 @@ Feature: Generate Slate documentation from test examples
225228
226229
`POST /orders`
227230
228-
229231
#### Parameters
230232
231233
@@ -253,38 +255,40 @@ Feature: Generate Slate documentation from test examples
253255
254256
255257
256-
257-
### cURL
258-
259-
<code>curl&nbsp;"http://localhost:3000/orders"&nbsp;-d&nbsp;'name=Order+3&amount=33.0'&nbsp;-X&nbsp;POST&nbsp;&#92;<br>&nbsp;&nbsp;-H&nbsp;"Host:&nbsp;example.org"&nbsp;&#92;<br>&nbsp;&nbsp;-H&nbsp;"Content-Type:&nbsp;application/x-www-form-urlencoded"&nbsp;&#92;<br>&nbsp;&nbsp;-H&nbsp;"Cookie:&nbsp;"</code>
258+
```shell
259+
curl "http://localhost:3000/orders" -d 'name=Order+3&amount=33.0' -X POST \
260+
-H "Host: example.org" \
261+
-H "Content-Type: application/x-www-form-urlencoded" \
262+
-H "Cookie: "
263+
```
260264
"""
261265

262266
Scenario: Example 'Deleting an order' docs should be created
263-
Then the file "doc/api/_generated_examples.markdown" should contain:
267+
Then the file "doc/api/index.html.md" should contain:
264268
"""
265269
## Deleting an order
266270
"""
267271

268272
Scenario: Example 'Getting a list of orders' docs should be created
269-
Then the file "doc/api/_generated_examples.markdown" should contain:
273+
Then the file "doc/api/index.html.md" should contain:
270274
"""
271275
## Getting a list of orders
272276
"""
273277

274278
Scenario: Example 'Getting a specific order' docs should be created
275-
Then the file "doc/api/_generated_examples.markdown" should contain:
279+
Then the file "doc/api/index.html.md" should contain:
276280
"""
277281
## Getting a specific order
278282
"""
279283

280284
Scenario: Example 'Updating an order' docs should be created
281-
Then the file "doc/api/_generated_examples.markdown" should contain:
285+
Then the file "doc/api/index.html.md" should contain:
282286
"""
283287
## Updating an order
284288
"""
285289

286290
Scenario: Example 'Getting welcome message' docs should be created
287-
Then the file "doc/api/_generated_examples.markdown" should contain:
291+
Then the file "doc/api/index.html.md" should contain:
288292
"""
289293
## Getting welcome message
290294
"""

lib/rspec_api_documentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module Views
5757
autoload :TextileExample
5858
autoload :MarkdownIndex
5959
autoload :MarkdownExample
60+
autoload :SlateIndex
6061
autoload :SlateExample
6162
end
6263

lib/rspec_api_documentation/views/slate_example.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,6 @@ def initialize(example, configuration)
55
super
66
self.template_name = "rspec_api_documentation/slate_example"
77
end
8-
9-
def curl_with_linebreaks
10-
requests.map {|request| request[:curl].lines }.flatten.map do |line|
11-
line.rstrip.gsub("\t", ' ').gsub(' ', '&nbsp;').gsub('\\', '&#92;')
12-
end.join "<br>"
13-
end
14-
15-
def explanation_with_linebreaks
16-
explanation.gsub "\n", "<br>\n"
17-
end
18-
19-
def write
20-
File.open(configuration.docs_dir.join("#{FILENAME}.#{extension}"), 'w+') do |file|
21-
file.write "# #{configuration.api_name}\n\n"
22-
index.examples.sort_by!(&:description) unless configuration.keep_source_order
23-
24-
index.examples.each do |example|
25-
markup_example = markup_example_class.new(example, configuration)
26-
file.write markup_example.render
27-
end
28-
end
29-
end
308
end
319
end
3210
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module RspecApiDocumentation
2+
module Views
3+
class SlateIndex < MarkdownIndex
4+
end
5+
end
6+
end

lib/rspec_api_documentation/writers/slate_writer.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,49 @@ module RspecApiDocumentation
22
module Writers
33

44
class SlateWriter < MarkdownWriter
5-
FILENAME = '_generated_examples'
5+
EXTENSION = 'html.md'
6+
FILENAME = 'index'
67

78
def self.clear_docs(docs_dir)
89
FileUtils.mkdir_p(docs_dir)
910
FileUtils.rm Dir[File.join docs_dir, "#{FILENAME}.*"]
1011
end
1112

13+
def markup_index_class
14+
RspecApiDocumentation::Views::SlateIndex
15+
end
16+
1217
def markup_example_class
1318
RspecApiDocumentation::Views::SlateExample
1419
end
1520

1621
def write
1722
File.open(configuration.docs_dir.join("#{FILENAME}.#{extension}"), 'w+') do |file|
18-
file.write "# #{configuration.api_name}\n\n"
19-
index.examples.sort_by!(&:description) unless configuration.keep_source_order
2023

21-
index.examples.each do |example|
22-
markup_example = markup_example_class.new(example, configuration)
23-
file.write markup_example.render
24+
file.write %Q{---\n}
25+
file.write %Q{title: "#{configuration.api_name}"\n}
26+
file.write %Q{language_tabs:\n}
27+
file.write %Q{ - json: JSON\n}
28+
file.write %Q{---\n\n}
29+
30+
IndexHelper.sections(index.examples, @configuration).each do |section|
31+
32+
file.write "# #{section[:resource_name]}\n\n"
33+
section[:examples].sort_by!(&:description) unless configuration.keep_source_order
34+
35+
section[:examples].each do |example|
36+
markup_example = markup_example_class.new(example, configuration)
37+
file.write markup_example.render
38+
end
39+
2440
end
41+
2542
end
2643
end
44+
45+
def extension
46+
EXTENSION
47+
end
2748
end
2849
end
2950
end

spec/views/slate_example_spec.rb

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,4 @@
1010
let(:configuration) { RspecApiDocumentation::Configuration.new }
1111
let(:slate_example) { described_class.new(rad_example, configuration) }
1212

13-
describe '#curl_with_linebreaks' do
14-
subject { slate_example.curl_with_linebreaks }
15-
16-
before(:each) { allow(slate_example).to receive(:requests).and_return requests }
17-
18-
context 'marshaling' do
19-
let(:requests) { [{curl: 'One'}, {curl: "Two \nThree" }, {curl: 'Four '}] }
20-
21-
it 'joins all the Curl requests with linebreaks, stripping trailing whitespace' do
22-
expect(subject).to be == [
23-
'One', 'Two', 'Three', 'Four'
24-
].join('<br>')
25-
end
26-
end
27-
28-
context 'escaping' do
29-
let(:requests) { [{curl: string}] }
30-
31-
context 'spaces' do
32-
let(:string) { 'a b' }
33-
34-
it 'replaces them with nonbreaking spaces' do
35-
expect(subject).to be == 'a&nbsp;b'
36-
end
37-
end
38-
39-
context 'tabs' do
40-
let(:string) { "a\tb" }
41-
42-
it 'replaces them with two nonbreaking spaces' do
43-
expect(subject).to be == 'a&nbsp;&nbsp;b'
44-
end
45-
end
46-
47-
context 'backslashes' do
48-
let(:string) { 'a\\b'}
49-
50-
it 'replaces them with an HTML entity' do
51-
expect(subject).to be == 'a&#92;b'
52-
end
53-
end
54-
end
55-
end
56-
57-
describe '#explanation_with_linebreaks' do
58-
it 'returns the explanation with HTML linebreaks' do
59-
explanation = "Line 1\nLine 2\nLine 3\Line 4"
60-
allow(slate_example).to receive(:explanation).and_return explanation
61-
expect(slate_example.explanation_with_linebreaks).to be == explanation.gsub("\n", "<br>\n")
62-
end
63-
end
6413
end

spec/writers/slate_writer_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@
1515
end
1616
end
1717

18+
describe "#write" do
19+
let(:writer) { described_class.new(index, configuration) }
20+
21+
before do
22+
template_dir = File.join(configuration.template_path, "rspec_api_documentation")
23+
FileUtils.mkdir_p(template_dir)
24+
File.open(File.join(template_dir, "markdown_index.mustache"), "w+") { |f| f << "{{ mustache }}" }
25+
FileUtils.mkdir_p(configuration.docs_dir)
26+
end
27+
28+
it "should write the index" do
29+
writer.write
30+
index_file = File.join(configuration.docs_dir, "index.html.md")
31+
expect(File.exists?(index_file)).to be_truthy
32+
end
33+
end
34+
1835
context 'instance methods' do
1936
let(:writer) { described_class.new(index, configuration) }
2037

templates/rspec_api_documentation/slate_example.mustache

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## {{ description }}
22

3+
{{# explanation }}
4+
{{{ explanation }}}
5+
{{/ explanation }}
6+
37
### Request
48

59
#### Endpoint
@@ -13,11 +17,6 @@
1317

1418
`{{ http_method }} {{ route }}`
1519

16-
{{# explanation }}
17-
18-
{{{ explanation_with_linebreaks }}}
19-
{{/ explanation }}
20-
2120
#### Parameters
2221

2322
{{# requests}}
@@ -78,9 +77,8 @@ None known.
7877
{{/ has_response_fields? }}
7978

8079
{{# curl }}
81-
82-
### cURL
83-
84-
<code>{{{ curl_with_linebreaks }}}</code>
80+
```shell
81+
{{{ curl }}}
82+
```
8583
{{/ curl }}
8684
{{/ requests}}

0 commit comments

Comments
 (0)