rapi_doc 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ module RapiDoc
2
+ module RapiConfig
3
+
4
+ # following helper methods return the directory location if no file type is specified or return the file location
5
+ # for that directory if one is supplied
6
+ def template_dir(f = nil)
7
+ @template_dir ||= File.join(File.dirname(__FILE__), '../../templates')
8
+ form_file_name @template_dir, f
9
+ end
10
+
11
+ def config_dir(f = nil)
12
+ @config_dir ||= File.join(::Rails.root.to_s, 'config/rapi_doc')
13
+ form_file_name @config_dir, f
14
+ end
15
+
16
+ def target_dir(f = nil)
17
+ @target_dir ||= File.join(::Rails.root.to_s, 'public/apidoc/')
18
+ form_file_name @target_dir, f
19
+ end
20
+
21
+ def controller_dir(f = nil)
22
+ @controller_dir ||= File.join(::Rails.root.to_s, 'app/controllers/')
23
+ form_file_name @controller_dir, f
24
+ end
25
+
26
+ # WARNING! - temp_dir will return different location for different runs. Use with Caution!
27
+ def temp_dir(f = nil)
28
+ @temp_dir ||= "#{Dir.mktmpdir("apidoc")}/"
29
+ form_file_name @temp_dir, f
30
+ end
31
+
32
+ def form_file_name(dir, file)
33
+ case file
34
+ when NilClass then dir
35
+ when String then File.join(dir, file)
36
+ else raise ArgumentError, "Invalid argument #{file}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require_relative 'method_doc'
3
+
4
+ module RapiDoc
5
+ # custom exception class
6
+ class ParsingException < Exception;
7
+ end
8
+ # ResourceDoc holds the information a resource contains. It parses the class header and also the
9
+ # method documentation, which will be contained in MethodDoc.
10
+ class ResourceDoc
11
+
12
+ attr_reader :name, :resource_location, :controller_name, :class_block, :function_blocks, :resource_header, :resource_methods
13
+
14
+ # Initializes ResourceDoc.
15
+ def initialize(name, resource_location, controller_location, options = {})
16
+ @name = name
17
+ @class_block = nil
18
+ @function_blocks = []
19
+ @resource_methods = []
20
+ @resource_header = ""
21
+ @standard_methods = options[:standard_methods] || [:put, :post, :get, :delete]
22
+ @resource_location = resource_location
23
+ @controller_location = controller_location
24
+ @controller_name = File.basename(controller_location)
25
+ end
26
+
27
+ # parse the controller
28
+ def parse_apidoc!(class_template, method_template)
29
+ lines = IO.readlines(@controller_location)
30
+ begin
31
+ @class_block, @function_blocks = ResourceDoc.parse_controller_doc(@name, lines)
32
+ rescue ParsingException => ex
33
+ puts "error #{ex} while parsing #{@controller_location}"
34
+ exit
35
+ else
36
+ @resource_header = Haml::Engine.new(class_template).render(@class_block) unless class_block.nil?
37
+ @resource_methods = @function_blocks.each_with_index.collect do |method_block, i|
38
+ Haml::Engine.new(method_template).render(method_block)
39
+ end
40
+ end
41
+ end
42
+
43
+ # This method parses the doc
44
+ def self.parse_controller_doc(name, lines)
45
+ current_api_block = nil
46
+ current_scope = :none
47
+ in_class = false
48
+ class_block = nil
49
+ function_blocks = []
50
+ order = 1
51
+ lines.each_with_index do |line, line_no|
52
+ line.gsub!(/^ *#/, '') # strip the starting '#' on the line
53
+ case line
54
+ when /=begin apidoc/
55
+ # if we get apidoc tag inside class definition, then they are for a method
56
+ current_scope = !in_class ? :class : :function
57
+ current_api_block = MethodDoc.new(name, current_scope, order)
58
+ when /=end/
59
+ if current_api_block.nil?
60
+ raise ParsingException, "#{line_no} - No starttag for '=end' found"
61
+ else
62
+ case current_scope
63
+ when :class
64
+ class_block = current_api_block
65
+ when :function
66
+ function_blocks << current_api_block
67
+ else
68
+ raise ParsingException, "logic error: unknown current scope #{current_scope}"
69
+ end
70
+ current_api_block = nil
71
+ current_scope = :none
72
+ order += 1
73
+ end
74
+ when /class/ # keep track of whether a resource or an api is being annotated
75
+ in_class = true
76
+ else
77
+ if current_api_block # process ines only if they are apidoc comments
78
+ current_scope = current_api_block.process_line(line, current_scope)
79
+ end
80
+ end
81
+ end
82
+ [class_block, function_blocks]
83
+ end
84
+ end
85
+ end
@@ -1,41 +1,40 @@
1
- include RapiDoc::RapiConfig
1
+ include RapiDoc::RAPIDoc
2
2
 
3
3
  namespace :rapi_doc do
4
+
4
5
  desc "Generate the config files"
5
6
  task :setup do
6
- if File.directory?(config_dir)
7
- puts "#{BASE_DIR}/#{File.basename(config_dir)} exists"
8
- else
9
- FileUtils.mkdir(config_dir)
10
- end
11
- %w(config_file layout_file class_layout_file frameset_file main_file).each do |type_file|
12
- target_file = send(type_file, :target)
13
- template_file = send(type_file, :template)
14
- if File.exist? target_file
15
- puts "#{BASE_DIR}/#{File.basename(target_file)} exists"
16
- else
17
- FileUtils.cp template_file, config_dir
18
- puts "Generated #{BASE_DIR}/#{File.basename(template_file)}" # TODO Add instructions for users to update the config file
19
- end
20
- end
7
+ create_structure!
8
+ #puts "Now specify controllers in config/rapi_doc/config.yml for which api documentation is to be generated and then run rapi_doc::generate"
21
9
  end
22
- desc "Generate the API Documentation"
10
+
11
+ desc "Generate the api documentation"
23
12
  task :generate do
24
- begin
25
- yml = YAML::load(File.open(config_file(:target)))
26
- rescue
27
- puts "It seems that you don't have the config files yet. Please run rake rapi_doc:setup"
13
+ resources = get_resources! do |controller, controller_url, controller_location|
14
+ print "Generate documentation for resource \"#{controller}\" mapped at \"#{controller_url}\" (\"#{File.basename(controller_location)}\")? (Y/n):"
15
+ response = STDIN.gets.chomp
16
+ ['y', 'Y'].include? response
28
17
  end
29
-
30
- # Generating documentations
31
- if yml
32
- resources = []
33
- yml.keys.each do |key|
34
- resources << RapiDoc::ResourceDoc.new(key, yml[key]["location"], yml[key]["controller_name"])
35
- end
36
-
18
+ if resources.empty?
19
+ puts "Nothing to generate"
20
+ #puts "Please specify controllers in config/rapi_doc/config.yml for which api documentation is to be generated and then run rapi_doc::generate again"
21
+ else
37
22
  # generate the apidoc
38
- RapiDoc::RAPIDoc.new(resources)
23
+ puts "Generating API documentation..."
24
+ generate_templates!(resources)
25
+ move_structure!
26
+ puts "Finished."
39
27
  end
40
28
  end
29
+
30
+ desc "Clean up generated documentation"
31
+ task :clean do
32
+ remove_structure!
33
+ end
34
+
35
+ desc "Clean up everything - generated documentation and all the config"
36
+ task :distclean do
37
+ remove_all!
38
+ end
39
+
41
40
  end
Binary file
@@ -0,0 +1,17 @@
1
+ .resource_header
2
+ %h2= @name
3
+ %p.resource_summary= @content
4
+ - if @xml || @json
5
+ %h3 Representations
6
+ - if @json
7
+ .json
8
+ %p
9
+ %strong JSON representation:
10
+ %p
11
+ %textarea= @json
12
+ - if @xml
13
+ .xml
14
+ %p
15
+ %strong XML representation:
16
+ %p
17
+ %textarea= @xml
@@ -0,0 +1,38 @@
1
+ .resource_methods
2
+ %a{:id => "#{@resource_name}_method_#{@method_order}"}
3
+ %p
4
+ %strong Description:
5
+ = @content
6
+ %h2= @url
7
+ - if @access
8
+ %strong Access:
9
+ = @access
10
+ %br/
11
+ - if @return
12
+ %strong Return:
13
+ = @return
14
+ %br/
15
+ - if @params
16
+ %strong Parameters:
17
+ %ul
18
+ - @params.each do |v|
19
+ %li= v
20
+ - unless @outputs.empty?
21
+ %ul.output_format
22
+ - @outputs.each do |output_format, output|
23
+ %li{:class => output_format}
24
+ %a{:href => "##{"#{@resource_name}_#{output_format}_#{@method_order}"}"}= output_format
25
+ .output
26
+ - @outputs.each do |output_format, output|
27
+ %div{:class => output_format}
28
+ %a{:id => "#{@resource_name}_#{output_format}_#{@method_order}"}
29
+ %pre.prettyprint= output
30
+ %br/
31
+ - unless @request.blank?
32
+ %strong Request:
33
+ %pre.code
34
+ %code= @request
35
+ - unless @response.blank?
36
+ %strong Response:
37
+ %pre.code
38
+ %code= @response
@@ -0,0 +1,40 @@
1
+ !!!
2
+ %html
3
+ %head
4
+ %meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
5
+ %link{:href => "styles.css", :rel => "stylesheet", :type => "text/css"}/
6
+ %script{:src => "http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js", :type => "text/javascript"}
7
+ %script{:src => "scripts.js", :type => "text/javascript"}
8
+ %title apidoc
9
+ %body
10
+ #resource_index
11
+ %form{:onsubmit => "return false;"}
12
+ %input{:type => "text"}/
13
+ %img{:alt => "search", :src => "Search.png"}
14
+ %h2 Resources
15
+ %ul#resource_index
16
+ - resource_docs.each do |resource|
17
+ %li
18
+ %a{:href => "##{resource.name}"}= resource.name
19
+ - if resource.function_blocks.any?
20
+ %ul
21
+ - resource.function_blocks.each do |method_block|
22
+ %li
23
+ %a{:href => "##{resource.name}_method_#{method_block.method_order}"}
24
+ = "#{method_block.method} #{method_block.url}"
25
+ #resources
26
+ %ul
27
+ %li
28
+ %strong Show XML
29
+ %a{:href => "#", :onclick => "$('.xml').toggle();"} toggle
30
+ %li
31
+ %strong Show JSON
32
+ %a{:href => "#", :onclick => "$('.json').toggle();"} toggle
33
+ - resource_docs.each do |resource|
34
+ %a{:id => "#{resource.name}"}
35
+ .resource
36
+ - if resource.resource_header
37
+ = resource.resource_header
38
+ - if resource.resource_methods
39
+ - resource.resource_methods.each do |method|
40
+ = method
@@ -0,0 +1,22 @@
1
+ /**
2
+ * created by: salilwadnerkar
3
+ * Date: 11/4/12
4
+ */
5
+ $(function () {
6
+ $('#resource_index input').keyup(function(event) {
7
+ if ( event.keyCode == 13 ) {
8
+ event.preventDefault();
9
+ } else {
10
+ var search_txt = $(this).val();
11
+ var search_regex = new RegExp(search_txt, 'g');
12
+ $("#resource_index ul li").each ( function() {
13
+ var elem = $(this)
14
+ var h_elem = elem.find('a')[0];
15
+ var link_txt = h_elem.text;
16
+ var match = link_txt.match(search_regex);
17
+ match ? elem.show() : elem.hide();
18
+ });
19
+ }
20
+ });
21
+ });
22
+
@@ -0,0 +1,80 @@
1
+ html, body {
2
+ width: 100%;
3
+ height: 100%;
4
+ }
5
+
6
+ body {
7
+ line-height: 1.5em;
8
+ }
9
+
10
+ #resources {
11
+ float: right;
12
+ width: 80%;
13
+ }
14
+
15
+ #resource_index {
16
+ float: left;
17
+ height: 100%;
18
+ position: fixed;
19
+ width: 19%;
20
+ background-color: #ade;
21
+ }
22
+
23
+ #resource_index li {
24
+ background-color: #6699AA;
25
+ color:white;
26
+ padding: 5px 10px 0;
27
+ }
28
+
29
+ #resource_index li a {
30
+ display: block;
31
+ text-decoration: none;
32
+ font-weight: bold;
33
+ color:#ffffff;
34
+ }
35
+
36
+ #resource_index form img {
37
+ height: 25px;
38
+ width: 25px;
39
+ }
40
+
41
+ #resources h2 {
42
+ background-color: #6699AA;
43
+ color:white;
44
+ }
45
+
46
+ ul, ul li {
47
+ margin: 0;
48
+ padding: 0;
49
+ list-style: none outside;
50
+ }
51
+
52
+ h1, h2, h3 {
53
+ padding-top: 10px;
54
+ padding-bottom: 10px;
55
+ }
56
+
57
+ textarea {
58
+ width:100%;
59
+ height:200px;
60
+ display:none;
61
+ }
62
+
63
+ #method {
64
+ padding: 15px 30px;
65
+ border-top:1px solid #ddd;
66
+ }
67
+
68
+ #http_verb {
69
+ color:#ff6600;
70
+ width:200px;
71
+ margin-right:20px;'
72
+ }
73
+
74
+ .description{
75
+ font-size: 1.4em;
76
+ color:#262626;
77
+ }
78
+ .code {
79
+ font-family: monospace;
80
+ }
metadata CHANGED
@@ -1,20 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapi_doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Husein Choroomi
9
9
  - Adinda Praditya
10
+ - Salil Wadnerkar
10
11
  autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2012-02-17 00:00:00.000000000Z
14
+ date: 2012-05-22 00:00:00.000000000Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: activesupport
17
- requirement: &70302725893440 !ruby/object:Gem::Requirement
18
+ requirement: &70219113103900 !ruby/object:Gem::Requirement
18
19
  none: false
19
20
  requirements:
20
21
  - - ! '>='
@@ -22,43 +23,65 @@ dependencies:
22
23
  version: '2.1'
23
24
  type: :runtime
24
25
  prerelease: false
25
- version_requirements: *70302725893440
26
+ version_requirements: *70219113103900
27
+ - !ruby/object:Gem::Dependency
28
+ name: haml
29
+ requirement: &70219113100140 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *70219113100140
38
+ - !ruby/object:Gem::Dependency
39
+ name: rdoc
40
+ requirement: &70219113098480 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *70219113098480
26
49
  - !ruby/object:Gem::Dependency
27
50
  name: rspec
28
- requirement: &70302725892500 !ruby/object:Gem::Requirement
51
+ requirement: &70219113096980 !ruby/object:Gem::Requirement
29
52
  none: false
30
53
  requirements:
31
- - - ~>
54
+ - - ! '>='
32
55
  - !ruby/object:Gem::Version
