Eliminate title cacheing for the time being.

Causing too many problems.
Also attempt to optimize Linkify.
This commit is contained in:
Zixaphir 2014-03-20 09:03:56 -07:00 committed by ccd0
parent 83cfa3fea9
commit af62b44df1

View File

@ -15,28 +15,26 @@ Linkify =
name: 'Linkify' name: 'Linkify'
cb: @node cb: @node
events: (post) ->
i = 0
items = $$ '.embedder', post.nodes.comment
while el = items[i++]
$.on el, 'click', Linkify.cb.toggle
Linkify.cb.toggle.call el if $.hasClass el, 'embedded'
return
node: -> node: ->
if @isClone return (if Conf['Embedding'] then Linkify.events @ else null) if @isClone
if Conf['Embedding']
i = 0
items = $$ '.embedder', @nodes.comment
while el = items[i++]
$.on el, 'click', Linkify.cb.toggle
Linkify.cb.toggle.call el if $.hasClass el, 'embedded'
return
return unless Linkify.regString.test @info.comment return unless Linkify.regString.test @info.comment
test = /[^\s'"]+/g test = /[^\s'"]+/g
space = /[\s'"]/ space = /[\s'"]/
snapshot = $.X './/br|.//text()', @nodes.comment snapshot = $.X './/br|.//text()', @nodes.comment
i = 0 i = 0
links = [] links = []
while node = snapshot.snapshotItem i++ while node = snapshot.snapshotItem i++
{data} = node {data} = node
continue if node.parentElement.nodeName is "A" or not data continue if !data or node.parentElement.nodeName is "A"
while result = test.exec data while result = test.exec data
{index} = result {index} = result
@ -61,41 +59,33 @@ Linkify =
i-- i--
break break
if Linkify.regString.exec word links.push Linkify.makeRange node, endNode, index, length if Linkify.regString.exec word
links.push Linkify.makeRange node, endNode, index, length
break unless test.lastIndex and node is endNode break unless test.lastIndex and node is endNode
for link in links.reverse() i = links.length
@nodes.links.push Linkify.makeLink link, @ while i--
link.detach() link = links[i]
Linkify.embedProcess Linkify.makeLink link, @
return unless Conf['Embedding'] or Conf['Link Title']
{links} = @nodes
i = 0
while link = links[i++]
if data = Linkify.services link
Linkify.embed data if Conf['Embedding']
Linkify.title data if Conf['Link Title']
return return
embedProcess: (link) ->
if data = Linkify.services link
Linkify.embed data if Conf['Embedding']
Linkify.title data if Conf['Link Title']
regString: ///( regString: ///(
# http, magnet, ftp, etc # http, magnet, ftp, etc
(https?|mailto|git|magnet|ftp|irc):( (https?|mailto|git|magnet|ftp|irc):(
[a-z\d%/] [a-z\d%/]
) )
| | # This should account for virtually all links posted without http:
# This should account for virtually all links posted without http:
[-a-z\d]+[.]( [-a-z\d]+[.](
aero|asia|biz|cat|com|coop|info|int|jobs|mobi|museum|name|net|org|post|pro|tel|travel|xxx|edu|gov|mil|[a-z]{2} aero|asia|biz|cat|com|coop|info|int|jobs|mobi|museum|name|net|org|post|pro|tel|travel|xxx|edu|gov|mil|[a-z]{2}
)(/|(?!.)) )(/|(?!.))
| | # IPv4 Addresses
# IPv4 Addresses
[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3} [\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}
| | # E-mails
# E-mails
[-\w\d.@]+@[a-z\d.-]+\.[a-z\d] [-\w\d.@]+@[a-z\d.-]+\.[a-z\d]
)///i )///i
@ -108,7 +98,7 @@ Linkify =
makeLink: (range) -> makeLink: (range) ->
text = range.toString() text = range.toString()
# Clean leading brackets, > # Clean start of range
i = 0 i = 0
i++ while /[(\[{<>]/.test text.charAt i i++ while /[(\[{<>]/.test text.charAt i
@ -118,7 +108,7 @@ Linkify =
range.setStart range.startContainer, range.startOffset + i if i range.setStart range.startContainer, range.startOffset + i if i
# Clean hanging brackets, commas, periods # Clean end of range
i = 0 i = 0
while /[)\]}>.,]/.test char = text.charAt text.length - (1 + i) while /[)\]}>.,]/.test char = text.charAt text.length - (1 + i)
break unless /[.,]/.test(char) or (text.match /[()\[\]{}<>]/g).length % 2 break unless /[.,]/.test(char) or (text.match /[()\[\]{}<>]/g).length % 2
@ -131,6 +121,7 @@ Linkify =
if i if i
range.setEnd range.endContainer, range.endOffset - i range.setEnd range.endContainer, range.endOffset - i
# Make our link 'valid' if it is formatted incorrectly.
unless /(https?|mailto|git|magnet|ftp|irc):/.test text unless /(https?|mailto|git|magnet|ftp|irc):/.test text
text = ( text = (
if /@/.test text if /@/.test text
@ -144,18 +135,19 @@ Linkify =
rel: 'nofollow noreferrer' rel: 'nofollow noreferrer'
target: '_blank' target: '_blank'
href: text href: text
# Insert the range into the anchor, the anchor into the range's DOM location, and destroy the range.
$.add a, range.extractContents() $.add a, range.extractContents()
range.insertNode a range.insertNode a
range.detach()
a a
services: (link) -> services: (link) ->
href = link.href {href} = link
for type in Linkify.ordered_types when match = type.regExp.exec href
for type in Linkify.ordered_types return if type.dummy
continue unless match = type.regExp.exec href
break if type.dummy
return [type.key, match[1], match[2], link] return [type.key, match[1], match[2], link]
return return
embed: (data) -> embed: (data) ->
@ -166,9 +158,7 @@ Linkify =
href: 'javascript:;' href: 'javascript:;'
textContent: '(embed)' textContent: '(embed)'
for name, value of {key, href, uid, options} embed.dataset[name] = value for name, value of {key, href, uid, options}
embed.dataset[name] = value
embed.dataset.nodedata = link.innerHTML embed.dataset.nodedata = link.innerHTML
$.addClass link, "#{embed.dataset.key}" $.addClass link, "#{embed.dataset.key}"
@ -176,13 +166,10 @@ Linkify =
$.on embed, 'click', Linkify.cb.toggle $.on embed, 'click', Linkify.cb.toggle
$.after link, [$.tn(' '), embed] $.after link, [$.tn(' '), embed]
if Conf['Auto-embed'] Linkify.cb.toggle.call embed if Conf['Auto-embed']
Linkify.cb.toggle.call embed
data.push embed data.push embed
return
title: (data) -> title: (data) ->
[key, uid, options, link, embed] = data [key, uid, options, link, embed] = data
return unless service = Linkify.types[key].title return unless service = Linkify.types[key].title
@ -195,20 +182,11 @@ Linkify =
embed.dataset.title = title[0] embed.dataset.title = title[0]
else else
try try
$.cache service.api(uid), $.cache service.api(uid), (-> Linkify.cb.title @, data), responseType: 'json'
-> title = Linkify.cb.title @, data
,
responseType: 'json'
catch err catch err
if link if link
link.innerHTML = "[#{key}] <span class=warning>Title Link Blocked</span> (are you using NoScript?)</a>" link.innerHTML = "[#{key}] <span class=warning>Title Link Blocked</span> (are you using NoScript?)</a>"
return return
if title
titles[uid] = [title, Date.now()]
$.set 'CachedTitles', titles
titleSync: (value) ->
Conf['CachedTitles'] = value
cb: cb:
toggle: -> toggle: ->
@ -247,21 +225,24 @@ Linkify =
return el return el
title: (response, data) -> title: (req, data) ->
[key, uid, options, link, embed] = data [key, uid, options, link, embed] = data
{status} = req
service = Linkify.types[key].title service = Linkify.types[key].title
switch response.status
text = "[#{key}] #{switch status
when 200, 304 when 200, 304
text = "#{service.text response.response}" service.text req.response
if Conf['Embedding']
embed.dataset.title = text
when 404 when 404
text = "[#{key}] Not Found" "Not Found"
when 403 when 403
text = "[#{key}] Forbidden or Private" "Forbidden or Private"
else else
text = "[#{key}] #{@status}'d" "#{status}'d"
link.textContent = text if link }"
embed.dataset.title = text if Conf['Embedding'] and status in [200, 304]
link.textContent = text if link
ordered_types: [ ordered_types: [
key: 'audio' key: 'audio'