diff --git a/app/assets/javascripts/app/lib/app_post/utils.js.coffee b/app/assets/javascripts/app/lib/app_post/utils.js.coffee index 9ec58a2be..f2e5dcda3 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.js.coffee @@ -6,7 +6,7 @@ class App.Utils .replace(/(\r\n|\n\r)/g, "\n") # cleanup .replace(/\r/g, "\n") # cleanup .replace(/[ ]\n/g, "\n") # remove tailing spaces - .replace(/\n{3,9}/g, "\n\n") # remove multible empty lines + .replace(/\n{3,20}/g, "\n\n") # remove multible empty lines # htmlEscapedAndLinkified = App.Utils.text2html( rawText ) @text2html: ( ascii ) -> @@ -22,24 +22,19 @@ class App.Utils # remove not needed new lines html = html.replace(/>\n/g, '>') - # convert to jquery - html = $('
' + html + '
') - # insert new lines - html.find('div, p, pre, code, center, blockquote, form, textarea, address, tr').replaceWith( -> - content = $(@).html() + "\n" - content - .replace(/
/g, "\n") - .replace(//g, "\n") - ) - - # replace
as string, is/was not possible throuh replaceWith - htmlTmp = html.html().replace(/
/g, "\n") + html = html + .replace(//g, "\n") + .replace(//g, "\n") + .replace(/<(div)(|.+?)>/g, "") + .replace(/<(p|blockquote|form|textarea|address|tr)(|.+?)>/g, "\n") + .replace(/<\/(div|p|blockquote|form|textarea|address|tr)>/g, "\n") # trim and cleanup - $('
' + htmlTmp + '
').text().trim() + $('
' + html + '
').text().trim() .replace(/(\r\n|\n\r)/g, "\n") # cleanup .replace(/\r/g, "\n") # cleanup + .replace(/\n{3,20}/g, "\n\n") # remove multible empty lines # htmlEscapedAndLinkified = App.Utils.linkify( rawText ) @linkify: (ascii) -> @@ -191,56 +186,77 @@ class App.Utils else true - # messageWithMarker = App.Utils.signatureIdentify( message, sender ) - @signatureIdentify: (message, sender = false) -> + # messageWithMarker = App.Utils.signatureIdentify( message, false ) + @signatureIdentify: (message, test = false) -> textToSearch = @html2text( message ) # count lines, if we do have lower the 10, ignore this textToSearchInLines = textToSearch.split("\n") - return message if textToSearchInLines.length < 10 + if !test + return message if textToSearchInLines.length < 10 quote = (str) -> (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") # search for signature seperator "--\n" - searchForSeperator = (textToSearchInLines) -> + markers = [] + searchForSeperator = (textToSearchInLines, markers) -> + lineCount = 0 for line in textToSearchInLines + lineCount += 1 if line && line.match( /^\s{0,10}--\s{0,10}$/ ) - return line - false - marker = searchForSeperator(textToSearchInLines) + marker = + line: line + lineCount: lineCount + type: 'seperator' + markers.push marker + searchForSeperator(textToSearchInLines, markers) # search for Apple Mail # On 01/04/15 10:55, Bob Smith wrote: - searchForAppleMail = (textToSearchInLines) -> + searchForAppleMail = (textToSearchInLines, markers) -> + lineCount = 0 for line in textToSearchInLines + lineCount += 1 if line && line.match( /^(On|Am)\s.+?\s(wrote|schrieb):/ ) - return line - false - if !marker - marker = searchForAppleMail(textToSearchInLines) + marker = + line: line + lineCount: lineCount + type: 'apple' + markers.push marker + searchForAppleMail(textToSearchInLines, markers) # search for otrs # 25.02.2015 10:26 - edv hotline schrieb: - searchForOtrs = (textToSearchInLines) -> + searchForOtrs = (textToSearchInLines, markers) -> + lineCount = 0 for line in textToSearchInLines + lineCount += 1 if line && line.match( /^.+?\s.+?\s-\s.+?\s(wrote|schrieb):/ ) - return line - false - if !marker - markerOtrs = searchForOtrs(textToSearchInLines) + marker = + line: line + lineCount: lineCount + type: 'Otrs' + markers.push marker + searchForOtrs(textToSearchInLines, markers) # search for Ms # From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc] # Send: Donnerstag, 2. April 2015 10:00 - searchForMs = (textToSearchInLines) -> + searchForMs = (textToSearchInLines, markers) -> + lineCount = 0 fromFound = undefined for line in textToSearchInLines + lineCount += 1 # find Sent if fromFound if line && line.match( /^(Sent|Gesendet):\s.+?/) - return fromFound + marker = + line: fromFound + lineCount: lineCount + type: 'Ms' + markers.push marker else fromFound = undefined @@ -248,21 +264,21 @@ class App.Utils else if line && line.match( /^(From|Von):\s.+?/ ) fromFound = line.replace(/\s{0,5}\[.+?\]/g, '') - false - if !marker && !markerOtrs - markerMs = searchForMs(textToSearchInLines) + searchForMs(textToSearchInLines, markers) # if no marker is found, return - return message if !marker && !markerMs && !markerOtrs + return message if !markers || !markers[0] # insert marker markerTemplate = '' - if marker - regex = new RegExp( "\>(\s{0,10}#{quote(marker)})\s{0,10}\<" ) + + # get first marker + markers = _.sortBy(markers, 'lineCount') + if markers[0].type is 'seperator' + regex = new RegExp( "\>(\s{0,10}#{quote(markers[0].line)})\s{0,10}\<" ) message.replace( regex, ">#{markerTemplate}\$1<" ) else - marker = markerMs || markerOtrs - regex = new RegExp( "\>(\s{0,10}#{quote(marker)})" ) + regex = new RegExp( "\>(\s{0,10}#{quote(markers[0].line)})" ) message.replace( regex, ">#{markerTemplate}\$1" ) # textReplaced = App.Utils.replaceTags( template, { user: { firstname: 'Bob', lastname: 'Smith' } } ) diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index 73c8e7fa3..3fcc28fac 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -70,7 +70,7 @@ test( "text2html", function() { test( "html2text", function() { var source = "
Some
Value

Test
" - var should = "Some\nValue\n\n\nTest" + var should = "Some\nValue\n\nTest" var result = App.Utils.html2text( source ) equal( result, should, source ) @@ -95,20 +95,24 @@ test( "html2text", function() { equal( result, should, source ) source = "
test 123










--
Bob Smith
" - should = "test 123 \n\n\n\n\n\n\n\n\n\n\n--\nBob Smith" + should = "test 123 \n\n--\nBob Smith" result = App.Utils.html2text( source ) equal( result, should, source ) source = "test 123










--
Bob Smith" - should = "test 123 \n\n\n\n\n\n\n\n\n\n\n--\nBob Smith" + should = "test 123 \n\n--\nBob Smith" result = App.Utils.html2text( source ) equal( result, should, source ) source = "
1









Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
\n
Gesendet: Donnerstag, 2. April 2015 11:32
" - should = "1\n\n\n\n\n\n\n\n\n\n\n\Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]\nGesendet: Donnerstag, 2. April 2015 11:32" + should = "1\n\nVon: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]\nGesendet: Donnerstag, 2. April 2015 11:32" result = App.Utils.html2text( source ) equal( result, should, source ) + source = "
test 123
lalala

