This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.js
100 lines (86 loc) · 2.76 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
var _ = require('lodash');
var fs = require('fs');
var file = process.argv[2];
if(!file) {
console.log('No json schema file specified');
return;
}
var schema = JSON.parse(fs.readFileSync(file, 'utf8'));
if(!schema){
console.log('Not a valid json schema');
return;
}
var propertyOpenHTML = function(property, propertyName, first, last) {
var comma = ',';
if(last) {
comma = '';
}
var html;
var description = property.description || '';
if(description.length !== 0) {
description = '// ' + description;
}
if(propertyName) {
html = '<div class="property"><span class="name">' + propertyName + '</span>: ';
} else {
html = '<div class="property">';
}
if(property.type === 'object') {
html += '<span class="type">{</span> <span class="description">'+description+'</span>';
} else if (property.type ==='array' && property.items.type ==='object') {
html += '<span class="type">[{</span> <span class="description">'+description+'</span>';
} else if (property.type ==='array') {
html += '<span class="type">[</span> <span class="description">'+description+'</span>';
} else {
html += '<span class="type">' + property.type + '</span>'+comma+' <span class="description">'+description+'</span>';
}
return html;
}
var propertyCloseHTML = function(property, propertyName, first, last) {
var html = '';
var comma = ',';
if(last) {
comma = '';
}
if(property.type === 'object') {
html += '<span class="type">}'+comma+'</span>';
};
if (property.type ==='array' && property.items.type ==='object') {
html += '<span class="type">}]'+comma+'</span>';
} else if(property.type === 'array') {
html += '<span class="type">]'+comma+'</span>';
};
html += '</div>'
return html;
}
var parseProperty = function (property) {
var properties = [];
_.each(property.properties, function(nestedProperty, key){
properties.push({property: nestedProperty, key: key});
});
if(property.items) {
if(property.items.properties) {
_.each(property.items.properties, function(nestedItemProperty, key){
properties.push({property: nestedItemProperty, key: key});
})
} else {
properties.push({property: property.items, key: null});
}
}
_.each(properties, function(property, index){
var last = (index+1 === properties.length ? true : false);
var first = (index === 0 ? true : false);
html += propertyOpenHTML(property.property, property.key, first, last);
parseProperty(property.property);
html += propertyCloseHTML(property.property, property.key, first, last);
});
};
// Start building the html string
var html = '<div class="json-schema">';
html += '<div class="property"><span class="type">{</span>';
parseProperty(schema);
html += '<span class="type">}</span></div>';
html += '</div>';
// Finished
fs.writeFileSync(file + '.html', html);