Commit 4ac7803d authored by Thibault Ehrhart's avatar Thibault Ehrhart

Refactor Adel Annotator offset calculation

parent ced6e391
......@@ -5,13 +5,7 @@ import { NewsService } from '../../services/news/news.service';
@Pipe({ name: 'colorize' })
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, startOffset: number, endOffset: number) {
......@@ -19,42 +13,36 @@ export class ColorizePipe implements PipeTransform {
var pattern = /<(?:(?! ))(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
var match = pattern.exec(htmlText);
var totalTagsLength = 0;
var titleOffset = 0;
while (match != null) {
if (match.index > startOffset + totalTagsLength + this.tagsOffset) {
if (match.index > startOffset + totalTagsLength) {
break;
}
//console.warn(match[0], match.index);
totalTagsLength += match[0].length;
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;
var start = startOffset + totalTagsLength;
var end = endOffset + totalTagsLength;
return {
start,
end,
titleOffset
text: htmlText.substr(start, end - start)
};
}
normalizeAdelText(text: string) {
return text.replace(/\n/g, ' ').replace(/<\/p> <p>/g, '</p> <p>');
}
transform(text: string, annotations: any): SafeHtml {
var previousTitleOffset = 0;
this.bonusOffset = 0;
text = this.normalizeAdelText(text);
console.warn(text);
if (annotations.length > 0) {
var indexOffset = 0;
var previousTitleOffset = 0;
// Create a flat array of annotations
var flatAnnotations:Array<any> = [];
......@@ -70,26 +58,26 @@ export class ColorizePipe implements PipeTransform {
return a.start > b.start ? 1 : -1;
});
console.warn(flatAnnotations);
flatAnnotations.forEach((value: any) => {
var colorType = NewsService.getAnnotationColor(value.type);
var adelOffset = this.calculateAdelOffset(text, value.start, value.end);
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;
console.warn(adelOffset);
var words = text.substr(adelOffset.start, adelOffset.end - adelOffset.start);
var closingTagIndex = words.indexOf('</');
if (closingTagIndex >= 0) {
// Hack: if Adel annotated words from two different paragraphs, ignore it
// (eg. one word from 1st paragraph then one word from 2nd paragraph)
return;
}
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>';
let replacement = '<a asrael href="' + value.source + '" target="_blank"><span asrael class="colorize color-' + colorType + '">' + words + '</span></a>';
text = text.substr(0, adelOffset.start) + replacement + text.substr(adelOffset.end);
this.tagsOffset += 2; // </span></a>
});
this.tagsOffset = 0;
}
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