--

some test
" + should = "test 123\nlalala\n--\nsome test" + result = App.Utils.html2text( source ) + equal( result, should, source ) }); // linkify @@ -509,81 +513,79 @@ test( "identify signature", function() { result = App.Utils.signatureIdentify( message ) equal( result, should ) - message = "
test 123










--
Bob Smith
" - should = '
test 123










--
Bob Smith
' + message = "
test 123
1
2
3
4
5
6
7
8
9

--
Bob Smith
" + should = '
test 123
1
2
3
4
5
6
7
8
9

--
Bob Smith
' result = App.Utils.signatureIdentify( message ) equal( result, should ) - message = "
test 123









--no not match--
--
Bob Smith
" - should = '
test 123









--no not match--
--
Bob Smith
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--
--
Bob Smith
" + should = '
test 123

--no not match--
--
Bob Smith
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123









--no not match--
--
Bob Smith
" - should = '
test 123









--no not match--
--
Bob Smith
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--
--
Bob Smith
" + should = '
test 123

--no not match--
--
Bob Smith
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123










--
Bob Smith




--
Bob Smith
" - should = '
test 123










--
Bob Smith




--
Bob Smith
' + message = "
test 123

--
Bob Smith




--
Bob Smith
" + should = '
test 123

--
Bob Smith




--
Bob Smith
' //should = '
test 123










--
Bob Smith




