Skip to content

Commit d7497a4

Browse files
authored
Merge pull request #210 from publify/refactor-notes-rendering
Refactor notes rendering
2 parents 838e72f + 0c2f2b4 commit d7497a4

11 files changed

Lines changed: 91 additions & 128 deletions

File tree

Manifest.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ app/views/notes/_note.html.erb
247247
app/views/notes/error.html.erb
248248
app/views/notes/index.html.erb
249249
app/views/notes/show.html.erb
250-
app/views/notes/show_in_reply.html.erb
251250
app/views/notification_mailer/_mail_footer.html.erb
252251
app/views/notification_mailer/_mail_header.html.erb
253252
app/views/notification_mailer/article.html.erb
@@ -386,7 +385,6 @@ lib/publify_core/text_filter/markdown.rb
386385
lib/publify_core/text_filter/markdown_smartquotes.rb
387386
lib/publify_core/text_filter/none.rb
388387
lib/publify_core/text_filter/smartypants.rb
389-
lib/publify_core/text_filter/twitterfilter.rb
390388
lib/publify_core/version.rb
391389
lib/publify_guid.rb
392390
lib/publify_plugins.rb

app/controllers/notes_controller.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ def index
1919
def show
2020
@note = Note.published.find_by! permalink: CGI.escape(params[:permalink])
2121

22-
if @note.in_reply_to_message.present?
23-
@reply = JSON.parse(@note.in_reply_to_message)
24-
render :show_in_reply
25-
end
22+
@reply = JSON.parse(@note.in_reply_to_message) if @note.in_reply_to_message.present?
2623
end
2724

2825
private

app/models/note.rb

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# frozen_string_literal: true
22

3+
require "twitter"
4+
require "json"
5+
require "uri"
6+
require "html/pipeline"
7+
require "html/pipeline/hashtag/hashtag_filter"
8+
39
class Note < Content
4-
require "twitter"
5-
require "json"
6-
require "uri"
710
include PublifyGuid
811
include ConfigManager
912

@@ -30,6 +33,40 @@ class Note < Content
3033
TWITTER_HTTPS_URL_LENGTH = 21
3134
TWITTER_LINK_LENGTH = 22
3235

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: "")
41+
end
42+
end
43+
44+
class TwitterMentionFilter < HTML::Pipeline::MentionFilter
45+
def initialize(text)
46+
super(text, base_url: "https://twitter.com")
47+
end
48+
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
54+
end
55+
end
56+
57+
# Override base link creator, removing the class
58+
def link_to_mentioned_user(login)
59+
result[:mentioned_usernames] |= [login]
60+
61+
url = base_url.dup
62+
url << "/" unless %r{[/~]\z}.match?(url)
63+
64+
"<a href='#{url << login}'>" \
65+
"@#{login}" \
66+
"</a>"
67+
end
68+
end
69+
3370
def set_permalink
3471
self.permalink = "#{id}-#{body.to_permalink[0..79]}" if permalink.blank?
3572
save
@@ -43,8 +80,22 @@ def tags
4380
[]
4481
end
4582

83+
def generate_html(field, text = nil)
84+
if field == :in_reply_to
85+
html = TextFilter.make_filter("none").filter_text(text)
86+
html_postprocess(field, html).to_s
87+
else
88+
super
89+
end
90+
end
91+
4692
def html_postprocess(field, html)
47-
super(field, PublifyCore::TextFilter::Twitterfilter.filtertext(html))
93+
helper = PublifyCore::ContentTextHelpers.new
94+
html = helper.auto_link(html)
95+
96+
html = TwitterHashtagFilter.new(html).call
97+
html = TwitterMentionFilter.new(html).call.to_s
98+
super
4899
end
49100

50101
def truncate(message, length)

app/views/notes/show.html.erb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<div class="hfeed">
22
<div class='h-entry hentry h-as-note'>
3+
<% if @reply %>
4+
<article class=' well well-small'>
5+
<% if @reply['user']['profile_image_url'] %>
6+
<%= image_tag(@reply['user']['profile_image_url'], class: 'alignleft', alt: @reply['user']['name']) %>
7+
<% end %>
8+
<strong><%= get_reply_context_url(@reply) %></strong>
9+
<p><%= nofollowify_links(@note.generate_html(:in_reply_to, @reply['text'])) %></p>
10+
<p>
11+
<small>
12+
<%= get_reply_context_twitter_link(@reply) %>
13+
</small>
14+
</p>
15+
</article>
16+
<% end %>
17+
318
<%= render partial: 'note', object: @note %>
419
</div>
520
</div>

app/views/notes/show_in_reply.html.erb

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/publify_core.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
require "publify_core/text_filter/markdown"
1313
require "publify_core/text_filter/markdown_smartquotes"
1414
require "publify_core/text_filter/smartypants"
15-
require "publify_core/text_filter/twitterfilter"
1615
require "publify_core/string_ext"
1716

1817
require "bootstrap"

lib/publify_core/text_filter/twitterfilter.rb

Lines changed: 0 additions & 55 deletions
This file was deleted.

spec/controllers/notes_controller_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@
3636
end
3737

3838
context "in reply" do
39+
render_views
40+
3941
let(:reply) do
4042
{
4143
"id_str" => "123456789",
4244
"created_at" => DateTime.new(2014, 1, 23, 13, 47).in_time_zone,
45+
"text" => "**original** #foo",
4346
"user" => {
4447
"screen_name" => "a screen name",
4548
"entities" => {
@@ -57,9 +60,13 @@
5760

5861
before { get :show, params: { permalink: permalink } }
5962

60-
it { expect(response).to be_successful }
61-
it { expect(response).to render_template("show_in_reply") }
62-
it { expect(assigns[:page_title]).to eq("Notes | test blog ") }
63+
it "successfully renders the tweet the note replies to without extra filters" do
64+
aggregate_failures do
65+
expect(response).to be_successful
66+
expect(response.body).to have_text "**original** #foo"
67+
expect(response.body).to have_link "#foo"
68+
end
69+
end
6370
end
6471

6572
context "note not found" do

spec/lib/text_filter_plugin_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
PublifyCore::TextFilter::None,
1010
PublifyCore::TextFilter::Markdown,
1111
PublifyCore::TextFilter::Smartypants,
12-
PublifyCore::TextFilter::MarkdownSmartquotes,
13-
PublifyCore::TextFilter::Twitterfilter)
12+
PublifyCore::TextFilter::MarkdownSmartquotes)
1413
end
1514
end
1615

spec/models/note_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@
294294
expect(note.html).to eq(expected)
295295
end
296296

297+
it "works with a hashtag and a mention" do
298+
note = create(:note, body: "A test tweet with a #hashtag and a @mention")
299+
expected =
300+
"<p>A test tweet with a <a href=\"https://twitter.com/search?q=%23hashtag" \
301+
"&amp;src=tren&amp;mode=realtime\">#hashtag</a> and a" \
302+
" <a href=\"https://twitter.com/mention\">@mention</a></p>"
303+
expect(note.html).to eq expected
304+
end
305+
297306
it "links markdown links only once" do
298307
note = create(:note, body: "A test tweet with [a markdown link](https://link.com)")
299308
expect(note.html)

0 commit comments

Comments
 (0)