2019-04-28 02:26:10 -05:00
|
|
|
import { Anchor, Href, BoolHash, AltText } from './Model';
|
|
|
|
|
|
|
|
export class Util {
|
2019-04-28 17:24:33 -05:00
|
|
|
public static regexLib: { [name: string]: () => RegExp } = {
|
|
|
|
anchors: () => /(\[((?:[^\[\]]+|\[(?:[^\[\]]+|\[(?:[^\[\]]+|\[(?:[^\[\]]+|\[(?:[^\[\]]+|\[(?:[^\[\]]+|\[\])*\])*\])*\])*\])*\])*)\]\([ ]*((?:[^()\s]+|\((?:[^()\s]+|\((?:[^()\s]+|\((?:[^()\s]+|\((?:[^()\s]+|\((?:[^()\s]+|\(\))*\))*\))*\))*\))*\))*)[ ]*((['"])(.*?)\5[ ]*)?\))/gm,
|
2019-04-28 02:26:10 -05:00
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
href: () => /^(?:\/([a-zA-Z](?:-?[a-zA-Z0-9])*))?(?:\?((?:!?[a-zA-Z](?:-?[a-zA-Z0-9])*)(?:&!?[a-zA-Z](?:-?[a-zA-Z0-9])*)*)?)?(?:#((?:[a-zA-Z](?:-?[a-zA-Z0-9])*)(?:\+[a-zA-Z](?:-?[a-zA-Z0-9])*)*))?$/,
|
2019-04-28 02:26:10 -05:00
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
trim: () => /^\s+|\s+$/g,
|
2019-04-28 02:26:10 -05:00
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
altText: () => /^((?:[^|\\]|\\.)*)(?:\|((?:[^|\\]|\\.)+))?$/,
|
2019-04-28 02:26:10 -05:00
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
escapeChar: () => /\\(?=[^\\])/g,
|
2019-04-28 02:26:10 -05:00
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
emptyListItem: () => /^[ ]*-\s*([\r\n]+|$)/gm,
|
2019-04-28 02:26:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
private static matchToAnchor(match: RegExpExecArray): Anchor {
|
|
|
|
const result: Anchor = {
|
|
|
|
anchor: match[1],
|
|
|
|
text: match[2],
|
|
|
|
href: match[3],
|
|
|
|
title: match[6],
|
|
|
|
};
|
|
|
|
if(result.href.indexOf('"') === 0) {
|
|
|
|
result.title = result.href.substring(1, result.href.length - 1);
|
|
|
|
result.href = '';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static matchAnchor(text: string): Anchor | undefined {
|
2019-04-28 17:24:33 -05:00
|
|
|
const match = this.regexLib.anchors().exec(text);
|
2019-04-28 02:26:10 -05:00
|
|
|
if(match) return this.matchToAnchor(match);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static matchAnchors(text: string): Anchor[] {
|
|
|
|
let match: RegExpExecArray | null;
|
|
|
|
const anchors: Anchor[] = [];
|
2019-04-28 17:24:33 -05:00
|
|
|
const regex = this.regexLib.anchors();
|
|
|
|
while((match = regex.exec(text)) !== null) {
|
2019-04-28 02:26:10 -05:00
|
|
|
anchors.push(this.matchToAnchor(match));
|
|
|
|
}
|
|
|
|
return anchors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static matchHref(text: string): Href | undefined {
|
2019-04-28 17:24:33 -05:00
|
|
|
const match = this.regexLib.href().exec(text);
|
2019-04-28 02:26:10 -05:00
|
|
|
if(match) {
|
|
|
|
return {
|
|
|
|
target: match[1],
|
|
|
|
conditions: match[2],
|
|
|
|
toggles: match[3],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static trimText(text: string): string {
|
2019-04-28 17:24:33 -05:00
|
|
|
return text.replace(this.regexLib.trim(), '');
|
2019-04-28 02:26:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static normalize(text: string): string {
|
|
|
|
return text.toLowerCase().replace(/^\W+|\W+$/g, '').replace(/\W+/g, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static toBoolHash(names: string[]): BoolHash | undefined {
|
|
|
|
if(names) {
|
|
|
|
const hash: BoolHash = {};
|
|
|
|
for(let name of names) {
|
|
|
|
if(name.indexOf('!') === 0) {
|
|
|
|
hash[name.substring(1, name.length)] = false;
|
|
|
|
} else {
|
|
|
|
hash[name] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
public static conditionsMet(state: BoolHash, conditions?: BoolHash): boolean {
|
|
|
|
if(!conditions) return true;
|
2019-04-28 02:26:10 -05:00
|
|
|
for(let [cond, val] of Object.entries(conditions)) {
|
|
|
|
if((val && !state[cond]) || (!val && state[cond])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-28 17:24:33 -05:00
|
|
|
public static splitAltText(text: string): AltText {
|
|
|
|
const match = this.regexLib.altText().exec(text);
|
2019-04-28 02:26:10 -05:00
|
|
|
if(match) {
|
|
|
|
return {
|
2019-04-28 17:24:33 -05:00
|
|
|
passed: match[1],
|
|
|
|
failed: match[2],
|
2019-04-28 02:26:10 -05:00
|
|
|
};
|
|
|
|
}
|
2019-04-28 17:24:33 -05:00
|
|
|
return {};
|
2019-04-28 02:26:10 -05:00
|
|
|
}
|
|
|
|
}
|