33
56
  version: 2.7.0
34
57
  type: :development
35
58
  prerelease: false
36
- version_requirements: *70302725892500
59
+ version_requirements: *70219113096980
37
60
  - !ruby/object:Gem::Dependency
38
61
  name: bundler
39
- requirement: &70302725891540 !ruby/object:Gem::Requirement
62
+ requirement: &70219113095540 !ruby/object:Gem::Requirement
40
63
  none: false
41
64
  requirements:
42
- - - ~>
65
+ - - ! '>='
43
66
  - !ruby/object:Gem::Version
44
67
  version: 1.0.0
45
68
  type: :development
46
69
  prerelease: false
47
- version_requirements: *70302725891540
70
+ version_requirements: *70219113095540
48
71
  - !ruby/object:Gem::Dependency
49
72
  name: jeweler
50
- requirement: &70302725890700 !ruby/object:Gem::Requirement
73
+ requirement: &70219113094520 !ruby/object:Gem::Requirement
51
74
  none: false
52
75
  requirements:
53
- - - ~>
76
+ - - ! '>='
54
77
  - !ruby/object:Gem::Version
55
78
  version: 1.6.4
56
79
  type: :development
57
80
  prerelease: false
58
- version_requirements: *70302725890700
81
+ version_requirements: *70219113094520
59
82
  - !ruby/object:Gem::Dependency
