mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Experemental Plain2Html/Html2Plain convertor (+ jQuery linkify)
This commit is contained in:
parent
8cfd52ef12
commit
e4333a6f90
18 changed files with 1211 additions and 604 deletions
|
|
@ -5,8 +5,6 @@
|
|||
|
||||
$div = $('<div></div>'),
|
||||
|
||||
trim = $.trim,
|
||||
|
||||
/**
|
||||
* @param {string} sHtml
|
||||
* @return {string}
|
||||
|
|
@ -14,40 +12,69 @@
|
|||
htmlToPlain = function (sHtml)
|
||||
{
|
||||
var
|
||||
iPos = 0,
|
||||
iP1 = 0,
|
||||
iP2 = 0,
|
||||
iP3 = 0,
|
||||
iLimit = 0,
|
||||
|
||||
sText = '',
|
||||
sQuoteChar = '> ',
|
||||
|
||||
convertBlockquote = function () {
|
||||
if (arguments && 1 < arguments.length)
|
||||
splitPlainText = function (sText)
|
||||
{
|
||||
var
|
||||
iLen = 100,
|
||||
sPrefix = '',
|
||||
sSubText = '',
|
||||
sResult = sText,
|
||||
iSpacePos = 0,
|
||||
iNewLinePos = 0
|
||||
;
|
||||
|
||||
while (sResult.length > iLen)
|
||||
{
|
||||
var
|
||||
sText = trim(arguments[1])
|
||||
.replace(/__bq__start__([\s\S\n\r]*)__bq__end__/gm, convertBlockquote)
|
||||
;
|
||||
sSubText = sResult.substring(0, iLen);
|
||||
iSpacePos = sSubText.lastIndexOf(' ');
|
||||
iNewLinePos = sSubText.lastIndexOf('\n');
|
||||
|
||||
sText = '\n' + sQuoteChar + trim(sText).replace(/\n/gm, '\n' + sQuoteChar) + '\n>\n';
|
||||
if (-1 !== iNewLinePos)
|
||||
{
|
||||
iSpacePos = iNewLinePos;
|
||||
}
|
||||
|
||||
return sText.replace(/\n([> ]+)/gm, function () {
|
||||
return (arguments && 1 < arguments.length) ? '\n' + trim(arguments[1].replace(/[\s]/, '')) + ' ' : '';
|
||||
});
|
||||
if (-1 === iSpacePos)
|
||||
{
|
||||
iSpacePos = iLen;
|
||||
}
|
||||
|
||||
sPrefix += sSubText.substring(0, iSpacePos) + '\n';
|
||||
sResult = sResult.substring(iSpacePos + 1);
|
||||
}
|
||||
|
||||
return '';
|
||||
return sPrefix + sResult;
|
||||
},
|
||||
|
||||
convertBlockquote = function (sText) {
|
||||
sText = splitPlainText($.trim(sText));
|
||||
sText = '> ' + sText.replace(/\n/gm, '\n> ');
|
||||
return sText.replace(/(^|\n)([> ]+)/gm, function () {
|
||||
return (arguments && 2 < arguments.length) ? arguments[1] + $.trim(arguments[2].replace(/[\s]/, '')) + ' ' : '';
|
||||
});
|
||||
},
|
||||
|
||||
convertDivs = function () {
|
||||
if (arguments && 1 < arguments.length)
|
||||
{
|
||||
var sText = trim(arguments[1]);
|
||||
var sText = $.trim(arguments[1]);
|
||||
if (0 < sText.length)
|
||||
{
|
||||
sText = sText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gmi, convertDivs);
|
||||
sText = '\n' + trim(sText) + '\n';
|
||||
sText = '\n' + $.trim(sText) + '\n';
|
||||
}
|
||||
|
||||
|
||||
return sText;
|
||||
}
|
||||
|
||||
|
||||
return '';
|
||||
},
|
||||
|
||||
|
|
@ -57,7 +84,7 @@
|
|||
},
|
||||
|
||||
convertLinks = function () {
|
||||
return (arguments && 1 < arguments.length) ? trim(arguments[1]) : '';
|
||||
return (arguments && 1 < arguments.length) ? $.trim(arguments[1]) : '';
|
||||
}
|
||||
;
|
||||
|
||||
|
|
@ -76,22 +103,63 @@
|
|||
.replace(/<blockquote[^>]*>/gmi, '\n__bq__start__\n')
|
||||
.replace(/<\/blockquote>/gmi, '\n__bq__end__\n')
|
||||
.replace(/<a [^>]*>([\s\S\r\n]*?)<\/a>/gmi, convertLinks)
|
||||
.replace(/<\/div>/gi, '\n')
|
||||
.replace(/ /gi, ' ')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/</gi, '<')
|
||||
.replace(/"/gi, '"')
|
||||
.replace(/&/gi, '&')
|
||||
.replace(/<[^>]*>/gm, '')
|
||||
;
|
||||
|
||||
sText = $div.html(sText).text();
|
||||
|
||||
sText = sText
|
||||
.replace(/\n[ \t]+/gm, '\n')
|
||||
.replace(/[\n]{3,}/gm, '\n\n')
|
||||
.replace(/__bq__start__([\s\S\r\n]*)__bq__end__/gm, convertBlockquote)
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/</gi, '<')
|
||||
;
|
||||
|
||||
iPos = 0;
|
||||
iLimit = 100;
|
||||
|
||||
while (0 < iLimit)
|
||||
{
|
||||
iLimit--;
|
||||
iP1 = sText.indexOf('__bq__start__', iPos);
|
||||
if (-1 < iP1)
|
||||
{
|
||||
iP2 = sText.indexOf('__bq__start__', iP1 + 5);
|
||||
iP3 = sText.indexOf('__bq__end__', iP1 + 5);
|
||||
|
||||
if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3)
|
||||
{
|
||||
sText = sText.substring(0, iP1) +
|
||||
convertBlockquote(sText.substring(iP1 + 13, iP3)) +
|
||||
sText.substring(iP3 + 11);
|
||||
|
||||
iPos = 0;
|
||||
}
|
||||
else if (-1 < iP2 && iP2 < iP3)
|
||||
{
|
||||
iPos = iP2 - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
iPos = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sText = sText
|
||||
.replace(/__bq__start__/gm, '')
|
||||
.replace(/__bq__end__/gm, '')
|
||||
;
|
||||
|
||||
return $div.html(sText).text();
|
||||
return sText;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -100,9 +168,77 @@
|
|||
*/
|
||||
plainToHtml = function (sPlain)
|
||||
{
|
||||
return sPlain.toString()
|
||||
.replace(/&/g, '&').replace(/>/g, '>').replace(/</g, '<')
|
||||
.replace(/\r/g, '').replace(/\n/g, '<br />');
|
||||
sPlain = sPlain.toString().replace(/\r/g, '');
|
||||
|
||||
var
|
||||
bIn = false,
|
||||
bDo = true,
|
||||
bStart = true,
|
||||
aNextText = [],
|
||||
sLine = '',
|
||||
iIndex = 0,
|
||||
aText = sPlain.split("\n")
|
||||
;
|
||||
|
||||
do
|
||||
{
|
||||
bDo = false;
|
||||
aNextText = [];
|
||||
for (iIndex = 0; iIndex < aText.length; iIndex++)
|
||||
{
|
||||
sLine = aText[iIndex];
|
||||
bStart = '>' === sLine.substr(0, 1);
|
||||
if (bStart && !bIn)
|
||||
{
|
||||
bDo = true;
|
||||
bIn = true;
|
||||
aNextText.push('~~~blockquote~~~');
|
||||
aNextText.push(sLine.substr(1));
|
||||
}
|
||||
else if (!bStart && bIn)
|
||||
{
|
||||
bIn = false;
|
||||
aNextText.push('~~~/blockquote~~~');
|
||||
aNextText.push(sLine);
|
||||
}
|
||||
else if (bStart && bIn)
|
||||
{
|
||||
aNextText.push(sLine.substr(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
aNextText.push(sLine);
|
||||
}
|
||||
}
|
||||
|
||||
if (bIn)
|
||||
{
|
||||
bIn = false;
|
||||
aNextText.push('~~~/blockquote~~~');
|
||||
}
|
||||
|
||||
aText = aNextText;
|
||||
}
|
||||
while (bDo);
|
||||
|
||||
sPlain = aText.join("\n");
|
||||
|
||||
sPlain = sPlain
|
||||
.replace(/&/g, '&')
|
||||
.replace(/>/g, '>').replace(/</g, '<')
|
||||
.replace(/~~~blockquote~~~[\s]*/g, '<blockquote>')
|
||||
.replace(/[\s]*~~~\/blockquote~~~/g, '</blockquote>')
|
||||
.replace(/[\-_~]{10,}/g, '<hr />')
|
||||
.replace(/\n/g, '<br />');
|
||||
|
||||
if ($.fn && $.fn.linkify)
|
||||
{
|
||||
sPlain = $div.html(sPlain)
|
||||
.linkify().find('.linkified').removeClass('linkified').end()
|
||||
.html();
|
||||
}
|
||||
|
||||
return sPlain;
|
||||
}
|
||||
;
|
||||
|
||||
|
|
@ -226,7 +362,7 @@ CKEDITOR.plugins.plain = {
|
|||
if (editor.mode === 'wysiwyg') {
|
||||
editor.fire('saveSnapshot');
|
||||
}
|
||||
|
||||
|
||||
editor.getCommand('plain').setState(CKEDITOR.TRISTATE_DISABLED);
|
||||
editor.setMode(editor.mode === 'plain' ? 'wysiwyg' : 'plain');
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue