diff --git a/README.md b/README.md index 52a37b1..45030c2 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,40 @@ -# aml2html -[![Build Status](https://travis-ci.org/aml-org/aml2html.svg?branch=master)](https://travis-ci.org/aml-org/aml2html) +# aml2doc +[![Build Status](https://travis-ci.org/aml-org/aml2doc.svg?branch=master)](https://travis-ci.org/aml-org/aml2doc) -Convert AML Vocabularies & Dialects to HTML. +Convert AML Vocabularies & Dialects to documentation. ## Installation ```sh -$ git clone git@github.com:aml-org/aml2html.git -$ cd aml2html +$ git clone git@github.com:aml-org/aml2doc.git +$ cd aml2doc $ npm install ``` ## Usage CLI command notation (*note the required `--` before arguments*): ``` -Usage: npm run aml2html -- [options] +Usage: npm run aml2doc -- [options] -Convert AML Vocabularies & Dialects to HTML +Convert AML Vocabularies & Dialects to documentation. Options: -d, --indir Path to input directory to convert. Takes precedence over --infile. -f, --infile Path to input file to convert (default: []) + -s, --syntax Output syntax (html or md) (default: "html") -c, --css Custom css file path (default: []) -g, --cfg Configuration file path - -h, --help output usage information + -h, --help Output usage information -t, --templates Path to optional custom templates for the documentation ``` E.g.: ```sh -$ npm run aml2html -- ./outdir --infile=test_data/amf/dialects/canonical_webapi.yaml --infile=test_data/amf/dialects/oas20.yaml --infile=test_data/amf/dialects/validation.yaml --infile=test_data/music/dialect/playlist.yaml +$ npm run aml2doc -- ./outdir --infile=test_data/amf/dialects/canonical_webapi.yaml --infile=test_data/amf/dialects/oas20.yaml --infile=test_data/amf/dialects/validation.yaml --infile=test_data/music/dialect/playlist.yaml ``` Or using `--indir` option: ```sh -$ npm run aml2html -- ./outdir --indir=./test_data +$ npm run aml2doc -- ./outdir --indir=./test_data ``` @@ -78,7 +79,7 @@ This file can be used when invoking the HTML generator in the following way: E.g. ```sh -$ npm run aml2html -- ./outdir --indir=./test_data --cfg=/path/to/cfg.js +$ npm run aml2doc -- ./outdir --indir=./test_data --cfg=/path/to/cfg.js ``` Configuration files will be searched relative to the working directory of the node interpreter. diff --git a/bin/aml2html.js b/bin/aml2doc.js similarity index 80% rename from bin/aml2html.js rename to bin/aml2doc.js index c3c855b..5d999d7 100755 --- a/bin/aml2html.js +++ b/bin/aml2doc.js @@ -2,7 +2,7 @@ const program = require('commander') -const aml2html = require('../src/index') +const aml2doc = require('../src/index') const utils = require('../src/utils') program @@ -10,10 +10,11 @@ program .action(outputDir => { program.outputDir = outputDir }) - .name('aml2html') - .description('Convert AML Vocabularies & Dialects to HTML') + .name('aml2doc') + .description('Convert AML Vocabularies & Dialects to documentation.') .option('-d, --indir ', 'Path to input directory to convert. Takes precedence over --infile.') .option('-f, --infile ', 'Path to input file to convert', utils.collectOpt, []) + .option('-s, --syntax ', 'Output syntax (html or md)', 'html') .option('-c, --css ', 'Custom css file path', utils.collectOpt, []) .option('-g, --cfg ', 'Configuration file path') .option('-t, --templates ', 'Optional path to custom templates for the documentation') @@ -28,4 +29,4 @@ if (!(program.infile.length > 0 || program.indir)) { program.help() } -aml2html(program) +aml2doc(program) diff --git a/package-lock.json b/package-lock.json index 3c76543..51e8cca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "@aml-org/aml2html", - "version": "0.1.3", + "name": "@aml-org/aml2doc", + "version": "0.1.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 735f4ce..1a7fcfc 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { - "name": "@aml-org/aml2html", - "version": "0.1.3", + "name": "@aml-org/aml2doc", + "version": "0.1.4", + "description": "Convert AML Vocabularies & Dialects to documentation", "main": "src/index.js", "license": "MIT", - "repository": "git://github.com/aml-org/aml2html", + "repository": "git://github.com/aml-org/aml2doc", "dependencies": { "amf-client-js": "^4.0.4", "commander": "^3.0.2", @@ -16,11 +17,11 @@ "registry": "https://npm.pkg.github.com/" }, "bin": { - "aml2html": "bin/aml2html.js" + "aml2doc": "bin/aml2doc.js" }, "scripts": { "lint": "standard", - "aml2html": "node bin/aml2html.js", + "aml2doc": "node bin/aml2doc.js", "test": "npm run lint && nyc mocha -R spec --bail test/*.test.js" }, "devDependencies": { diff --git a/src/data_collectors.js b/src/data_collectors.js index cfea38c..53931e0 100644 --- a/src/data_collectors.js +++ b/src/data_collectors.js @@ -18,7 +18,7 @@ function collectVocabularyData (doc, ctx, acc) { vocabularyData.usage = usage[0]['@value'] } vocabularyData.slug = utils.slugify(vocabularyData.name + '_vocab') - vocabularyData.htmlName = `${vocabularyData.slug}.html` + vocabularyData.pageName = vocabularyData.slug console.log(`Collecting nodes info for vocabulary ${id}`) vocabularyData.nodeMappings = collectVocabularyNodesData( doc, vocabularyData, ctx).sort(utils.nameSorter) @@ -46,7 +46,7 @@ function collectDialectData (doc, ctx, acc, ontologyTerms) { dialectData.usage = usage[0]['@value'] } dialectData.slug = utils.slugify(dialectData.name) - dialectData.htmlName = `${dialectData.slug}.html` + dialectData.pageName = dialectData.slug console.log(`Collecting nodes info for dialect ${id}`) dialectData.nodeMappings = collectNodesData( doc, dialectData, ctx, ontologyTerms) @@ -91,9 +91,9 @@ function collectVocabularyNodesData (doc, dialectData, ctx) { dialectName: dialectData.name, dialectLabel: ctx.config.labelMapping(dialectData.name) } - // htmlName + // pageName nodeData.slug = utils.slugify(`${nodeData.name}_${cred.type}`) - nodeData.htmlName = utils.makeSchemaHtmlName( + nodeData.pageName = utils.makeSchemaPageName( dialectData.slug, nodeData.slug) // save acc[nodeId] = nodeData @@ -120,9 +120,9 @@ function collectNodesData (doc, dialectData, ctx, ontologyTerms) { dialectName: dialectData.name, dialectLabel: ctx.config.labelMapping(dialectData.name) } - // htmlName + // pageName nodeData.slug = utils.slugify(nodeData.name) - nodeData.htmlName = utils.makeSchemaHtmlName( + nodeData.pageName = utils.makeSchemaPageName( dialectData.slug, nodeData.slug) const isUnion = node.query('@type') @@ -198,7 +198,7 @@ function collectLinkPropsData (doc, node, dialectSlug, ontologyTerms, ctx) { } const decl = doc.query(`amldoc:declares[@id=${rangeId}]`) if (decl) { - data.rangeHtmlName = utils.makeSchemaHtmlName( + data.rangePageName = utils.makeSchemaPageName( utils.slugify(decl.parent().query('> core:name @value')), utils.slugify(data.rangeName)) } @@ -270,7 +270,7 @@ function collectCommonNavData (dialectsData, ctx) { return { name: name, label: ctx.config.labelMapping(name), - htmlName: data.htmlName, + pageName: data.pageName, active: false } }), @@ -288,7 +288,7 @@ function collectNavData (dialectData, commonNavData, ctx) { return { name: name, label: ctx.config.labelMapping(name), - htmlName: data.htmlName, + pageName: data.pageName, active: false } }) diff --git a/src/index.js b/src/index.js index 5952e86..229f402 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,7 @@ const collect = require('./data_collectors') let TMPL_DIR = path.join(utils.TMPL_DIR) /** Runs all the logic. */ -async function aml2html (program) { +async function aml2doc (program) { let ctx = utils.getDefaultContext() await amf.AMF.init() @@ -85,8 +85,8 @@ async function aml2html (program) { // Render dialect overview template utils.renderTemplate( { ...dialectData, ...ctx.config, ...links }, - path.join(TMPL_DIR, 'dialect.mustache'), - path.join(outDir, dialectData.htmlName)) + path.join(TMPL_DIR, program.syntax, 'dialect.mustache'), + path.join(outDir, `${dialectData.pageName}.${program.syntax}`)) // Render nodeMappings item data dialectData.nodeMappings.forEach(nodeData => { @@ -97,8 +97,8 @@ async function aml2html (program) { nodeData.css = program.css utils.renderTemplate( { ...nodeData, ...ctx.config }, - path.join(TMPL_DIR, 'node.mustache'), - path.join(outDir, nodeData.htmlName)) + path.join(TMPL_DIR, program.syntax, 'node.mustache'), + path.join(outDir, `${nodeData.pageName}.${program.syntax}`)) }) }) @@ -113,10 +113,10 @@ async function aml2html (program) { ...ctx.config, ...indexLinks }, - path.join(TMPL_DIR, 'index.mustache'), - path.join(outDir, 'index.html')) + path.join(TMPL_DIR, program.syntax, 'index.mustache'), + path.join(outDir, `index.${program.syntax}`)) utils.copyStaticFiles(outDir) } -module.exports = aml2html +module.exports = aml2doc diff --git a/src/utils.js b/src/utils.js index 3aa68f9..cbf49ba 100644 --- a/src/utils.js +++ b/src/utils.js @@ -58,21 +58,19 @@ function parseHashValue (id) { return afterHash } -/** Renders Mustache template with data and writes it to an HTML file. +/** Renders Mustache template with data and writes it to a file. * * @param data Data to be renreder in a template. - * @param tmplType Template type. Must match type-specific template - folders names. * @param tmplPath Mustache template path. - * @param htmlPath Output HTML path. + * @param outPath Output file path. */ -function renderTemplate (data, tmplPath, htmlPath) { +function renderTemplate (data, tmplPath, outPath) { console.log( `Rendering "${tmplPath}" template`, data.id ? `for ${data.id}` : '') const tmplStr = fs.readFileSync(tmplPath, 'utf-8') - const htmlStr = Mustache.render(tmplStr, data) - fs.writeFileSync(htmlPath, htmlStr) + const renderedStr = Mustache.render(tmplStr, addTmplUtils(data)) + fs.writeFileSync(outPath, renderedStr) } /* Sorts objects by property. To be used in Array.sort() */ @@ -88,14 +86,14 @@ function sorterBy (p) { } } -/* Makes a slug used in html names creation. */ +/* Makes a slug used in page urls creation. */ function slugify (val) { return val.split(' ').join('').toLowerCase() } -/* Creates an html page name for nodeMappings item. */ -function makeSchemaHtmlName (dialectSlug, schemaName) { - return `schema_${dialectSlug}_${schemaName}.html` +/* Creates a schema page name for nodeMappings item. */ +function makeSchemaPageName (dialectSlug, schemaName) { + return `schema_${dialectSlug}_${schemaName}` } /* Marks item with matching name as active/selected. */ @@ -238,6 +236,22 @@ function getOntologyTerms (ontologyObj) { return ontologyTerms } +/* Adds template utility functions */ +function addTmplUtils (data) { + return { + ...data, + stripn: stripn + } +} + +/* Strips newlines */ +function stripn () { + return (text, render) => { + const rendered = render(text) + return rendered ? rendered.split('\n').join(' ') : rendered + } +} + module.exports = { walkSync: walkSync, getJsonLdGraph: getJsonLdGraph, @@ -249,11 +263,12 @@ module.exports = { nameSorter: sorterBy('name'), sorterBy: sorterBy, slugify: slugify, - makeSchemaHtmlName: makeSchemaHtmlName, + makeSchemaPageName: makeSchemaPageName, markActive: markActive, getDefaultContext: getDefaultContext, loadConfig: loadConfig, collectOpt: collectOpt, + addTmplUtils: addTmplUtils, processLinks: processLinks, collectJsonGraphs: collectJsonGraphs, getOntologyTerms: getOntologyTerms diff --git a/templates/dialect.mustache b/templates/html/dialect.mustache similarity index 92% rename from templates/dialect.mustache rename to templates/html/dialect.mustache index 2f728e9..1963fb0 100644 --- a/templates/dialect.mustache +++ b/templates/html/dialect.mustache @@ -27,7 +27,7 @@
{{#navData.dialects}}

- {{name}} + {{name}}

{{/navData.dialects}} @@ -36,7 +36,7 @@
{{#navData.nodeMappings}}

- {{name}} + {{name}}

{{/navData.nodeMappings}} @@ -76,7 +76,7 @@ diff --git a/templates/index.mustache b/templates/html/index.mustache similarity index 97% rename from templates/index.mustache rename to templates/html/index.mustache index 4be0c46..a173d8b 100644 --- a/templates/index.mustache +++ b/templates/html/index.mustache @@ -26,7 +26,7 @@
{{#navData.dialects}}

- {{name}} + {{name}}

{{/navData.dialects}} diff --git a/templates/node.mustache b/templates/html/node.mustache similarity index 88% rename from templates/node.mustache rename to templates/html/node.mustache index 8e8e1d0..bfda9ce 100644 --- a/templates/node.mustache +++ b/templates/html/node.mustache @@ -26,7 +26,7 @@
{{#navData.dialects}}

- {{name}} + {{name}}

{{/navData.dialects}} @@ -35,7 +35,7 @@
{{#navData.nodeMappings}}

- {{name}} + {{name}}

{{/navData.nodeMappings}} @@ -64,12 +64,12 @@
    {{#linkedSchemas}}
  • - {{#rangeHtmlName}} - {{rangeName}} - {{/rangeHtmlName}} - {{^rangeHtmlName}} + {{#rangePageName}} + {{rangeName}} + {{/rangePageName}} + {{^rangePageName}} {{rangeName}} - {{/rangeHtmlName}} + {{/rangePageName}}
  • {{/linkedSchemas}}
@@ -131,12 +131,12 @@
    {{#range}}
  • - {{#rangeHtmlName}} - {{rangeName}} - {{/rangeHtmlName}} - {{^rangeHtmlName}} + {{#rangePageName}} + {{rangeName}} + {{/rangePageName}} + {{^rangePageName}} {{rangeName}} - {{/rangeHtmlName}} + {{/rangePageName}}
  • {{/range}}
diff --git a/templates/md/dialect.mustache b/templates/md/dialect.mustache new file mode 100644 index 0000000..6f6f23d --- /dev/null +++ b/templates/md/dialect.mustache @@ -0,0 +1,39 @@ +# {{label}} + +## Navigation + +**Index:** • [Index](index.md) + +**{{dialectsHeader}}:**{{#navData.dialects}} • {{#active}}*{{name}}*{{/active}}{{^active}}[{{name}}]({{pageName}}.md){{/active}}{{/navData.dialects}} + +**{{schemasHeader}}:**{{#navData.nodeMappings}} • {{#active}}*{{name}}*{{/active}}{{^active}}[{{name}}]({{pageName}}.md){{/active}}{{/navData.nodeMappings}} + +## {{label}} +URL: {{id}} + +{{#usage}} +Description: {{#stripn}}{{usage}}{{/stripn}} +{{/usage}} + +{{#version}} +Version: {{version}} +{{/version}} + +{{#hasPrimaryLinks}} +Download: +{{#primaryLinks}} + * [{{text}}]({{{href}}}) +{{/primaryLinks}} +{{/hasPrimaryLinks}} + +Schemas: +{{#nodeMappings}} + * [{{label}}]({{pageName}}.md) +{{/nodeMappings}} + +{{#hasSecondaryLinks}} +Download in other formats: +{{#secondaryLinks}} + * [{{text}}]({{{href}}}) +{{/secondaryLinks}} +{{/hasSecondaryLinks}} diff --git a/templates/md/index.mustache b/templates/md/index.mustache new file mode 100644 index 0000000..a024b41 --- /dev/null +++ b/templates/md/index.mustache @@ -0,0 +1,46 @@ +# Index + +## Navigation + +**Index:** • Index + +**{{dialectsHeader}}:**{{#navData.dialects}} • [{{name}}]({{pageName}}.md){{/navData.dialects}} + +## {{mainHeader}} +{{#indexHeader}} +Title: {{indexHeader}} +{{/indexHeader}} + +{{#indexVersion}} +Version: {{indexVersion}} +{{/indexVersion}} + +{{#indexDescription}} +Description: {{indexDescription}} +{{/indexDescription}} + +{{#hasIndexLinks}} +Download as a single file in the following formats: +{{#indexLinks}} + * [{{text}}]({{{href}}}) +{{/indexLinks}} +{{/hasIndexLinks}} + + +## {{dialectsHeader}} +Dialect | Usage +------- | ----- +{{#dialects}} +{{label}} | {{usage}} +{{/dialects}} + +## {{schemasHeader}} +{{#dialects}} + +### {{label}} +Schema | Description +------ | ----------- +{{#nodeMappings}} +{{label}} | {{#stripn}}{{description}}{{/stripn}} +{{/nodeMappings}} +{{/dialects}} diff --git a/templates/md/node.mustache b/templates/md/node.mustache new file mode 100644 index 0000000..16a8440 --- /dev/null +++ b/templates/md/node.mustache @@ -0,0 +1,75 @@ +# {{dialectName}} :: {{label}} + +## Navigation + +**Index:** • [Index](index.md) + +**{{dialectsHeader}}:**{{#navData.dialects}} • {{#active}}*{{name}}*{{/active}}{{^active}}[{{name}}]({{pageName}}.md){{/active}}{{/navData.dialects}} + +**{{schemasHeader}}:**{{#navData.nodeMappings}} • {{#active}}*{{name}}*{{/active}}{{^active}}[{{name}}]({{pageName}}.md){{/active}}{{/navData.nodeMappings}} + +## {{dialectName}} :: {{label}} +Term ID: {{targetClassId}} + +{{#description}} +Description: {{#stripn}}{{description}}{{/stripn}} +{{/description}} + +{{^linkedSchemas}}No {{/linkedSchemas}}Linked schemas +{{#linkedSchemas}} +{{#rangePageName}} + * [{{rangeName}}]({{rangePageName}}.md) +{{/rangePageName}} +{{^rangePageName}} + * {{rangeName}} +{{/rangePageName}} +{{/linkedSchemas}} + +### Scalar Properties +{{^scalarProperties}}No Scalar Properties {{/scalarProperties}} + +{{#scalarProperties}} + +#### {{name}} + +Field | Value +----- | ----- +Term ID | {{id}} +Range | {{range}} +{{#propDesc}} +Description | {{#stripn}}{{propDesc}}{{/stripn}} +{{/propDesc}} + +{{#constraints.length}} +Constraint | Value +---------- | ----- +{{#constraints}} +{{name}} | {{#stripn}}{{value}}{{/stripn}} +{{/constraints}} +{{/constraints.length}} +{{/scalarProperties}} + + +### Link Properties +{{^linkProperties}}No Link Properties {{/linkProperties}} + +{{#linkProperties}} + +#### {{name}} + +Field | Value +----- | ----- +Term ID | {{id}} +Range | {{#range}} • {{#rangePageName}}[{{rangeName}}]({{rangePageName}}.md){{/rangePageName}}{{^rangePageName}}{{rangeName}}{{/rangePageName}}{{/range}} +{{#propDesc}} +Description | {{#stripn}}{{propDesc}}{{/stripn}} +{{/propDesc}} + +{{#constraints.length}} +Constraint | Value +---------- | ----- +{{#constraints}} +{{name}} | {{#stripn}}{{value}}{{/stripn}} +{{/constraints}} +{{/constraints.length}} +{{/linkProperties}} diff --git a/test/data_collectors.test.js b/test/data_collectors.test.js index e1c051d..555cce6 100644 --- a/test/data_collectors.test.js +++ b/test/data_collectors.test.js @@ -16,11 +16,11 @@ function getDialectData () { name: 'WebAPI', id: 'file://test/fixtures/musicDialect.yaml', slug: 'webapi', - htmlName: 'webapi.html', + pageName: 'webapi', nodeMappings: [{ name: 'Scope', id: 'file://test/fixtures/musicDialect.yaml#/declarations/Scope', - htmlName: 'node_scope.html', + pageName: 'node_scope', description: null, scalarProperties: [{ name: 'name', @@ -61,7 +61,7 @@ describe('data_collectors.processVocabulary', async function () { version: null, label: 'Music Vocabulary', slug: 'musicvocabulary_vocab', - htmlName: 'musicvocabulary_vocab.html' + pageName: 'musicvocabulary_vocab' }) expect(acc[id]) .to.have.property('nodeMappings').and @@ -76,7 +76,7 @@ describe('data_collectors.processVocabulary', async function () { dialectName: 'Music Vocabulary', dialectLabel: 'Music Vocabulary', slug: 'musicartist_class', - htmlName: 'schema_musicvocabulary_vocab_musicartist_class.html' + pageName: 'schema_musicvocabulary_vocab_musicartist_class' }) }) }) @@ -98,7 +98,7 @@ describe('data_collectors.processDialect', async function () { id: 'file://test/fixtures/musicDialect.yaml', version: '1.0', slug: 'playlist', - htmlName: 'playlist.html' + pageName: 'playlist' }) expect(acc[id]) .to.have.property('nodeMappings') @@ -111,7 +111,7 @@ describe('data_collectors.processDialect', async function () { dialectName: 'Playlist', dialectLabel: 'Playlist', slug: 'artistnode', - htmlName: 'schema_playlist_artistnode.html', + pageName: 'schema_playlist_artistnode', description: 'A person or a group of people (or a computer :-) ), whose musical creative work shows sensitivity and imagination\n', targetClassId: 'http://a.ml/vocabularies/music#MusicArtist' }) @@ -159,7 +159,7 @@ describe('data_collectors.collectNavData', async function () { nodeMappings: [{ name: 'Scope', label: 'Scope', - htmlName: 'node_scope.html', + pageName: 'node_scope', active: false }] }) @@ -176,7 +176,7 @@ describe('data_collectors.collectCommonNavData', async function () { dialects: [{ name: 'WebAPI', label: 'WebAPI', - htmlName: 'webapi.html', + pageName: 'webapi', active: false }], nodeMappings: [] @@ -239,17 +239,17 @@ describe('data_collectors.collectLinkPropsData', async function () { rangeDescription: 'hello', rangeName: 'shapeValidationNode', rangeLabel: 'shapeValidationNode', - rangeHtmlName: 'schema_validationprofile_shapevalidationnode.html' + rangePageName: 'schema_validationprofile_shapevalidationnode' }, { rangeDescription: undefined, rangeName: 'queryValidationNode', rangeLabel: 'queryValidationNode', - rangeHtmlName: 'schema_validationprofile_queryvalidationnode.html' + rangePageName: 'schema_validationprofile_queryvalidationnode' }, { rangeDescription: undefined, rangeName: 'functionValidationNode', rangeLabel: 'functionValidationNode', - rangeHtmlName: 'schema_validationprofile_functionvalidationnode.html' + rangePageName: 'schema_validationprofile_functionvalidationnode' }] }]) }) @@ -349,7 +349,7 @@ describe('data_collectors.collectNodesData', async function () { dialectName: 'WebAPI', dialectLabel: 'WebAPI', slug: 'propertyconstraintnode', - htmlName: 'schema_webapi_propertyconstraintnode.html', + pageName: 'schema_webapi_propertyconstraintnode', description: '', targetClassId: 'http://www.w3.org/ns/shacl#PropertyShape' }) @@ -381,7 +381,7 @@ describe('data_collectors.collectVocabularyNodesData', async function () { dialectName: 'WebAPI', dialectLabel: 'WebAPI', slug: 'musicartist_class', - htmlName: 'schema_webapi_musicartist_class.html' + pageName: 'schema_webapi_musicartist_class' }, { type: 'datatypeProperty', name: 'duration', @@ -391,7 +391,7 @@ describe('data_collectors.collectVocabularyNodesData', async function () { dialectName: 'WebAPI', dialectLabel: 'WebAPI', slug: 'duration_datatypeproperty', - htmlName: 'schema_webapi_duration_datatypeproperty.html' + pageName: 'schema_webapi_duration_datatypeproperty' }]) }) }) @@ -408,7 +408,7 @@ describe('data_collectors.collectDialectData', async function () { id: 'file://test/fixtures/validationDialect.yaml', version: '1.0', slug: 'validationprofile', - htmlName: 'validationprofile.html' + pageName: 'validationprofile' }) expect(data) .to.have.property('nodeMappings').and @@ -428,7 +428,7 @@ describe('data_collectors.collectVocabularyData', async function () { version: null, label: 'Music Vocabulary', slug: 'musicvocabulary_vocab', - htmlName: 'musicvocabulary_vocab.html' + pageName: 'musicvocabulary_vocab' }) expect(data) .to.have.property('nodeMappings').and diff --git a/test/integration.test.js b/test/integration.test.js index ffbef38..1b7e244 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -5,11 +5,11 @@ const tmp = require('tmp') const path = require('path') const fs = require('fs-extra') -const aml2html = require('../src/index') +const aml2doc = require('../src/index') const FIXTURES_DIR = path.join(__dirname, 'fixtures') -describe('aml2html integration test', function () { +describe('aml2doc html integration test', function () { let outDir before(async function () { outDir = tmp.dirSync() @@ -20,9 +20,10 @@ describe('aml2html integration test', function () { path.join(FIXTURES_DIR, 'musicDialect.yaml') ], css: ['inexisting/css/file.css'], - templates: path.join(__dirname, '..', 'templates') + templates: path.join(__dirname, '..', 'templates'), + syntax: 'html' } - await aml2html(program) + await aml2doc(program) }) after(function () { try { fs.removeSync(outDir.name) } catch (e) {} @@ -92,3 +93,77 @@ describe('aml2html integration test', function () { expect(html).to.contain('Union of ArtistNode, ArtistNode') }) }) + +describe('aml2doc md integration test', function () { + let outDir + before(async function () { + outDir = tmp.dirSync() + const program = { + outputDir: outDir.name, + cfg: path.join(FIXTURES_DIR, 'cfg.js'), + infile: [ + path.join(FIXTURES_DIR, 'musicDialect.yaml') + ], + css: ['inexisting/css/file.css'], + templates: path.join(__dirname, '..', 'templates'), + syntax: 'md' + } + await aml2doc(program) + }) + after(function () { + try { fs.removeSync(outDir.name) } catch (e) {} + }) + it('should render templates', function () { + expect(fs.readdirSync(outDir.name)).to.deep.equal([ + 'index.md', + 'playlist.md', + 'schema_playlist_artistnode.md', + 'schema_playlist_artistunion.md', + 'static' + ]) + expect(fs.readdirSync(path.join(outDir.name, 'static'))) + .to.deep.equal(['css']) + expect(fs.readdirSync(path.join(outDir.name, 'static', 'css'))) + .to.deep.equal([ + 'bootstrap.min.css', + 'custom.css' + ]) + }) + it('should render proper data at index page', function () { + const fpath = path.join(outDir.name, 'index.md') + const md = fs.readFileSync(fpath).toString() + expect(md) + .to.contain('playlist.md').and + .to.contain('Playlist_modified').and + .to.contain('ArtistNode_modified').and + .to.contain('hi://test.else/vocabulary.aml').and + .to.contain('hi://test.com/vocabulary.pdf').and + .to.contain('hi://test.com/vocabulary.txt') + }) + it('should render proper data at dialect page', function () { + const fpath = path.join(outDir.name, 'playlist.md') + const md = fs.readFileSync(fpath).toString() + expect(md) + .to.contain('index.md').and + .to.contain('schema_playlist_artistnode.md').and + .to.contain('schema_playlist_artistunion.md').and + .to.contain('Version:').and + .to.contain('ArtistNode_modified').and + .to.contain('musicDialect.yaml_modified') + }) + it('should render proper data at schema page', function () { + const fpath = path.join(outDir.name, 'schema_playlist_artistnode.md') + const md = fs.readFileSync(fpath).toString() + expect(md) + .to.contain('index.md').and + .to.contain('No Linked schemas').and + .to.contain('schema.org').and + .to.contain('string').and + .to.contain('No Link Properties') + }) + it('should render proper data at union schema page', function () { + const fpath = path.join(outDir.name, 'schema_playlist_artistunion.md') + const md = fs.readFileSync(fpath).toString() + expect(md).to.contain('Union of ArtistNode, ArtistNode') + }) +}) diff --git a/test/utils.test.js b/test/utils.test.js index fd59a13..bda5ad3 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -201,11 +201,11 @@ describe('utils.markActive', function () { }) }) -describe('utils.makeSchemaHtmlName', function () { - const makeSchemaHtmlName = utils.__get__('makeSchemaHtmlName') - it('should compose schema html page name', function () { - expect(makeSchemaHtmlName('dialect1', 'schema1')).to.equal( - 'schema_dialect1_schema1.html') +describe('utils.makeSchemaPageName', function () { + const makeSchemaPageName = utils.__get__('makeSchemaPageName') + it('should compose schema page name', function () { + expect(makeSchemaPageName('dialect1', 'schema1')).to.equal( + 'schema_dialect1_schema1') }) }) @@ -238,15 +238,15 @@ describe('utils.sorterBy', function () { }) describe('utils.renderTemplate', function () { - const htmlPath = path.join(FIXTURES_DIR, 'test.html') + const outPath = path.join(FIXTURES_DIR, 'test.html') const renderTemplate = utils.__get__('renderTemplate') afterEach(function () { - try { fs.removeSync(htmlPath) } catch (e) {} + try { fs.removeSync(outPath) } catch (e) {} }) it('should render mustache template to a file', function () { const tmplPath = path.join(FIXTURES_DIR, 'test.mustache') - renderTemplate({ foo: 'hello', bar: 1 }, tmplPath, htmlPath) - const cont = fs.readFileSync(htmlPath).toString() + renderTemplate({ foo: 'hello', bar: 1 }, tmplPath, outPath) + const cont = fs.readFileSync(outPath).toString() expect(cont).to.equal('hello: 1') }) }) @@ -347,3 +347,30 @@ describe('utils.getOntologyTerms', function () { }) }) }) + +describe('utils.addTmplUtils', function () { + const addTmplUtils = utils.__get__('addTmplUtils') + it('should add template utility functions to an object', function () { + const obj = addTmplUtils({ foo: 1 }) + expect(obj).to.have.property('foo', 1) + expect(obj).to.have.property('stripn') + }) +}) + +describe('utils.stripn', function () { + const stripn = utils.__get__('stripn') + it('should rendex text and remove newlines from it', function () { + const fn = stripn() + const stripped = fn( + 'hello\nworld\nagain', + x => x + '\nmore') + expect(stripped).to.equal('hello world again more') + }) + context('when it fails to renders text', function () { + it('should returned render results as is', function () { + const fn = stripn() + const stripped = fn('hello\nworld\nagain', x => null) + expect(stripped).to.equal(null) + }) + }) +})