--
Bob Smith
' - result = App.Utils.signatureIdentify( message ) + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123
test 123
test 123
test 123
test 123
test 123
test 123
test 123
--
Bob Smith
" - should = "
test 123
test 123
test 123
test 123
test 123
test 123
test 123
test 123
--
Bob Smith
" - result = App.Utils.signatureIdentify( message ) + message = "
test 123
test 123
--
Bob Smith
" + should = "
test 123
test 123
--
Bob Smith
" + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "

test 123

test 123

test 123

test 123

test 123

test 123

test 123

test 123

--

Bob Smith

" - should = "

test 123

test 123

test 123

test 123

test 123

test 123

test 123

test 123

--

Bob Smith

" - result = App.Utils.signatureIdentify( message ) + message = "

test 123

test 123

--

Bob Smith

" + should = "

test 123

test 123

--

Bob Smith

" + result = App.Utils.signatureIdentify( message, true ) // apple - message = "
test 123









--no not match--

Bob Smith
On 01/04/15 10:55, Bob Smith wrote:
lalala
" - should = '
test 123









--no not match--

Bob Smith
On 01/04/15 10:55, Bob Smith wrote:
lalala
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--

Bob Smith
On 01/04/15 10:55, Bob Smith wrote:
lalala

--

some test
" + should = '
test 123

--no not match--

Bob Smith
On 01/04/15 10:55, Bob Smith wrote:
lalala

--

some test
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123









--no not match--

Bob Smith
Am 01/04/15 10:55, Bob Smith schrieb:
lalala
" - should = '
test 123









--no not match--

Bob Smith
Am 01/04/15 10:55, Bob Smith schrieb:
lalala
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--

Bob Smith
Am 01/04/15 10:55, Bob Smith schrieb:
lalala
" + should = '
test 123

--no not match--

Bob Smith
Am 01/04/15 10:55, Bob Smith schrieb:
lalala
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) // ms - message = "
test 123









--no not match--

Bob Smith
Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Gesendet: Donnerstag, 2. April 2015 10:00
lalala
" - should = '
test 123









--no not match--

Bob Smith
Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Gesendet: Donnerstag, 2. April 2015 10:00
lalala
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--

Bob Smith
Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Gesendet: Donnerstag, 2. April 2015 10:00
lalala
" + should = '
test 123

--no not match--

Bob Smith
Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Gesendet: Donnerstag, 2. April 2015 10:00
lalala
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123









--no not match--

Bob Smith
From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Sent: Donnerstag, 2. April 2015 10:00
lalala
" - should = '
test 123









--no not match--

Bob Smith
From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Sent: Donnerstag, 2. April 2015 10:00
lalala
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--

Bob Smith
From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Sent: Donnerstag, 2. April 2015 10:00
lalala
" + should = '
test 123

--no not match--

Bob Smith
From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
Sent: Donnerstag, 2. April 2015 10:00
lalala
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
1









Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
\n
Gesendet: Donnerstag, 2. April 2015 11:32
" - should = "
1









Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
\n
Gesendet: Donnerstag, 2. April 2015 11:32
" - result = App.Utils.signatureIdentify( message ) + message = "
1

Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
\n
Gesendet: Donnerstag, 2. April 2015 11:32
" + should = "
1

Von: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
\n
Gesendet: Donnerstag, 2. April 2015 11:32
" + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) // otrs - message = "
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith wrote:
lalala
" - should = '
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith wrote:
lalala
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith wrote:
lalala
" + should = '
test 123

--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith wrote:
lalala
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith schrieb:
lalala
" - should = '
test 123









--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith schrieb:
lalala
' - result = App.Utils.signatureIdentify( message ) + message = "
test 123

--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith schrieb:
lalala
" + should = '
test 123

--no not match--

Bob Smith
01/04/15 10:55 - Bob Smith schrieb:
lalala
' + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) - message = "
test 123








24.02.2015 14:20 - Roy Kaldung via Znuny Sales schrieb:  
" - should = "
test 123








24.02.2015 14:20 - Roy Kaldung via Znuny Sales schrieb:  
" - result = App.Utils.signatureIdentify( message ) + message = "
test 123

24.02.2015 14:20 - Roy Kaldung via Znuny Sales schrieb:  
" + should = "
test 123

24.02.2015 14:20 - Roy Kaldung via Znuny Sales schrieb:  
" + result = App.Utils.signatureIdentify( message, true ) equal( result, should ) -// - }); // replace tags