Skip to content

Commit fe08d2a

Browse files
authored
Merge pull request #3268 from citizenos/develop
getLineHTMLForExport - Fixes #2486 but breaks plugins
2 parents bacc37c + a96aa88 commit fe08d2a

File tree

1 file changed

+85
-105
lines changed

1 file changed

+85
-105
lines changed

src/node/utils/ExportHtml.js

Lines changed: 85 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
268268
}
269269

270270
// close all tags upto the outer most
271-
if (outermostTag != -1)
271+
if (outermostTag !== -1)
272272
{
273273
while ( outermostTag >= 0 )
274274
{
@@ -282,7 +282,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
282282
{
283283
if (openTags.indexOf(usedAttribs[i]) === -1)
284284
{
285-
emitOpenTag(usedAttribs[i])
285+
emitOpenTag(usedAttribs[i]);
286286
}
287287
}
288288

@@ -304,7 +304,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
304304
// close all the tags that are open after the last op
305305
while (openTags.length > 0)
306306
{
307-
emitCloseTag(openTags[0])
307+
emitCloseTag(openTags[0]);
308308
}
309309
} // end processNextChars
310310
if (urls)
@@ -333,148 +333,128 @@ function getHTMLFromAtext(pad, atext, authorColors)
333333
// so we want to do something reasonable there. We also
334334
// want to deal gracefully with blank lines.
335335
// => keeps track of the parents level of indentation
336-
var lists = []; // e.g. [[1,'bullet'], [3,'bullet'], ...]
337-
var listLevels = []
336+
var openLists = [];
338337
for (var i = 0; i < textLines.length; i++)
339338
{
339+
var context;
340340
var line = _analyzeLine(textLines[i], attribLines[i], apool);
341341
var lineContent = getLineHTML(line.text, line.aline);
342-
listLevels.push(line.listLevel)
343342

344343
if (line.listLevel)//If we are inside a list
345344
{
346-
// do list stuff
347-
var whichList = -1; // index into lists or -1
348-
if (line.listLevel)
345+
context = {
346+
line: line,
347+
lineContent: lineContent,
348+
apool: apool,
349+
attribLine: attribLines[i],
350+
text: textLines[i],
351+
padId: pad.id
352+
};
353+
var prevLine = null;
354+
var nextLine = null;
355+
if (i > 0)
349356
{
350-
whichList = lists.length;
351-
for (var j = lists.length - 1; j >= 0; j--)
357+
prevLine = _analyzeLine(textLines[i -1], attribLines[i -1], apool);
358+
}
359+
if (i < textLines.length)
360+
{
361+
nextLine = _analyzeLine(textLines[i + 1], attribLines[i + 1], apool);
362+
}
363+
hooks.aCallAll('getLineHTMLForExport', context);
364+
//To create list parent elements
365+
if ((!prevLine || prevLine.listLevel !== line.listLevel) || (prevLine && line.listTypeName !== prevLine.listTypeName))
366+
{
367+
var exists = _.find(openLists, function (item)
352368
{
353-
if (line.listLevel <= lists[j][0])
369+
return (item.level === line.listLevel && item.type === line.listTypeName);
370+
});
371+
if (!exists) {
372+
var prevLevel = 0;
373+
if (prevLine && prevLine.listLevel) {
374+
prevLevel = prevLine.listLevel;
375+
}
376+
if (prevLine && line.listTypeName !== prevLine.listTypeName)
354377
{
355-
whichList = j;
378+
prevLevel = 0;
356379
}
357-
}
358-
}
359380

360-
if (whichList >= lists.length)//means we are on a deeper level of indentation than the previous line
361-
{
362-
if(lists.length > 0){
363-
pieces.push('</li>')
381+
for (var diff = prevLevel; diff < line.listLevel; diff++) {
382+
openLists.push({level: diff, type: line.listTypeName});
383+
var prevPiece = pieces[pieces.length - 1];
384+
385+
if (prevPiece.indexOf("<ul") === 0 || prevPiece.indexOf("<ol") === 0 || prevPiece.indexOf("</li>") === 0)
386+
{
387+
pieces.push("<li>");
388+
}
389+
390+
if (line.listTypeName === "number")
391+
{
392+
pieces.push("<ol class=\"" + line.listTypeName + "\">");
393+
}
394+
else
395+
{
396+
pieces.push("<ul class=\"" + line.listTypeName + "\">");
397+
}
398+
}
364399
}
365-
lists.push([line.listLevel, line.listTypeName]);
400+
}
366401

367-
// if there is a previous list we need to open x tags, where x is the difference of the levels
368-
// if there is no previous list we need to open x tags, where x is the wanted level
369-
var toOpen = lists.length > 1 ? line.listLevel - lists[lists.length - 2][0] - 1 : line.listLevel - 1
402+
pieces.push("<li>", context.lineContent);
370403

371-
if(line.listTypeName == "number")
372-
{
373-
if(toOpen > 0){
374-
pieces.push(new Array(toOpen + 1).join('<ol>'))
375-
}
376-
pieces.push('<ol class="'+line.listTypeName+'"><li>', lineContent || '<br>');
404+
// To close list elements
405+
if (nextLine && nextLine.listLevel === line.listLevel && line.listTypeName === nextLine.listTypeName)
406+
{
407+
pieces.push("</li>");
408+
}
409+
if ((!nextLine || !nextLine.listLevel || nextLine.listLevel < line.listLevel) || (nextLine && line.listTypeName !== nextLine.listTypeName))
410+
{
411+
var nextLevel = 0;
412+
if (nextLine && nextLine.listLevel) {
413+
nextLevel = nextLine.listLevel;
377414
}
378-
else
415+
if (nextLine && line.listTypeName !== nextLine.listTypeName)
379416
{
380-
if(toOpen > 0){
381-
pieces.push(new Array(toOpen + 1).join('<ul>'))
382-
}
383-
pieces.push('<ul class="'+line.listTypeName+'"><li>', lineContent || '<br>');
417+
nextLevel = 0;
384418
}
385-
}
386-
//the following code *seems* dead after my patch.
387-
//I keep it just in case I'm wrong...
388-
/*else if (whichList == -1)//means we are not inside a list
389-
{
390-
if (line.text)
419+
420+
for (var diff = nextLevel; diff < line.listLevel; diff++)
391421
{
392-
console.log('trace 1');
393-
// non-blank line, end all lists
394-
if(line.listTypeName == "number")
422+
openLists = openLists.filter(function(el)
395423
{
396-
pieces.push(new Array(lists.length + 1).join('</li></ol>'));
397-
}
398-
else
424+
return el.level !== diff && el.type !== line.listTypeName;
425+
});
426+
427+
if (pieces[pieces.length - 1].indexOf("</ul") === 0 || pieces[pieces.length - 1].indexOf("</ol") === 0)
399428
{
400-
pieces.push(new Array(lists.length + 1).join('</li></ul>'));
429+
pieces.push("</li>");
401430
}
402-
lists.length = 0;
403-
pieces.push(lineContent, '<br>');
404-
}
405-
else
406-
{
407-
console.log('trace 2');
408-
pieces.push('<br><br>');
409-
}
410-
}*/
411-
else//means we are getting closer to the lowest level of indentation or are at the same level
412-
{
413-
var toClose = lists.length > 0 ? listLevels[listLevels.length - 2] - line.listLevel : 0
414-
if( toClose > 0){
415-
pieces.push('</li>')
416-
if(lists[lists.length - 1][1] == "number")
431+
432+
if (line.listTypeName === "number")
417433
{
418-
pieces.push(new Array(toClose+1).join('</ol>'))
419-
pieces.push('<li>', lineContent || '<br>');
434+
pieces.push("</ol>");
420435
}
421436
else
422437
{
423-
pieces.push(new Array(toClose+1).join('</ul>'))
424-
pieces.push('<li>', lineContent || '<br>');
438+
pieces.push("</ul>");
425439
}
426-
lists = lists.slice(0,whichList+1)
427-
} else {
428-
pieces.push('</li><li>', lineContent || '<br>');
429-
}
440+
}
430441
}
431442
}
432443
else//outside any list, need to close line.listLevel of lists
433444
{
434-
if(lists.length > 0){
435-
if(lists[lists.length - 1][1] == "number"){
436-
pieces.push('</li></ol>');
437-
pieces.push(new Array(listLevels[listLevels.length - 2]).join('</ol>'))
438-
} else {
439-
pieces.push('</li></ul>');
440-
pieces.push(new Array(listLevels[listLevels.length - 2]).join('</ul>'))
441-
}
442-
}
443-
lists = []
444-
445-
var context = {
445+
context = {
446446
line: line,
447447
lineContent: lineContent,
448448
apool: apool,
449449
attribLine: attribLines[i],
450450
text: textLines[i],
451451
padId: pad.id
452-
}
453-
454-
var lineContentFromHook = hooks.callAllStr("getLineHTMLForExport", context, " ", " ", "");
452+
};
455453

456-
if (lineContentFromHook)
457-
{
458-
pieces.push(lineContentFromHook, '');
459-
}
460-
else
461-
{
462-
pieces.push(lineContent, '<br>');
454+
hooks.aCallAll("getLineHTMLForExport", context);
455+
pieces.push(context.lineContent, "<br>");
463456
}
464457
}
465-
}
466-
467-
for (var k = lists.length - 1; k >= 0; k--)
468-
{
469-
if(lists[k][1] == "number")
470-
{
471-
pieces.push('</li></ol>');
472-
}
473-
else
474-
{
475-
pieces.push('</li></ul>');
476-
}
477-
}
478458

479459
return pieces.join('');
480460
}

0 commit comments

Comments
 (0)