Created
February 19, 2014 04:28
-
-
Save knubie/9086078 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regexps = | |
code: regex.code | |
url: regex.url | |
channel: regex.channel | |
parse = (str) -> | |
strings = [{text: str}] | |
for name, regexp of regexps | |
for string, i in strings | |
if not string.token? and matches = string.text.match regexp | |
results = [] | |
start = 0 | |
for match in matches | |
matchIndex = string.text.indexOf match | |
first = string.text.slice start, matchIndex | |
second = string.text.slice matchIndex, (matchIndex + match.length) | |
third = string.text.slice (matchIndex + match.length) | |
results.pop() | |
results.push {text: first} | |
results.push {text: second, token: name} | |
results.push {text: third} if third.length > 0 | |
start = matchIndex + match.length | |
strings[i] = results | |
strings = _.flatten strings | |
return strings | |
# Get message text. | |
p = $(@find('p')) | |
message = p.html() | |
ptext = for string in parse(message) | |
switch string.token | |
when 'code' | |
string.text.replace(/`([^`]*)`/g, "<code>$1</code>") | |
when 'url' | |
string.text.replace regexps.url, (str) -> | |
youtubeMatch = str.match regex.youtube | |
if Meteor.user().profile.inlineMedia and str.match /\.(?:jpe?g|gif|png)/i | |
""" | |
<a href="#{str}" target="_blank"> | |
<img onload="scrollToPlace();" src="#{str}" alt=""/> | |
</a> | |
""" | |
else if youtubeMatch and | |
youtubeMatch[1].length is 11 and | |
Meteor.user().profile.inlineMedia | |
"<iframe src=\"//www.youtube.com/embed/#{youtubeMatch[1]}\" frameborder=\"0\" allowfullscreen></iframe>" | |
else # All other links | |
"<a href=\"#{str}\" target=\"_blank\">#{str}</a>" | |
when 'channel' | |
string.text.replace(/#(\d*[a-zA-Z_]+)/g, '<a href="/channels/$1">#$1</a>') | |
else | |
if @data.channel? | |
for nick of Channels.findOne(name: @data.channel).nicks | |
string.text = string.text.replace regex.nick(nick), "$1<a href=\"/users/$2\">$2</a>$3" | |
string.text.replace(/\*\*([^\*]*)\*\*/g, "<strong>$1</strong>") | |
.replace(/\*([^\*]*)\*/g, "<em>$1</em>") | |
.replace(/_([^_]*)_/g, '<span class="underline">$1</span>') | |
p.html(ptext.join('')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment