Updating cut to support more configurations, fixes #34
This commit is contained in:
parent
37dcd37e04
commit
2a50830c7f
|
@ -230,7 +230,7 @@ function replaceExceptions(options) {
|
||||||
|
|
||||||
/* Replaces cut statements with cuts */
|
/* Replaces cut statements with cuts */
|
||||||
function replaceCuts() {
|
function replaceCuts() {
|
||||||
const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\//i;
|
const searchTerm = /cut\/([^\/]+)\/(-?[\d]+)\/(?:(-?[\d]+)\/)?/i;
|
||||||
walk(document.body, searchTerm, null, cutMethod);
|
walk(document.body, searchTerm, null, cutMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,26 +360,41 @@ function exceptionMethod(node, searchTerm, options) {
|
||||||
exceptionMethod(node, searchTerm, options);
|
exceptionMethod(node, searchTerm, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allows for custom words to be cut off */
|
/* Allows for custom words to be truncated */
|
||||||
function cutMethod(node, searchTerm, unused) {
|
function cutMethod(node, searchTerm, unused) {
|
||||||
const match = node.nodeValue.match(searchTerm);
|
const match = node.nodeValue.match(searchTerm);
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
const target = match[1];
|
const target = match[1];
|
||||||
const amount = match[2];
|
let start = Number(match[2]);
|
||||||
|
let end = Number(match[3]);
|
||||||
|
|
||||||
if (target.length <= Math.abs(amount)) {
|
if (isNaN(end)) {
|
||||||
node.nodeValue = node.nodeValue.replace(searchTerm, target);
|
if (start < 0) { // Cut off from the right side
|
||||||
cutMethod(node, searchTerm, unused);
|
end = target.length + start;
|
||||||
return;
|
start = 0;
|
||||||
|
} else { // Cut off from the left side
|
||||||
|
end = target.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
let replaceValue;
|
|
||||||
if (amount < 0) {
|
|
||||||
replaceValue = target.slice(0, amount);
|
|
||||||
} else {
|
} else {
|
||||||
replaceValue = target.slice(amount);
|
const isNegStart = (start < 0) || ((start == 0) && (end < 0));
|
||||||
|
if (isNegStart) { // Starting from the right, move by end
|
||||||
|
const mod = start;
|
||||||
|
start = target.length + mod;
|
||||||
|
end = target.length + mod + end;
|
||||||
|
} else { // Starting from the left, move by end
|
||||||
|
end = start + end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start should always be less than end
|
||||||
|
if (start > end) {
|
||||||
|
const greater = start;
|
||||||
|
start = end;
|
||||||
|
end = greater;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const replaceValue = target.slice(start, end);
|
||||||
|
|
||||||
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
|
node.nodeValue = node.nodeValue.replace(searchTerm, replaceValue);
|
||||||
cutMethod(node, searchTerm, unused);
|
cutMethod(node, searchTerm, unused);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue