Skip to content

Commit a8f2d3b

Browse files
committed
Write all examples to one file so that there's only one thing to include in Slate. [zipmark#275]
1 parent 8abb266 commit a8f2d3b

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

features/slate_documentation.feature

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,8 @@ Feature: Generate Slate documentation from test examples
143143
And the output should contain "6 examples, 0 failures"
144144
And the exit status should be 0
145145

146-
Scenario: Index file should look like we expect
147-
Then the file "doc/api/index.markdown" should contain exactly:
148-
"""
149-
# Example API
150-
151-
## Help
152-
153-
* [Getting welcome message](help/getting_welcome_message.markdown)
154-
155-
## Orders
156-
157-
* [Creating an order](orders/creating_an_order.markdown)
158-
* [Deleting an order](orders/deleting_an_order.markdown)
159-
* [Getting a list of orders](orders/getting_a_list_of_orders.markdown)
160-
* [Getting a specific order](orders/getting_a_specific_order.markdown)
161-
* [Updating an order](orders/updating_an_order.markdown)
162-
"""
163-
164-
Scenario: Example 'Getting a list of orders' file should look like we expect
165-
Then the file "doc/api/orders/getting_a_list_of_orders.markdown" should contain exactly:
146+
Scenario: Example 'Getting a list of orders' docs should look like we expect
147+
Then the file "doc/api/_generated_examples.markdown" should contain:
166148
"""
167149
## Getting a list of orders
168150
@@ -226,8 +208,8 @@ Feature: Generate Slate documentation from test examples
226208
<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>
227209
"""
228210

229-
Scenario: Example 'Creating an order' file should look like we expect
230-
Then the file "doc/api/orders/creating_an_order.markdown" should contain exactly:
211+
Scenario: Example 'Creating an order' docs should look like we expect
212+
Then the file "doc/api/_generated_examples.markdown" should contain:
231213
"""
232214
## Creating an order
233215
@@ -277,17 +259,32 @@ Feature: Generate Slate documentation from test examples
277259
<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>
278260
"""
279261

280-
Scenario: Example 'Deleting an order' file should be created
281-
Then a file named "doc/api/orders/deleting_an_order.markdown" should exist
262+
Scenario: Example 'Deleting an order' docs should be created
263+
Then the file "doc/api/_generated_examples.markdown" should contain:
264+
"""
265+
## Deleting an order
266+
"""
282267

283-
Scenario: Example 'Getting a list of orders' file should be created
284-
Then a file named "doc/api/orders/getting_a_list_of_orders.markdown" should exist
268+
Scenario: Example 'Getting a list of orders' docs should be created
269+
Then the file "doc/api/_generated_examples.markdown" should contain:
270+
"""
271+
## Getting a list of orders
272+
"""
285273

286-
Scenario: Example 'Getting a specific order' file should be created
287-
Then a file named "doc/api/orders/getting_a_specific_order.markdown" should exist
274+
Scenario: Example 'Getting a specific order' docs should be created
275+
Then the file "doc/api/_generated_examples.markdown" should contain:
276+
"""
277+
## Getting a specific order
278+
"""
288279

289-
Scenario: Example 'Updating an order' file should be created
290-
Then a file named "doc/api/orders/updating_an_order.markdown" should exist
280+
Scenario: Example 'Updating an order' docs should be created
281+
Then the file "doc/api/_generated_examples.markdown" should contain:
282+
"""
283+
## Updating an order
284+
"""
291285

292-
Scenario: Example 'Getting welcome message' file should be created
293-
Then a file named "doc/api/help/getting_welcome_message.markdown" should exist
286+
Scenario: Example 'Getting welcome message' docs should be created
287+
Then the file "doc/api/_generated_examples.markdown" should contain:
288+
"""
289+
## Getting welcome message
290+
"""

lib/rspec_api_documentation/views/slate_example.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ def curl_with_linebreaks
1515
def explanation_with_linebreaks
1616
explanation.gsub "\n", "<br>\n"
1717
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
1830
end
1931
end
2032
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
module RspecApiDocumentation
22
module Writers
3+
FILENAME = '_generated_examples'
4+
35
class SlateWriter < MarkdownWriter
6+
def self.clear_docs(docs_dir)
7+
FileUtils.mkdir_p(docs_dir)
8+
FileUtils.rm Dir[File.join docs_dir, "#{FILENAME}.*"]
9+
end
10+
411
def markup_example_class
512
RspecApiDocumentation::Views::SlateExample
613
end
14+
15+
def write
16+
File.open(configuration.docs_dir.join("#{FILENAME}.#{extension}"), 'w+') do |file|
17+
file.write "# #{configuration.api_name}\n\n"
18+
index.examples.sort_by!(&:description) unless configuration.keep_source_order
19+
20+
index.examples.each do |example|
21+
markup_example = markup_example_class.new(example, configuration)
22+
file.write markup_example.render
23+
end
24+
end
25+
end
726
end
827
end
928
end

0 commit comments

Comments
 (0)