From d979d454dc0a110be88df7489f48b3538ebe725d Mon Sep 17 00:00:00 2001 From: Leonard Chin Date: Thu, 15 Mar 2018 16:22:34 +0000 Subject: [PATCH 1/2] Include API explanation in Slate template --- features/slate_documentation.feature | 8 +++++++- lib/rspec_api_documentation/writers/slate_writer.rb | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/features/slate_documentation.feature b/features/slate_documentation.feature index 0a3531b0..d1d77b6c 100644 --- a/features/slate_documentation.feature +++ b/features/slate_documentation.feature @@ -49,7 +49,7 @@ Feature: Generate Slate documentation from test examples RspecApiDocumentation.configure do |config| config.app = App config.api_name = "Example API" - config.api_explanation = "Description" + config.api_explanation = "An explanation of the API" config.format = :slate config.curl_host = 'http://localhost:3000' config.request_headers_to_include = %w[Content-Type Host] @@ -293,3 +293,9 @@ Feature: Generate Slate documentation from test examples """ ## Getting welcome message """ + + Scenario: API explanation should be included + Then the file "doc/api/index.html.md" should contain: + """ + An explanation of the API + """ diff --git a/lib/rspec_api_documentation/writers/slate_writer.rb b/lib/rspec_api_documentation/writers/slate_writer.rb index 61929873..8911f800 100644 --- a/lib/rspec_api_documentation/writers/slate_writer.rb +++ b/lib/rspec_api_documentation/writers/slate_writer.rb @@ -28,6 +28,8 @@ def write file.write %Q{ - shell: cURL\n} file.write %Q{---\n\n} + file.write configuration.api_explanation if configuration.api_explanation + IndexHelper.sections(index.examples, @configuration).each do |section| file.write "# #{section[:resource_name]}\n\n" From 3d22451a7b7e00d2340b6a36dcbc9e19e92c9a12 Mon Sep 17 00:00:00 2001 From: Leonard Chin Date: Thu, 15 Mar 2018 16:49:29 +0000 Subject: [PATCH 2/2] Add index template for Slate format Rather than hard coding the front matter and api explanation rendering, use a mustache template for the index. --- lib/rspec_api_documentation/views/slate_index.rb | 4 ++++ lib/rspec_api_documentation/writers/slate_writer.rb | 9 +-------- spec/writers/slate_writer_spec.rb | 2 +- templates/rspec_api_documentation/slate_index.mustache | 8 ++++++++ 4 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 templates/rspec_api_documentation/slate_index.mustache diff --git a/lib/rspec_api_documentation/views/slate_index.rb b/lib/rspec_api_documentation/views/slate_index.rb index f3d518ef..ea288729 100644 --- a/lib/rspec_api_documentation/views/slate_index.rb +++ b/lib/rspec_api_documentation/views/slate_index.rb @@ -1,6 +1,10 @@ module RspecApiDocumentation module Views class SlateIndex < MarkdownIndex + def initialize(index, configuration) + super + self.template_name = "rspec_api_documentation/slate_index" + end end end end diff --git a/lib/rspec_api_documentation/writers/slate_writer.rb b/lib/rspec_api_documentation/writers/slate_writer.rb index 8911f800..d2373dcb 100644 --- a/lib/rspec_api_documentation/writers/slate_writer.rb +++ b/lib/rspec_api_documentation/writers/slate_writer.rb @@ -21,14 +21,7 @@ def markup_example_class def write File.open(configuration.docs_dir.join("#{FILENAME}.#{extension}"), 'w+') do |file| - file.write %Q{---\n} - file.write %Q{title: "#{configuration.api_name}"\n} - file.write %Q{language_tabs:\n} - file.write %Q{ - json: JSON\n} - file.write %Q{ - shell: cURL\n} - file.write %Q{---\n\n} - - file.write configuration.api_explanation if configuration.api_explanation + file.write markup_index_class.new(index, configuration).render IndexHelper.sections(index.examples, @configuration).each do |section| diff --git a/spec/writers/slate_writer_spec.rb b/spec/writers/slate_writer_spec.rb index 603be2ef..9c1398da 100644 --- a/spec/writers/slate_writer_spec.rb +++ b/spec/writers/slate_writer_spec.rb @@ -22,7 +22,7 @@ FakeFS do template_dir = File.join(configuration.template_path, "rspec_api_documentation") FileUtils.mkdir_p(template_dir) - File.open(File.join(template_dir, "markdown_index.mustache"), "w+") { |f| f << "{{ mustache }}" } + File.open(File.join(template_dir, "slate_index.mustache"), "w+") { |f| f << "{{ mustache }}" } FileUtils.mkdir_p(configuration.docs_dir) writer.write diff --git a/templates/rspec_api_documentation/slate_index.mustache b/templates/rspec_api_documentation/slate_index.mustache new file mode 100644 index 00000000..be0e5ae8 --- /dev/null +++ b/templates/rspec_api_documentation/slate_index.mustache @@ -0,0 +1,8 @@ +--- +title: {{ api_name }} +language_tabs: + - json: JSON + - shell: cURL +--- + +{{{ api_explanation }}}