Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update schema table generator for nested objects

  • Loading branch information...
commit 4b0a6f54beb5c2786b47559c23a206408d11ea08 1 parent 3367f77
@titanous titanous authored
Showing with 43 additions and 8 deletions.
  1. +2 −0  content/docs/posts.md
  2. +41 −8 lib/schema_table.rb
View
2  content/docs/posts.md
@@ -4,6 +4,8 @@ title: Posts
## Schema
+{post schema}
+
## Type
## Content
View
49 lib/schema_table.rb
@@ -10,17 +10,45 @@ def run(content, params={})
private
+ def table_header
+ el('thead', %w(Property Server App Type Description).map { |v| el('th', v) }.join)
+ end
+
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")
+ el('table', table_header + el('tbody', TentSchemas[schema]['properties'].map { |k,v| property_rows(k, v) }.join), class: 'table table-striped table-bordered')
+ end
+
+ def property_rows(name, attrs)
+ type = capitalize(attrs['type'])
+ if attrs['type'] == 'array'
+ type += " of #{capitalize(attrs['items']['type'])}s"
+ end
+
+ attrs = resolve_ref(attrs['$ref']) if attrs['$ref']
+ rows = [el('tr',
+ el('td', el('code', name)) +
+ el('td', required(attrs['required'])) +
+ el('td', required(attrs['app_required'])) +
+ el('td', type) +
+ el('td', attrs['description'])
+ )]
+ if attrs['items'] && attrs['items']['type'] == 'object'
+ attrs['items']['properties'].each { |k,v| rows << property_rows("#{name}[].#{k}", v)}
+ elsif attrs['properties']
+ attrs['properties'].each { |k,v| rows << property_rows("#{name}.#{k}", v)}
+ end
+
+ rows.join
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']]
+ def required(flag)
+ case flag
+ when true
+ 'Required'
+ when 'import'
+ 'Import'
+ else
+ 'Optional'
end
end
@@ -31,4 +59,9 @@ def capitalize(str)
def resolve_ref(ref)
TentSchemas[ref.sub('#/schemas/', '')]
end
+
+ def el(el, content, attributes = {})
+ attrs = attributes ? ' ' + attributes.map { |k,v| "#{k}=\"#{v}\"" }.join(' ') : ''
+ "<#{el}#{attrs}>\n#{content}</#{el}>\n"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.