2017-07-17 16:21:47 -04:00
|
|
|
export class TextPostProcessRuler {
|
|
|
|
constructor() {
|
|
|
|
this.rules = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
getRules() {
|
|
|
|
return this.rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO error handling
|
|
|
|
getMatcher() {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (this.matcher) {
|
|
|
|
return this.matcher;
|
|
|
|
}
|
2017-07-17 16:21:47 -04:00
|
|
|
|
2017-07-20 15:33:36 -04:00
|
|
|
this.matcherIndex = [];
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
let rules = this.rules.map(
|
|
|
|
r => "(" + r.rule.matcher.toString().slice(1, -1) + ")"
|
2017-07-20 15:33:36 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
let i;
|
|
|
|
let regexString = "";
|
|
|
|
let last = 1;
|
|
|
|
|
|
|
|
// this code is a bit tricky, our matcher may have multiple capture groups
|
|
|
|
// we want to dynamically determine how many
|
2018-06-15 11:03:24 -04:00
|
|
|
for (i = 0; i < rules.length; i++) {
|
2017-07-20 15:33:36 -04:00
|
|
|
this.matcherIndex[i] = last;
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (i === rules.length - 1) {
|
2017-07-20 15:33:36 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (i > 0) {
|
|
|
|
regexString = regexString + "|";
|
2017-07-20 15:33:36 -04:00
|
|
|
}
|
|
|
|
regexString = regexString + rules[i];
|
2017-07-17 16:21:47 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
let regex = new RegExp(regexString + "|(x)");
|
|
|
|
last = "x".match(regex).length - 1;
|
2017-07-20 15:33:36 -04:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.matcher = new RegExp(rules.join("|"), "g");
|
2017-07-17 16:21:47 -04:00
|
|
|
return this.matcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyRule(buffer, match, state) {
|
|
|
|
let i;
|
2018-06-15 11:03:24 -04:00
|
|
|
for (i = 0; i < this.rules.length; i++) {
|
2017-07-20 15:33:36 -04:00
|
|
|
let index = this.matcherIndex[i];
|
|
|
|
|
|
|
|
if (match[index]) {
|
2018-06-15 11:03:24 -04:00
|
|
|
this.rules[i].rule.onMatch(
|
|
|
|
buffer,
|
|
|
|
match.slice(index, this.matcherIndex[i + 1]),
|
|
|
|
state
|
|
|
|
);
|
2017-07-17 16:21:47 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO validate inputs
|
|
|
|
push(name, rule) {
|
2018-06-15 11:03:24 -04:00
|
|
|
this.rules.push({ name, rule });
|
2017-07-17 16:21:47 -04:00
|
|
|
this.matcher = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function allowedBoundary(content, index, utils) {
|
|
|
|
let code = content.charCodeAt(index);
|
2018-06-15 11:03:24 -04:00
|
|
|
return (
|
|
|
|
utils.isWhiteSpace(code) || utils.isPunctChar(String.fromCharCode(code))
|
|
|
|
);
|
2017-07-17 16:21:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function textPostProcess(content, state, ruler) {
|
|
|
|
let result = null,
|
2018-06-15 11:03:24 -04:00
|
|
|
match,
|
|
|
|
pos = 0;
|
2017-07-17 16:21:47 -04:00
|
|
|
|
|
|
|
const matcher = ruler.getMatcher();
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
while ((match = matcher.exec(content))) {
|
2017-07-17 16:21:47 -04:00
|
|
|
// check boundary
|
|
|
|
if (match.index > 0) {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (!allowedBoundary(content, match.index - 1, state.md.utils)) {
|
2017-07-17 16:21:47 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check forward boundary as well
|
|
|
|
if (match.index + match[0].length < content.length) {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (
|
|
|
|
!allowedBoundary(content, match.index + match[0].length, state.md.utils)
|
|
|
|
) {
|
2017-07-17 16:21:47 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match.index > pos) {
|
|
|
|
result = result || [];
|
2018-06-15 11:03:24 -04:00
|
|
|
let token = new state.Token("text", "", 0);
|
2017-07-17 16:21:47 -04:00
|
|
|
token.content = content.slice(pos, match.index);
|
|
|
|
result.push(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = result || [];
|
|
|
|
|
|
|
|
ruler.applyRule(result, match, state);
|
|
|
|
|
|
|
|
pos = match.index + match[0].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result && pos < content.length) {
|
2018-06-15 11:03:24 -04:00
|
|
|
let token = new state.Token("text", "", 0);
|
2017-07-17 16:21:47 -04:00
|
|
|
token.content = content.slice(pos);
|
|
|
|
result.push(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
helper.registerPlugin(md => {
|
|
|
|
const ruler = md.core.textPostProcess.ruler;
|
|
|
|
const replacer = (content, state) => textPostProcess(content, state, ruler);
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
md.core.ruler.push("text-post-process", state =>
|
2017-07-17 16:21:47 -04:00
|
|
|
md.options.discourse.helpers.textReplace(state, replacer, true)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|