Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify buildMultipage #603

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 48 additions & 63 deletions src/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,36 +740,46 @@ export default class Spec {
});
}

private checkValidSectionId(ele: Element) {
private readSectionId(ele: Element) {
if (ele.id == null) {
this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message: 'When using --multipage, top-level sections must have ids',
node: ele,
});
return undefined;
}
if (!ele.id.startsWith('sec-')) {
this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message: 'When using --multipage, top-level sections must have ids beginning with `sec-`',
node: ele,
});
return false;
return undefined;
}
if (!/^[A-Za-z0-9-_]+$/.test(ele.id)) {
const name = ele.id.substring(4);
if (!/^[A-Za-z0-9-_]+$/.test(name)) {
Comment on lines -753 to +763
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a behavioral change, but we'd never consider id="sec-" to be valid anyway.

this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message:
'When using --multipage, top-level sections must have ids matching /^[A-Za-z0-9-_]+$/',
node: ele,
});
return false;
return undefined;
}
if (ele.id.toLowerCase() === 'sec-index') {
if (name.toLowerCase() === 'index') {
this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message: 'When using --multipage, top-level sections must not be named "index"',
node: ele,
});
return false;
return undefined;
}
return true;
return name;
}

private propagateEffects() {
Expand Down Expand Up @@ -834,82 +844,57 @@ export default class Spec {
const clauseTypes = ['EMU-ANNEX', 'EMU-CLAUSE'];
// @ts-ignore
for (const child of wrapper.children) {
let section: { name: string; eles: Element[] };
if (stillIntro) {
if (clauseTypes.includes(child.nodeName)) {
throw new Error('cannot make multipage build without intro');
} else if (child.nodeName === 'EMU-INTRO') {
stillIntro = false;

if (child.id == null) {
this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message: 'When using --multipage, top-level sections must have ids',
node: child,
});
continue;
}
if (child.id !== 'sec-intro') {
this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message: 'When using --multipage, the introduction must have id "sec-intro"',
node: child,
});
continue;
}

const name = 'index';
} else if (child.nodeName !== 'EMU-INTRO') {
// anything before emu-intro is considered part of the introduction
introEles.push(child);
sections.push({ name, eles: introEles });
continue;
}

const contained: string[] = [];
sectionToContainedIds.set(name, contained);
stillIntro = false;

for (const item of introEles) {
if (item.id) {
contained.push(item.id);
containedIdToSection.set(item.id, name);
}
}

// @ts-ignore
for (const item of [...introEles].flatMap(e => [...e.querySelectorAll('[id]')])) {
contained.push(item.id);
containedIdToSection.set(item.id, name);
}
} else {
introEles.push(child);
}
} else {
if (!clauseTypes.includes(child.nodeName)) {
throw new Error('non-clause children are not yet implemented: ' + child.nodeName);
}
if (child.id == null) {
if (child.id !== 'sec-intro') {
this.warn({
type: 'node',
ruleId: 'top-level-section-id',
message: 'When using --multipage, top-level sections must have ids',
message: 'When using --multipage, the introduction must have id "sec-intro"',
node: child,
});
continue;
}
if (!this.checkValidSectionId(child)) {

const name = 'index';
introEles.push(child);
section = { name, eles: introEles };
} else {
if (!clauseTypes.includes(child.nodeName)) {
throw new Error('non-clause children are not yet implemented: ' + child.nodeName);
}

const name = this.readSectionId(child);
if (name === undefined) {
continue;
}

const name = child.id.substring(4);
const contained: string[] = [];
sectionToContainedIds.set(name, contained);
section = { name, eles: [child] };
}

contained.push(child.id);
containedIdToSection.set(child.id, name);
sections.push(section);

for (const item of child.querySelectorAll('[id]')) {
const contained: string[] = [];
sectionToContainedIds.set(section.name, contained);
for (const ele of section.eles) {
if (ele.id) {
contained.push(ele.id);
containedIdToSection.set(ele.id, section.name);
}
for (const item of ele.querySelectorAll('[id]')) {
contained.push(item.id);
containedIdToSection.set(item.id, name);
containedIdToSection.set(item.id, section.name);
}
sections.push({ name, eles: [child] });
}
}

Expand Down
Loading