ÿþ/ *   e n c o d i n g :   u t f - 8  
  
     * * * *     l i n k i f y   p l u g i n   f o r   j Q u e r y   -   a u t o m a t i c a l l y   f i n d s   a n d   c h a n g e s   U R L s   i n   t e x t   c o n t e n t   i n t o   p r o p e r   h y p e r l i n k s     * * * *  
  
     V e r s i o n :   1 . 0  
  
     C o p y r i g h t   ( c )   2 0 0 9  
         M á r   Ö r l y g s s o n     ( h t t p : / / m a r . a n o m y . n e t / )     &  
         H u g s m i ð j a n   e h f .   ( h t t p : / / w w w . h u g s m i d j a n . i s )  
  
     D u a l   l i c e n s e d   u n d e r   a   M I T   l i c e n c e   ( h t t p : / / e n . w i k i p e d i a . o r g / w i k i / M I T _ L i c e n s e )  
     a n d   G P L   2 . 0   o r   a b o v e   ( h t t p : / / w w w . g n u . o r g / l i c e n s e s / o l d - l i c e n s e s / g p l - 2 . 0 . h t m l ) .  
  
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     D e m o   a n d   Q u n i t - t e s t s :  
         *   < . / j q u e r y . l i n k i f y - 1 . 0 - d e m o . h t m l >  
         *   < . / j q u e r y . l i n k i f y - 1 . 0 - t e s t . h t m l >  
  
     D o c u m e n t a t i o n :  
         *   . . .  
  
     G e t   u p d a t e s   f r o m :  
         *   < h t t p : / / g i t h u b . c o m / m a r a n o m y n e t / l i n k i f y / >  
         *   < g i t : / / g i t h u b . c o m / m a r a n o m y n e t / l i n k i f y . g i t >  
  
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
  
     R e q u i r e s :  
         *   j Q u e r y   ( 1 . 2 . 6   o r   l a t e r )  
  
     U s a g e :  
  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( ) ;  
  
             / /   a d d i n g   p l u g i n s :  
             j Q u e r y . e x t e n d (   j Q u e r y . f n . l i n k i f y . p l u g i n s ,   {  
                     n a m e 1 :   {  
                             r e :       R e g E x p  
                             t m p l :   S t r i n g / F u n c t i o n  
                         } ,  
                     n a m e 2 :   f u n c t i o n ( h t m l ) {   r e t u r n   h t m l ;   }  
                 } ) ;  
  
             / /   U s e s   a l l   p l u g i n s   b y   d e f a u l t :  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( ) ;  
  
             / /   U s e   o n l y   c e r t a i n   p l u g i n s :  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y (   ' n a m e 1 , n a m e 2 '   ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   ' n a m e 1 , n a m e 2 '     } ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   [ ' n a m e 1 ' , ' n a m e 2 ' ]     } ) ;  
  
             / /   E x p l i c i t l y   u s e   a l l   p l u g i n s :  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( ' * ' ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   ' * '     } ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   [ ' * ' ]     } ) ;  
  
             / /   U s e   n o   p l u g i n s :  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( ' ' ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   ' '     } ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   [ ]     } ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     u s e :   [ ' ' ]     } ) ;  
  
             / /   P e r f m o r m   a c t i o n s   o n   a l l   n e w l y   c r e a t e d   l i n k s :  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y (   f u n c t i o n   ( l i n k s ) {   l i n k s . a d d C l a s s ( ' l i n k i f i e d ' ) ;   }   ) ;  
             j Q u e r y ( ' . a r t i c l e b o d y ' ) . l i n k i f y ( {     h a n d l e L i n k s :   f u n c t i o n   ( l i n k s ) {   l i n k s . a d d C l a s s ( ' l i n k i f i e d ' ) ;   }     } ) ;  
  
 * /  
  
 ( f u n c t i o n ( $ ) {  
  
     v a r   n o P r o t o c o l U r l   =   / ( ^ | [ " ' ( \ s ] | & l t ; ) ( w w w \ . . + ? \ . . + ? ) ( ( ? : [ : ? ] | \ . + ) ? ( ? : \ s | $ ) | & g t ; | [ ) " ' , ] ) / g ,  
             h t t p O r M a i l t o U r l   =   / ( ^ | [ " ' ( \ s ] | & l t ; ) ( ( ? : ( ? : h t t p s ? | f t p ) : \ / \ / | m a i l t o : ) . + ? ) ( ( ? : [ : ? ] | \ . + ) ? ( ? : \ s | $ ) | & g t ; | [ ) " ' , ] ) / g ,  
             l i n k i f i e r   =   f u n c t i o n   (   h t m l   )   {  
                     r e t u r n   h t m l  
                                             . r e p l a c e (   n o P r o t o c o l U r l ,   ' $ 1 < a   h r e f = " < ` ` > : / / $ 2 " > $ 2 < / a > $ 3 '   )     / /   N O T E :   w e   e s c a p e   ` " h t t p `   a s   ` " < ` ` > `   t o   m a k e   s u r e   ` h t t p O r M a i l t o U r l `   b e l o w   d o e s n ' t   f i n d   i t   a s   a   f a l s e - p o s i t i v e  
                                             . r e p l a c e (   h t t p O r M a i l t o U r l ,   ' $ 1 < a   h r e f = " $ 2 " > $ 2 < / a > $ 3 '   )  
                                             . r e p l a c e (   / " < ` ` > / g ,   ' " h t t p '   ) ;     / /   r e i n s e r t   ` " h t t p `  
                 } ,  
  
  
             l i n k i f y   =   $ . f n . l i n k i f y   =   f u n c t i o n   (   c f g   )   {  
                     i f   (   ! $ . i s P l a i n O b j e c t (   c f g   )   )  
                     {  
                         c f g   =   {  
                                 u s e :                   ( t y p e o f   c f g   = =   ' s t r i n g ' )   ?   c f g   :   u n d e f i n e d ,  
                                 h a n d l e L i n k s :   $ . i s F u n c t i o n ( c f g )   ?   c f g   :   a r g u m e n t s [ 1 ]  
                             } ;  
                     }  
                     v a r   u s e   =   c f g . u s e ,  
                             a l l P l u g i n s   =   l i n k i f y . p l u g i n s   | |   { } ,  
                             p l u g i n s   =   [ l i n k i f i e r ] ,  
                             t m p C o n t ,  
                             n e w L i n k s   =   [ ] ,  
                             c a l l b a c k   =   c f g . h a n d l e L i n k s ;  
                     i f   (   u s e   = =   u n d e f i n e d   | |     u s e   = =   ' * '   )   / /   u s e   = = =   u n d e f i n e d     | |     u s e   = = =   n u l l  
                     {  
                         f o r   (   v a r   n a m e   i n   a l l P l u g i n s   )  
                         {  
                             p l u g i n s . p u s h (   a l l P l u g i n s [ n a m e ]   ) ;  
                         }  
                     }  
                     e l s e  
                     {  
                         u s e   =   $ . i s A r r a y (   u s e   )   ?   u s e   :   $ . t r i m ( u s e ) . s p l i t (   /   * ,   * /   ) ;  
                         v a r   p l u g i n ,  
                                 n a m e ;  
                         f o r   (   v a r   i = 0 ,   l = u s e . l e n g t h ;     i < l ;     i + +   )  
                         {  
                             n a m e   =   u s e [ i ] ;  
                             p l u g i n   =   a l l P l u g i n s [ n a m e ] ;  
                             i f   (   p l u g i n   )  
                             {  
                                 p l u g i n s . p u s h (   p l u g i n   ) ;  
                             }  
                         }  
                     }  
  
                     t h i s . e a c h ( f u n c t i o n   ( )   {  
                             v a r   c h i l d N o d e s   =   t h i s . c h i l d N o d e s ,  
                                     i   =   c h i l d N o d e s . l e n g t h ;  
                             w h i l e   (   i - -   )  
                             {  
                                 v a r   n   =   c h i l d N o d e s [ i ] ;  
                                 i f   (   n . n o d e T y p e   = =   3   )  
                                 {  
                                     v a r   h t m l   =   n . n o d e V a l u e ;  
                                     i f   (   h t m l . l e n g t h > 1     & &     / \ S / . t e s t ( h t m l )   )  
                                     {  
                                         v a r   h t m l C h a n g e d ,  
                                                 p r e H t m l ;  
                                         t m p C o n t   =   t m p C o n t   | |   $ ( ' < d i v / > ' ) [ 0 ] ;  
                                         t m p C o n t . i n n e r H T M L   =   ' ' ;  
                                         t m p C o n t . a p p e n d C h i l d (   n . c l o n e N o d e ( f a l s e )   ) ;  
                                         v a r   t m p C o n t N o d e s   =   t m p C o n t . c h i l d N o d e s ;  
  
                                         f o r   ( v a r   j = 0 ,   p l u g i n ;   ( p l u g i n   =   p l u g i n s [ j ] ) ;   j + + )  
                                         {  
                                             v a r   k   =   t m p C o n t N o d e s . l e n g t h ,  
                                                     t m p N o d e ;  
                                             w h i l e   (   k - -   )  
                                             {  
                                                 t m p N o d e   =   t m p C o n t N o d e s [ k ] ;  
                                                 i f   (   t m p N o d e . n o d e T y p e   = =   3   )  
                                                 {  
                                                     h t m l   =   t m p N o d e . n o d e V a l u e ;  
                                                     i f   (   h t m l . l e n g t h > 1     & &     / \ S / . t e s t ( h t m l )   )  
                                                     {  
                                                         p r e H t m l   =   h t m l ;  
                                                         h t m l   =   h t m l  
                                                                             . r e p l a c e (   / & / g ,   ' & a m p ; '   )  
                                                                             . r e p l a c e (   / < / g ,   ' & l t ; '   )  
                                                                             . r e p l a c e (   / > / g ,   ' & g t ; '   ) ;  
                                                         h t m l   =   $ . i s F u n c t i o n (   p l u g i n   )   ?    
                                                                                 p l u g i n (   h t m l   ) :  
                                                                                 h t m l . r e p l a c e (   p l u g i n . r e ,   p l u g i n . t m p l   ) ;  
                                                         h t m l C h a n g e d   =   h t m l C h a n g e d   | |   p r e H t m l ! = h t m l ;  
                                                         p r e H t m l ! = h t m l     & &     $ ( t m p N o d e ) . a f t e r ( h t m l ) . r e m o v e ( ) ;  
                                                     }  
                                                 }  
                                             }  
                                         }  
                                         h t m l   =   t m p C o n t . i n n e r H T M L ;  
                                         i f   (   c a l l b a c k   )  
                                         {  
                                             h t m l   =   $ ( ' < d i v / > ' ) . h t m l ( h t m l ) ;  
                                             / / n e w L i n k s . p u s h . a p p l y (   n e w L i n k s ,     h t m l . f i n d ( ' a ' ) . t o A r r a y ( )   ) ;  
                                             n e w L i n k s   =   n e w L i n k s . c o n c a t (   h t m l . f i n d ( ' a ' ) . t o A r r a y ( ) . r e v e r s e ( )   ) ;  
                                             h t m l   =   h t m l . c o n t e n t s ( ) ;  
                                         }  
                                         h t m l C h a n g e d     & &     $ ( n ) . a f t e r ( h t m l ) . r e m o v e ( ) ;  
                                     }  
                                 }  
                                 e l s e   i f   (   n . n o d e T y p e   = =   1     & &     ! / ^ ( a | b u t t o n | t e x t a r e a ) $ / i . t e s t ( n . t a g N a m e )   )  
                                 {  
                                     a r g u m e n t s . c a l l e e . c a l l (   n   ) ;  
                                 }  
                             } ;  
                     } ) ;  
                     c a l l b a c k     & &     c a l l b a c k (   $ ( n e w L i n k s . r e v e r s e ( ) )   ) ;  
                     r e t u r n   t h i s ;  
                 } ;  
  
     l i n k i f y . p l u g i n s   =   {  
             / /   d e f a u l t   m a i l t o :   p l u g i n  
             m a i l t o :   {  
                     r e :   / ( ^ | [ " ' ( \ s ] | & l t ; ) ( [ ^ " ' ( \ s & ] + ? @ . + \ . [ a - z ] { 2 , 7 } ) ( ( [ : ? ] | \ . + ) ? ( \ s | $ ) | & g t ; | [ ) " ' , ] ) / g i ,  
                     t m p l :   ' $ 1 < a   h r e f = " m a i l t o : $ 2 " > $ 2 < / a > $ 3 '  
                 }  
         } ;  
  
 } ) ( j Q u e r y ) ; 
