grape-swagger 0.26.0 → 0.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.gitignore +1 -0
- data/.rspec +0 -1
- data/.rubocop.yml +4 -0
- data/.travis.yml +26 -24
- data/CHANGELOG.md +14 -0
- data/Gemfile +3 -5
- data/README.md +9 -85
- data/UPGRADING.md +9 -0
- data/lib/grape-swagger.rb +20 -71
- data/lib/grape-swagger/doc_methods.rb +4 -6
- data/lib/grape-swagger/doc_methods/parse_params.rb +5 -9
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +18 -5
- data/lib/grape-swagger/endpoint.rb +8 -10
- data/lib/grape-swagger/errors.rb +7 -6
- data/lib/grape-swagger/rake/oapi_tasks.rb +5 -1
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/issues/532_allow_custom_format_spec.rb +36 -0
- data/spec/issues/537_enum_values_spec.rb +47 -0
- data/spec/issues/572_array_post_body_spec.rb +49 -0
- data/spec/lib/endpoint_spec.rb +42 -1
- data/spec/lib/extensions_spec.rb +42 -0
- data/spec/lib/oapi_tasks_spec.rb +13 -2
- data/spec/lib/operation_id_spec.rb +1 -5
- data/spec/lib/parse_params_spec.rb +58 -0
- data/spec/lib/tag_name_description_spec.rb +77 -0
- data/spec/lib/version_spec.rb +26 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +1 -77
- data/spec/swagger_v2/description_not_initialized.rb +1 -1
- data/spec/swagger_v2/guarded_endpoint_spec.rb +2 -2
- metadata +16 -9
- data/lib/grape-swagger/markdown/kramdown_adapter.rb +0 -37
- data/lib/grape-swagger/markdown/redcarpet_adapter.rb +0 -92
- data/spec/markdown/kramdown_adapter_spec.rb +0 -31
- data/spec/markdown/redcarpet_adapter_spec.rb +0 -66
@@ -7,11 +7,7 @@ describe GrapeSwagger::DocMethods::OperationId do
|
|
7
7
|
specify { expect(subject).to respond_to :build }
|
8
8
|
|
9
9
|
describe 'build' do
|
10
|
-
|
11
|
-
let(:route) { Grape::Route.new(method: method) }
|
12
|
-
else
|
13
|
-
let(:route) { Grape::Router::Route.new(method, '/path', requirements: {}) }
|
14
|
-
end
|
10
|
+
let(:route) { Grape::Router::Route.new(method, '/path', requirements: {}) }
|
15
11
|
|
16
12
|
describe 'GET' do
|
17
13
|
let(:method) { 'GET' }
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GrapeSwagger::DocMethods::ParseParams do
|
4
|
+
subject { described_class }
|
5
|
+
let(:start_value) { -5 }
|
6
|
+
let(:end_value) { 5 }
|
7
|
+
|
8
|
+
describe '#parse_range_values' do
|
9
|
+
specify do
|
10
|
+
parsed_range = subject.send(:parse_range_values, start_value..end_value)
|
11
|
+
expect(parsed_range).to eql(minimum: start_value, maximum: end_value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#parse_enum_or_range_values' do
|
16
|
+
describe 'value as Range' do
|
17
|
+
describe 'first Integer' do
|
18
|
+
specify do
|
19
|
+
parsed_range = subject.send(:parse_enum_or_range_values, start_value..end_value)
|
20
|
+
expect(parsed_range).to eql(minimum: start_value, maximum: end_value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'first String' do
|
25
|
+
specify do
|
26
|
+
parsed_range = subject.send(:parse_enum_or_range_values, 'a'..'z')
|
27
|
+
expect(parsed_range).to be_nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'value as Proc' do
|
33
|
+
describe 'as Range' do
|
34
|
+
let(:values) { proc { start_value..end_value } }
|
35
|
+
specify do
|
36
|
+
parsed_range = subject.send(:parse_enum_or_range_values, values)
|
37
|
+
expect(parsed_range).to eql(minimum: start_value, maximum: end_value)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'as Array' do
|
42
|
+
let(:values) { proc { %w(a b c) } }
|
43
|
+
specify do
|
44
|
+
parsed_range = subject.send(:parse_enum_or_range_values, values)
|
45
|
+
expect(parsed_range).to eql(enum: %w(a b c))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'values as Array -> enums' do
|
51
|
+
let(:values) { %w(a b c) }
|
52
|
+
specify do
|
53
|
+
parsed_range = subject.send(:parse_enum_or_range_values, values)
|
54
|
+
expect(parsed_range).to eql(enum: %w(a b c))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GrapeSwagger::DocMethods::TagNameDescription do
|
4
|
+
describe '#build_memo' do
|
5
|
+
let(:tag) { 'some_string' }
|
6
|
+
subject { described_class.send(:build_memo, tag) }
|
7
|
+
|
8
|
+
specify do
|
9
|
+
expect(subject.keys).to eql [:name, :description]
|
10
|
+
expect(subject).to eql(
|
11
|
+
name: tag,
|
12
|
+
description: "Operations about #{tag.pluralize}"
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#build' do
|
18
|
+
subject { described_class.build(paths) }
|
19
|
+
describe 'empty paths' do
|
20
|
+
let(:paths) { {} }
|
21
|
+
specify do
|
22
|
+
expect(subject).to eql([])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'paths given' do
|
27
|
+
describe 'uniq as String' do
|
28
|
+
let(:paths) do
|
29
|
+
{ key_1: { post: { tags: 'tags_given' } } }
|
30
|
+
end
|
31
|
+
|
32
|
+
specify do
|
33
|
+
expect(subject).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'uniq as Array' do
|
38
|
+
let(:paths) do
|
39
|
+
{ key_1: { post: { tags: ['tags_given'] } } }
|
40
|
+
end
|
41
|
+
|
42
|
+
specify do
|
43
|
+
expect(subject).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'multiple' do
|
48
|
+
describe 'uniq key' do
|
49
|
+
let(:paths) do
|
50
|
+
{
|
51
|
+
key_1: { post: { tags: %w(tags_given another_tag_given) } }
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
specify do
|
56
|
+
expect(subject).to eql [
|
57
|
+
{ name: 'tags_given', description: 'Operations about tags_givens' },
|
58
|
+
{ name: 'another_tag_given', description: 'Operations about another_tag_givens' }
|
59
|
+
]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
describe 'under different keys' do
|
63
|
+
let(:paths) do
|
64
|
+
{
|
65
|
+
key_1: { post: { tags: ['tags_given'] } },
|
66
|
+
key_2: { post: { tags: ['tags_given'] } }
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
specify do
|
71
|
+
expect(subject).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GrapeSwagger::DocMethods::Version do
|
4
|
+
let(:route) { OpenStruct.new(version: version) }
|
5
|
+
subject { described_class.get(route) }
|
6
|
+
|
7
|
+
describe 'grape 0.16.2 version' do
|
8
|
+
let(:version) { '[:v1, :v2]' }
|
9
|
+
it { is_expected.to be_a Array }
|
10
|
+
it { is_expected.to eql [:v1, :v2] }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'newer grape versions' do
|
14
|
+
describe 'as String' do
|
15
|
+
let(:version) { 'v1' }
|
16
|
+
it { is_expected.to be_a String }
|
17
|
+
it { is_expected.to eql 'v1' }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'as Array' do
|
21
|
+
let(:version) { [:v1, :v2] }
|
22
|
+
it { is_expected.to be_a Array }
|
23
|
+
it { is_expected.to eql [:v1, :v2] }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter 'spec/'
|
6
|
+
add_filter 'example/'
|
7
|
+
end
|
8
|
+
|
9
|
+
Coveralls.wear!
|
10
|
+
|
1
11
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
12
|
|
3
13
|
MODEL_PARSER = ENV.key?('MODEL_PARSER') ? ENV['MODEL_PARSER'].to_s.downcase.sub('grape-swagger-', '') : 'mock'
|
4
14
|
|
5
15
|
require 'grape'
|
6
16
|
require 'grape-swagger'
|
17
|
+
|
7
18
|
Dir[File.join(Dir.getwd, 'spec/support/*.rb')].each { |f| require f }
|
8
19
|
require "grape-swagger/#{MODEL_PARSER}" if MODEL_PARSER != 'mock'
|
9
20
|
require File.join(Dir.getwd, "spec/support/model_parsers/#{MODEL_PARSER}_parser.rb")
|
@@ -46,7 +46,7 @@ describe 'details' do
|
|
46
46
|
{ 'declared_params' => declared(params) }
|
47
47
|
end
|
48
48
|
|
49
|
-
add_swagger_documentation
|
49
|
+
add_swagger_documentation markdown: 'foo'
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -74,80 +74,4 @@ describe 'details' do
|
|
74
74
|
expect(subject['paths']['/use_detail_block']['get']['description']).to eql 'detailed description of the route inside the `desc` block'
|
75
75
|
end
|
76
76
|
end
|
77
|
-
|
78
|
-
describe 'details, convert markdown with kramdown' do
|
79
|
-
include_context "#{MODEL_PARSER} swagger example"
|
80
|
-
|
81
|
-
before :all do
|
82
|
-
module TheApi
|
83
|
-
class GfmDetailApi < Grape::API
|
84
|
-
format :json
|
85
|
-
|
86
|
-
desc 'This returns something',
|
87
|
-
detail: details,
|
88
|
-
entity: Entities::UseResponse,
|
89
|
-
failure: [{ code: 400, model: Entities::ApiError }]
|
90
|
-
get '/use_gfm_detail' do
|
91
|
-
{ 'declared_params' => declared(params) }
|
92
|
-
end
|
93
|
-
|
94
|
-
add_swagger_documentation markdown: GrapeSwagger::Markdown::KramdownAdapter.new
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def app
|
100
|
-
TheApi::GfmDetailApi
|
101
|
-
end
|
102
|
-
|
103
|
-
subject do
|
104
|
-
get '/swagger_doc'
|
105
|
-
JSON.parse(last_response.body)
|
106
|
-
end
|
107
|
-
|
108
|
-
specify do
|
109
|
-
expect(subject['paths']['/use_gfm_detail']['get']).to include('description')
|
110
|
-
expect(subject['paths']['/use_gfm_detail']['get']['description']).to eql(
|
111
|
-
"<h1 id=\"burgers-in-heaven\">Burgers in Heaven</h1>\n\n<blockquote>\n <p>A burger doesn’t come for free</p>\n</blockquote>\n\n<p>If you want to reserve a burger in heaven, you have to do<br />\nsome crazy stuff on earth.</p>\n\n<pre><code>def do_good\nputs 'help people'\nend\n</code></pre>\n\n<ul>\n <li><em>Will go to Heaven:</em> Probably</li>\n <li><em>Will go to Hell:</em> Probably not</li>\n</ul>"
|
112
|
-
)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
describe 'details, convert markdown with redcarpet', unless: RUBY_PLATFORM.eql?('java') do
|
117
|
-
include_context "#{MODEL_PARSER} swagger example"
|
118
|
-
|
119
|
-
before :all do
|
120
|
-
module TheApi
|
121
|
-
class GfmRcDetailApi < Grape::API
|
122
|
-
format :json
|
123
|
-
|
124
|
-
desc 'This returns something',
|
125
|
-
detail: details,
|
126
|
-
entity: Entities::UseResponse,
|
127
|
-
failure: [{ code: 400, model: Entities::ApiError }]
|
128
|
-
get '/use_gfm_rc_detail' do
|
129
|
-
{ 'declared_params' => declared(params) }
|
130
|
-
end
|
131
|
-
|
132
|
-
add_swagger_documentation markdown: GrapeSwagger::Markdown::RedcarpetAdapter.new
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def app
|
138
|
-
TheApi::GfmRcDetailApi
|
139
|
-
end
|
140
|
-
|
141
|
-
subject do
|
142
|
-
get '/swagger_doc'
|
143
|
-
JSON.parse(last_response.body)
|
144
|
-
end
|
145
|
-
|
146
|
-
specify do
|
147
|
-
expect(subject['paths']['/use_gfm_rc_detail']['get']).to include('description')
|
148
|
-
expect(subject['paths']['/use_gfm_rc_detail']['get']['description']).to eql(
|
149
|
-
"<h1>Burgers in Heaven</h1>\n\n<blockquote>\n<p>A burger doesn't come for free</p>\n</blockquote>\n\n<p>If you want to reserve a burger in heaven, you have to do\nsome crazy stuff on earth.</p>\n<pre class=\"highlight plaintext\"><code>def do_good\nputs 'help people'\nend\n</code></pre>\n<ul>\n<li><em>Will go to Heaven:</em> Probably</li>\n<li><em>Will go to Hell:</em> Probably not</li>\n</ul>"
|
150
|
-
)
|
151
|
-
end
|
152
|
-
end
|
153
77
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'details' do
|
4
|
-
describe '
|
4
|
+
describe 'has no description, if details or description are nil' do
|
5
5
|
before :all do
|
6
6
|
module TheApi
|
7
7
|
class GfmRcDetailApi < Grape::API
|
@@ -36,8 +36,8 @@ class SampleAuth < Grape::Middleware::Base
|
|
36
36
|
context.protected_endpoint = context.options[:route_options][:auth].present?
|
37
37
|
|
38
38
|
return unless context.protected_endpoint?
|
39
|
-
scopes = context.options[:route_options][:auth][:scopes]
|
40
|
-
authorize!(*scopes) unless scopes.include?
|
39
|
+
scopes = context.options[:route_options][:auth][:scopes]
|
40
|
+
authorize!(*scopes) unless scopes.include? false
|
41
41
|
context.access_token = env['HTTP_AUTHORIZATION']
|
42
42
|
end
|
43
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.26.
|
4
|
+
version: 0.26.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Vandecasteele
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -31,6 +31,7 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- ".coveralls.yml"
|
34
35
|
- ".document"
|
35
36
|
- ".gitignore"
|
36
37
|
- ".rspec"
|
@@ -71,18 +72,19 @@ files:
|
|
71
72
|
- lib/grape-swagger/doc_methods/version.rb
|
72
73
|
- lib/grape-swagger/endpoint.rb
|
73
74
|
- lib/grape-swagger/errors.rb
|
74
|
-
- lib/grape-swagger/markdown/kramdown_adapter.rb
|
75
|
-
- lib/grape-swagger/markdown/redcarpet_adapter.rb
|
76
75
|
- lib/grape-swagger/model_parsers.rb
|
77
76
|
- lib/grape-swagger/rake/oapi_tasks.rb
|
78
77
|
- lib/grape-swagger/version.rb
|
79
78
|
- spec/issues/403_versions_spec.rb
|
80
79
|
- spec/issues/427_entity_as_string_spec.rb
|
81
80
|
- spec/issues/430_entity_definitions_spec.rb
|
81
|
+
- spec/issues/532_allow_custom_format_spec.rb
|
82
82
|
- spec/issues/533_specify_status_code_spec.rb
|
83
|
+
- spec/issues/537_enum_values_spec.rb
|
83
84
|
- spec/issues/539_array_post_body_spec.rb
|
84
85
|
- spec/issues/542_array_of_type_in_post_body_spec.rb
|
85
86
|
- spec/issues/553_align_array_put_post_params_spec.rb
|
87
|
+
- spec/issues/572_array_post_body_spec.rb
|
86
88
|
- spec/lib/data_type_spec.rb
|
87
89
|
- spec/lib/endpoint_spec.rb
|
88
90
|
- spec/lib/extensions_spec.rb
|
@@ -91,10 +93,11 @@ files:
|
|
91
93
|
- spec/lib/oapi_tasks_spec.rb
|
92
94
|
- spec/lib/operation_id_spec.rb
|
93
95
|
- spec/lib/optional_object_spec.rb
|
96
|
+
- spec/lib/parse_params_spec.rb
|
94
97
|
- spec/lib/path_string_spec.rb
|
95
98
|
- spec/lib/produces_consumes_spec.rb
|
96
|
-
- spec/
|
97
|
-
- spec/
|
99
|
+
- spec/lib/tag_name_description_spec.rb
|
100
|
+
- spec/lib/version_spec.rb
|
98
101
|
- spec/spec_helper.rb
|
99
102
|
- spec/support/empty_model_parser.rb
|
100
103
|
- spec/support/grape_version.rb
|
@@ -172,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
175
|
version: '0'
|
173
176
|
requirements: []
|
174
177
|
rubyforge_project:
|
175
|
-
rubygems_version: 2.6.
|
178
|
+
rubygems_version: 2.6.10
|
176
179
|
signing_key:
|
177
180
|
specification_version: 4
|
178
181
|
summary: Add auto generated documentation to your Grape API that can be displayed
|
@@ -181,10 +184,13 @@ test_files:
|
|
181
184
|
- spec/issues/403_versions_spec.rb
|
182
185
|
- spec/issues/427_entity_as_string_spec.rb
|
183
186
|
- spec/issues/430_entity_definitions_spec.rb
|
187
|
+
- spec/issues/532_allow_custom_format_spec.rb
|
184
188
|
- spec/issues/533_specify_status_code_spec.rb
|
189
|
+
- spec/issues/537_enum_values_spec.rb
|
185
190
|
- spec/issues/539_array_post_body_spec.rb
|
186
191
|
- spec/issues/542_array_of_type_in_post_body_spec.rb
|
187
192
|
- spec/issues/553_align_array_put_post_params_spec.rb
|
193
|
+
- spec/issues/572_array_post_body_spec.rb
|
188
194
|
- spec/lib/data_type_spec.rb
|
189
195
|
- spec/lib/endpoint_spec.rb
|
190
196
|
- spec/lib/extensions_spec.rb
|
@@ -193,10 +199,11 @@ test_files:
|
|
193
199
|
- spec/lib/oapi_tasks_spec.rb
|
194
200
|
- spec/lib/operation_id_spec.rb
|
195
201
|
- spec/lib/optional_object_spec.rb
|
202
|
+
- spec/lib/parse_params_spec.rb
|
196
203
|
- spec/lib/path_string_spec.rb
|
197
204
|
- spec/lib/produces_consumes_spec.rb
|
198
|
-
- spec/
|
199
|
-
- spec/
|
205
|
+
- spec/lib/tag_name_description_spec.rb
|
206
|
+
- spec/lib/version_spec.rb
|
200
207
|
- spec/spec_helper.rb
|
201
208
|
- spec/support/empty_model_parser.rb
|
202
209
|
- spec/support/grape_version.rb
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module GrapeSwagger
|
2
|
-
class Markdown
|
3
|
-
class KramdownAdapter
|
4
|
-
attr_reader :options
|
5
|
-
|
6
|
-
###
|
7
|
-
# Initializes the kramdown adapter with options.
|
8
|
-
# See kramdown documentation what options can be passed.
|
9
|
-
# Default it uses Github flavoured markup as input and won't use coderay as converter for syntax highlighting.
|
10
|
-
# config: an hash of configuration options to be passed to the kramdown.
|
11
|
-
# usage:
|
12
|
-
# Add the kramdown gem to your gemfile or run:
|
13
|
-
# $ (sudo) gem install kramdown
|
14
|
-
#
|
15
|
-
# Then pass a new instance of GrapeSwagger::Markdown::KramdownAdapter as markdown option.
|
16
|
-
###
|
17
|
-
def initialize(config = {})
|
18
|
-
require 'kramdown'
|
19
|
-
defaults = {
|
20
|
-
input: 'GFM',
|
21
|
-
enable_coderay: false
|
22
|
-
}
|
23
|
-
@options = defaults.merge(config)
|
24
|
-
rescue LoadError
|
25
|
-
raise GrapeSwagger::Errors::MarkdownDependencyMissingError, 'kramdown'
|
26
|
-
end
|
27
|
-
|
28
|
-
###
|
29
|
-
# marks down the given text to html format.
|
30
|
-
# text: The text to be formatted.
|
31
|
-
###
|
32
|
-
def markdown(text)
|
33
|
-
Kramdown::Document.new(text, @options).to_html
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,92 +0,0 @@
|
|
1
|
-
module GrapeSwagger
|
2
|
-
class Markdown
|
3
|
-
class RedcarpetAdapter
|
4
|
-
module RenderWithoutSyntaxHighlighter
|
5
|
-
require 'cgi'
|
6
|
-
|
7
|
-
def block_code(code, language)
|
8
|
-
language ||= 'text'
|
9
|
-
"<div class=\"code_highlight\">
|
10
|
-
<pre><code class=\"highlight #{language}\">#{CGI.escapeHTML(code)}</code></pre>
|
11
|
-
</div>"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
attr_reader :extension_options
|
16
|
-
|
17
|
-
attr_reader :render_options
|
18
|
-
|
19
|
-
###
|
20
|
-
# Initializes the redcarpet adapter with markup options.
|
21
|
-
# See redcarpet documentation what options can be passed.
|
22
|
-
# Default it uses fenced_code_blocks, autolinks and rouge as syntax highlighter.
|
23
|
-
# To configure an highlighter add {highlighter: :value} to the extentions hash.
|
24
|
-
# Currently supported highlighters:
|
25
|
-
# :rouge
|
26
|
-
#
|
27
|
-
# extensions: an hash of configuration options to be passed to markdown.
|
28
|
-
# render_options: an hash of configuration options to be passed to renderer.
|
29
|
-
#
|
30
|
-
# usage:
|
31
|
-
# Add the redcarpet gem to your gemfile or run:
|
32
|
-
# $ (sudo) gem install redcarpet
|
33
|
-
# when you want to have rouge as syntax highlighter add rouge to the gemfile or run:
|
34
|
-
# $ (sudo) gem install rouge
|
35
|
-
#
|
36
|
-
# GrapeSwagger::Markdown::RedcarpetAdapter.new({highlighter: :none},{no_links: true})
|
37
|
-
# will use no syntax highlighter and won't render links.
|
38
|
-
###
|
39
|
-
def initialize(options = {})
|
40
|
-
require 'redcarpet'
|
41
|
-
extentions_defaults = {
|
42
|
-
fenced_code_blocks: true,
|
43
|
-
autolink: true
|
44
|
-
}
|
45
|
-
render_defaults = { highlighter: :rouge }
|
46
|
-
@extension_options = extentions_defaults.merge(options.fetch(:extensions, {}))
|
47
|
-
@render_options = render_defaults.merge(options.fetch(:render_options, {}))
|
48
|
-
@renderer = new_redcarpet_renderer(@render_options.delete(:highlighter)).new(@render_options)
|
49
|
-
@markdown = Redcarpet::Markdown.new(@renderer, @extension_options)
|
50
|
-
rescue LoadError
|
51
|
-
raise GrapeSwagger::Errors::MarkdownDependencyMissingError, 'redcarpet'
|
52
|
-
end
|
53
|
-
|
54
|
-
###
|
55
|
-
# Marks down the given text to html format.
|
56
|
-
###
|
57
|
-
def markdown(text)
|
58
|
-
@markdown.render(text)
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
###
|
64
|
-
# Creates a new redcarpet renderer based on the highlighter given.
|
65
|
-
#
|
66
|
-
# render_options: options passed to the renderer.
|
67
|
-
#
|
68
|
-
# usage:
|
69
|
-
# new_redcarpet_renderer(:rouge) # uses rouge as highlighter.
|
70
|
-
# new_redcarpet_renderer # no highlight plugin
|
71
|
-
###
|
72
|
-
def new_redcarpet_renderer(syntax_highlighter = nil)
|
73
|
-
case syntax_highlighter
|
74
|
-
when :rouge
|
75
|
-
begin
|
76
|
-
Class.new(Redcarpet::Render::HTML) do
|
77
|
-
require 'rouge'
|
78
|
-
require 'rouge/plugins/redcarpet'
|
79
|
-
include Rouge::Plugins::Redcarpet
|
80
|
-
end
|
81
|
-
rescue LoadError
|
82
|
-
raise GrapeSwagger::Errors::MarkdownDependencyMissingError, 'rouge'
|
83
|
-
end
|
84
|
-
else
|
85
|
-
Class.new(Redcarpet::Render::HTML) do
|
86
|
-
include RenderWithoutSyntaxHighlighter
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|