Skip to content

Commit

Permalink
ES6ify and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Aug 18, 2015
1 parent 64ac505 commit d192fbe
Show file tree
Hide file tree
Showing 25 changed files with 695 additions and 718 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"curly": [2, "multi-line"],
"array-bracket-spacing": [2, "never"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"semi": [2, "always"]
"semi": [2, "always"],
"no-var": 2
}
}
6 changes: 3 additions & 3 deletions bin/args.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
var path = require('path');
const path = require('path');

module.exports = require('nomnom')
.script('ecmarkup')
.help('Compile ecmarkup documents to html by passing your input file and output file.')
.options({
help: { abbr: 'h', flag: true, help: 'Display this help message' },
biblio: { abbr: 'b', metavar: 'FILE', help: 'Write a biblio file to FILE' },
biblio: { abbr: 'b', metaconst: 'FILE', help: 'Write a biblio file to FILE' },

This comment has been minimized.

Copy link
@domenic

domenic Aug 19, 2015

Member

Hrm

This comment has been minimized.

Copy link
@bterlson

bterlson Aug 19, 2015

Author Member

metaconst! ^^

toc: { flag: true, help: 'Don\'t include the table of contents' },
verbose: { flag: true, default: false, help: 'Display document build progress' },
version: {
Expand All @@ -27,6 +27,6 @@ module.exports = require('nomnom')
});

function printVersion() {
var p = require(path.resolve(__dirname, '..', 'package.json'));
const p = require(path.resolve(__dirname, '..', 'package.json'));
return 'ecmarkup v' + p.version;
}
10 changes: 5 additions & 5 deletions bin/ecmarkup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env node
'use strict';

var args = require('./args').parse();
const args = require('./args').parse();
if (!args.biblio && !args.outfile) {
console.log('No outfile specified. See --help for more details.');
process.exit();
}

// requires after arg checking to avoid expensive load times
var ecmarkup = require('../lib/ecmarkup');
var Promise = require('bluebird');
var fs = require('fs');
var readFile = Promise.promisify(fs.readFile);
const ecmarkup = require('../lib/ecmarkup');
const Promise = require('bluebird');
const fs = require('fs');
const readFile = Promise.promisify(fs.readFile);

function fetch(path) {
return readFile(path, 'utf8');
Expand Down
32 changes: 14 additions & 18 deletions lib/Algorithm.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
'use strict';

module.exports = Algorithm;
const Builder = require('./Builder');
const emd = require('ecmarkdown');

var emd = require('ecmarkdown');
module.exports = class Algorithm extends Builder {
build() {
const contents = this.node.innerHTML;
let html = emd.document(contents);

function Algorithm(spec, algNode) {
this.spec = spec;
this.node = algNode;
}
html = html.replace(/([A-Z]\w+)\(/g, function (match, name) {
let op = this.spec.biblio.ops[name];
if (!op) op = this.spec.externalBiblio.ops[name];
if (!op) return match;

Algorithm.prototype.build = function () {
var contents = this.node.innerHTML;
var html = emd.document(contents);
return '<a href="' + op.location + '#' + op.id + '">' + name + '</a>(';
}.bind(this));

html = html.replace(/([A-Z]\w+)\(/g, function (match, name) {
var op = this.spec.biblio.ops[name];
if (!op) op = this.spec.externalBiblio.ops[name];
if (!op) return match;

return '<a href="' + op.location + '#' + op.id + '">' + name + '</a>(';
}.bind(this));

this.node.innerHTML = html;
this.node.innerHTML = html;
}
};
12 changes: 12 additions & 0 deletions lib/Builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = class Builder {
constructor(spec, node) {
this.spec = spec;
this.node = node;
}

build() {
throw new Error('Builder not implemented');
}
};
199 changes: 100 additions & 99 deletions lib/Clause.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,119 @@
'use strict';

module.exports = Clause;
var CLAUSE_ELEMS = ['EMU-INTRO', 'EMU-CLAUSE', 'EMU-ANNEX'];
var Note = require('./Note');
var emd = require('ecmarkdown');
var utils = require('./utils');

function Clause(spec, node) {
node._clause = this;

this.header = node.querySelector('h1');
if (!this.header) {
throw new Error('Clause doesn\'t have header: ' + node.outerHTML);
}
const CLAUSE_ELEMS = ['EMU-INTRO', 'EMU-CLAUSE', 'EMU-ANNEX'];
const Note = require('./Note');
const emd = require('ecmarkdown');
const utils = require('./utils');
const Builder = require('./Builder');

module.exports = class Clause extends Builder {
constructor(spec, node) {
super(spec, node);
node._clause = this;

this.header = node.querySelector('h1');
if (!this.header) {
throw new Error('Clause doesn\'t have header: ' + node.outerHTML);
}

this.spec = spec;
this.node = node;
this.parentClause = getParentClause(node);
this.title = this.header.textContent;
this.subclauses = [];
this.id = node.id;

if (this.parentClause) {
this.depth = this.parentClause.depth + 1;
this.parentClause.subclauses.push(this);
} else {
this.depth = 0;
this.spec.subclauses.push(this);
}
this.parentClause = getParentClause(node);
this.title = this.header.textContent;
this.subclauses = [];
this.id = node.id;

if (this.parentClause) {
this.depth = this.parentClause.depth + 1;
this.parentClause.subclauses.push(this);
} else {
this.depth = 0;
this.spec.subclauses.push(this);
}

if (node.nodeName === 'EMU-INTRO') {
this.number = '';
} else {
this.number = spec.getNextClauseNumber(this.depth,
node.nodeName === 'EMU-ANNEX'
);
}
if (node.nodeName === 'EMU-INTRO') {
this.number = '';
} else {
this.number = spec.getNextClauseNumber(this.depth,
node.nodeName === 'EMU-ANNEX'
);
}

this.aoid = node.getAttribute('aoid');
if (this.aoid !== null) {
if (this.aoid === '') this.aoid = this.id;
this.aoid = node.getAttribute('aoid');
if (this.aoid !== null) {
if (this.aoid === '') this.aoid = this.id;

this.spec.biblio.ops[this.aoid] = {
aoid: this.aoid,
id: this.id,
this.spec.biblio.ops[this.aoid] = {
aoid: this.aoid,
id: this.id,
location: '',
};
}

this.spec.biblio.clauses[this.id] = {
location: '',
id: this.id,
aoid: this.aoid,
title: this.title,
number: this.number
};
}

this.spec.biblio.clauses[this.id] = {
location: '',
id: this.id,
aoid: this.aoid,
title: this.title,
number: this.number
};

this.notes = this.getNotes();
}
this.notes = this.getNotes();
}

Clause.prototype.build = function () {
var numElem = this.spec.doc.createElement('span');
numElem.setAttribute('class', 'secnum');
numElem.textContent = this.number;
this.header.insertBefore(numElem, this.header.firstChild);
build() {
const numElem = this.spec.doc.createElement('span');
numElem.setAttribute('class', 'secnum');
numElem.textContent = this.number;
this.header.insertBefore(numElem, this.header.firstChild);

this.header.appendChild(this.buildUtils());
processEmd(this);
this.header.appendChild(this.buildUtils());
processEmd(this);

this.buildNotes();
};
this.buildNotes();
}

Clause.prototype.buildNotes = function () {
if (this.notes.length === 1) {
this.notes[0].build();
} else {
// pass along note index
this.notes.forEach(function (note, i) {
note.build(i + 1);
}, this);
buildNotes() {
if (this.notes.length === 1) {
this.notes[0].build();
} else {
// pass along note index
this.notes.forEach(function (note, i) {
note.build(i + 1);
}, this);
}
}
};

Clause.prototype.getNotes = function () {
var notes = [];
getNotes() {
const notes = [];

utils.domWalk(this.node, function (child) {
if (CLAUSE_ELEMS.indexOf(child.nodeName) > -1) {
return false;
}
utils.domWalk(this.node, function (child) {
if (CLAUSE_ELEMS.indexOf(child.nodeName) > -1) {
return false;
}

if (child.nodeName === 'EMU-NOTE') {
notes.push(new Note(this.spec, child));
}
}.bind(this));
if (child.nodeName === 'EMU-NOTE') {
notes.push(new Note(this.spec, child));
}
}.bind(this));

return notes;
};
return notes;
}

Clause.prototype.buildUtils = function () {
var utilsElem = this.spec.doc.createElement('span');
utilsElem.setAttribute('class', 'utils');
buildUtils() {
const utilsElem = this.spec.doc.createElement('span');
utilsElem.setAttribute('class', 'utils');

var anchorElem = this.spec.doc.createElement('span');
anchorElem.setAttribute('class', 'anchor');
anchorElem.innerHTML = '<a href="#' + this.id + '">#</a>';
const anchorElem = this.spec.doc.createElement('span');
anchorElem.setAttribute('class', 'anchor');
anchorElem.innerHTML = '<a href="#' + this.id + '">#</a>';

utilsElem.appendChild(anchorElem);
utilsElem.appendChild(anchorElem);

return utilsElem;
return utilsElem;
}
};

function getParentClause(node) {
var current = node.parentNode;
let current = node.parentNode;
while (current) {
if (CLAUSE_ELEMS.indexOf(current.nodeName) > -1) return current._clause;
current = current.parentNode;
Expand All @@ -121,20 +122,20 @@ function getParentClause(node) {
return null;
}

var NO_EMD = ['PRE', 'CODE', 'EMU-CLAUSE', 'EMU-PRODUCTION', 'EMU-ALG', 'EMU-GRAMMAR'];
var textNodesUnder = utils.textNodesUnder(NO_EMD);
const NO_EMD = ['PRE', 'CODE', 'EMU-CLAUSE', 'EMU-PRODUCTION', 'EMU-ALG', 'EMU-GRAMMAR'];
const textNodesUnder = utils.textNodesUnder(NO_EMD);
function processEmd(clause) {
var doc = clause.spec.doc;
var textNodes = textNodesUnder(clause.node);
for (var j = 0; j < textNodes.length; j++) {
var node = textNodes[j];
const doc = clause.spec.doc;
const textNodes = textNodesUnder(clause.node);
for (let j = 0; j < textNodes.length; j++) {
const node = textNodes[j];
if (node.textContent.trim().length === 0) continue;

// emd strips starting and ending spaces which we want to preserve
var startSpace = node.textContent.match(/^\s*/)[0];
var endSpace = node.textContent.match(/\s*$/)[0];
const startSpace = node.textContent.match(/^\s*/)[0];
const endSpace = node.textContent.match(/\s*$/)[0];

var template = doc.createElement('template');
const template = doc.createElement('template');
template.innerHTML = startSpace + emd.fragment(node.textContent) + endSpace;

utils.replaceTextNode(node, template);
Expand Down
16 changes: 16 additions & 0 deletions lib/Dfn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const Builder = require('./Builder');
const parent = require('./utils').parent;

module.exports = class Dfn extends Builder {
build() {
const parentClause = parent(this.node, ['EMU-CLAUSE', 'EMU-INTRO', 'EMU-ANNEX']);
if (!parentClause) return;

this.spec.biblio.terms[this.node.textContent] = {
id: parentClause.id,
location: ''
};
}
};
22 changes: 22 additions & 0 deletions lib/Grammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const Builder = require('./Builder');
const gmd = require('grammarkdown');
const GrammarFile = gmd.Grammar;
const EmitFormat = gmd.EmitFormat;

const gmdCompile = function (text) {
let out;
function readFile(file) { return text; }
function writeFile(file, output) { out = output; }
const g = new GrammarFile(['file.grammar'], { format: EmitFormat.ecmarkup }, readFile);
g.emit(undefined, writeFile);

return out;
};

module.exports = class Grammar extends Builder {
build() {
this.node.innerHTML = gmdCompile(this.node.textContent);
}
};
Loading

0 comments on commit d192fbe

Please sign in to comment.