rain-doc 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/doc.rb +8 -4
- data/lib/doc_part.rb +16 -2
- data/lib/parser.rb +21 -2
- data/lib/rain.rb +63 -5
- data/templates/doc.erb +6 -4
- data/templates/layout.erb +11 -3
- data/templates/nav.erb +7 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ac2daf49d1502a2f8042be98b8bcf2f0a8c285f
|
4
|
+
data.tar.gz: 55fa316386cf105a6c5fe59b0deac896e0e27156
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45a73cb660db67b8a21a012dbf1c05e32d70d853ddbdf4ec805e9c4600e0ccce561e309605c9cbabb55b74f37e86e19bdf81b5dd97a44258c5f789d75de72994
|
7
|
+
data.tar.gz: 90634848c3256aa1261a72e234f7df6216d0c4bafa9a77b8618b04be8f2bcc9104d9e20f8f8c2f3e0ca6d09c5b82c9ad8ea37e33347e081b0e48803699b093df
|
data/lib/doc.rb
CHANGED
@@ -18,10 +18,11 @@ module Rain
|
|
18
18
|
@@open_param_type = nil
|
19
19
|
@@open_param_default = nil
|
20
20
|
@@log_lines = false
|
21
|
+
@@parse_signatures = false
|
21
22
|
|
22
23
|
# sets up the doc using the file name and contents
|
23
24
|
# as a basis.
|
24
|
-
def initialize(file_name, file_contents, log_lines = false)
|
25
|
+
def initialize(file_name, file_contents, log_lines = false, parse_signatures = false)
|
25
26
|
|
26
27
|
# set up basic options and defaults
|
27
28
|
self.file_name = file_name
|
@@ -30,6 +31,7 @@ module Rain
|
|
30
31
|
self.parts = []
|
31
32
|
self.lines = 0
|
32
33
|
@@log_lines = log_lines
|
34
|
+
@@parse_signatures = parse_signatures
|
33
35
|
|
34
36
|
# set the doc type based on extension
|
35
37
|
case self.file_ext
|
@@ -69,7 +71,7 @@ module Rain
|
|
69
71
|
self.file_contents.each_line do |line|
|
70
72
|
|
71
73
|
# parse the current line
|
72
|
-
result = self.parser.parse(line)
|
74
|
+
result = self.parser.parse(line, @@parse_signatures)
|
73
75
|
|
74
76
|
# if there is no documentation for the result,
|
75
77
|
# create a new part and then set the current part to nil.
|
@@ -95,6 +97,8 @@ module Rain
|
|
95
97
|
self.current_part.set_route(result[:route])
|
96
98
|
when :method
|
97
99
|
self.current_part.set_method(result[:method])
|
100
|
+
when :signature
|
101
|
+
self.current_part.set_signature(result[:text])
|
98
102
|
when :response
|
99
103
|
|
100
104
|
# open the current response tag using the code as a key
|
@@ -133,9 +137,9 @@ module Rain
|
|
133
137
|
# add the part and create a new one
|
134
138
|
self.new_part
|
135
139
|
|
136
|
-
# remove any empty parts (for ruby docs)
|
140
|
+
# remove any empty parts or parts without method signatures (for ruby docs)
|
137
141
|
if self.type == :RUBY
|
138
|
-
self.parts = self.parts.select{ |part| part.route != "//" }
|
142
|
+
self.parts = self.parts.select{ |part| part.route != "//" || !part.signature.nil? }
|
139
143
|
end
|
140
144
|
end
|
141
145
|
|
data/lib/doc_part.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module Rain
|
2
|
+
|
3
|
+
# the doc part is a model to store information about
|
2
4
|
class DocPart
|
3
|
-
|
5
|
+
|
6
|
+
attr_accessor :http_method, :responses, :route, :doc, :params, :headers, :signature
|
4
7
|
|
5
8
|
def initialize
|
6
9
|
self.responses = []
|
@@ -124,6 +127,16 @@ module Rain
|
|
124
127
|
return header[:text]
|
125
128
|
end
|
126
129
|
|
130
|
+
# sets a method signature
|
131
|
+
def set_signature(sig)
|
132
|
+
self.signature = sig
|
133
|
+
end
|
134
|
+
|
135
|
+
# gets the method signature
|
136
|
+
def get_signature
|
137
|
+
self.signature
|
138
|
+
end
|
139
|
+
|
127
140
|
def to_hash
|
128
141
|
return {
|
129
142
|
route: self.route,
|
@@ -131,7 +144,8 @@ module Rain
|
|
131
144
|
headers: self.headers,
|
132
145
|
doc: self.doc,
|
133
146
|
responses: self.responses,
|
134
|
-
http_method: self.http_method
|
147
|
+
http_method: self.http_method,
|
148
|
+
signature: self.signature
|
135
149
|
}
|
136
150
|
end
|
137
151
|
end
|
data/lib/parser.rb
CHANGED
@@ -16,9 +16,18 @@ module Rain
|
|
16
16
|
# parses the current line, determining which tag
|
17
17
|
# the line contains and returning the result
|
18
18
|
# accordingly in a hash with the tag type
|
19
|
-
def parse(line)
|
19
|
+
def parse(line, parse_signatures = false)
|
20
|
+
|
20
21
|
# return nil if there is no # on the line for ruby.
|
21
|
-
|
22
|
+
basic_stripped = line.strip().sub(' do', '')
|
23
|
+
return nil if self.type == :RUBY && basic_stripped == ''
|
24
|
+
|
25
|
+
# see if a signature part can be extracted if a signature is enabled
|
26
|
+
if parse_signatures
|
27
|
+
return nil if self.type == :RUBY && !basic_stripped.start_with?('#') && !(basic_stripped =~ /class|def|get|put|post|delete/)
|
28
|
+
else
|
29
|
+
return nil if self.type == :RUBY && !basic_stripped.start_with?('#')
|
30
|
+
end
|
22
31
|
|
23
32
|
# strip blanks and # from the current line
|
24
33
|
line = strip_current_line(line)
|
@@ -77,6 +86,16 @@ module Rain
|
|
77
86
|
}
|
78
87
|
end
|
79
88
|
|
89
|
+
# return method signature if found
|
90
|
+
if parse_signatures
|
91
|
+
if line.start_with?('def ', 'class ', 'get "', "get '", 'put "', "put '", 'post "', "post '", 'delete "', "delete '")
|
92
|
+
return {
|
93
|
+
tag: :signature,
|
94
|
+
text: line
|
95
|
+
}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
80
99
|
# return simple doc line if no tags fit
|
81
100
|
return {
|
82
101
|
tag: :doc,
|
data/lib/rain.rb
CHANGED
@@ -7,14 +7,15 @@ class Rain::CLI < Thor
|
|
7
7
|
@@docs = []
|
8
8
|
|
9
9
|
desc "generate [file/sources/**]", "Generates the rain documentation"
|
10
|
-
method_option :log_parse, aliases: "--lp", desc: "Show the output of each line parse"
|
10
|
+
method_option :log_parse, aliases: "--lp", desc: "Show the output of each line parse."
|
11
|
+
method_option :parse_signatures, aliases: "--s", desc: "Parse method and class documentation too. Defaults to false."
|
11
12
|
def generate(*sources)
|
12
13
|
print "Rain is parsing files in the directories #{sources} \n"
|
13
14
|
|
14
15
|
# loop through all of the file sources and generate docs
|
15
16
|
sources.each do |source|
|
16
17
|
print "Parsing #{source} \n"
|
17
|
-
@doc = Rain::Doc.new(source, File.read(Dir.pwd + "/#{source}"), options[:log_parse])
|
18
|
+
@doc = Rain::Doc.new(source, File.read(Dir.pwd + "/#{source}"), options[:log_parse].nil? ? false : true, options[:parse_signatures].nil? ? false : true)
|
18
19
|
@doc.parse
|
19
20
|
|
20
21
|
@@docs << @doc
|
@@ -41,6 +42,8 @@ class Rain::CLI < Thor
|
|
41
42
|
end
|
42
43
|
|
43
44
|
no_commands do
|
45
|
+
|
46
|
+
# Builds the HTML for each doc in the @@docs array.
|
44
47
|
def build_html
|
45
48
|
|
46
49
|
# delete the old output files and create the dir
|
@@ -57,6 +60,7 @@ class Rain::CLI < Thor
|
|
57
60
|
# load the templates
|
58
61
|
layout_template = File.read('./templates/layout.erb')
|
59
62
|
doc_template = File.read('./templates/doc.erb')
|
63
|
+
nav_template = File.read('./templates/nav.erb')
|
60
64
|
|
61
65
|
else
|
62
66
|
|
@@ -67,6 +71,51 @@ class Rain::CLI < Thor
|
|
67
71
|
# load the templates
|
68
72
|
layout_template = File.read(spec.gem_dir + '/templates/layout.erb')
|
69
73
|
doc_template = File.read(spec.gem_dir + '/templates/doc.erb')
|
74
|
+
nav_template = File.read(spec.gem_dir + '/templates/nav.erb')
|
75
|
+
end
|
76
|
+
|
77
|
+
# loop through all of the docs and generate navigation from the docs and their parts
|
78
|
+
nav_parts = []
|
79
|
+
@@docs.each do |doc|
|
80
|
+
|
81
|
+
# get the html file name (same used for output) from the doc openstruct
|
82
|
+
doc_os = OpenStruct.new(doc.to_hash)
|
83
|
+
html_file_name = File.basename(doc_os.file_name, doc_os.file_ext) + '.html'
|
84
|
+
|
85
|
+
# set up the nav hash for the doc
|
86
|
+
nav = {
|
87
|
+
navdoc: {
|
88
|
+
title: doc_os.title,
|
89
|
+
url: "#{html_file_name}",
|
90
|
+
parts: []
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
# markdown docs do not have parts as the whole page counts as a "doc"
|
95
|
+
if doc_os.type != "MARKDOWN"
|
96
|
+
|
97
|
+
# loop through all of the parts and depending on whether
|
98
|
+
# it is for a signature or a route, construct the #anchor
|
99
|
+
# url differnetly
|
100
|
+
doc_os.parts.each do |part|
|
101
|
+
if !part[:signature].nil?
|
102
|
+
nav[:navdoc][:parts] << {
|
103
|
+
title: part[:signature],
|
104
|
+
url: "#{html_file_name}##{part[:signature].gsub(' ', '-').gsub(',', '').gsub('(', '-').gsub(')', '-')[0..12]}"
|
105
|
+
}
|
106
|
+
else
|
107
|
+
nav[:navdoc][:parts] << {
|
108
|
+
title: part[:route],
|
109
|
+
url: "#{html_file_name}##{part[:route].gsub('/', '-').gsub(':', '')}"
|
110
|
+
}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# render the nav and append it to the array of nav parts
|
116
|
+
nav_os = OpenStruct.new(nav)
|
117
|
+
nav_rendered = ERB.new(nav_template).result(nav_os.instance_eval {binding})
|
118
|
+
nav_parts << nav_rendered
|
70
119
|
end
|
71
120
|
|
72
121
|
# load the doc properties and parts into the doc template
|
@@ -74,11 +123,10 @@ class Rain::CLI < Thor
|
|
74
123
|
|
75
124
|
# create an openstruct from the current doc and render into the template
|
76
125
|
doc_os = OpenStruct.new(doc.to_hash)
|
77
|
-
|
78
126
|
doc_rendered = ERB.new(doc_template).result(doc_os.instance_eval {binding})
|
79
127
|
|
80
|
-
# create a struct with the rendered doc output then render into the layout
|
81
|
-
layout_os = OpenStruct.new({ doc_output: doc_rendered, title: doc_os.title })
|
128
|
+
# create a struct with the rendered doc output then render into the layout with nav
|
129
|
+
layout_os = OpenStruct.new({ doc_output: doc_rendered, title: doc_os.title, nav_parts: nav_parts })
|
82
130
|
html = ERB.new(layout_template).result(layout_os.instance_eval {binding})
|
83
131
|
|
84
132
|
# write the html to a file
|
@@ -86,7 +134,17 @@ class Rain::CLI < Thor
|
|
86
134
|
end
|
87
135
|
end
|
88
136
|
|
137
|
+
# Creates the html file for the specified doc
|
138
|
+
# with the HTML rendered from the ERB template
|
139
|
+
#
|
140
|
+
# {param doc hash}
|
141
|
+
# A hash representation of the Rain::Doc class, containing a single document and its parts.
|
142
|
+
# {/param}
|
143
|
+
# {param html string}
|
144
|
+
# The HTML rendered from the ERB layout and doc template.
|
145
|
+
# {/param}
|
89
146
|
def output_html(doc, html)
|
147
|
+
|
90
148
|
# replace file_name extenstions with .html
|
91
149
|
file_name = File.basename(doc.file_name, doc.file_ext) + '.html'
|
92
150
|
|
data/templates/doc.erb
CHANGED
@@ -3,13 +3,15 @@
|
|
3
3
|
|
4
4
|
<!-- HTTP method and route (if both specified) -->
|
5
5
|
<% if !part[:route].nil? && !part[:http_method].nil? %>
|
6
|
-
<p><strong><%= part[:http_method] %></strong> <%= part[:route] %></p>
|
6
|
+
<p id="<%= part[:route].gsub('/', '-').gsub(':', '') %>"><strong><%= part[:http_method] %></strong> <%= part[:route] %></p>
|
7
|
+
<% elsif !part[:signature].nil? %>
|
8
|
+
<p id="<%= part[:signature].gsub(' ', '-').gsub(',', '').gsub('(', '-').gsub(')', '-')[0..12] %>"><strong><%= part[:signature] %></strong></p>
|
7
9
|
<% end %>
|
8
10
|
|
9
11
|
<!-- display all of the docs together for the part -->
|
10
|
-
|
11
|
-
|
12
|
-
<% end
|
12
|
+
<p><% part[:doc].each do |doc| %>
|
13
|
+
<%= doc %>
|
14
|
+
<% end %></p>
|
13
15
|
|
14
16
|
<!-- show all of the params in a table -->
|
15
17
|
<% if part[:params].length > 0 %>
|
data/templates/layout.erb
CHANGED
@@ -6,9 +6,17 @@
|
|
6
6
|
</head>
|
7
7
|
<body>
|
8
8
|
<div class="container">
|
9
|
-
<div class="
|
10
|
-
<
|
11
|
-
|
9
|
+
<div class="row">
|
10
|
+
<div class="three columns" style="padding: 20px 0">
|
11
|
+
<h5>Navigation</h5>
|
12
|
+
<% nav_parts.each do |part| %>
|
13
|
+
<%= part %>
|
14
|
+
<% end %>
|
15
|
+
</div>
|
16
|
+
<div class="nine columns" style="padding: 20px 0">
|
17
|
+
<h2><%= title %></h2>
|
18
|
+
<%= doc_output %>
|
19
|
+
</div>
|
12
20
|
</div>
|
13
21
|
</div>
|
14
22
|
</body>
|
data/templates/nav.erb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rain-doc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Brennan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- templates/css/skeleton.css
|
76
76
|
- templates/doc.erb
|
77
77
|
- templates/layout.erb
|
78
|
+
- templates/nav.erb
|
78
79
|
homepage: http://martin-brennan.github.io/rain/
|
79
80
|
licenses:
|
80
81
|
- MIT
|