Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "twitter"
require "json"
require "uri"
require "html/pipeline"
require "html/pipeline/hashtag/hashtag_filter"
require "html_pipeline"
require "html_pipeline/node_filter/mention_filter"

class Note < Content
include PublifyGuid
Expand Down Expand Up @@ -33,37 +33,51 @@ class Note < Content
TWITTER_HTTPS_URL_LENGTH = 21
TWITTER_LINK_LENGTH = 22

class TwitterHashtagFilter < HTML::Pipeline::HashtagFilter
def initialize(text)
super(text,
tag_url: "https://twitter.com/search?q=%%23%<tag>s&src=tren&mode=realtime",
tag_link_attr: "")
class TwitterHashtagFilter < HTMLPipeline::NodeFilter
def after_initialize
context[:tag_url] ||= "https://twitter.com/search?q=%%23%<tag>s&src=tren&mode=realtime"
end
end

class TwitterMentionFilter < HTML::Pipeline::MentionFilter
def initialize(text)
super(text, base_url: "https://twitter.com")
SELECTOR = Selma::Selector.new(match_text_within: "*",
ignore_text_within: ["a"])

def selector
SELECTOR
end

# Override base mentions finder, treating @mention just like any other @foo.
def self.mentioned_logins_in(text, username_pattern = UsernamePattern)
text.gsub MentionPatterns[username_pattern] do |match|
login = Regexp.last_match(1)
yield match, login, false
HASHTAG_PATTERN = /(?<=^|\W)#([-_A-Za-z0-9]+)(?=\W|$)/

def handle_text_chunk(chunk)
text = chunk.to_s

html = text.gsub(HASHTAG_PATTERN) do |match|
tag = Regexp.last_match(1)
url = format context[:tag_url], tag: tag
"<a href=\"#{url}\">#{match}</a>"
end

return chunk if html == text

chunk.replace(html, as: :html)
end
end

class TwitterMentionFilter < HTMLPipeline::NodeFilter::MentionFilter
def after_initialize
super
context[:base_url] ||= "https://twitter.com"
context[:info_url] ||= "https://foo.com"
end

# Override base link creator, removing the class
def link_to_mentioned_user(login)
def link_to_mentioned_user(base_url, login)
result[:mentioned_usernames] |= [login]

url = base_url.dup
url << "/" unless %r{[/~]\z}.match?(url)
excluded_prefixes = %r{[/(?:~|@]\z}
url << "/" unless excluded_prefixes.match?(url)

"<a href='#{url << login}'>" \
"@#{login}" \
"</a>"
"<a href=\"#{url << login}\">@#{login}</a>"
end
end

Expand All @@ -82,7 +96,8 @@ def tags

def generate_html(field, text = nil)
if field == :in_reply_to
html = TextFilter.make_filter("none").filter_text(text)
helper = PublifyCore::ContentTextHelpers.new
html = helper.simple_format(text)
html_postprocess(field, html).to_s
else
super
Expand All @@ -93,8 +108,8 @@ def html_postprocess(field, html)
helper = PublifyCore::ContentTextHelpers.new
html = helper.auto_link(html)

html = TwitterHashtagFilter.new(html).call
html = TwitterMentionFilter.new(html).call.to_s
html = TwitterHashtagFilter.call(html)
html = TwitterMentionFilter.call(html).to_s
super
end

Expand Down
3 changes: 1 addition & 2 deletions publify_core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ Gem::Specification.new do |s|
s.add_dependency "devise_zxcvbn", "~> 6.0"
s.add_dependency "fog-aws", "~> 3.2"
s.add_dependency "fog-core", "~> 2.2"
s.add_dependency "html-pipeline", "~> 2.14"
s.add_dependency "html-pipeline-hashtag", "~> 0.1.2"
s.add_dependency "html-pipeline", "~> 3.2"
s.add_dependency "jquery-rails", ">= 4.5", "< 4.7"
s.add_dependency "jquery-ui-rails", ">= 7", "< 9"
s.add_dependency "kaminari", ["~> 1.2", ">= 1.2.1"]
Expand Down