Skip to content

Commit

Permalink
feat(markdown): generate header again
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 9, 2019
1 parent d6b9e5e commit 011427c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 76 deletions.
80 changes: 46 additions & 34 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const generate = require('./lib/generateName');
const filterRefs = require('./lib/filterRefs');
const validate = require('./lib/validateSchemas');
const build = require('./lib/markdownBuilder');
const { writereadme } = require('./lib/writeMarkdown');
const { writereadme, writemarkdown } = require('./lib/writeMarkdown');
const readme = require('./lib/readmeBuilder');
const formatInfo = require('./lib/formatInfo');
const { loader } = require('./lib/schemaProxy');
Expand Down Expand Up @@ -139,58 +139,70 @@ const schemaloader = loader();
// list all schema files in the specified directory
readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtension}` })
// then collect data about the schemas and turn everything into a big object
.then(schemas => pipe(
schemas,
map(schema => schema.fullPath),
map(path => schemaloader(require(path), path)),
// validate
// validate(ajv, logger),
// find contained schemas
traverse,

// remove pure ref schemas
// filterRefs,

.then((schemas) => {
pipe(
schemas,
map(schema => schema.fullPath),
map(path => schemaloader(require(path), path)),
// find contained schemas
traverse,
// build readme
readme({
readme: !argv.n,
}),

writereadme({
readme: !argv.n,
out: argv.o,
info,
error,
debug,
meta: argv.m,
}),
);

// generate Markdown
return pipe(
schemas,
map(schema => schema.fullPath),
map(path => schemaloader(require(path), path)),
// validate
// validate(ajv, logger),
// find contained schemas
traverse,

// remove pure ref schemas
// filterRefs,

/*
(x) => {
console.log('emitting');
const y = list(x);
console.log(y);
return y;
},
*/

// build readme
readme({
readme: !argv.n,
}),

writereadme({
readme: !argv.n,
out: argv.o,
info,
error,
debug,
meta: argv.m,
}),
// generate Markdown ASTs
build({
header: argv.h,
links: docs,
}),

/* skip for now
// generate Markdown ASTs
build({
header: argv.h,
links: docs,
}),


// write to files

write({
writemarkdown({
out: argv.o,
info,
error,
debug,
meta: argv.m,
}),
*/
))
);
})

.then((schemas) => {
// console.log('allschemas', schemas);
Expand Down
9 changes: 8 additions & 1 deletion lib/formatInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ function gettype(schema) {
if (isabstract(schema)) {
return gettype(schema.definitions);
}
return undefined;
if (schema.type === undefined) {
return `undefined`;
}
if (schema.oneOf || schema.anyOf || schema.allOf || schema.not) {
return 'merged';
}
console.log('Unknown type', schema.type);
return `undefined`;
}

function plaindescription(schema) {
Expand Down
14 changes: 7 additions & 7 deletions lib/formattingTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
* governing permissions and limitations under the License.
*/
const i18n = require('es2015-i18n-tag').default;
const { list } = require('mdast-builder');
const stringify = require('mdast-util-to-string');
const inspect = require('unist-util-inspect');
const remark = require('remark');
const unified = require('unified');
const strip = require('strip-markdown');
const s = require('./symbols');

function gentitle(titles, type) {
Expand All @@ -27,7 +21,13 @@ function gentitle(titles, type) {
if (lasttitle) {
return lasttitle;
}
return i18n`Untitled ${type} in ${firsttitle}`;
if (typeof type === 'string') {
return i18n`Untitled ${type} in ${firsttitle}`;
}
if (typeof type === 'undefined') {
return i18n`Untitled undefined type in ${firsttitle}`;
}
return i18n`Untitled complex type in ${firsttitle}`;
}

function gendescription(schema) {
Expand Down
57 changes: 38 additions & 19 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
* governing permissions and limitations under the License.
*/
const {
each, values, map, list: flist, iter, flat, filter, size,
each, values, map, list: flist, iter, flat, filter, size, foldl,
} = require('ferrum');
const {
root, paragraph, text, heading, code, table, tableRow, tableCell, link, inlineCode,
} = require('mdast-builder');
const i18n = require('es2015-i18n-tag').default;
const s = require('./symbols');
const { gentitle } = require('./formattingTools');

function build({ header, links = {} }) {
const headerprops = [
Expand All @@ -24,6 +26,14 @@ function build({ header, links = {} }) {
title: i18n`Type`,
objectlabel: i18n`Object`,
arraylabel: i18n`Array`,
multiplelabel: i18n`Multiple`,
mergedlabel: i18n`Merged`,
undefinedlabel: i18n`Undefined`,
numberlabel: i18n`Number`,
booleanlabel: i18n`Boolean`,
stringlabel: i18n`String`,
integerlabel: i18n`Integer`,
nulllabel: i18n`Null`,
},
{
name: 'abstract',
Expand Down Expand Up @@ -84,9 +94,9 @@ function build({ header, links = {} }) {
function makeheader(schema) {
if (header) {
return [
heading(1, text(i18n`${schema.title} Schema`)),
paragraph(code('txt', schema.id + (schema.pointer ? `#${schema.pointer}` : ''))),
schema.longdescription,
heading(1, text(i18n`${gentitle(schema[s.titles], schema.type)} Schema`)),
paragraph(code('txt', schema[s.id] + (schema[s.pointer] ? `#${schema[s.pointer]}` : ''))),
schema[s.meta].longdescription,
table('left', [
// iterate over header
tableRow(
Expand All @@ -105,14 +115,20 @@ function build({ header, links = {} }) {
map(headerprops,
(prop) => {
// this is a linked property
if (schema.meta
&& typeof schema.meta[prop.name] === 'object'
&& schema.meta[prop.name].link
&& schema.meta[prop.name].text) {
return tableCell(link(schema.meta[prop.name].link, i18n`open original schema`, [text(schema.meta[prop.name].text)]));
if (schema[s.meta]
&& typeof schema[s.meta][prop.name] === 'object'
&& schema[s.meta][prop.name].link
&& schema[s.meta][prop.name].text) {
return tableCell(link(schema[s.meta][prop.name].link, i18n`open original schema`, [text(schema[s.meta][prop.name].text)]));
}
const value = schema[s.meta] ? schema[s.meta][prop.name] : undefined;
if (prop[`${String(value)}label`]) {
return tableCell(text(prop[`${String(value)}label`] || i18n`Unknown`));
} else {
const warn = `Unknown label in ${prop.name} for value ${String(value)}`;
console.log(warn);
return tableCell(text(i18n`Unknown`));
}
const value = schema.meta ? schema.meta[prop.name] : undefined;
return tableCell(text(prop[`${String(value)}label`] || i18n`Unknown`));
}), Array,
),
),
Expand Down Expand Up @@ -204,15 +220,18 @@ function build({ header, links = {} }) {
return [];
}

console.log('generating markdown');
return (schemas) => {
// eslint-disable-next-line no-return-assign, no-param-reassign
each(values(schemas), schema => schema.markdown = root([
// todo add more elements
...makeheader(schema),
...makedefinitions(schema),
...makeproperties(schema),
]));
return schemas;
return foldl(schemas, {}, (pv, schema) => {
// eslint-disable-next-line no-param-reassign
pv[schema[s.slug]] = root([
// todo add more elements
...makeheader(schema),
// ...makedefinitions(schema),
// ...makeproperties(schema),
]);
return pv;
});
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/readmeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function build({ readme = true }) {
filter(schema => !schema[s.parent]), // remove schemas with a parent
mapSort(schema => gentitle(schema[s.titles], schema.type)),
map(schema => listItem(paragraph([
link(`./${schema[s.slug]}.schema.md`, gendescription(schema), [text(gentitle(schema[s.titles], schema.type))]),
link(`./${schema[s.slug]}.md`, gendescription(schema), [text(gentitle(schema[s.titles], schema.type))]),
text(' – '),
inlineCode(schema.$id),
]))),
Expand All @@ -45,7 +45,7 @@ function build({ readme = true }) {
filter(schema => !!schema[s.parent]), // keep only schemas with a parent
mapSort(schema => gentitle(schema[s.titles], schema.type)),
map(schema => listItem(paragraph([
link(`./${schema[s.slug]}.schema.md`, gendescription(schema), [text(gentitle(schema[s.titles], schema.type))]),
link(`./${schema[s.slug]}.md`, gendescription(schema), [text(gentitle(schema[s.titles], schema.type))]),
text(' – '),
inlineCode(`${schema[s.id]}#${schema[s.pointer]}`),
]))),
Expand Down
17 changes: 4 additions & 13 deletions lib/writeMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,30 @@ const yaml = require('js-yaml');
function writemarkdown({
out, debug, error, info, meta,
}) {
const dbg = (message) => {
if (debug && typeof message === 'object') {
debug(inspect(message));
} else if (debug) {
debug(message);
}
};

const processor = unified()
.use(stringify);

fs.mkdirpSync(out);

return (schemas) => {
each(pairs(schemas), ([name, schema]) => {
dbg(schema.markdown);
const fileName = path.resolve(out, `${name}.schema.md`);
each(pairs(schemas), ([name, markdown]) => {
const fileName = path.resolve(out, `${name}.md`);

const output =
// add YAML frontmatter
(!meta ? '' : '---\n')
+ (!meta ? '' : yaml.safeDump(meta))
+ (!meta ? '' : '---\n\n')

+ processor.stringify(schema.markdown);
+ processor.stringify(markdown);


fs.writeFile(fileName, output, (err) => {
if (err) {
error(err);
}
// info(`${fileName} created`);
schema.outFile = fileName;
//info(`${fileName} created`);
});
});

Expand Down

0 comments on commit 011427c

Please sign in to comment.