html lists export fix
parent
b4ad7cf452
commit
8502c04bee
|
@ -362,25 +362,14 @@ function getHTMLFromAtext (pad, atext, authorColors) {
|
||||||
// so we want to do something reasonable there. We also
|
// so we want to do something reasonable there. We also
|
||||||
// want to deal gracefully with blank lines.
|
// want to deal gracefully with blank lines.
|
||||||
// => keeps track of the parents level of indentation
|
// => keeps track of the parents level of indentation
|
||||||
var lists = []; // e.g. [[1,'bullet'], [3,'bullet'], ...]
|
var openLists = [];
|
||||||
var listLevels = [];
|
|
||||||
for (var i = 0; i < textLines.length; i++) {
|
for (var i = 0; i < textLines.length; i++) {
|
||||||
var context;
|
var context;
|
||||||
var line = _analyzeLine(textLines[i], attribLines[i], apool);
|
var line = _analyzeLine(textLines[i], attribLines[i], apool);
|
||||||
var lineContent = getLineHTML(line.text, line.aline);
|
var lineContent = getLineHTML(line.text, line.aline);
|
||||||
listLevels.push(line.listLevel);
|
|
||||||
|
|
||||||
if (line.listLevel) { //If we are inside a list
|
if (line.listLevel) { //If we are inside a list
|
||||||
// do list stuff
|
|
||||||
var whichList = -1; // index into lists or -1
|
|
||||||
if (line.listLevel) {
|
|
||||||
whichList = lists.length;
|
|
||||||
for (var j = lists.length - 1; j >= 0; j--) {
|
|
||||||
if (line.listLevel <= lists[j][0]) {
|
|
||||||
whichList = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
context = {
|
context = {
|
||||||
line: line,
|
line: line,
|
||||||
lineContent: lineContent,
|
lineContent: lineContent,
|
||||||
|
@ -389,57 +378,74 @@ function getHTMLFromAtext (pad, atext, authorColors) {
|
||||||
text: textLines[i],
|
text: textLines[i],
|
||||||
padId: pad.id
|
padId: pad.id
|
||||||
};
|
};
|
||||||
|
var prevLine = null;
|
||||||
|
var nextLine = null;
|
||||||
|
if (i > 0) {
|
||||||
|
prevLine = _analyzeLine(textLines[i - 1], attribLines[i - 1], apool);
|
||||||
|
}
|
||||||
|
if (i < textLines.length) {
|
||||||
|
nextLine = _analyzeLine(textLines[i + 1], attribLines[i + 1], apool);
|
||||||
|
}
|
||||||
hooks.callAll('getLineHTMLForExport', context);
|
hooks.callAll('getLineHTMLForExport', context);
|
||||||
if (whichList >= lists.length) {
|
//To create list parent elements
|
||||||
if (lists.length > 0) {
|
if ((!prevLine || prevLine.listLevel !== line.listLevel) || (prevLine && line.listTypeName !== prevLine.listTypeName)) {
|
||||||
pieces.push('</li>');
|
//pieces.push('<ul class="' + line.listTypeName + '">');
|
||||||
}
|
var exists = _.find(openLists, function (item) {
|
||||||
lists.push([line.listLevel, line.listTypeName]);
|
return (item.level === line.listLevel && item.type === line.listTypeName);
|
||||||
|
});
|
||||||
// if there is a previous list we need to open x tags, where x is the difference of the levels
|
if (!exists) {
|
||||||
// if there is no previous list we need to open x tags, where x is the wanted level
|
var prevLevel = prevLine.listLevel || 0;
|
||||||
var toOpen = lists.length > 1 ? line.listLevel - lists[lists.length - 2][0] - 1 : line.listLevel - 1
|
if (prevLine && line.listTypeName !== prevLine.listTypeName) {
|
||||||
|
prevLevel = 0;
|
||||||
if (line.listTypeName === 'number') {
|
|
||||||
if (toOpen > 0) {
|
|
||||||
pieces.push(new Array(toOpen + 1).join('<ol></li><li>'));
|
|
||||||
}
|
}
|
||||||
pieces.push('<ol class="' + line.listTypeName + '"><li>', context.lineContent || '<br>');
|
|
||||||
} else {
|
for (var diff = prevLevel; diff < line.listLevel; diff++) {
|
||||||
if (toOpen > 0) {
|
openLists.push({level: diff, type: line.listTypeName});
|
||||||
pieces.push(new Array(toOpen + 1).join('<li><ul><li>'));
|
var prevPiece = pieces[pieces.length - 1];
|
||||||
}
|
|
||||||
pieces.push('<ul class="' + line.listTypeName + '"><li>', context.lineContent || '<br>');
|
if (prevPiece.indexOf('<ul') === 0 || prevPiece.indexOf('<ol') === 0 || prevPiece.indexOf('</li>') === 0) {
|
||||||
|
pieces.push('<li>');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.listTypeName === 'number') {
|
||||||
|
pieces.push('<ol class="' + line.listTypeName + '">');
|
||||||
|
} else {
|
||||||
|
pieces.push('<ul class="' + line.listTypeName + '">');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else { //means we are getting closer to the lowest level of indentation or are at the same level
|
|
||||||
var toClose = lists.length > 0 ? listLevels[listLevels.length - 2] - line.listLevel : 0
|
}
|
||||||
if (toClose > 0) {
|
|
||||||
pieces.push('</li>');
|
pieces.push('<li>', context.lineContent);
|
||||||
if (lists[lists.length - 1][1] === 'number') {
|
|
||||||
pieces.push(new Array(toClose + 1).join('</ol></li>'));
|
// To close list elements
|
||||||
pieces.push('<li>', context.lineContent || '<br>');
|
if (nextLine && nextLine.listLevel === line.listLevel && line.listTypeName === nextLine.listTypeName) {
|
||||||
|
pieces.push('</li>');
|
||||||
|
}
|
||||||
|
if ((!nextLine || !nextLine.listLevel || nextLine.listLevel < line.listLevel) || (nextLine && line.listTypeName !== nextLine.listTypeName)) {
|
||||||
|
var nextLevel = nextLine.listLevel || 0;
|
||||||
|
if (nextLine && line.listTypeName !== nextLine.listTypeName) {
|
||||||
|
nextLevel = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var diff = nextLevel; diff < line.listLevel; diff++) {
|
||||||
|
openLists = openLists.filter(function(el) {
|
||||||
|
return el.level !== diff && el.type !== line.listTypeName;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (pieces[pieces.length - 1].indexOf('</ul') === 0 || pieces[pieces.length - 1].indexOf('</ol') === 0) {
|
||||||
|
pieces.push('</li>');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.listTypeName === 'number') {
|
||||||
|
pieces.push('</ol>');
|
||||||
} else {
|
} else {
|
||||||
pieces.push(new Array(toClose + 1).join('</ul></li>'));
|
pieces.push('</ul>');
|
||||||
pieces.push('<li>', context.lineContent || '<br>');
|
|
||||||
}
|
}
|
||||||
lists = lists.slice(0, whichList + 1);
|
}
|
||||||
} else {
|
|
||||||
pieces.push('</li><li>', context.lineContent || '<br>');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else { //outside any list, need to close line.listLevel of lists
|
} else { //outside any list, need to close line.listLevel of lists
|
||||||
|
|
||||||
if (lists.length > 0) {
|
|
||||||
if (lists[lists.length - 1][1] === 'number') {
|
|
||||||
pieces.push('</li></ol>');
|
|
||||||
pieces.push(new Array(listLevels[listLevels.length - 2]).join('</ol></li>'));
|
|
||||||
} else {
|
|
||||||
pieces.push('</li></ul>');
|
|
||||||
pieces.push(new Array(listLevels[listLevels.length - 2]).join('</ul></li>'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lists = [];
|
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
line: line,
|
line: line,
|
||||||
lineContent: lineContent,
|
lineContent: lineContent,
|
||||||
|
@ -454,14 +460,6 @@ function getHTMLFromAtext (pad, atext, authorColors) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var k = lists.length - 1; k >= 0; k--) {
|
|
||||||
if (lists[k][1] === 'number') {
|
|
||||||
pieces.push('</li></ol>');
|
|
||||||
} else {
|
|
||||||
pieces.push('</li></ul>');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pieces.join('');
|
return pieces.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue