Skip to content

Commit

Permalink
Add emu-eqn
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Aug 19, 2015
1 parent d192fbe commit bcf387a
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 19 deletions.
9 changes: 9 additions & 0 deletions css/elements.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ emu-alg ol ol ol, ol ol ol ol ol ol {
list-style-type: lower-roman;
}

emu-eqn {
display: block;
margin-left: 4em;
}

emu-eqn div:first-child {
margin-left: -2em;
}

emu-note {
display: block;
margin-left: 5em;
Expand Down
9 changes: 5 additions & 4 deletions lib/Algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ const emd = require('ecmarkdown');
module.exports = class Algorithm extends Builder {
build() {
const contents = this.node.innerHTML;
let html = emd.document(contents);
let html = this.autolinkOps(emd.document(contents));
this.node.innerHTML = html;
}

html = html.replace(/([A-Z]\w+)\(/g, function (match, name) {
autolinkOps(contents) {
return contents.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;

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

this.node.innerHTML = html;
}
};
22 changes: 22 additions & 0 deletions lib/Eqn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const Algorithm = require('./Algorithm');
module.exports = class Eqn extends Algorithm {
constructor(spec, node) {
super(spec, node);
this.aoid = node.getAttribute('aoid');
this.id = this.aoid;
this.spec.biblio.ops[this.aoid] = {
aoid: this.aoid,
id: this.id,
location: '',
};
}

build() {
let contents = this.node.innerHTML;
contents = '<div>' + contents.split(/\r?\n/g)
.filter(function(s) { return s.trim().length > 0; })
.join('</div><div>');
this.node.innerHTML = this.autolinkOps(contents);
}
};
2 changes: 1 addition & 1 deletion lib/Production.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = class Production extends Builder {
this.rhsesById = {};

const rhses = this.node.querySelectorAll('emu-rhs');
for (const i = 0; i < rhses.length; i++) {
for (let i = 0; i < rhses.length; i++) {
const rhs = new RHS(this.spec, this, rhses[i]);
this.rhses.push(rhs);

Expand Down
23 changes: 15 additions & 8 deletions lib/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Production = require('./Production');
const ProdRef = require('./ProdRef');
const Grammar = require('./Grammar');
const Xref = require('./Xref');
const Eqn = require('./Eqn');

const NO_CLAUSE_AUTOLINK = ['PRE', 'CODE', 'EMU-CLAUSE', 'EMU-ALG', 'EMU-PRODUCTION', 'EMU-GRAMMAR', 'EMU-XREF', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
const clauseTextNodesUnder = utils.textNodesUnder(NO_CLAUSE_AUTOLINK);
Expand Down Expand Up @@ -67,16 +68,21 @@ module.exports = class Spec {
elems = selector;
}

const builders = [];
const ps = [];

for (let i = 0; i < elems.length; i++) {
const b = new Builder(this, elems[i]);
builders.push(b);
}

if (opts.buildArgs) {
for (const i = 0; i < elems.length; i++) {
const b = new Builder(this, elems[i]);
ps.push(b.build.apply(b, opts.buildArgs));
for (let i = 0; i < elems.length; i++) {
ps.push(builders[i].build.apply(builders[i], opts.buildArgs));
}
} else {
for (const i = 0; i < elems.length; i++) {
ps.push(new Builder(this, elems[i]).build());
for (let i = 0; i < elems.length; i++) {
ps.push(builders[i].build());
}
}

Expand All @@ -89,6 +95,7 @@ module.exports = class Spec {
.then(this.loadBiblios.bind(this))
.then(this.buildAll.bind(this, 'emu-clause, emu-intro, emu-annex', Clause))
.then(this.buildAll.bind(this, 'emu-grammar', Grammar))
.then(this.buildAll.bind(this, 'emu-eqn', Eqn))
.then(this.buildAll.bind(this, 'emu-alg', Algorithm))
.then(this.buildAll.bind(this, 'emu-production', Production))
.then(this.buildAll.bind(this, 'emu-prodref', ProdRef))
Expand Down Expand Up @@ -177,7 +184,7 @@ module.exports = class Spec {
highlightCode() {
this._log('Highlighting syntax...');
const codes = this.doc.querySelectorAll('pre code');
for (const i = 0; i < codes.length; i++) {
for (let i = 0; i < codes.length; i++) {
hljs.highlightBlock(codes[i]);
}
}
Expand Down Expand Up @@ -214,7 +221,7 @@ module.exports = class Spec {
autolinkWalk(clauseReplacer, autolinkmap, this, this);

const algs = this.doc.getElementsByTagName('emu-alg');
for (const i = 0; i < algs.length; i++) {
for (let i = 0; i < algs.length; i++) {
const alg = algs[i];
autolink(algReplacer, termlinkmap, this, alg);
}
Expand Down Expand Up @@ -248,7 +255,7 @@ function autolinkWalk(replacer, autolinkmap, spec, rootClause) {
function autolink(replacer, autolinkmap, spec, node, parentId) {
const textNodes = clauseTextNodesUnder(node);

for (const i = 0; i < textNodes.length; i++) {
for (let i = 0; i < textNodes.length; i++) {
const node = textNodes[i];

const template = spec.doc.createElement('template');
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.domWalk = function (root, cb) {
const childNodes = root.childNodes;
const childLen = childNodes.length;

for (const i = 0; i < childLen; i++) {
for (let i = 0; i < childLen; i++) {
const node = childNodes[i];
if (node.nodeType !== 1) continue;

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": "ecmarkup",
"version": "2.0.0-beta1",
"version": "2.0.0-beta2",
"description": "Custom element definitions and core utilities for markup that specifies ECMAScript and related technologies.",
"main": "lib/ecmarkup.js",
"scripts": {
Expand Down
12 changes: 9 additions & 3 deletions spec/biblio.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"http://people.mozilla.org/~jorendorff/es6-draft.html": {
"abstract operations": {
"ReturnIfAbrupt": "#sec-returnifabrupt",
"Get": "#sec-get-o-p"
"ops": {
"ReturnIfAbrupt": {
"id": "sec-returnifabrupt",
"aoid": "ReturnIfAbrupt"
},
"Get": {
"id": "sec-get-o-p",
"aoid": "Get"
}
}
}
}
9 changes: 8 additions & 1 deletion spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h2>Stylesheet</h2>
<emu-clause id="metadata">
<h1>Metadata</h1>
<p>There are a number of settings that allow customizations or enable generation of boilerplate. Metadata can be included in the document and passed on the command line, for example `--no-toc --title "Document 1"`. Metadata given on the command line takes precedence.</p>
<p>To add metadata to your document, use yaml syntax inside a `<pre class=metadata>` element somewhere in the root of your document.</p>
<p>To add metadata to your document, use yaml syntax inside a `&lt;pre class=metadata>` element somewhere in the root of your document.</p>
<p>The following table lists the currently supported metadata:</p>
<table>
<tr><td>toc</td><td>Emit table of contents. Boolean, default true.</td></tr>
Expand Down Expand Up @@ -108,6 +108,13 @@ <h3>Result</h3>
</emu-alg>
</emu-clause>

<emu-clause id="emu-eqn">
<h1>emu-eqn</h1>
<p>An equation, similar to those found in ES6 <emu-xref href="#sec-year-number"></emu-xref>.</p>

<h2>Attributes</h2>
<p><b>aoid:</b> Required: the abstract operation id that this equation defines.</p>
</emu-clause>
<emu-clause id="emu-note">
<h1>emu-note</h1>
<p>Non-normative explanatory text.</p>
Expand Down
28 changes: 28 additions & 0 deletions test/eqn.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<link rel=stylesheet href="./css/elements.css">
<pre class=metadata>toc: false</pre>
<emu-alg>
1. Return Value(_val_);
</emu-alg>
<emu-eqn aoid="Value2">
Value2(t)
= DateValue(t) if Type(t) is string
= t
</emu-eqn>
<emu-eqn aoid="DateValue">
DateValue(t)
= 0 if t = 0
= 1 if t = 1
= 2
</emu-eqn>
<emu-eqn aoid="Value">
Value(t)
= DateValue(t) if Type(t) is string
= t
</emu-eqn>
<emu-alg>
1. Return Value(_val_);
</emu-alg>
13 changes: 13 additions & 0 deletions test/eqn.html.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<head><link rel="stylesheet" href="./css/elements.css">
</head><body>
<emu-alg><ol><li>Return <a href="#Value">Value</a>(<var>val</var>);</li></ol></emu-alg>

<emu-eqn aoid="Value2"><div> <a href="#Value2">Value2</a>(t)</div><div> = <a href="#DateValue">DateValue</a>(t) if <a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ecmascript-data-types-and-values">Type</a>(t) is string</div><div> = t</div></emu-eqn>

<emu-eqn aoid="DateValue"><div> <a href="#DateValue">DateValue</a>(t)</div><div> = 0 if t = 0</div><div> = 1 if t = 1</div><div> = 2</div></emu-eqn>

This comment has been minimized.

Copy link
@domenic

domenic Aug 19, 2015

Member

Shouldn't e.g. t be wrapped in <var>? (and maybe 0 in <emu-val>...) Or I guess we should make authors do that in their EMU/EMD input, and this is just a slightly-incomplete example? Disclaimer: I haven't actually checked the spec's rendering.

This comment has been minimized.

Copy link
@bterlson

bterlson Aug 19, 2015

Author Member

Yeah, true. I am going to parse the emu-eqn as an emd paragraph.


<emu-eqn aoid="Value"><div> <a href="#Value">Value</a>(t)</div><div> = <a href="#DateValue">DateValue</a>(t) if <a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-ecmascript-data-types-and-values">Type</a>(t) is string</div><div> = t</div></emu-eqn>

<emu-alg><ol><li>Return <a href="#Value">Value</a>(<var>val</var>);</li></ol></emu-alg>
</body>

0 comments on commit bcf387a

Please sign in to comment.