Implement basic table generator using the schemas
This commit is contained in:
parent
28b839a891
commit
89e20162ed
4 changed files with 45 additions and 0 deletions
1
Gemfile
1
Gemfile
|
@ -15,3 +15,4 @@ gem 'puma'
|
||||||
gem 'rack-rewrite'
|
gem 'rack-rewrite'
|
||||||
gem 'pygments.rb', :git => 'git://github.com/akzhan/pygments.rb.git', :branch => 'rubypython-0.6'
|
gem 'pygments.rb', :git => 'git://github.com/akzhan/pygments.rb.git', :branch => 'rubypython-0.6'
|
||||||
gem 'clogger'
|
gem 'clogger'
|
||||||
|
gem 'tent-schemas', :git => 'git@git:tent-schemas.git'
|
||||||
|
|
|
@ -6,6 +6,13 @@ GIT
|
||||||
pygments.rb (0.2.12)
|
pygments.rb (0.2.12)
|
||||||
rubypython (~> 0.6.1)
|
rubypython (~> 0.6.1)
|
||||||
|
|
||||||
|
GIT
|
||||||
|
remote: git@git:tent-schemas.git
|
||||||
|
revision: 965ad389012f24ed220156f978905038d93a67e6
|
||||||
|
specs:
|
||||||
|
tent-schemas (0.1.0)
|
||||||
|
json-schema
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
remote: http://rubygems.org/
|
remote: http://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
@ -19,6 +26,7 @@ GEM
|
||||||
cri (2.3.0)
|
cri (2.3.0)
|
||||||
colored (>= 1.2)
|
colored (>= 1.2)
|
||||||
ffi (1.0.11)
|
ffi (1.0.11)
|
||||||
|
json-schema (1.0.9)
|
||||||
mime-types (1.19)
|
mime-types (1.19)
|
||||||
nanoc (3.4.0)
|
nanoc (3.4.0)
|
||||||
cri (~> 2.2)
|
cri (~> 2.2)
|
||||||
|
@ -58,3 +66,4 @@ DEPENDENCIES
|
||||||
rainpress
|
rainpress
|
||||||
redcarpet
|
redcarpet
|
||||||
slim
|
slim
|
||||||
|
tent-schemas!
|
||||||
|
|
1
Rules
1
Rules
|
@ -38,6 +38,7 @@ compile '*' do
|
||||||
when 'slim'
|
when 'slim'
|
||||||
filter :slim
|
filter :slim
|
||||||
when 'md'
|
when 'md'
|
||||||
|
filter :schema_table
|
||||||
filter :redcarpet, renderer: MarkdownHTML, options: {
|
filter :redcarpet, renderer: MarkdownHTML, options: {
|
||||||
fenced_code_blocks: true,
|
fenced_code_blocks: true,
|
||||||
no_intra_emphasis: true,
|
no_intra_emphasis: true,
|
||||||
|
|
34
lib/schema_table.rb
Normal file
34
lib/schema_table.rb
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
require 'tent-schemas'
|
||||||
|
|
||||||
|
class SchemaTableFilter < Nanoc::Filter
|
||||||
|
identifier :schema_table
|
||||||
|
type :text
|
||||||
|
|
||||||
|
def run(content, params={})
|
||||||
|
content.gsub(/\{(\w+) schema\}/) { schema_table($1) }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def schema_table(schema)
|
||||||
|
table = [['Property', 'Required', 'Type', 'Description']]
|
||||||
|
table << ['---'] * table.first.size
|
||||||
|
table += properties_table(TentSchemas[schema]['properties'])
|
||||||
|
table.map { |r| r.unshift(nil).push(nil).join(' | ').strip }.join("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
def properties_table(properties)
|
||||||
|
properties.map do |prop,attrs|
|
||||||
|
attrs = resolve_ref(attrs['$ref']) if attrs['$ref']
|
||||||
|
["`#{prop}`", attrs['required'] ? 'Required' : 'Optional', capitalize(attrs['type']), attrs['description']]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def capitalize(str)
|
||||||
|
str[0].upcase + str[1..-1]
|
||||||
|
end
|
||||||
|
|
||||||
|
def resolve_ref(ref)
|
||||||
|
TentSchemas[ref.sub('#/schemas/', '')]
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue