Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a commandline option for the **MUST** keyword. #57

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ There's also a version [published on npm](https://www.npmjs.com/package/wetzel).

* The `-l` option specifies the starting header level.
* The `-c` option lets you specify a custom symbol to place in front of required properties.
* The `-k` option replaces the word `must` with a specified keyword, such as `**MUST**`.
* The `-p` option lets you specify the relative path that should be used when referencing the schema, relative to where you store the documentation.
* The `-s` option lets you specify the path string that should be used when loading the schema reference paths.
* The `-e` option writes an additional output file that embeds the full text of JSON schemas (AsciiDoctor mode only).
Expand Down
6 changes: 4 additions & 2 deletions bin/wetzel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ if (!defined(argv._[0]) || defined(argv.h) || defined(argv.help)) {
var help = 'Usage: node ' + path.basename(__filename) + ' [path-to-json-schema-file] [OPTIONS]\n' +
' -l, --headerLevel Top-level header. Default: 1\n' +
' -c, --checkmark Symbol for required properties. Default: ✓\n' +
' -k, --keyword Use a particular keyword in place of "must", for example "**MUST**".\n' +
' -p, --schemaPath The path string that should be used when generating the schema reference paths.\n' +
' -s, --searchPath The path string that should be used when loading the schema reference paths.\n' +
' -e, --embedOutput The output path for a document that embeds JSON schemas directly (AsciiDoctor only).\n' +
' -m, --outputMode The output mode, Markdown (the default) or AsciiDoctor (a).' +
' -n, --noTOC Skip writing the Table of Contents.' +
' -m, --outputMode The output mode, Markdown (the default) or AsciiDoctor (a).\n' +
' -n, --noTOC Skip writing the Table of Contents.\n' +
' -a, --autoLink Aggressively auto-inter-link types referenced in descriptions.\n' +
' Add =cqo to auto-link types that are in code-quotes only.\n' +
' -i An array of schema filenames (no paths) that should not get their own\n' +
Expand Down Expand Up @@ -73,6 +74,7 @@ var options = {
writeTOC: !defaultValue(defaultValue(argv.n, argv.noTOC), false),
headerLevel: defaultValue(defaultValue(argv.l, argv.headerLevel), 1),
checkmark: defaultValue(defaultValue(argv.c, argv.checkmark), null),
mustKeyword: defaultValue(defaultValue(argv.k, argv.keyword), null),
schemaRelativeBasePath: defaultValue(defaultValue(argv.p, argv.schemaPath), null),
embedMode: enums.embedMode.none,
debug: defaultValue(defaultValue(argv.d, argv.debug), null),
Expand Down
20 changes: 13 additions & 7 deletions lib/generateMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function generateMarkdown(options) {
style.setCheckmark(options.checkmark);
}

if (defined(options.mustKeyword)) {
style.setMustKeyword(options.mustKeyword);
}

// Verify JSON Schema version
var schemaRef = schema.$schema;
var resolved = null;
Expand Down Expand Up @@ -278,9 +282,11 @@ function createPropertiesDetails(schema, title, headerLevel, knownTypes, autoLin

md += style.bulletItem(style.propertyDetails('Type') + ': ' + summary.formattedType, 0);

var eachElementInTheArrayMust = 'Each element in the array' + style.mustKeyword;

var uniqueItems = property.uniqueItems;
if (defined(uniqueItems) && uniqueItems) {
md += style.bulletItem('Each element in the array must be unique.', 1);
md += style.bulletItem(eachElementInTheArrayMust + 'be unique.', 1);
}

// TODO: items is a full schema
Expand All @@ -293,21 +299,21 @@ function createPropertiesDetails(schema, title, headerLevel, knownTypes, autoLin
var maxString = itemsExclusiveMaximum ? 'less than' : 'less than or equal to';

if (defined(items.minimum) && defined(items.maximum)) {
md += style.bulletItem('Each element in the array must be ' + minString + ' ' +
md += style.bulletItem(eachElementInTheArrayMust + 'be ' + minString + ' ' +
style.minMax(items.minimum) + ' and ' + maxString + ' ' + style.minMax(items.maximum) + '.', 1);
} else if (defined(items.minimum)) {
md += style.bulletItem('Each element in the array must be ' + minString + ' ' + style.minMax(items.minimum) + '.', 1);
md += style.bulletItem(eachElementInTheArrayMust + 'be ' + minString + ' ' + style.minMax(items.minimum) + '.', 1);
} else if (defined(items.maximum)) {
md += style.bulletItem('Each element in the array must be ' + maxString + ' ' + style.minMax(items.maximum) + '.', 1);
md += style.bulletItem(eachElementInTheArrayMust + 'be ' + maxString + ' ' + style.minMax(items.maximum) + '.', 1);
}

if (defined(items.minLength) && defined(items.maxLength)) {
md += style.bulletItem('Each element in the array must have length between ' + style.minMax(items.minLength) +
md += style.bulletItem(eachElementInTheArrayMust + 'have length between ' + style.minMax(items.minLength) +
' and ' + style.minMax(items.maxLength) + '.', 1);
} else if (defined(items.minLength)) {
md += style.bulletItem('Each element in the array must have length greater than or equal to ' + style.minMax(items.minLength) + '.', 1);
md += style.bulletItem(eachElementInTheArrayMust + 'have length greater than or equal to ' + style.minMax(items.minLength) + '.', 1);
} else if (defined(items.maxLength)) {
md += style.bulletItem('Each element in the array must have length less than or equal to ' + style.minMax(items.maxLength) + '.', 1);
md += style.bulletItem(eachElementInTheArrayMust + 'have length less than or equal to ' + style.minMax(items.maxLength) + '.', 1);
}

var itemsString = getEnumString(items, type, 2);
Expand Down
24 changes: 22 additions & 2 deletions lib/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {

setCheckmark: setCheckmark,

setMustKeyword: setMustKeyword,

getHeaderMarkdown: getHeaderMarkdown,

getSectionMarkdown: getSectionMarkdown,
Expand Down Expand Up @@ -123,9 +125,14 @@ module.exports = {
embedJsonSchema: embedJsonSchema,

/**
* @property {string} The markdown string used for displaying the icon used to indicate a value is required.
* @property {string} requiredIcon - The markdown string used for displaying the icon used to indicate a value is required.
*/
requiredIcon: ' ✓ '
requiredIcon: ' ✓ ',

/**
* @property {string} mustKeyword - The keyword used when a condition must be true.
*/
mustKeyword: ' must ',
};

const REFERENCE = "reference-";
Expand Down Expand Up @@ -160,6 +167,19 @@ function setCheckmark(checkmark) {
}
}

/**
* @function setMustKeyword
* Set the keyword used in place of the word "must".
* @param {string} mustKeyword The keyword used when a condition must be true.
*/
function setMustKeyword(mustKeyword) {
if (mustKeyword.length > 0) {
module.exports.mustKeyword = ' ' + mustKeyword + ' ';
} else {
module.exports.mustKeyword = ' must ';
}
}

/**
* @function getHeaderMarkdown
* Gets the markdown syntax for the start of a header.
Expand Down
43 changes: 43 additions & 0 deletions test/test-golden/example-keyword.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Objects
* [`example`](#reference-example) (root object)


---------------------------------------
<a name="reference-example"></a>
## example

Example description.

**`example` Properties**

| |Type|Description|Required|
|---|---|---|---|
|**byteOffset**|`integer`|The offset relative to the start of the buffer in bytes.|No, default: `0`|
|**type**|`string`|Specifies if the elements are scalars, vectors, or matrices.| &#10003; Yes|

Additional properties are not allowed.

### example.byteOffset

The offset relative to the start of the buffer in bytes.

* **Type**: `integer`
* **Required**: No, default: `0`
* **Minimum**: ` >= 0`

### example.type

Specifies if the elements are scalars, vectors, or matrices.

* **Type**: `string`
* **Required**: &#10003; Yes
* **Allowed values**:
* `"SCALAR"`
* `"VEC2"`
* `"VEC3"`
* `"VEC4"`
* `"MAT2"`
* `"MAT3"`
* `"MAT4"`


Loading