Skip to content

Commit f51d4d9

Browse files
Add support for markdown in notes field.
1 parent b148a01 commit f51d4d9

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source "http://rubygems.org"
44
# gem "activesupport", ">= 2.3.5"
55

66
gem 'grape'
7+
gem 'kramdown'
78

89
# Add dependencies to develop your gem here.
910
# Include everything needed to run rake, tests, features, etc.

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ GEM
5252
railties (>= 3.2.0, < 5.0)
5353
thor (~> 0.14)
5454
json (1.7.3)
55+
kramdown (0.13.7)
5556
mail (2.4.4)
5657
i18n (>= 0.4.0)
5758
mime-types (~> 1.16)
@@ -126,6 +127,7 @@ DEPENDENCIES
126127
grape
127128
jeweler (~> 1.8.4)
128129
jquery-rails
130+
kramdown
129131
rack-test
130132
rails (~> 3.2)
131133
rdoc (~> 3.12)

README.markdown

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ You can pass a hash with some configuration possibilities to ```add_swagger_docu
4141
* ```:api_version``` Version of the API that's being exposed
4242
* ```:base_path``` Basepath of the API that's being exposed
4343

44+
## Swagger additions
45+
grape-swagger allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI. The default Swagger UI doesn't allow HTML in the notes field, so you need to use an adapted version of Swagger UI (you can find one at https://github.com/tim-vandecasteele/swagger-ui/tree/vasco).
46+
47+
We're using [kramdown](http://kramdown.rubyforge.org) for parsing the markdown, specific syntax can be found [here](http://kramdown.rubyforge.org/syntax.html).
48+
49+
50+
``` ruby
51+
desc "Reserve a virgin in heaven", {
52+
:notes => <<-NOTE
53+
Virgins in heaven
54+
-----------------
55+
56+
> A virgin doesn't come for free
57+
58+
If you want to reserve a virgin in heaven, you have to do
59+
some crazy stuff on earth.
60+
61+
def do_good
62+
puts 'help people'
63+
end
64+
65+
* _Will go to Heaven:_ Probably
66+
* _Will go to Hell:_ Probably not
67+
NOTE
68+
}
69+
```
70+
4471
## Contributing to grape-swagger
4572

4673
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.

grape-swagger.gemspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
99

1010
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
1111
s.authors = ["Tim Vandecasteele"]
12-
s.date = "2012-07-27"
12+
s.date = "2012-08-02"
1313
s.description = "A simple way to add proper auto generated documentation - that can be displayed with swagger - to your inline described grape API"
1414
s.email = "[email protected]"
1515
s.extra_rdoc_files = [
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
4343

4444
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
4545
s.add_runtime_dependency(%q<grape>, [">= 0"])
46+
s.add_runtime_dependency(%q<kramdown>, [">= 0"])
4647
s.add_development_dependency(%q<shoulda>, [">= 0"])
4748
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
4849
s.add_development_dependency(%q<bundler>, ["> 1.0.0"])
@@ -55,6 +56,7 @@ Gem::Specification.new do |s|
5556
s.add_development_dependency(%q<rspec>, [">= 0"])
5657
else
5758
s.add_dependency(%q<grape>, [">= 0"])
59+
s.add_dependency(%q<kramdown>, [">= 0"])
5860
s.add_dependency(%q<shoulda>, [">= 0"])
5961
s.add_dependency(%q<rdoc>, ["~> 3.12"])
6062
s.add_dependency(%q<bundler>, ["> 1.0.0"])
@@ -68,6 +70,7 @@ Gem::Specification.new do |s|
6870
end
6971
else
7072
s.add_dependency(%q<grape>, [">= 0"])
73+
s.add_dependency(%q<kramdown>, [">= 0"])
7174
s.add_dependency(%q<shoulda>, [">= 0"])
7275
s.add_dependency(%q<rdoc>, ["~> 3.12"])
7376
s.add_dependency(%q<bundler>, ["> 1.0.0"])

lib/grape-swagger.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'kramdown'
2+
13
module Grape
24
class API
35
class << self
@@ -68,17 +70,18 @@ def self.setup(options)
6870

6971
desc 'Swagger compatible API description for specific API', :params =>
7072
{
71-
"name" => { :desc => "Class name of mounted API", :type => "string", :required => true },
73+
"name" => { :desc => "Resource name of mounted API", :type => "string", :required => true },
7274
}
7375
get "#{@@mount_path}/:name" do
7476
header['Access-Control-Allow-Origin'] = '*'
7577
header['Access-Control-Request-Method'] = '*'
7678
routes = @@target_class::combined_routes[params[:name]]
7779
routes_array = routes.map do |route|
80+
notes = route.route_notes ? Kramdown::Document.new(route.route_notes.strip_heredoc).to_html : nil
7881
{
7982
:path => parse_path(route.route_path),
8083
:operations => [{
81-
:notes => route.route_notes,
84+
:notes => notes,
8285
:summary => route.route_description || '',
8386
:nickname => Random.rand(1000000),
8487
:httpMethod => route.route_method,
@@ -129,3 +132,12 @@ def parse_path(path)
129132
end
130133
end
131134
end
135+
136+
class String
137+
# strip_heredoc from rails
138+
# File activesupport/lib/active_support/core_ext/string/strip.rb, line 22
139+
def strip_heredoc
140+
indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
141+
gsub(/^[ \t]{#{indent}}/, '')
142+
end
143+
end

0 commit comments

Comments
 (0)