Skip to content

Commit

Permalink
Allow xrefing a note by id
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Aug 20, 2015
1 parent acf7fed commit 47280f4
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/Clause.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module.exports = class Clause extends Builder {
}

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

if (child.nodeName === 'EMU-EXAMPLE') {
Expand Down
19 changes: 19 additions & 0 deletions lib/Note.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@
const Builder = require('./Builder');

module.exports = class Note extends Builder {
constructor(spec, node, clause) {
super(spec, node);
this.clause = clause;
if (this.node.hasAttribute('id')) {
this.id = node.getAttribute('id');
}
}

build(number) {
if (this.id) {
// biblio is added during the build step as we don't know
// the number at build time. Could probably be fixed.
this.spec.biblio.notes[this.id] = {
location: '',
id: this.id,
number: number || 1,
clauseId: this.clause.id
};
}

const noteSpan = this.spec.doc.createElement('span');
noteSpan.setAttribute('class', 'note');

Expand Down
8 changes: 5 additions & 3 deletions lib/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ module.exports = class Spec {
ops: {},
productions: {},
terms: {},
examles: {}
examples: {},
notes: {}
};
this.biblio = {
clauses: {},
ops: {},
productions: {},
terms: {},
examples: {}
examples: {},
notes: {}
};
this._prodsByName = {};

Expand Down Expand Up @@ -237,7 +239,7 @@ module.exports = class Spec {
}

lookupBiblioEntryById(id) {
const types = ['clause', 'production', 'example'];
const types = ['clause', 'production', 'example', 'note'];
for (let i = 0; i < types.length; i++) {
const type = types[i];
const entry = this.spec.biblio[type + 's'][id] || this.spec.externalBiblio[type + 's'][id];
Expand Down
17 changes: 10 additions & 7 deletions lib/Xref.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = class Xref extends Builder {

const entry = this.spec.lookupBiblioEntryById(id);
if (!entry) {
console.log('Warning: can\'t find clause, production or example with id ' + href);
console.log('Warning: can\'t find clause, production, note or example with id ' + href);
return;
}

Expand All @@ -40,7 +40,10 @@ module.exports = class Xref extends Builder {
buildProductionLink(xref, entry.entry);
break;
case 'example':
buildExampleLink(this.spec, xref, entry.entry);
buildFigureLink(this.spec, xref, entry.entry, 'Example');
break;
case 'note':
buildFigureLink(this.spec, xref, entry.entry, 'Note');
break;
default:
console.log('Warning: found unknown biblio entry (this is a bug, please file it)');
Expand Down Expand Up @@ -87,22 +90,22 @@ function buildAOLink(xref, entry) {
}
}

function buildExampleLink(spec, xref, entry) {
function buildFigureLink(spec, xref, entry, type) {
if (xref.textContent.trim() === '') {
// first need to find the associated clause
const clauseEntry = spec.lookupBiblioEntryById(entry.clauseId);
if (clauseEntry.type !== 'clause') {
console.log('Warning: could not find parent clause for example id ' + entry.id);
console.log('Warning: could not find parent clause for ' + type + ' id ' + entry.id);
}

const parentClause = utils.parent(xref, ['EMU-CLAUSE', 'EMU-INTRO', 'EMU-ANNEX']);
if (parentClause && parentClause.id === clauseEntry.entry.id) {
xref.innerHTML = buildXrefLink(entry, 'Example ' + entry.number);
xref.innerHTML = buildXrefLink(entry, type + ' ' + entry.number);
} else {
if (xref.hasAttribute('title')) {
xref.innerHTML = buildXrefLink(entry, clauseEntry.entry.title + ' Example ' + entry.number);
xref.innerHTML = buildXrefLink(entry, clauseEntry.entry.title + ' ' + type + ' ' + entry.number);
} else {
xref.innerHTML = buildXrefLink(entry, clauseEntry.entry.number + ' Example ' + entry.number);
xref.innerHTML = buildXrefLink(entry, clauseEntry.entry.number + ' ' + type + ' ' + entry.number);
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ <h2>Example</h2>

<emu-clause id="emu-xref">
<h1>emu-xref</h1>
<p>Cross-reference another clause, production, example, or abstract operation. If the text content of this element is empty, a suitable default is used. The `title` attribute controls this default - when present, clauses are referred to using their title rather than number. This also applies to examples which are indexed first by their containing clause and then their example number.</p>
<p>Cross-reference another clause, production, note, example, or abstract operation. If the text content of this element is empty, a suitable default is used. The `title` attribute controls this default - when present, clauses are referred to using their title rather than number. This also applies to examples which are indexed first by their containing clause and then their example number.</p>
<p>Cross-references to an id check for clauses, productions, and examples in this order. For each type, the local document is consulted before looking for external sources including the default ES6 biblio.</p>

<h2>Attributes</h2>
Expand Down
15 changes: 15 additions & 0 deletions test/xref.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ <h1>Clause Title</h1>
<emu-xref href="#example-3" title></emu-xref>
<emu-xref href="#example-2">with link text</emu-xref>
<!-- refs to notes -->
<emu-xref href="#note-1"></emu-xref>
<emu-xref href="#note-1" title></emu-xref>
<emu-xref href="#note-2"></emu-xref> <!-- examples in current clause shouldn't include clause name -->
<emu-xref href="#note-2" title></emu-xref> <!-- examples in current clause shouldn't include clause name -->
<emu-xref href="#note-2">with link text</emu-xref>
<emu-clause id="sec-abstract-op" aoid="AbstractOp">
<h1>AbstractOp</h1>
<emu-alg>
Expand All @@ -53,11 +60,19 @@ <h1>AbstractOp</h1>
<emu-example id="example-3" caption="Foo Caption">
This is an example
</emu-example>
<emu-note id="note-1">
This is another note
</emu-note>
</emu-clause>
<emu-example id="example-1" caption="Foo Caption">
This is an example
</emu-example>
<emu-note id="note-2">
This is a note!
</emu-note>
</emu-clause>
<emu-grammar>
Expand Down
15 changes: 15 additions & 0 deletions test/xref.html.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
<emu-xref href="#example-3" title=""><a href="#example-3">AbstractOp Example 2</a></emu-xref>
<emu-xref href="#example-2"><a href="#example-2">with link text</a></emu-xref>

<!-- refs to notes -->
<emu-xref href="#note-1"><a href="#note-1">1.1 Note 1</a></emu-xref>
<emu-xref href="#note-1" title=""><a href="#note-1">AbstractOp Note 1</a></emu-xref>
<emu-xref href="#note-2"><a href="#note-2">Note 1</a></emu-xref> <!-- examples in current clause shouldn't include clause name -->
<emu-xref href="#note-2" title=""><a href="#note-2">Note 1</a></emu-xref> <!-- examples in current clause shouldn't include clause name -->
<emu-xref href="#note-2"><a href="#note-2">with link text</a></emu-xref>

<emu-clause id="sec-abstract-op" aoid="AbstractOp">
<h1><span class="secnum">1.1</span>AbstractOp<span class="utils"><span class="anchor"><a href="#sec-abstract-op">#</a></span></span></h1>
<emu-alg><ol><li>Return.</li></ol></emu-alg>
Expand All @@ -52,11 +59,19 @@
<emu-example id="example-3" caption="Foo Caption"><figure>
This is an example
<figcaption>Example 2 (Informative): Foo Caption</figcaption></figure></emu-example>

<emu-note id="note-1"><span class="note">Note</span>
This is another note
</emu-note>
</emu-clause>

<emu-example id="example-1" caption="Foo Caption"><figure>
This is an example
<figcaption>Example (Informative): Foo Caption</figcaption></figure></emu-example>

<emu-note id="note-2"><span class="note">Note</span>
This is a note!
</emu-note>
</emu-clause>

<emu-grammar><emu-production name="TestProduction" type="lexical" id="prod-TestProduction">
Expand Down

0 comments on commit 47280f4

Please sign in to comment.