Skip to content

Commit

Permalink
Merge pull request #17 from SmoDav/working
Browse files Browse the repository at this point in the history
adds generator and helpers to entry point
  • Loading branch information
SmoDav authored Aug 23, 2018
2 parents 46a9a8c + 6cbe341 commit 10a03a0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
7 changes: 3 additions & 4 deletions examples/auto generation/index.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions examples/manual generation/index.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 8 additions & 9 deletions src/Engine/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -67,38 +67,37 @@ 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<T extends keyof ElementType>(name: T|string, content:string|null = null): AbstractNode {
static createNode<T extends keyof ElementType>(name: T|string, content:string|null = null): AbstractNode {
if (!elementMap[name]) {
throw new Error(`Element ${name} is not available`);
}

return new Elements[elementMap[name] as T](content || '');
}

createAttribute<T extends keyof AttributeType>(name: T|string, value: string): AbstractAttribute {
static createAttribute<T extends keyof AttributeType>(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);
}

}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
export { Abstracts, AttributeGroups, Attributes, ComplexTypes, Elements, Interfaces, SimpleTypes, Engine, Helpers, elementMap, attributeMap };
6 changes: 6 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';");
Expand All @@ -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');
Expand Down

0 comments on commit 10a03a0

Please sign in to comment.