Skip to content

Commit 6dc360d

Browse files
committed
feat: simplify property formating
1 parent f98e51a commit 6dc360d

File tree

9 files changed

+81
-102
lines changed

9 files changed

+81
-102
lines changed

templates/md/array-type.ejs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/ %>
77
<% if (schema.items!==undefined&&schema.items.type==="array") { %>
8-
Nested array type: <%=schema.items.simpletype %>
8+
* Nested array type: <%=schema.items.simpletype %>
99
<% } else if (nested===undefined||nested===false) { %>
10-
Array type: <%=schema.simpletype %>
11-
<% } %>
12-
<% if (schema.items!==undefined) { %>
13-
<% if (!(schema.items!==undefined&&schema.items.type==="array")) { %>
14-
All items must be of the type:<% }
15-
if (schema.items.type==="string") {
16-
%><%- include("string-type",{schema:schema.items,_:_})
17-
%><% } else if (schema.items.type==="number"||schema.items.type==="integer") {
10+
* Array type<% } %><% if (schema.items!==undefined) { %><% if (!(schema.items!==undefined&&schema.items.type==="array")) { %>
11+
* All items must be of the type:<% }
12+
if (schema.items.type==="string") {
13+
%> `string`
14+
<%- include("string-type",{schema:schema.items,_:_})
15+
%><% } else if (schema.items.type==="number"||schema.items.type==="integer") {
1816
%><%- include("number-type",{schema:schema.items,_:_})
1917
%><% } else if (schema.items.type==="boolean") {
2018
%><%- include("boolean-type",{schema:schema.items,_:_})
2119
%><% } else if (schema.items.type==="object") {
22-
%><%- include("object-type",{schema:schema.items,_:_})
23-
%><% } else if (schema.items.type==="array") {
24-
%><%- include("array-type",{schema:schema.items,_:_, nested:true})
20+
%><%- include("object-type",{schema:schema.items,_:_})
21+
%><% } else if (schema.items.type==="array") {
22+
%><%- include("array-type",{schema:schema.items,_:_, nested:true})
2523
%><% } else if (schema.items.anyOf!==undefined || schema.items.allOf!==undefined || schema.items.oneOf!==undefined) {
2624
%><%- include("join-type",{schema:schema.items,_:_,schemas:schema.items.anyOf || schema.items.allOf || schema.items.oneOf, ejs:ejs}) %>
27-
%><% } else if (schema.items.$ref!==undefined) {
25+
%><% } else if (schema.items.$ref!==undefined) {
2826
%><%- include("referenced-type",{schema:schema.items,_:_}) %>
2927
<% } else { %>
3028
Unknown type `<%= schema.items.type %>`.
@@ -37,7 +35,7 @@ Unknown type `<%= schema.items.type %>`.
3735
<% } %>
3836
3937
<% if (schema.items.description!==undefined) { %>
40-
38+
4139
<%- (""+schema.items.description).replace(/\n/g, '\n\n') %>
4240
4341
<% } %>

templates/md/nested-properties.ejs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License. You may obtain a copy
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6-
*/ %>
7-
8-
<% const hasDefault = _.keys(props).filter(property => _.has(props, property + '.default')).length > 0; %>
9-
6+
*/ %><% const hasDefault = _.keys(props).filter(property => _.has(props, property + '.default')).length > 0; %>
107
| Property | Type | Required |<% if(hasDefault) { %> Default |<% } %>
11-
|----------|------|----------|<% if(hasDefault) { %>---------|<% } %>
12-
<% _.keys(props).sort().forEach(property => {
8+
|----------|------|----------|<% if(hasDefault) { %>---------|<% } %><% _.keys(props).sort().forEach(property => {
139
const schema = props[property]; %>
14-
| `<%= property %>`| <%= schema.type %> | <%= (Array.isArray(outer.required)&&outer.required.indexOf(property)>=0) ? "**Required**" : "Optional" %> |<% if(hasDefault) { %> <% if (schema.default!==undefined) { %>`<%- JSON.stringify(schema.default) %>`<% } %> |<% } %>
15-
<%
10+
| `<%= property %>`| <%= schema.type %> | <%= (Array.isArray(outer.required)&&outer.required.indexOf(property)>=0) ? "**Required**" : "Optional" %> |<% if(hasDefault) { %> <% if (schema.default!==undefined) { %>`<%- JSON.stringify(schema.default) %>`<% } %> |<% } %><%
1611
});
17-
%>
12+
%>

templates/md/nested-property.ejs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/ %>
77

8-
#### <%=name %>
8+
#### `<%=name %>`
99

1010
<% if (schema.title) { %>
1111
##### <%= schema.title %>
1212
1313
<% } %>
14-
14+
1515
<% if (schema.description) { %>
1616
<%- (""+schema.description).replace(/\n/g, '\n\n') %>
1717
<% } %>
1818

1919

20-
`<%=name %>`
21-
2220

2321
* is <% if (required) { %>**required**<% } else { %>optional<% } %>
2422
* type: <%=schema.simpletype %><% if (schema.simpletype.match(/\[\]\[\]/)||schema.simpletype==="`array[]`") { %> (nested array)<% } %><% if (schema.type==='array') { -%>
@@ -41,22 +39,20 @@ The value of this property **must** be equal to:
4139
The value of this property **must** be equal to one of the [known values below](#<%=nameSlug %>-known-values).
4240
<% } else { %>
4341
44-
##### <%=name %> Type
45-
4642
<% if (schema.type==="string") { %>
47-
<%- include("string-type",{schema:schema,_:_}) %>
43+
<%- include("string-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
4844
<% } else if (schema.type==="number"||schema.type==="integer") { %>
49-
<%- include("number-type",{schema:schema,_:_}) %>
45+
<%- include("number-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5046
<% } else if (schema.type==="boolean") { %>
51-
<%- include("boolean-type",{schema:schema,_:_}) %>
47+
<%- include("boolean-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5248
<% } else if (schema.type==="object") { %>
53-
<%- include("object-type",{schema:schema,_:_}) %>
49+
<%- include("object-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5450
<% } else if (schema.type==="array") { %>
55-
<%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}) %>
51+
<%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}).replace(/^/gm, ' ') %>
5652
<% } else if (schema.$ref!==undefined) { %>
57-
<%- include("referenced-type",{schema:schema,_:_}) %>
53+
<%- include("referenced-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5854
<% } else if (schema.anyOf!==undefined || schema.allOf!==undefined || schema.oneOf!==undefined) { %>
59-
<%- include("join-type",{schema:schema,_:_,schemas:schema.anyOf || schema.allOf || schema.oneOf,ejs:ejs}) %>
55+
<%- include("join-type",{schema:schema,_:_,schemas:schema.anyOf || schema.allOf || schema.oneOf,ejs:ejs}).replace(/^/gm, ' ') %>
6056
<% } else { %>
6157
Unknown type `<%= schema.type %>`.
6258
@@ -96,4 +92,3 @@ Unknown type `<%= schema.type %>`.
9692
9793
<% }); %>
9894
<% } %>
99-

templates/md/number-type.ejs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
* you may not use this file except in compliance with the License. You may obtain a copy
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/ %>
7-
8-
`<%=schema.type %>`<%=schema.nullable ? ", nullable" : "" %>
9-
10-
7+
<%=schema.nullable ? "* nullable" : "" %>
118
<% if(schema.exclusiveMinumum!==undefined) {
129
%>* value must not be smaller or equal than: `<%=schema.exclusiveMinumum %>`<%
1310
} else if(schema.minimum!==undefined) {

templates/md/object-type.ejs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@
44
* you may not use this file except in compliance with the License. You may obtain a copy
55
* of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/ %>
7-
87
`object`<%=schema.nullable ? ", nullable," : "" %> with following properties:
9-
10-
<%- include("nested-properties",{outer:schema,props: schema.properties,_:_, nested:true}) %>
8+
<%- include("nested-properties",{outer:schema,props: schema.properties,_:_,
9+
nested:true}).replace(/^/gm, '') %>
1110

1211
<% _.keys(schema.properties).sort().forEach(property => {
1312
const inner = schema.properties[property];
1413
simpletype(inner)
1514
const required = (Array.isArray(schema.required)&&schema.required.indexOf(property)>=0); %>
1615
1716
<%- include("nested-property",{
18-
_:_,
19-
nested:true,
20-
name: property,
17+
_:_,
18+
nested:true,
19+
name: property,
2120
schema: inner,
2221
required: required,
2322
examples: inner.examples,
2423
nameSlug:nameSlug
25-
}) %>
24+
}).replace(/^/gm, '') %>
2625
2726
<%
2827
});
29-
%>
28+
%>

templates/md/pattern-property.ejs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,18 @@ The value of this property **must** be equal to:
4040
The value of this properties with this pattern **must** be equal to one of the [known values below](#<%=name %>-known-values).
4141
<% } else { %>
4242
43-
### Pattern <%=name %> Type
44-
4543
<% if (schema.type==="string") { %>
46-
<%- include("string-type",{schema:schema,_:_}) %>
44+
<%- include("string-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
4745
<% } else if (schema.type==="number"||schema.type==="integer") { %>
48-
<%- include("number-type",{schema:schema,_:_}) %>
46+
<%- include("number-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
4947
<% } else if (schema.type==="boolean") { %>
50-
<%- include("boolean-type",{schema:schema,_:_}) %>
48+
<%- include("boolean-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5149
<% } else if (schema.type==="array") { %>
52-
<%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}) %>
50+
<%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}).replace(/^/gm, ' ') %>
5351
<% } else if (schema.$ref!==undefined) { %>
54-
<%- include("referenced-type",{schema:schema,_:_}) %>
52+
<%- include("referenced-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5553
<% } else if (schema.anyOf!==undefined || schema.allOf!==undefined || schema.oneOf!==undefined) { %>
56-
<%- include("join-type",{schema:schema,_:_,schemas:schema.anyOf || schema.allOf || schema.oneOf,ejs:ejs}) %>
54+
<%- include("join-type",{schema:schema,_:_,schemas:schema.anyOf || schema.allOf || schema.oneOf,ejs:ejs}).replace(/^/gm, ' ') %>
5755
<% } else { %>
5856
Unknown type `<%= schema.type %>`.
5957
@@ -93,4 +91,3 @@ Unknown type `<%= schema.type %>`.
9391
9492
<% }); %>
9593
<% } %>
96-

templates/md/properties.ejs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
*/ %>
77
# Properties
88

9+
<% function githubSlugify(val) {
10+
return val.toLowerCase().replace(/ /g,'-')
11+
// single chars that are removed
12+
.replace(/[`~!@#$%^&*()+=<>?,./:;"'|{}\[\]\\–—]/g, '')
13+
// CJK punctuations that are removed
14+
.replace(/[ 。?!,、;:“”【】()〔〕[]﹃﹄“”‘’﹁﹂—…-~《》〈〉「」]/g, '')
15+
} %>
916
<% const hasDefault = _.keys(props).filter(property => _.has(props, property + '.default')).length > 0; %>
1017
<% const hasDefinition = _.keys(props).filter(property => _.has(props, property + '.$oSchema')).length > 0; %>
1118

@@ -22,7 +29,7 @@
2229
});
2330
%><% _.keys(pprops).forEach(property => {
2431
const schema = pprops[property]; %>
25-
| `<%= property.replace(':', '') %>` | <%= schema.simpletype %> | Pattern | <%= schema.nullable === true ? "Yes" : "No" %> |<% if(hasDefault) { %> <% if (schema.default!==undefined) { %>`<%- JSON.stringify(schema.default) %>`<% } %> |<% } %> <% if(hasDefinition) { %> <%
32+
| [`<%= property %>`](#pattern-<%= githubSlugify(property) %>) | <%= schema.simpletype %> | Pattern | <%= schema.nullable === true ? "Yes" : "No" %> |<% if(hasDefault) { %> <% if (schema.default!==undefined) { %>`<%- JSON.stringify(schema.default) %>`<% } %> |<% } %> <% if(hasDefinition) { %> <%
2633
if (schema.$oSchema) {
2734
%>[<%= schema.$oSchema.$linkVal %>](<%= schema.$oSchema.$linkPath %>#<%= property.replace(':', '').toLowerCase() %>)<%
2835
} else {

templates/md/property.ejs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
* no more than `<%=schema.maxItems %>` items in the array
3030
<% } else if (schema.minItems!==undefined) { %>
3131
* at least `<%=schema.minItems %>` items in the array<% } %>
32-
<% } -%>
33-
34-
<% if (schema.const!==undefined) { %>
32+
<% } -%><% if (schema.const!==undefined) { %>
3533
The value of this property **must** be equal to:
3634
3735
@@ -41,28 +39,23 @@ The value of this property **must** be equal to:
4139
4240
<% } else if (schema.enum!==undefined) { %>
4341
The value of this property **must** be equal to one of the [known values below](#<%=nameSlug %>-known-values).
44-
<% } else { %>
45-
46-
### <%=name %> Type
47-
48-
<% if (schema.type==="string") { %>
49-
<%- include("string-type",{schema:schema,_:_}) %>
42+
<% } else { %><% if (schema.type==="string") { %>
43+
<%- include("string-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5044
<% } else if (schema.type==="number"||schema.type==="integer") { %>
51-
<%- include("number-type",{schema:schema,_:_}) %>
45+
<%- include("number-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5246
<% } else if (schema.type==="null") { %>
53-
<%- include("null-type",{schema:schema,_:_}) %>
47+
<%- include("null-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5448
<% } else if (schema.type==="boolean") { %>
55-
<%- include("boolean-type",{schema:schema,_:_}) %>
49+
<%- include("boolean-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
5650
<% } else if (schema.type==="object") { %>
57-
<%- include("object-type",{schema:schema,_:_,nameSlug:nameSlug}) %>
58-
<% } else if (schema.type==="array") { %>
59-
<%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}) %>
51+
<%- include("object-type",{schema:schema,_:_,nameSlug:nameSlug}).replace(/^/gm, ' ') %>
52+
<% } else if (schema.type==="array") { %><%- include("array-type",{schema:schema,_:_,nested:false,ejs:ejs}).replace(/^/gm, ' ') %>
6053
<% } else if (schema.$ref!==undefined) { %>
61-
<%- include("referenced-type",{schema:schema,_:_}) %>
54+
<%- include("referenced-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
6255
<% } else if (schema.anyOf!==undefined || schema.allOf!==undefined || schema.oneOf!==undefined) { %>
63-
<%- include("join-type",{schema:schema,_:_,schemas:schema.anyOf || schema.allOf || schema.oneOf,ejs:ejs}) %>
56+
<%- include("join-type",{schema:schema,_:_,schemas:schema.anyOf || schema.allOf || schema.oneOf,ejs:ejs}).replace(/^/gm, ' ') %>
6457
<% } else if (schema.simpletype==="multiple") { %>
65-
<%- include("multiple-type",{schema:schema,_:_}) %>
58+
<%- include("multiple-type",{schema:schema,_:_}).replace(/^/gm, ' ') %>
6659
<% } else { %>
6760
Unknown type `<%= schema.type %>`.
6861
@@ -74,12 +67,10 @@ Unknown type `<%= schema.type %>`.
7467
<% }} %>
7568

7669
<% if (schema.enum!==undefined||schema["meta:enum"]!==undefined) { %>
77-
### <%=name %> Known Values
78-
79-
| Value | Description |
80-
|-------|-------------|
70+
| Value | Description |
71+
|-------|-------------|
8172
<% _.forIn(schema["meta:enum"], (value, key, object) => { %>
82-
| `<%= key %>` | <%= value %> |
73+
| `<%= key %>` | <%= value %> |
8374
<% }); %>
8475
8576
<% } %>

0 commit comments

Comments
 (0)