From 6cbe341b75341c0d79f1f2a098fd73a83810c9f7 Mon Sep 17 00:00:00 2001 From: SmoDav Date: Thu, 23 Aug 2018 11:12:40 +0300 Subject: [PATCH] adds generator and helpers to entry --- README.md | 10 ++++++++++ examples/auto generation/index.js | 7 +++---- examples/manual generation/index.js | 7 +++---- package.json | 2 +- src/Engine/Generator.ts | 17 ++++++++--------- src/index.ts | 4 +++- src/init.js | 6 ++++++ 7 files changed, 34 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b238375..94257fe 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,16 @@ This library will hopefully help in creating AkomaNtoso documents with more skil ## Examples You can generate the AkN document either manually or using the auto-generator. Both examples are provided in the examples folder. +To generate a Nkyimu node using the `nodeName`, use the available engine module: + +``` +import { Engine, Helpers } from 'nkyimu'; + +const section = Engine.Generator.createNode('section'); + +const text = Engine.Generator.createNode('', 'Text node over here'); +``` + ## Contribution After making any addition, modification or removal of the source files, you will need to regenerate the required index files and the element map that is used to link an AkN element and a Nkyimu element: diff --git a/examples/auto generation/index.js b/examples/auto generation/index.js index 10826ba..42e0c73 100644 --- a/examples/auto generation/index.js +++ b/examples/auto generation/index.js @@ -1,8 +1,7 @@ -import { Generator } from '../../build/src/Engine/Generator'; -import { forDisplay } from '../../build/src/common/helpers'; +import { Engine, Helpers } from 'nkyimu'; function displayResult(source) { - const generated = (new Generator()).fromText(source); + const generated = (new Engine.Generator()).fromText(source); let display = document.querySelector('pre'); @@ -11,7 +10,7 @@ function displayResult(source) { document.querySelector('body').appendChild(display); } - display.innerHTML = forDisplay(generated.toXML()); + display.innerHTML = Helpers.forDisplay(generated.toXML()); console.log(generated); } diff --git a/examples/manual generation/index.js b/examples/manual generation/index.js index ca89113..6867815 100644 --- a/examples/manual generation/index.js +++ b/examples/manual generation/index.js @@ -1,5 +1,4 @@ -import { Elements, Attributes } from '../../build/src'; -import { forDisplay } from '../../build/src/common/helpers'; +import { Elements, Attributes, Helpers } from 'nkyimu'; // Generation of Wet Tropics of Queensland World Heritage Area Conservation Act 1994 class TestDocument { @@ -166,7 +165,7 @@ const node = generated.getDocument().getElement(); /** Lets display the original */ let display = document.createElement('pre'); -display.innerHTML = forDisplay(generated.render()); +display.innerHTML = Helpers.forDisplay(generated.render()); document.querySelector('body').appendChild(display); @@ -188,7 +187,7 @@ if (longTitle) { } display = document.createElement('pre'); -display.innerHTML = forDisplay(generated.render()); +display.innerHTML = Helpers.forDisplay(generated.render()); document.querySelector('body').appendChild(display); diff --git a/package.json b/package.json index fc38ac6..c8108bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nkyimu", - "version": "1.1.0", + "version": "1.1.1", "description": "A library written in TypeScript that allows creating self validating AkomaNtoso documents", "author": "Libryo Ltd", "main": "build/src/index.js", diff --git a/src/Engine/Generator.ts b/src/Engine/Generator.ts index a3b950e..01ccca3 100644 --- a/src/Engine/Generator.ts +++ b/src/Engine/Generator.ts @@ -24,7 +24,7 @@ export class Generator { generateNodes() { const node = this.doc.children[0]; - const created = this.createNodeAndAttributes(node); + const created = Generator.createNodeAndAttributes(node); this.generateChildNodes(node) .forEach((childNode: AbstractNode) => { @@ -41,7 +41,7 @@ export class Generator { if (node.children.length !== 0) { Object.keys(node.children).forEach((key: string) => { - const createdNode:AbstractNode = this.createNodeAndAttributes(node.children[Number(key)]); + const createdNode:AbstractNode = Generator.createNodeAndAttributes(node.children[Number(key)]); this.generateChildNodes(node.children[Number(key)]) .forEach((childNode: AbstractNode) => { @@ -67,25 +67,25 @@ export class Generator { generateAlternateChildNodes(childNode: Node & ChildNode): AbstractNode|null { if (childNode.nodeType === Node.TEXT_NODE) { - return this.createNode('', childNode.textContent); + return Generator.createNode('', childNode.textContent); } return null; } - createNodeAndAttributes(node: Element): AbstractNode { - const created = this.createNode(node.nodeName); + static createNodeAndAttributes(node: Element): AbstractNode { + const created = Generator.createNode(node.nodeName); Object.keys(node.attributes).forEach((attr: string) => { const currentAttr: Attr = node.attributes[Number(attr)]; - created.setAttribute(this.createAttribute(currentAttr.name, currentAttr.value)); + created.setAttribute(Generator.createAttribute(currentAttr.name, currentAttr.value)); }); return created; } - createNode(name: T|string, content:string|null = null): AbstractNode { + static createNode(name: T|string, content:string|null = null): AbstractNode { if (!elementMap[name]) { throw new Error(`Element ${name} is not available`); } @@ -93,12 +93,11 @@ export class Generator { return new Elements[elementMap[name] as T](content || ''); } - createAttribute(name: T|string, value: string): AbstractAttribute { + static createAttribute(name: T|string, value: string): AbstractAttribute { if (!attributeMap[name]) { throw new Error(`Element ${name} is not available`); } return new Attributes[attributeMap[name] as T](value); } - } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 70bab29..8aa71f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,9 @@ import * as ComplexTypes from './ComplexTypes'; import * as Elements from './Elements'; import * as Interfaces from './Interfaces'; import * as SimpleTypes from './SimpleTypes'; +import * as Engine from './Engine/Generator'; +import * as Helpers from './common/helpers'; import { elementMap } from './elementMap'; import { attributeMap } from './attributeMap'; -export { Abstracts, AttributeGroups, Attributes, ComplexTypes, Elements, Interfaces, SimpleTypes, elementMap, attributeMap }; \ No newline at end of file +export { Abstracts, AttributeGroups, Attributes, ComplexTypes, Elements, Interfaces, SimpleTypes, Engine, Helpers, elementMap, attributeMap }; \ No newline at end of file diff --git a/src/init.js b/src/init.js index b8485a7..47ad15e 100644 --- a/src/init.js +++ b/src/init.js @@ -106,6 +106,9 @@ class Generator { const content = this.directories .map(dir => `import * as ${dir.replace('./', '')} from '${dir}';`); + content.push("import * as Engine from './Engine/Generator';"); + content.push("import * as Helpers from './common/helpers';"); + if (includeMaps) { content.push("import { elementMap } from './elementMap';"); content.push("import { attributeMap } from './attributeMap';"); @@ -114,6 +117,9 @@ class Generator { content.push(''); const exportList = this.directories.map(dir => dir.replace('./', '')); + exportList.push('Engine'); + exportList.push('Helpers'); + if (includeMaps) { exportList.push('elementMap'); exportList.push('attributeMap');