mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 02:51:14 +08:00
DEV: Apply syntax_tree formatting to plugins/*
This commit is contained in:
@ -7,40 +7,40 @@
|
||||
|
||||
hide_plugin if self.respond_to?(:hide_plugin)
|
||||
|
||||
register_asset 'stylesheets/common/discourse-local-dates.scss'
|
||||
register_asset 'moment.js', :vendored_core_pretty_text
|
||||
register_asset 'moment-timezone.js', :vendored_core_pretty_text
|
||||
register_asset "stylesheets/common/discourse-local-dates.scss"
|
||||
register_asset "moment.js", :vendored_core_pretty_text
|
||||
register_asset "moment-timezone.js", :vendored_core_pretty_text
|
||||
|
||||
enabled_site_setting :discourse_local_dates_enabled
|
||||
|
||||
after_initialize do
|
||||
module ::DiscourseLocalDates
|
||||
PLUGIN_NAME ||= 'discourse-local-dates'.freeze
|
||||
POST_CUSTOM_FIELD ||= 'local_dates'.freeze
|
||||
PLUGIN_NAME ||= "discourse-local-dates".freeze
|
||||
POST_CUSTOM_FIELD ||= "local_dates".freeze
|
||||
end
|
||||
|
||||
%w[../lib/discourse_local_dates/engine.rb].each do |path|
|
||||
load File.expand_path(path, __FILE__)
|
||||
end
|
||||
%w[../lib/discourse_local_dates/engine.rb].each { |path| load File.expand_path(path, __FILE__) }
|
||||
|
||||
register_post_custom_field_type(DiscourseLocalDates::POST_CUSTOM_FIELD, :json)
|
||||
|
||||
on(:before_post_process_cooked) do |doc, post|
|
||||
dates = []
|
||||
|
||||
doc.css('span.discourse-local-date').map do |cooked_date|
|
||||
next if cooked_date.ancestors("aside").length > 0
|
||||
date = {}
|
||||
cooked_date.attributes.values.each do |attribute|
|
||||
data_name = attribute.name&.gsub('data-', '')
|
||||
if data_name && %w[date time timezone recurring].include?(data_name)
|
||||
unless attribute.value == 'undefined'
|
||||
date[data_name] = CGI.escapeHTML(attribute.value || '')
|
||||
doc
|
||||
.css("span.discourse-local-date")
|
||||
.map do |cooked_date|
|
||||
next if cooked_date.ancestors("aside").length > 0
|
||||
date = {}
|
||||
cooked_date.attributes.values.each do |attribute|
|
||||
data_name = attribute.name&.gsub("data-", "")
|
||||
if data_name && %w[date time timezone recurring].include?(data_name)
|
||||
unless attribute.value == "undefined"
|
||||
date[data_name] = CGI.escapeHTML(attribute.value || "")
|
||||
end
|
||||
end
|
||||
end
|
||||
dates << date
|
||||
end
|
||||
dates << date
|
||||
end
|
||||
|
||||
if dates.present?
|
||||
post.custom_fields[DiscourseLocalDates::POST_CUSTOM_FIELD] = dates
|
||||
@ -51,22 +51,22 @@ after_initialize do
|
||||
end
|
||||
end
|
||||
|
||||
add_to_class(:post, :local_dates) do
|
||||
custom_fields[DiscourseLocalDates::POST_CUSTOM_FIELD] || []
|
||||
end
|
||||
add_to_class(:post, :local_dates) { custom_fields[DiscourseLocalDates::POST_CUSTOM_FIELD] || [] }
|
||||
|
||||
on(:reduce_excerpt) do |fragment, post|
|
||||
fragment.css('.discourse-local-date').each do |container|
|
||||
container.content = "#{container.content} (UTC)"
|
||||
end
|
||||
fragment
|
||||
.css(".discourse-local-date")
|
||||
.each { |container| container.content = "#{container.content} (UTC)" }
|
||||
end
|
||||
|
||||
on(:reduce_cooked) do |fragment|
|
||||
fragment.css('.discourse-local-date').each do |container|
|
||||
if container.attributes['data-email-preview']
|
||||
preview = container.attributes['data-email-preview'].value
|
||||
container.content = preview
|
||||
fragment
|
||||
.css(".discourse-local-date")
|
||||
.each do |container|
|
||||
if container.attributes["data-email-preview"]
|
||||
preview = container.attributes["data-email-preview"].value
|
||||
container.content = preview
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,9 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe "Local Dates" do
|
||||
before do
|
||||
freeze_time DateTime.parse('2018-11-10 12:00')
|
||||
end
|
||||
before { freeze_time DateTime.parse("2018-11-10 12:00") }
|
||||
|
||||
it "should work without timezone" do
|
||||
post = Fabricate(:post, raw: <<~MD)
|
||||
@ -15,14 +13,12 @@ RSpec.describe "Local Dates" do
|
||||
expect(cooked).to include('class="discourse-local-date"')
|
||||
expect(cooked).to include('data-date="2018-05-08"')
|
||||
expect(cooked).to include('data-format="L LTS"')
|
||||
expect(cooked).not_to include('data-timezone=')
|
||||
expect(cooked).not_to include("data-timezone=")
|
||||
|
||||
expect(cooked).to include(
|
||||
'data-timezones="Europe/Paris|America/Los_Angeles"'
|
||||
)
|
||||
expect(cooked).to include('data-timezones="Europe/Paris|America/Los_Angeles"')
|
||||
|
||||
expect(cooked).to include('data-email-preview="2018-05-08T22:00:00Z UTC"')
|
||||
expect(cooked).to include('05/08/2018 10:00:00 PM')
|
||||
expect(cooked).to include("05/08/2018 10:00:00 PM")
|
||||
end
|
||||
|
||||
it "should work with timezone" do
|
||||
@ -33,10 +29,10 @@ RSpec.describe "Local Dates" do
|
||||
cooked = post.cooked
|
||||
|
||||
expect(cooked).to include('data-timezone="Asia/Calcutta"')
|
||||
expect(cooked).to include('05/08/2018 4:30:00 PM')
|
||||
expect(cooked).to include("05/08/2018 4:30:00 PM")
|
||||
end
|
||||
|
||||
it 'requires the right attributes to convert to a local date' do
|
||||
it "requires the right attributes to convert to a local date" do
|
||||
post = Fabricate(:post, raw: <<~MD)
|
||||
[date]
|
||||
MD
|
||||
@ -44,10 +40,10 @@ RSpec.describe "Local Dates" do
|
||||
cooked = post.cooked
|
||||
|
||||
expect(post.cooked).to include("<p>[date]</p>")
|
||||
expect(cooked).to_not include('data-date=')
|
||||
expect(cooked).to_not include("data-date=")
|
||||
end
|
||||
|
||||
it 'requires the right attributes to convert to a local date' do
|
||||
it "requires the right attributes to convert to a local date" do
|
||||
post = Fabricate(:post, raw: <<~MD)
|
||||
[date]
|
||||
MD
|
||||
@ -55,48 +51,48 @@ RSpec.describe "Local Dates" do
|
||||
cooked = post.cooked
|
||||
|
||||
expect(post.cooked).to include("<p>[date]</p>")
|
||||
expect(cooked).to_not include('data-date=')
|
||||
expect(cooked).to_not include("data-date=")
|
||||
end
|
||||
|
||||
it 'it works with only a date and time' do
|
||||
it "it works with only a date and time" do
|
||||
raw = "[date=2018-11-01 time=12:00]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
expect(cooked).to include('data-date="2018-11-01"')
|
||||
expect(cooked).to include('data-time="12:00"')
|
||||
end
|
||||
|
||||
it 'doesn’t include format by default' do
|
||||
it "doesn’t include format by default" do
|
||||
raw = "[date=2018-11-01 time=12:00]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
expect(cooked).not_to include('data-format=')
|
||||
expect(cooked).not_to include("data-format=")
|
||||
end
|
||||
|
||||
it 'doesn’t include timezone by default' do
|
||||
it "doesn’t include timezone by default" do
|
||||
raw = "[date=2018-11-01 time=12:00]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
|
||||
expect(cooked).not_to include("data-timezone=")
|
||||
end
|
||||
|
||||
it 'supports countdowns' do
|
||||
it "supports countdowns" do
|
||||
raw = "[date=2018-11-01 time=12:00 countdown=true]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
|
||||
expect(cooked).to include("data-countdown=")
|
||||
end
|
||||
|
||||
describe 'ranges' do
|
||||
it 'generates ranges without time' do
|
||||
describe "ranges" do
|
||||
it "generates ranges without time" do
|
||||
raw = "[date-range from=2022-01-06 to=2022-01-08]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
|
||||
expect(cooked).to include('data-date="2022-01-06')
|
||||
expect(cooked).to include('data-range="from"')
|
||||
expect(cooked).to include('data-range="to"')
|
||||
expect(cooked).not_to include('data-time=')
|
||||
expect(cooked).not_to include("data-time=")
|
||||
end
|
||||
|
||||
it 'supports time and timezone' do
|
||||
it "supports time and timezone" do
|
||||
raw = "[date-range from=2022-01-06T13:00 to=2022-01-08 timezone=Australia/Sydney]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
|
||||
@ -107,7 +103,7 @@ RSpec.describe "Local Dates" do
|
||||
expect(cooked).to include('data-timezone="Australia/Sydney"')
|
||||
end
|
||||
|
||||
it 'generates single date when range without end date' do
|
||||
it "generates single date when range without end date" do
|
||||
raw = "[date-range from=2022-01-06T13:00]"
|
||||
cooked = Fabricate(:post, raw: raw).cooked
|
||||
|
||||
|
@ -15,94 +15,118 @@ def generate_html(text, opts = {})
|
||||
end
|
||||
|
||||
RSpec.describe PrettyText do
|
||||
before do
|
||||
freeze_time
|
||||
end
|
||||
before { freeze_time }
|
||||
|
||||
describe 'emails simplified rendering' do
|
||||
it 'works with default markup' do
|
||||
describe "emails simplified rendering" do
|
||||
it "works with default markup" do
|
||||
cooked = PrettyText.cook("[date=2018-05-08]")
|
||||
cooked_mail = generate_html("2018-05-08T00:00:00Z UTC",
|
||||
date: "2018-05-08",
|
||||
email_preview: "2018-05-08T00:00:00Z UTC"
|
||||
)
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
|
||||
it 'works with time' do
|
||||
cooked = PrettyText.cook("[date=2018-05-08 time=20:00:00]")
|
||||
cooked_mail = generate_html("2018-05-08T20:00:00Z UTC",
|
||||
date: "2018-05-08",
|
||||
email_preview: "2018-05-08T20:00:00Z UTC",
|
||||
time: "20:00:00"
|
||||
)
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
|
||||
it 'works with multiple timezones' do
|
||||
cooked = PrettyText.cook('[date=2018-05-08 timezone="Europe/Paris" timezones="America/Los_Angeles|Pacific/Auckland"]')
|
||||
cooked_mail = generate_html("2018-05-07T22:00:00Z UTC",
|
||||
date: "2018-05-08",
|
||||
email_preview: "2018-05-07T22:00:00Z UTC",
|
||||
timezone: "Europe/Paris",
|
||||
timezones: "America/Los_Angeles|Pacific/Auckland"
|
||||
)
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
|
||||
describe 'discourse_local_dates_email_format' do
|
||||
before do
|
||||
SiteSetting.discourse_local_dates_email_format = "DD/MM"
|
||||
end
|
||||
|
||||
it 'uses the site setting' do
|
||||
cooked = PrettyText.cook("[date=2018-05-08]")
|
||||
cooked_mail = generate_html("08/05 UTC",
|
||||
cooked_mail =
|
||||
generate_html(
|
||||
"2018-05-08T00:00:00Z UTC",
|
||||
date: "2018-05-08",
|
||||
email_preview: "08/05 UTC"
|
||||
email_preview: "2018-05-08T00:00:00Z UTC",
|
||||
)
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
|
||||
it "works with time" do
|
||||
cooked = PrettyText.cook("[date=2018-05-08 time=20:00:00]")
|
||||
cooked_mail =
|
||||
generate_html(
|
||||
"2018-05-08T20:00:00Z UTC",
|
||||
date: "2018-05-08",
|
||||
email_preview: "2018-05-08T20:00:00Z UTC",
|
||||
time: "20:00:00",
|
||||
)
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
|
||||
it "works with multiple timezones" do
|
||||
cooked =
|
||||
PrettyText.cook(
|
||||
'[date=2018-05-08 timezone="Europe/Paris" timezones="America/Los_Angeles|Pacific/Auckland"]',
|
||||
)
|
||||
cooked_mail =
|
||||
generate_html(
|
||||
"2018-05-07T22:00:00Z UTC",
|
||||
date: "2018-05-08",
|
||||
email_preview: "2018-05-07T22:00:00Z UTC",
|
||||
timezone: "Europe/Paris",
|
||||
timezones: "America/Los_Angeles|Pacific/Auckland",
|
||||
)
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
|
||||
describe "discourse_local_dates_email_format" do
|
||||
before { SiteSetting.discourse_local_dates_email_format = "DD/MM" }
|
||||
|
||||
it "uses the site setting" do
|
||||
cooked = PrettyText.cook("[date=2018-05-08]")
|
||||
cooked_mail = generate_html("08/05 UTC", date: "2018-05-08", email_preview: "08/05 UTC")
|
||||
|
||||
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'excerpt simplified rendering' do
|
||||
let(:post) { Fabricate(:post, raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone="America/New_York"]') }
|
||||
describe "excerpt simplified rendering" do
|
||||
let(:post) do
|
||||
Fabricate(
|
||||
:post,
|
||||
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone="America/New_York"]',
|
||||
)
|
||||
end
|
||||
|
||||
it 'adds UTC' do
|
||||
it "adds UTC" do
|
||||
excerpt = PrettyText.excerpt(post.cooked, 200)
|
||||
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'special quotes' do
|
||||
it 'converts special quotes to regular quotes' do
|
||||
describe "special quotes" do
|
||||
it "converts special quotes to regular quotes" do
|
||||
# german
|
||||
post = Fabricate(:post, raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=„America/New_York“]')
|
||||
post =
|
||||
Fabricate(
|
||||
:post,
|
||||
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=„America/New_York“]',
|
||||
)
|
||||
excerpt = PrettyText.excerpt(post.cooked, 200)
|
||||
expect(excerpt).to eq('Wednesday, October 16, 2019 6:00 PM (UTC)')
|
||||
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
||||
|
||||
# french
|
||||
post = Fabricate(:post, raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]')
|
||||
post =
|
||||
Fabricate(
|
||||
:post,
|
||||
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]',
|
||||
)
|
||||
excerpt = PrettyText.excerpt(post.cooked, 200)
|
||||
expect(excerpt).to eq('Wednesday, October 16, 2019 6:00 PM (UTC)')
|
||||
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
||||
|
||||
post = Fabricate(:post, raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=“America/New_York”]')
|
||||
post =
|
||||
Fabricate(
|
||||
:post,
|
||||
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=“America/New_York”]',
|
||||
)
|
||||
excerpt = PrettyText.excerpt(post.cooked, 200)
|
||||
expect(excerpt).to eq('Wednesday, October 16, 2019 6:00 PM (UTC)')
|
||||
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'french quotes' do
|
||||
let(:post) { Fabricate(:post, raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]') }
|
||||
describe "french quotes" do
|
||||
let(:post) do
|
||||
Fabricate(
|
||||
:post,
|
||||
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]',
|
||||
)
|
||||
end
|
||||
|
||||
it 'converts french quotes to regular quotes' do
|
||||
it "converts french quotes to regular quotes" do
|
||||
excerpt = PrettyText.excerpt(post.cooked, 200)
|
||||
expect(excerpt).to eq('Wednesday, October 16, 2019 6:00 PM (UTC)')
|
||||
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,12 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Post do
|
||||
before { Jobs.run_immediately! }
|
||||
|
||||
before do
|
||||
Jobs.run_immediately!
|
||||
end
|
||||
|
||||
describe '#local_dates' do
|
||||
describe "#local_dates" do
|
||||
it "should have correct custom fields" do
|
||||
post = Fabricate(:post, raw: <<~SQL)
|
||||
[date=2018-09-17 time=01:39:00 format="LLL" timezone="Europe/Paris" timezones="Europe/Paris|America/Los_Angeles"]
|
||||
@ -37,7 +34,7 @@ RSpec.describe Post do
|
||||
end
|
||||
|
||||
it "should not contain dates from examples" do
|
||||
Oneboxer.stubs(:cached_onebox).with('https://example.com').returns(<<-HTML)
|
||||
Oneboxer.stubs(:cached_onebox).with("https://example.com").returns(<<-HTML)
|
||||
<aside class="onebox githubcommit">
|
||||
<span class="discourse-local-date" data-format="ll" data-date="2020-01-20" data-time="15:06:58" data-timezone="UTC">03:06PM - 20 Jan 20 UTC</span>
|
||||
</aside>
|
||||
@ -48,5 +45,4 @@ RSpec.describe Post do
|
||||
expect(post.local_dates.count).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -4,18 +4,11 @@ describe "Local dates", type: :system, js: true do
|
||||
fab!(:topic) { Fabricate(:topic) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
|
||||
before do
|
||||
create_post(
|
||||
user: user,
|
||||
topic: topic,
|
||||
title: "Date range test post",
|
||||
raw: <<~RAW
|
||||
before { create_post(user: user, topic: topic, title: "Date range test post", raw: <<~RAW) }
|
||||
First option: [date=2022-12-15 time=14:19:00 timezone="Asia/Singapore"]
|
||||
Second option: [date=2022-12-15 time=01:20:00 timezone="Asia/Singapore"], or [date=2022-12-15 time=02:40:00 timezone="Asia/Singapore"]
|
||||
Third option: [date-range from=2022-12-15T11:25:00 to=2022-12-16T00:26:00 timezone="Asia/Singapore"] or [date-range from=2022-12-22T11:57:00 to=2022-12-23T11:58:00 timezone="Asia/Singapore"]
|
||||
RAW
|
||||
)
|
||||
end
|
||||
|
||||
let(:topic_page) { PageObjects::Pages::Topic.new }
|
||||
|
||||
@ -53,12 +46,18 @@ describe "Local dates", type: :system, js: true do
|
||||
post_dates[3].click
|
||||
tippy_date = topic_page.find(".tippy-content .current .date-time")
|
||||
|
||||
expect(tippy_date).to have_text("Thursday, December 15, 2022\n11:25 AM → 12:26 AM", exact: true)
|
||||
expect(tippy_date).to have_text(
|
||||
"Thursday, December 15, 2022\n11:25 AM → 12:26 AM",
|
||||
exact: true,
|
||||
)
|
||||
|
||||
post_dates[5].click
|
||||
tippy_date = topic_page.find(".tippy-content .current .date-time")
|
||||
|
||||
expect(tippy_date).to have_text("Thursday, December 22, 2022 11:57 AM → Friday, December 23, 2022 11:58 AM", exact: true)
|
||||
expect(tippy_date).to have_text(
|
||||
"Thursday, December 22, 2022 11:57 AM → Friday, December 23, 2022 11:58 AM",
|
||||
exact: true,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user