Commit abeda839 authored by Fred Chasen's avatar Fred Chasen

Update counters to allow page and footnote resets

parent 1431319b
Pipeline #725 failed with stage
in 6 minutes and 27 seconds
This diff is collapsed.
......@@ -5,7 +5,7 @@ import json from 'rollup-plugin-json';
// import builtins from 'rollup-plugin-node-builtins';
// import globals from 'rollup-plugin-node-globals';
import pkg from './package.json';
// import pkg from './package.json';
import serve from 'rollup-plugin-serve'
import livereload from 'rollup-plugin-livereload'
......
......@@ -14,17 +14,28 @@ class Counters extends Handler {
let property = declaration.property;
if (property === "counter-increment") {
let inc = this.handleIncrement(declaration, rule);
if (inc) {
this.handleIncrement(declaration, rule);
// clean up empty declaration
let hasProperities = false;
declaration.value.children.forEach((data) => {
if (data.type && data.type !== "WhiteSpace") {
hasProperities = true;
}
});
if (!hasProperities) {
dList.remove(dItem);
}
} else if (property === "counter-reset") {
let reset = this.handleReset(declaration, rule);
for (const r of reset) {
if (r.selector !== ".pagedjs_page") {
dList.remove(dItem);
break;
this.handleReset(declaration, rule);
// clean up empty declaration
let hasProperities = false;
declaration.value.children.forEach((data) => {
if (data.type && data.type !== "WhiteSpace") {
hasProperities = true;
}
});
if (!hasProperities) {
dList.remove(dItem);
}
}
}
......@@ -49,22 +60,24 @@ class Counters extends Handler {
}
handleIncrement(declaration, rule) {
let names = [];
let values = [];
let increments = [];
declaration.value.children.forEach((child) => {
if (child.type && child.type === "Identifier") {
names.push(child.name);
}
if (child.type && child.type === "Number") {
values.push(child.value);
}
});
for (const name of names) {
let number = values.shift();
let children = declaration.value.children;
children.forEach((data, item) => {
if (data.type && data.type === "Identifier") {
let name = data.name;
if (name === "page" || name.indexOf("target-counter-") === 0) {
continue;
return;
}
let whitespace, number, value;
if (item.next && item.next.data.type === "WhiteSpace") {
whitespace = item.next;
}
if (whitespace && whitespace.next && whitespace.next.data.type === "Number") {
number = whitespace.next;
value = parseInt(number.data.value);
}
let selector = csstree.generate(rule.ruleNode.prelude);
......@@ -77,31 +90,45 @@ class Counters extends Handler {
}
let increment = {
selector: selector,
number: number || 1
number: value || 1
};
counter.increments[selector] = increment;
increments.push(increment);
// Remove the parsed resets
children.remove(item);
if (whitespace) {
children.remove(whitespace);
}
if (number) {
children.remove(number);
}
}
});
return increments;
}
handleReset(declaration, rule) {
let names = [];
let values = [];
let resets = [];
declaration.value.children.forEach((child) => {
if (child.type && child.type === "Identifier") {
names.push(child.name);
let children = declaration.value.children;
children.forEach((data, item) => {
if (data.type && data.type === "Identifier") {
let name = data.name;
let whitespace, number, value;
if (item.next && item.next.data.type === "WhiteSpace") {
whitespace = item.next;
}
if (child.type && child.type === "Number") {
values.push(child.value);
if (whitespace && whitespace.next && whitespace.next.data.type === "Number") {
number = whitespace.next;
value = parseInt(number.data.value);
}
});
for (const name of names) {
let counter;
let selector;
let prelude = rule.ruleNode.prelude;
let number = values.shift();
if (rule.ruleNode.type === "Atrule" && rule.ruleNode.name === "page") {
selector = ".pagedjs_page";
} else {
......@@ -120,11 +147,25 @@ class Counters extends Handler {
let reset = {
selector: selector,
number: number || 0
number: value || 0
};
counter.resets[selector] = reset;
resets.push(reset);
if (selector !== ".pagedjs_page") {
// Remove the parsed resets
children.remove(item);
if (whitespace) {
children.remove(whitespace);
}
if (number) {
children.remove(number);
}
}
}
});
return resets;
}
......@@ -322,8 +363,10 @@ class Counters extends Handler {
resets.push(`footnote-marker ${value}`);
});
if (resets.length) {
this.styleSheet.insertRule(`[data-page-number="${pageElement.dataset.pageNumber}"] { counter-increment: none; counter-reset: ${resets.join(" ")} }`, this.styleSheet.cssRules.length);
}
}
}
......
......@@ -36,7 +36,7 @@ class PageCounterIncrement extends Handler {
const number = declaration.value.children.getSize() > 1 ? declaration.value.children.last().value : 1;
const name = identifier && identifier.name;
if (name.indexOf("target-counter-") === 0) {
if (name && name.indexOf("target-counter-") === 0) {
return;
}
// A counter named page is automatically created and incremented by 1 on every page of the document,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment