33require "twitter"
44require "json"
55require "uri"
6- require "html/pipeline "
7- require "html/pipeline/hashtag/hashtag_filter "
6+ require "html_pipeline "
7+ require "html_pipeline/node_filter/mention_filter "
88
99class Note < Content
1010 include PublifyGuid
@@ -33,37 +33,51 @@ class Note < Content
3333 TWITTER_HTTPS_URL_LENGTH = 21
3434 TWITTER_LINK_LENGTH = 22
3535
36- class TwitterHashtagFilter < HTML ::Pipeline ::HashtagFilter
37- def initialize ( text )
38- super ( text ,
39- tag_url : "https://twitter.com/search?q=%%23%<tag>s&src=tren&mode=realtime" ,
40- tag_link_attr : "" )
36+ class TwitterHashtagFilter < HTMLPipeline ::NodeFilter
37+ def after_initialize
38+ context [ :tag_url ] ||= "https://twitter.com/search?q=%%23%<tag>s&src=tren&mode=realtime"
4139 end
42- end
4340
44- class TwitterMentionFilter < HTML ::Pipeline ::MentionFilter
45- def initialize ( text )
46- super ( text , base_url : "https://twitter.com" )
41+ SELECTOR = Selma ::Selector . new ( match_text_within : "*" ,
42+ ignore_text_within : [ "a" ] )
43+
44+ def selector
45+ SELECTOR
4746 end
4847
49- # Override base mentions finder, treating @mention just like any other @foo.
50- def self . mentioned_logins_in ( text , username_pattern = UsernamePattern )
51- text . gsub MentionPatterns [ username_pattern ] do |match |
52- login = Regexp . last_match ( 1 )
53- yield match , login , false
48+ HASHTAG_PATTERN = /(?<=^|\W )#([-_A-Za-z0-9]+)(?=\W |$)/
49+
50+ def handle_text_chunk ( chunk )
51+ text = chunk . to_s
52+
53+ html = text . gsub ( HASHTAG_PATTERN ) do |match |
54+ tag = Regexp . last_match ( 1 )
55+ url = format context [ :tag_url ] , tag : tag
56+ "<a href=\" #{ url } \" >#{ match } </a>"
5457 end
58+
59+ return chunk if html == text
60+
61+ chunk . replace ( html , as : :html )
62+ end
63+ end
64+
65+ class TwitterMentionFilter < HTMLPipeline ::NodeFilter ::MentionFilter
66+ def after_initialize
67+ super
68+ context [ :base_url ] ||= "https://twitter.com"
69+ context [ :info_url ] ||= "https://foo.com"
5570 end
5671
5772 # Override base link creator, removing the class
58- def link_to_mentioned_user ( login )
73+ def link_to_mentioned_user ( base_url , login )
5974 result [ :mentioned_usernames ] |= [ login ]
6075
6176 url = base_url . dup
62- url << "/" unless %r{[/~]\z } . match? ( url )
77+ excluded_prefixes = %r{[/(?:~|@]\z }
78+ url << "/" unless excluded_prefixes . match? ( url )
6379
64- "<a href='#{ url << login } '>" \
65- "@#{ login } " \
66- "</a>"
80+ "<a href=\" #{ url << login } \" >@#{ login } </a>"
6781 end
6882 end
6983
@@ -82,7 +96,8 @@ def tags
8296
8397 def generate_html ( field , text = nil )
8498 if field == :in_reply_to
85- html = TextFilter . make_filter ( "none" ) . filter_text ( text )
99+ helper = PublifyCore ::ContentTextHelpers . new
100+ html = helper . simple_format ( text )
86101 html_postprocess ( field , html ) . to_s
87102 else
88103 super
@@ -93,8 +108,8 @@ def html_postprocess(field, html)
93108 helper = PublifyCore ::ContentTextHelpers . new
94109 html = helper . auto_link ( html )
95110
96- html = TwitterHashtagFilter . new ( html ) . call
97- html = TwitterMentionFilter . new ( html ) . call . to_s
111+ html = TwitterHashtagFilter . call ( html )
112+ html = TwitterMentionFilter . call ( html ) . to_s
98113 super
99114 end
100115
0 commit comments