Skip to content

Commit

Permalink
adds provision to add notes and references on the akn document, updat…
Browse files Browse the repository at this point in the history
…es abbreviations (#26)
  • Loading branch information
SmoDav authored and samuel-cloete committed Nov 13, 2018
1 parent 533eb01 commit ff2f65d
Show file tree
Hide file tree
Showing 74 changed files with 401 additions and 97 deletions.
22 changes: 21 additions & 1 deletion examples/manual generation/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Elements, Attributes, Helpers } from 'nkyimu';
import { Elements, Attributes, Helpers, Engine } from 'nkyimu';

// Generation of Wet Tropics of Queensland World Heritage Area Conservation Act 1994
class TestDocument {
Expand Down Expand Up @@ -154,9 +154,29 @@ class TestDocument {

getBody() {
const body = new Elements.Body();
const { root, last } = Engine.Generator.fromSelector('chapter>part[num:1,heading:Some Part]>section>subsection>content>p');
const noteRef = this.createNote();
const sampleBody = new Elements.TextElement('Always have notes.');
last.appendChild(sampleBody);
last.appendChild(noteRef);

body.appendChild(root);

console.log(root, last);


return body;
}

createNote() {
const note = new Elements.Note();
const p = new Elements.P();

p.appendChild(new Elements.TextElement('This is a note'));
note.appendChild(p);

return this.doc.addNote(note);
}
}

const generated = new TestDocument();
Expand Down
7 changes: 7 additions & 0 deletions src/Abstracts/AbstractAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export abstract class AbstractAttribute {
return this.name;
}

/**
* Get the name of the attribute
*/
static getName(): string {
throw new Error('Implement the function getClassName');
}

/**
* Get the value of the attribute
*/
Expand Down
104 changes: 98 additions & 6 deletions src/Abstracts/AbstractNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ export abstract class AbstractNode implements HasChildrenMap {
this.node.textContent = str;
}

/**
* Get the text content of a node.
*/
getTextContent(): string|null {
if (this.getNodeType() !== NodeType.TEXT_NODE && this.getNodeType() !== NodeType.COMMENT_NODE) {
throw new Error(`${this.getNodeName()} is a non-text node`);
}

return this.node.textContent;
}

/**
* Extract the nodes from the nested CHILDREN_MAP object.
*
Expand Down Expand Up @@ -611,24 +622,71 @@ export abstract class AbstractNode implements HasChildrenMap {
const nodeCount:{ [key:string]: number } = {};

node.children.forEach((child: AbstractNode) => {
if (child.abbreviation.length < 1) {
const requiresEId = child.ATTRIBUTE_GROUPS
.some(e => e.attribute.getName() === 'eId' && e.required === true);

const currentEId = this.getElement().getAttribute('eId');

if (!requiresEId || child.abbreviation.length < 1) {
this.generateIds(child, prefix);

return;
}

if (!nodeCount[child.getNodeName()]) {
nodeCount[child.getNodeName()] = 0;
if (currentEId && currentEId.indexOf('__replace__') === -1) {
this.generateIds(child, currentEId);

return;
}

nodeCount[child.getNodeName()] += 1;
const childId = this.getCorrectNodeId(prefix, nodeCount, child);

child.setElementId(`${prefix}${child.abbreviation}_${nodeCount[child.getNodeName()]}`);
child.setElementId(childId);

this.generateIds(child, `${child.generatedId}_`);
});
}

private getCorrectNodeId(prefix: string, nodeCount: { [key:string]: number }, node: AbstractNode): string {
const nodePrefix = `${prefix}${node.abbreviation}_`;

const nodeNumber = this.getNodeNumberContent(node);

if (nodeNumber) {
return `${nodePrefix}${nodeNumber}`;
}

if (!nodeCount[node.getNodeName()]) {
nodeCount[node.getNodeName()] = 0;
}

nodeCount[node.getNodeName()] += 1;

return `${nodePrefix}${nodeCount[node.getNodeName()]}`;
}

private getNodeNumberContent(node: AbstractNode) {
const numTag = node.children.filter((e) => e.getNodeName() === 'num');

if (numTag.length < 1 || numTag[0].children.length < 1) return null;

const text = numTag[0].children.filter((e) => e.getNodeName() === '');

if (text.length < 1) return null;

const content = text[0].getTextContent();

if (!content) return null;

const nodeRegx = new RegExp(node.getNodeName(), 'gi');

return content
.replace(nodeRegx, '')
.replace(/[.()-]/gi, '')
.trim()
.replace(/\s/g, '_');
}

setElementId(id: string) {
this.generatedId = id;

Expand All @@ -638,8 +696,42 @@ export abstract class AbstractNode implements HasChildrenMap {

const attr = this.getElement().getAttribute('eId');

if (attr && attr.indexOf('__replace__') !== -1) {
if (!attr) {
this.setAttribute(new EIdAttribute(id));

return;
}

if (attr.indexOf('__replace__') !== -1) {
this.setAttribute(new EIdAttribute(id));
}
}

getFirstDescendant(nodeName: string): AbstractNode|null {
let match: AbstractNode|null = null;

this.children.some((child) => {
if (child.getNodeName() === nodeName) {
match = child;
return true;
}

return false;
});

if (match) return match;

this.children.some((e) => {
const childMatch = e.getFirstDescendant(nodeName);

if (childMatch) {
match = childMatch;
return true;
}

return false;
});

return match;
}
}
2 changes: 1 addition & 1 deletion src/Elements/Blocks/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AttributeGroupItem } from "../../Interfaces/AttributeGroupItem";
import { NodeRules } from "../../Interfaces/NodeRules";

export class Table extends AbstractNode {
abbreviation = 'tbl';
abbreviation = 'table';

getNodeName(): string { return 'table'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new ItemType();
* that is called citation
*/
export class Citation extends AbstractNode {
abbreviation = 'citation';
abbreviation = 'cit';

getNodeName(): string { return 'citation'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/ComponentRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const type = new SrcType();
* internal (within the components element)
*/
export class ComponentRef extends AbstractNode {
abbreviation = 'componentref';
abbreviation = 'cref';

getNodeName(): string { return 'componentRef'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new DocContainerType();
* attachment elements
*/
export class Attachment extends AbstractNode {
abbreviation = 'attachment';
abbreviation = 'att';

getNodeName(): string { return 'attachment'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/BlockContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new BlockContainerType();
* individual block elements in a block context
*/
export class BlockContainer extends AbstractNode {
abbreviation = 'blc';
abbreviation = 'blockcontainer';

getNodeName(): string { return 'blockContainer'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/BlockList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new BlockListType();
* of many individual item elements to be treated as in a list.
*/
export class BlockList extends AbstractNode {
abbreviation = 'bll';
abbreviation = 'list';

getNodeName(): string { return 'blockList'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Citations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const type = new CitationHierarchy();
* The element container is a generic element for a container.
*/
export class Citations extends AbstractNode {
abbreviation = 'citations';
abbreviation = 'cits';

getNodeName(): string { return 'citations'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const type = new ContainerType();
* The element container is a generic element for a container.
*/
export class Container extends AbstractNode {
abbreviation = 'co';
abbreviation = 'container';

getNodeName(): string { return 'container'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const type = new Blocksreq();
* specified
*/
export class Content extends AbstractNode {
abbreviation = 'cnt';
abbreviation = 'content';

getNodeName(): string { return 'content'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const type = new Blocksreq();
* specification of the actual type of formula.
*/
export class Formula extends AbstractNode {
abbreviation = 'fo';
abbreviation = 'formula';

getNodeName(): string { return 'formula'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/LongTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const type = new Blocksreq();
* or coverPage that is called long title.
*/
export class LongTitle extends AbstractNode {
abbreviation = 'lt';
abbreviation = 'longtitle';

getNodeName(): string { return 'longTitle'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Recitals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const type = new RecitalHierarchy();
* The element recitals is the section of the preamble that contains recitals.
*/
export class Recitals extends AbstractNode {
abbreviation = 'recitals';
abbreviation = 'recs';

getNodeName(): string { return 'recitals'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Containers/Tblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new ItemType();
* hierarchical structure
*/
export class Tblock extends AbstractNode {
abbreviation = 'tbl';
abbreviation = 'tblock';

getNodeName(): string { return 'tblock'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/ActiveRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new ReferenceType();
* document (i.e., an active references)
*/
export class ActiveRef extends AbstractNode {
abbreviation = 'activeRef';
abbreviation = 'activeref';

getNodeName(): string {
return 'activeRef';
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/AttachmentOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const type = new ReferenceType();
* document is an attachment
*/
export class AttachmentOf extends AbstractNode {
abbreviation = "";
abbreviation = "attachmentof";

getNodeName(): string {
return "attachmentOf";
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/EventRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const type = new AnyOtherType();
* that generated the event must be referenced.
*/
export class EventRef extends AbstractNode {
abbreviation = 'eventref';
abbreviation = 'eref';

getNodeName(): string {
return 'eventRef';
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/HasAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const type = new ReferenceType();
* document is an attachment
*/
export class HasAttachment extends AbstractNode {
abbreviation = "";
abbreviation = "hasattachment";

getNodeName(): string {
return "hasAttachment";
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/Jurisprudence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new ReferenceType();
* document is an attachment
*/
export class Jurisprudence extends AbstractNode {
abbreviation = "";
abbreviation = "jurisprudence";

getNodeName(): string {
return "jurisprudence";
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const type = new SubFlowStructure();
* endnote specified.
*/
export class Note extends AbstractNode {
abbreviation = 'nt';
abbreviation = 'note';

getNodeName(): string { return 'note'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/Notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Source } from "../../AttributeGroups/Source";
import { Xmllang } from "../../AttributeGroups/Xmllang";

export class Notes extends AbstractNode {
abbreviation = 'nts';
abbreviation = 'notes';

getNodeName(): string { return 'notes'; }

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/PassiveRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const type = new ReferenceType();
* on this document (i.e., a passive references)
*/
export class PassiveRef extends AbstractNode {
abbreviation = "";
abbreviation = "passiveref";

getNodeName(): string {
return "passiveRef";
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/References.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const refItems = new RefItems();
* anything else is managed by the Akoma Ntoso ontology.
*/
export class References extends AbstractNode {
abbreviation = "";
abbreviation = "references";
getNodeName(): string {
return "references";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Core/TLCConcept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const type = new ReferenceType();
* Akoma Ntoso IRI of an ontology instance of the class Concept
*/
export class TLCConcept extends AbstractNode {
abbreviation = "";
abbreviation = "tlcconcept";

getNodeName(): string {
return "TLCConcept";
Expand Down
Loading

0 comments on commit ff2f65d

Please sign in to comment.