Commit 1bb73fb5 authored by Thibault Ehrhart's avatar Thibault Ehrhart

Improve ADEL offset calculation

parent 7b708014
......@@ -7,37 +7,52 @@ export class ColorizePipe implements PipeTransform {
// Each html tag created by this transform increases the total offset
tagsOffset: number;
// ADEL adds an additional offset after each paragraph followed by a title
bonusOffset: number;
constructor(public sanitizer: DomSanitizer) {
this.tagsOffset = 0;
}
calculateAdelOffset(htmlText: string, start: number, end: number) {
calculateAdelOffset(htmlText: string, startOffset: number, endOffset: number) {
// Adel is based on stripped html text
var pattern = /<(?:(?! ))(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
var match = pattern.exec(htmlText);
var totalTagsLength = 0;
var titleOffset = 0;
while (match != null) {
if (match.index > start + totalTagsLength + this.tagsOffset) {
if (match.index > startOffset + totalTagsLength + this.tagsOffset) {
break;
}
// console.log(match[0], match.index);
//console.warn(match[0], match.index);
totalTagsLength += match[0].length;
if (match[0].startsWith('</')) {
if (match[0].startsWith('</') && match[0] !== '</p>') {
totalTagsLength -= 1; // Adel Annotator adds a space after closing tags
}
if (match[0].startsWith('</hl')) {
titleOffset = match.index;
}
match = pattern.exec(htmlText)
}
var start = startOffset + totalTagsLength + this.tagsOffset + this.bonusOffset;
var end = endOffset + totalTagsLength + this.tagsOffset + this.bonusOffset;
return {
start: start + totalTagsLength + this.tagsOffset,
end: end + totalTagsLength + this.tagsOffset
start,
end,
titleOffset
};
}
transform(text: string, annotations: any): SafeHtml {
var previousTitleOffset = 0;
this.bonusOffset = 0;
if (annotations.length > 0) {
var indexOffset = 0;
......@@ -55,14 +70,18 @@ export class ColorizePipe implements PipeTransform {
return a.start > b.start ? 1 : -1;
});
// console.log(flatAnnotations);
flatAnnotations.forEach((value: any) => {
var colorType = NewsService.getAnnotationColor(value.type);
var adelOffset = this.calculateAdelOffset(text, value.start, value.end);
// console.log('"' + text.substr(adelOffset.start, adelOffset.end - adelOffset.start) + '"');
if (adelOffset.titleOffset > previousTitleOffset) {
// When there is a title after a paragraph, ADEL adds an extra space
// to the offset, so we have to increment ours.
this.bonusOffset += 1;
adelOffset.start += 1;
adelOffset.end += 1;
previousTitleOffset = adelOffset.titleOffset;
}
let replacement = '<a asrael href="' + value.source + '" target="_blank"><span asrael class="colorize color-' + colorType + '">' + text.substr(adelOffset.start, adelOffset.end - adelOffset.start) + '</span></a>';
text = text.substr(0, adelOffset.start) + replacement + text.substr(adelOffset.end);
......@@ -71,11 +90,9 @@ export class ColorizePipe implements PipeTransform {
});
this.tagsOffset = 0;
return text;
} else {
return text;
}
return this.sanitizer.bypassSecurityTrustHtml(text);
}
}
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