From 89e20162edef912a66b63972d2489667494060be Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Fri, 31 Aug 2012 00:15:32 -0400 Subject: [PATCH] Implement basic table generator using the schemas --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ Rules | 1 + lib/schema_table.rb | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 lib/schema_table.rb diff --git a/Gemfile b/Gemfile index bfd587c..80bb40b 100644 --- a/Gemfile +++ b/Gemfile @@ -15,3 +15,4 @@ gem 'puma' gem 'rack-rewrite' gem 'pygments.rb', :git => 'git://github.com/akzhan/pygments.rb.git', :branch => 'rubypython-0.6' gem 'clogger' +gem 'tent-schemas', :git => 'git@git:tent-schemas.git' diff --git a/Gemfile.lock b/Gemfile.lock index 2a9b9ca..a8c2f55 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,13 @@ GIT pygments.rb (0.2.12) rubypython (~> 0.6.1) +GIT + remote: git@git:tent-schemas.git + revision: 965ad389012f24ed220156f978905038d93a67e6 + specs: + tent-schemas (0.1.0) + json-schema + GEM remote: http://rubygems.org/ specs: @@ -19,6 +26,7 @@ GEM cri (2.3.0) colored (>= 1.2) ffi (1.0.11) + json-schema (1.0.9) mime-types (1.19) nanoc (3.4.0) cri (~> 2.2) @@ -58,3 +66,4 @@ DEPENDENCIES rainpress redcarpet slim + tent-schemas! diff --git a/Rules b/Rules index 74ca30f..572ff4a 100644 --- a/Rules +++ b/Rules @@ -38,6 +38,7 @@ compile '*' do when 'slim' filter :slim when 'md' + filter :schema_table filter :redcarpet, renderer: MarkdownHTML, options: { fenced_code_blocks: true, no_intra_emphasis: true, diff --git a/lib/schema_table.rb b/lib/schema_table.rb new file mode 100644 index 0000000..5deb87f --- /dev/null +++ b/lib/schema_table.rb @@ -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