60
83
  name: rcov
61
- requirement: &70302725889940 !ruby/object:Gem::Requirement
84
+ requirement: &70219113092920 !ruby/object:Gem::Requirement
62
85
  none: false
63
86
  requirements:
64
87
  - - ! '>='
@@ -66,8 +89,31 @@ dependencies:
66
89
  version: '0'
67
90
  type: :development
68
91
  prerelease: false
69
- version_requirements: *70302725889940
70
- description: Rails API Doc Generator
92
+ version_requirements: *70219113092920
93
+ - !ruby/object:Gem::Dependency
94
+ name: haml
95
+ requirement: &70219113085900 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ type: :runtime
102
+ prerelease: false
103
+ version_requirements: *70219113085900
104
+ - !ruby/object:Gem::Dependency
105
+ name: rdoc
106
+ requirement: &70219113085080 !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ type: :runtime
113
+ prerelease: false
114
+ version_requirements: *70219113085080
115
+ description: Rails API Doc Generator. Parses the apidoc annotations to generate HTML
116
+ pages.
71
117
  email: hchoroomi@gmail.com
72
118
  executables: []
73
119
  extensions: []
@@ -75,6 +121,7 @@ extra_rdoc_files:
75
121
  - LICENSE.txt
76
122
  - README.md
77
123
  files:
124
+ - .project
78
125
  - .rvmrc
79
126
  - Gemfile
80
127
  - Gemfile.lock
@@ -82,26 +129,25 @@ files:
82
129
  - README.md
83
130
  - Rakefile
84
131
  - VERSION
132
+ - autoscan.log
133
+ - configure.scan
85
134
  - init.rb
86
135
  - install.rb
87
- - lib/doc_parser.rb
88
- - lib/doc_util.rb
89
- - lib/method_doc.rb
90
- - lib/rapi_config.rb
91
136
  - lib/rapi_doc.rb
137
+ - lib/rapi_doc/method_doc.rb
92
138
  - lib/rapi_doc/railtie.rb
139
+ - lib/rapi_doc/rapi_config.rb
140
+ - lib/rapi_doc/resource_doc.rb
93
141
  - lib/rapi_doc/tasks/rapi_doc_tasks.rake
94
- - lib/resource_doc.rb
95
142
  - spec/doc_parser_spec.rb
96
143
  - spec/method_doc_spec.rb
97
144
  - spec/spec_helper.rb
98
- - templates/_resource_header.html.erb
99
- - templates/_resource_method.html.erb
100
- - templates/class_layout.html.erb
101
- - templates/config.yml
102
- - templates/frameset.html.erb
103
- - templates/layout.html.erb
104
- - templates/main.html.erb
145
+ - templates/Search.png
146
+ - templates/_resource_header.html.haml
147
+ - templates/_resource_method.html.haml
148
+ - templates/index.html.haml
149
+ - templates/scripts.js
150
+ - templates/styles.css
105
151
  - uninstall.rb
106
152
  homepage: http://github.com/elc/rapi_doc
107
153
  licenses:
@@ -118,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
164
  version: '0'
119
165
  segments:
120
166
  - 0
121
- hash: -4532277263417640116
167
+ hash: 3223693436194821202
122
168
  required_rubygems_version: !ruby/object:Gem::Requirement
123
169
  none: false
124
170
  requirements: