From 16b75ac62c9e835c698fbde568938ba377d6a813 Mon Sep 17 00:00:00 2001 From: Lars Trieloff <trieloff@adobe.com> Date: Wed, 13 Dec 2017 20:56:42 +0000 Subject: [PATCH] Add support for examples #14 --- examples/docs/README.md | 1 + examples/docs/example.schema.md | 5 +++ examples/docs/examples.schema.md | 29 ++++++++++++++ .../generated-schemas/examples.schema.json | 38 +++++++++++++++++++ examples/schemas/examples.example.1.json | 4 ++ examples/schemas/examples.example.2.json | 4 ++ lib/markdownWriter.js | 12 ++++++ templates/md/examples.ejs | 21 ++++++++++ 8 files changed, 114 insertions(+) create mode 100644 examples/docs/examples.schema.md create mode 100644 examples/generated-schemas/examples.schema.json create mode 100644 examples/schemas/examples.example.1.json create mode 100644 examples/schemas/examples.example.2.json create mode 100644 templates/md/examples.ejs diff --git a/examples/docs/README.md b/examples/docs/README.md index 3af4eca..3d8ad58 100644 --- a/examples/docs/README.md +++ b/examples/docs/README.md @@ -11,6 +11,7 @@ * [Custom](./deepextending.schema.md) – `https://example.com/schemas/deepextending` * [Definitions](./definitions.schema.md) – `https://example.com/schemas/definitions` * [Example](./example.schema.md) – `https://example.com/schemas/example` +* [Examples](./examples.schema.md) – `https://example.com/schemas/examples` * [Extending](./extending.schema.md) – `https://example.com/schemas/extending` * [Extensible](./extensible.schema.md) – `https://example.com/schemas/extensible` * [Simple](./simple.schema.md) – `https://example.com/schemas/simple` diff --git a/examples/docs/example.schema.md b/examples/docs/example.schema.md index ce44767..17925e4 100644 --- a/examples/docs/example.schema.md +++ b/examples/docs/example.schema.md @@ -15,4 +15,9 @@ This is an example schema with examples. Too many examples? There can never be t |----------|------------|-------------------|------------| | Can be instantiated | No | Forbidden | [example.schema.json](example.schema.json) | +## Example Example +```json +{"foo":"bar","bar":"baz"} +``` + --- \ No newline at end of file diff --git a/examples/docs/examples.schema.md b/examples/docs/examples.schema.md new file mode 100644 index 0000000..641b7da --- /dev/null +++ b/examples/docs/examples.schema.md @@ -0,0 +1,29 @@ +--- +template: reference +foo: bar +--- + +# Examples Schema + +``` +https://example.com/schemas/examples +``` + +This is an example schema with *multiple* examples. Too many examples? There can never be too many examples! + +| Abstract | Extensible | Custom Properties | Defined In | +|----------|------------|-------------------|------------| +| Can be instantiated | No | Forbidden | [examples.schema.json](examples.schema.json) | + +## Examples Examples + +```json +{"foo":"bi","bar":"bu"} +``` + +```json +{"foo":"zip","bar":"zap"} +``` + + +--- \ No newline at end of file diff --git a/examples/generated-schemas/examples.schema.json b/examples/generated-schemas/examples.schema.json new file mode 100644 index 0000000..91c9b2a --- /dev/null +++ b/examples/generated-schemas/examples.schema.json @@ -0,0 +1,38 @@ +{ + "meta:license": [ + "Copyright 2017 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" + ], + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "https://example.com/schemas/examples", + "title": "Examples", + "type": "object", + "description": "This is an example schema with *multiple* examples. Too many examples? There can never be too many examples!", + "properties": { + "foo": { + "type": "string", + "description": "A simple string.", + "meta:example": "bar" + }, + "bar": { + "type": "string", + "description": "A simple string.", + "meta:example": [ + "bar", + "baz" + ] + } + }, + "examples": [ + { + "foo": "bi", + "bar": "bu" + }, + { + "foo": "zip", + "bar": "zap" + } + ] +} \ No newline at end of file diff --git a/examples/schemas/examples.example.1.json b/examples/schemas/examples.example.1.json new file mode 100644 index 0000000..ab2d990 --- /dev/null +++ b/examples/schemas/examples.example.1.json @@ -0,0 +1,4 @@ +{ + "foo": "bi", + "bar": "bu" +} \ No newline at end of file diff --git a/examples/schemas/examples.example.2.json b/examples/schemas/examples.example.2.json new file mode 100644 index 0000000..36b50ca --- /dev/null +++ b/examples/schemas/examples.example.2.json @@ -0,0 +1,4 @@ +{ + "foo": "zip", + "bar": "zap" +} \ No newline at end of file diff --git a/lib/markdownWriter.js b/lib/markdownWriter.js index d7ba957..d8d19c6 100644 --- a/lib/markdownWriter.js +++ b/lib/markdownWriter.js @@ -62,6 +62,17 @@ function flatten(dependencies) { return deps; } +function stringifyExamples(examples) { + if (examples) { + console.log(examples); + return examples.map(example => { + return JSON.stringify(example, 2); + }); + } else { + return false; + } +} + const generateMarkdown = function(filename, schema, schemaPath, outDir, dependencyMap) { var ctx = { schema: schema, @@ -83,6 +94,7 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen props: schemaProps(schema, schemaPath, filename) } ], //[ 'divider.ejs', null ], //[ 'topSchema.ejs', ctx ], + [ 'examples.ejs', { examples: stringifyExamples(schema.examples), title: schema.title } ], [ 'divider.ejs', null ] ].map(([ template, context ]) => { return [ diff --git a/templates/md/examples.ejs b/templates/md/examples.ejs new file mode 100644 index 0000000..76c997d --- /dev/null +++ b/templates/md/examples.ejs @@ -0,0 +1,21 @@ +<% /** + * Copyright 2017 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 + */ %><% if (examples && examples.length == 1) { %> +## <%=title %> Example + +```json +<%- examples[0] %> +``` +<% } else if (examples && examples.length > 1) { %> +## <%=title %> Examples +<% _.forEach(examples, example => { %> + +```json +<%- example %> +``` + +<% }); %> +<% } %> \ No newline at end of file -- GitLab