Skip to content
Snippets Groups Projects
Unverified Commit 50e7ef15 authored by Andy Steed's avatar Andy Steed Committed by GitHub
Browse files

Merge pull request #45 from adobe/header-refactor

Refactor Header Generation
parents 55aa0699 18e13ca2
No related branches found
No related tags found
No related merge requests found
......@@ -14,13 +14,13 @@ This is an example schema that uses types defined in other schemas.
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | No | Experimental | Forbidden | Permitted | [complex.schema.json](complex.schema.json) |
## Schema Hierarchy
* Complex References `https://example.com/schemas/complex`
* [Abstract](abstract.schema.md) `https://example.com/schemas/abstract`
* [Simple](simple.schema.md) `https://example.com/schemas/simple`
# Complex References Properties
| Property | Type | Required | Defined by |
......
......@@ -14,7 +14,6 @@ This is an extending schema. It is extending another extending schema. It pulls
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | Yes | Experimental | Forbidden | Permitted | [deepextending.schema.json](deepextending.schema.json) |
## Schema Hierarchy
* Deeply Extending `https://example.com/schemas/deepextending`
......@@ -22,6 +21,7 @@ This is an extending schema. It is extending another extending schema. It pulls
* [Definitions](definitions.schema.md) `https://example.com/schemas/definitions`
* [Extending](extending.schema.md) `https://example.com/schemas/extending`
# Deeply Extending Properties
| Property | Type | Required | Defined by |
......
......@@ -14,13 +14,13 @@ This is an extending schema. It pulls `definitions` from other schemas.
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | Yes | Experimental | Forbidden | Permitted | [extending.schema.json](extending.schema.json) |
## Schema Hierarchy
* Extending `https://example.com/schemas/extending`
* [Extensible](extensible.schema.md) `https://example.com/schemas/extensible`
* [Definitions](definitions.schema.md) `https://example.com/schemas/definitions`
# Extending Properties
| Property | Type | Required | Defined by |
......
/**
* Copyright 2018 Adobe Systems Incorporated. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
var _ = require('lodash');
function custom(schema) {
if (schema.allOf) {
for (let i=0; i<schema.allOf.length; i++) {
if (schema.allOf[i].$ref && schema.allOf[i].$ref === 'https://ns.adobe.com/xdm/common/extensible.schema.json#/definitions/@context') {
return true;
}
}
}
return false;
}
function schemaProps(schema, schemaPath, filename) {
return {
// if there are definitions, but no properties
abstract: (schema.definitions !== undefined && _.keys(schema.properties).length === 0) ? 'Cannot be instantiated' : 'Can be instantiated',
extensible: (schema.definitions !== undefined || schema['meta:extensible'] === true) ? 'Yes' : 'No',
status: schema['meta:status'] !== undefined ? (schema['meta:status'].charAt(0).toUpperCase() + schema['meta:status'].slice(1)) : 'Experimental',
custom: custom(schema) ? 'Allowed' : 'Forbidden',
original: filename.substr(schemaPath.length).substr(1),
};
}
class Header {
constructor(name, docs, value, links) {
this.name = name;
this.docs = docs;
this.value = value;
this.links = links;
this.renderHeader = this.render(this.name, this.docs);
this.renderValue = this.render(this.value, this.links);
}
render(text, link) {
return function() {
if (link) {
return `[${text}](${link})`;
} else {
return text;
}
};
}
}
function header(name, docs, value, links) {
this.name = name;
this.docs = docs;
this.value = value;
this.links = links;
this.render = function(text, link) {
return function() {
if (link) {
return `[${text}](${link})`;
} else {
return text;
}
};
};
this.renderHeader = this.render(this.name, this.docs);
this.renderValue = this.render(this.value, this.links);
return this;
}
function headers(schema, path, filename, docs) {
const props = schemaProps(schema, path, filename);
this.doclinks = docs ? docs : {};
this.myheaders = [];
this.myheaders.push(new Header('Abstract', this.doclinks['abstract'], props.abstract));
this.myheaders.push(new Header('Extensible', this.doclinks['extensible'], props.extensible));
this.myheaders.push(new Header('Status', this.doclinks['status'], props.status));
this.myheaders.push(new Header('Custom Properties', this.doclinks['custom'], props.custom));
this.myheaders.push(new Header('Additional Properties', this.doclinks['additional'], schema.additionalProperties===false ? 'Forbidden' : 'Permitted'));
this.myheaders.push(new Header('Defined In', undefined, props.original, props.original));
this.render = function() {
var buf = '';
//header
buf += '|';
_.forEach(this.myheaders, myheader => {
buf += ' ' + myheader.renderHeader() + ' |';
});
buf += '\n';
//divider
buf += '|';
_.forEach(this.myheaders, myheader => {
buf += '-' + myheader.renderHeader().replace(/./g, '-') + '-|';
});
buf += '\n';
//body
buf += '|';
_.forEach(this.myheaders, myheader => {
buf += ' ' + myheader.renderValue() + ' |';
});
return buf;
};
return this;
}
module.exports.headers = headers;
module.exports.header = header;
module.exports.Header = Header;
......@@ -12,6 +12,7 @@ var _ = require('lodash');
var ejs = require('ejs');
const pejs = Promise.promisifyAll(ejs);
var validUrl = require('valid-url');
const { headers } = require('./header');
function render([ template, context ]) {
return pejs.renderFileAsync(template, context, { debug: false });
......@@ -29,28 +30,6 @@ function assoc(obj, key, value) {
return obj;
}
function custom(schema) {
if (schema.allOf) {
for (let i=0; i<schema.allOf.length; i++) {
if (schema.allOf[i].$ref && schema.allOf[i].$ref === 'https://ns.adobe.com/xdm/common/extensible.schema.json#/definitions/@context') {
return true;
}
}
}
return false;
}
function schemaProps(schema, schemaPath, filename) {
return {
// if there are definitions, but no properties
abstract: (schema.definitions !== undefined && _.keys(schema.properties).length === 0) ? 'Cannot be instantiated' : 'Can be instantiated',
extensible: (schema.definitions !== undefined || schema['meta:extensible'] === true) ? 'Yes' : 'No',
status: schema['meta:status'] !== undefined ? (schema['meta:status'].charAt(0).toUpperCase() + schema['meta:status'].slice(1)) : 'Experimental',
custom: custom(schema) ? 'Allowed' : 'Forbidden',
original: filename.substr(schemaPath.length).substr(1),
};
}
function flatten(dependencies) {
let deps = [];
if (dependencies) {
......@@ -172,7 +151,7 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen
[ 'header.ejs', {
schema: schema,
dependencies: flatten(dependencyMap),
props: schemaProps(schema, schemaPath, filename) } ],
table: headers(schema, schemaPath, filename).render() } ],
//[ 'divider.ejs', null ],
//[ 'topSchema.ejs', ctx ],
[ 'examples.ejs', { examples: stringifyExamples(schema.examples), title: schema.title } ]
......
......@@ -8,6 +8,7 @@
},
"scripts": {
"lint": "eslint .",
"lint-fix": "eslint . --fix",
"start": "node cli.js",
"test": "npm run lint && jasmine",
"cover": "istanbul cover --root lib --print detail jasmine"
......@@ -29,6 +30,7 @@
"eslint": "^4.10.0",
"istanbul": "^0.4.5",
"jasmine": "~2.8.0",
"jasmine-diff": "^0.1.3",
"npm-snapshot": "^1.0.3"
},
"engines": {
......
......@@ -14,13 +14,13 @@ This is an example schema that uses types defined in other schemas.
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | No | Experimental | Forbidden | Permitted | [complex.schema.json](complex.schema.json) |
## Schema Hierarchy
* Complex References `https://example.com/schemas/complex`
* [Abstract](abstract.schema.md) `https://example.com/schemas/abstract`
* [Simple](simple.schema.md) `https://example.com/schemas/simple`
# Complex References Properties
| Property | Type | Required | Defined by |
......
......@@ -14,7 +14,6 @@ This is an extending schema. It is extending another extending schema. It pulls
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | Yes | Experimental | Forbidden | Permitted | [deepextending.schema.json](deepextending.schema.json) |
## Schema Hierarchy
* Deeply Extending `https://example.com/schemas/deepextending`
......@@ -22,6 +21,7 @@ This is an extending schema. It is extending another extending schema. It pulls
* [Definitions](definitions.schema.md) `https://example.com/schemas/definitions`
* [Extending](extending.schema.md) `https://example.com/schemas/extending`
# Deeply Extending Properties
| Property | Type | Required | Defined by |
......
......@@ -14,13 +14,13 @@ This is an extending schema. It pulls `definitions` from other schemas.
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | Yes | Experimental | Forbidden | Permitted | [extending.schema.json](extending.schema.json) |
## Schema Hierarchy
* Extending `https://example.com/schemas/extending`
* [Extensible](extensible.schema.md) `https://example.com/schemas/extensible`
* [Definitions](definitions.schema.md) `https://example.com/schemas/definitions`
# Extending Properties
| Property | Type | Required | Defined by |
......
const { Header, headers } = require('../../lib/header');
beforeEach(function() {
jasmine.addMatchers(require('jasmine-diff')(jasmine, {
colors: true,
inline: true
}));
});
describe('Header Integration Test', () => {
it('Renders a header without link if link is unknown', () => {
const h = new Header('Foo');
expect(h.renderHeader()).toEqual('Foo');
});
it('Renders a header with link if link is known', () => {
const h = new Header('Foo', '../bar.md');
expect(h.renderHeader()).toEqual('[Foo](../bar.md)');
});
it('Renders a header body without link if link is unknown', () => {
const h = new Header('Foo', null, 'Bar');
expect(h.renderValue()).toEqual('Bar');
});
it('Renders a header body with link if link is known', () => {
const h = new Header('Foo', '../bar.md', 'Bar', '#bar-md');
expect(h.renderValue()).toEqual('[Bar](#bar-md)');
});
});
describe('Headers Integration Test', () => {
it('Abstract', () => {
const schema = {
additionalProperties: true,
'meta:extensible': false,
properties: { 'foo':'bar', 'bar': 'baz' }
};
const h = headers(schema, '/home/lars', '/home/lars/complex.schema.json');
const result = `| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| Can be instantiated | No | Experimental | Forbidden | Permitted | [complex.schema.json](complex.schema.json) |`;
expect(h.render()).toEqual(result);
});
});
const { spawn } = require('child_process');
const { readFile, readdirSync } = require('fs');
beforeEach(function() {
jasmine.addMatchers(require('jasmine-diff')(jasmine, {
colors: true,
inline: true
}));
});
describe('Process examples', () => {
it('Run jsonschema2md on example schemas', done => {
......
......@@ -15,11 +15,7 @@
<%= schema.description.replace(/\n/g, '\n\n') %>
| Abstract | Extensible | Status | Custom Properties | Additional Properties | Defined In |
|----------|------------|--------|-------------------|-----------------------|------------|
| <%=props.abstract %> | <%=props.extensible %> | <%=props.status %> | <%=props.custom %> | <%= schema.additionalProperties===false ? "Forbidden" : "Permitted" %> | [<%=props.original %>](<%=props.original %>) |
<%= table %>
<% if (dependencies.length > 0) { %>
## Schema Hierarchy
......@@ -28,4 +24,4 @@
<% _.forEach(dependencies, ({$linkVal, $linkPath, $id}) => { %>
* [<%= $linkVal %>](<%=$linkPath %>) `<%= $id %>`
<% }); %>
<% } %>
\ No newline at end of file
<% } %>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment