Implement basic table generator using the schemas

This commit is contained in:
Jonathan Rudenberg 2012-08-31 00:15:32 -04:00
parent 28b839a891
commit 89e20162ed
4 changed files with 45 additions and 0 deletions

34
lib/schema_table.rb Normal file
View 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