Browse Source

Merge pull request #10 from dryabov/master

fix parsing of mentions and hahtags
master
iHedgehog 11 years ago
parent
commit
d9d0a4eed3
  1. 52
      js/twister_formatpost.js

52
js/twister_formatpost.js

@ -143,8 +143,11 @@ function htmlFormatMsg( msg, output, mentions ) {
var tmp; var tmp;
var match = null; var match = null;
var index; var index;
var reAll = new RegExp("(#|@|http[s]?://)"); var strUrlRegexp = "http[s]?://";
var reHttp = new RegExp("http[s]?://"); var strEmailRegexp = "\\S+@\\S+\\.\\S+";
var reAll = new RegExp("(?:^|[ \\n\\t.,:\\/?!])(#|@|" + strUrlRegexp + "|" + strEmailRegexp + ")");
var reHttp = new RegExp(strUrlRegexp);
var reEmail = new RegExp(strEmailRegexp);
msg = escapeHtmlEntities(msg); msg = escapeHtmlEntities(msg);
@ -152,9 +155,10 @@ function htmlFormatMsg( msg, output, mentions ) {
match = reAll.exec(msg); match = reAll.exec(msg);
if( match ) { if( match ) {
if( match[0] == "@" ) { index = (match[0] === match[1]) ? match.index : match.index + 1;
output.append($.emotions(msg.substr(0, match.index))); if( match[1] == "@" ) {
tmp = msg.substr(match.index+1); output.append($.emotions(msg.substr(0, index)));
tmp = msg.substr(index+1);
var username = _extractUsername(tmp); var username = _extractUsername(tmp);
if( username.length ) { if( username.length ) {
if( mentions.indexOf(username) < 0 ) if( mentions.indexOf(username) < 0 )
@ -167,11 +171,14 @@ function htmlFormatMsg( msg, output, mentions ) {
msg = tmp.substr(String(username).length); msg = tmp.substr(String(username).length);
continue; continue;
} }
output.append('@');
msg = tmp;
continue;
} }
if( reHttp.exec(match[0]) ) { if( reHttp.exec(match[1]) ) {
output.append($.emotions(msg.substr(0, match.index))); output.append($.emotions(msg.substr(0, index)));
tmp = msg.substring(match.index); tmp = msg.substring(index);
var space = tmp.indexOf(" "); var space = tmp.indexOf(" ");
var url; var url;
if( space != -1 ) url = tmp.substring(0,space); else url = tmp; if( space != -1 ) url = tmp.substring(0,space); else url = tmp;
@ -179,17 +186,35 @@ function htmlFormatMsg( msg, output, mentions ) {
var extLinkTemplate = $("#external-page-link-template").clone(true); var extLinkTemplate = $("#external-page-link-template").clone(true);
extLinkTemplate.removeAttr("id"); extLinkTemplate.removeAttr("id");
extLinkTemplate.attr("href",url); extLinkTemplate.attr("href",url);
extLinkTemplate.text(url); extLinkTemplate.html(url);
extLinkTemplate.attr("title",url); extLinkTemplate.attr("title",url);
output.append(extLinkTemplate); output.append(extLinkTemplate);
msg = tmp.substr(String(url).length); msg = tmp.substr(String(url).length);
continue; continue;
} }
} }
if( reEmail.exec(match[1]) ) {
output.append($.emotions(msg.substr(0, index)));
tmp = msg.substring(index);
var space = tmp.indexOf(" ");
var email;
if( space != -1 ) email = tmp.substring(0,space); else email = tmp;
if( email.length ) {
var extLinkTemplate = $("#external-page-link-template").clone(true);
extLinkTemplate.removeAttr("id");
extLinkTemplate.attr("href","mailto:" + email);
extLinkTemplate.html(email);
extLinkTemplate.attr("title",email);
output.append(extLinkTemplate);
msg = tmp.substr(String(email).length);
continue;
}
}
if( match[0] == "#" ) { if( match[1] == "#" ) {
output.append($.emotions(msg.substr(0, match.index))); output.append($.emotions(msg.substr(0, index)));
tmp = msg.substr(match.index+1); tmp = msg.substr(index+1);
var hashtag = _extractHashtag(tmp); var hashtag = _extractHashtag(tmp);
if( hashtag.length ) { if( hashtag.length ) {
var hashtagLinkTemplate = $("#hashtag-link-template").clone(true); var hashtagLinkTemplate = $("#hashtag-link-template").clone(true);
@ -200,6 +225,9 @@ function htmlFormatMsg( msg, output, mentions ) {
msg = tmp.substr(String(hashtag).length); msg = tmp.substr(String(hashtag).length);
continue; continue;
} }
output.append('#');
msg = tmp;
continue;
} }
} }

Loading…
Cancel
Save