2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-23 00:19:24 +08:00
|
|
|
def generate_html(text, opts = {})
|
2018-11-26 21:20:32 +08:00
|
|
|
output = "<p><span"
|
2024-05-24 16:47:45 +08:00
|
|
|
output += " class=\"discourse-local-date\""
|
2018-11-23 00:19:24 +08:00
|
|
|
output += " data-date=\"#{opts[:date]}\"" if opts[:date]
|
2024-05-24 16:47:45 +08:00
|
|
|
output += " data-email-preview=\"#{opts[:email_preview]}\"" if opts[:email_preview]
|
|
|
|
output += " data-format=\"#{opts[:format]}\"" if opts[:format]
|
2018-11-23 00:19:24 +08:00
|
|
|
output += " data-time=\"#{opts[:time]}\"" if opts[:time]
|
2019-01-16 19:53:41 +08:00
|
|
|
output += " data-timezone=\"#{opts[:timezone]}\"" if opts[:timezone]
|
2024-05-24 16:47:45 +08:00
|
|
|
output += " data-timezones=\"#{opts[:timezones]}\"" if opts[:timezones]
|
2018-11-23 00:19:24 +08:00
|
|
|
output += ">"
|
|
|
|
output += text
|
|
|
|
output + "</span></p>"
|
|
|
|
end
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe PrettyText do
|
2018-05-18 21:35:37 +08:00
|
|
|
before { freeze_time }
|
2018-11-23 00:19:24 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "emails simplified rendering" do
|
2018-11-23 00:19:24 +08:00
|
|
|
it "works with default markup" do
|
|
|
|
cooked = PrettyText.cook("[date=2018-05-08]")
|
2019-01-16 19:53:41 +08:00
|
|
|
cooked_mail =
|
|
|
|
generate_html(
|
|
|
|
"2018-05-08T00:00:00Z UTC",
|
2018-11-23 00:19:24 +08:00
|
|
|
date: "2018-05-08",
|
2019-01-16 19:53:41 +08:00
|
|
|
email_preview: "2018-05-08T00:00:00Z UTC",
|
2018-11-23 00:19:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
|
|
end
|
|
|
|
|
2019-01-16 19:53:41 +08:00
|
|
|
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",
|
2018-11-23 00:19:24 +08:00
|
|
|
date: "2018-05-08",
|
2019-01-16 19:53:41 +08:00
|
|
|
email_preview: "2018-05-08T20:00:00Z UTC",
|
|
|
|
time: "20:00:00",
|
2018-11-23 00:19:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
|
|
end
|
|
|
|
|
2019-01-16 19:53:41 +08:00
|
|
|
it "works with multiple timezones" do
|
|
|
|
cooked =
|
|
|
|
PrettyText.cook(
|
2024-03-11 10:04:51 +08:00
|
|
|
'[date=2023-05-08 timezone="Europe/Paris" timezones="America/Los_Angeles|Pacific/Auckland"]',
|
2019-01-16 19:53:41 +08:00
|
|
|
)
|
|
|
|
cooked_mail =
|
|
|
|
generate_html(
|
2024-03-11 10:04:51 +08:00
|
|
|
"2023-05-07T22:00:00Z UTC",
|
|
|
|
date: "2023-05-08",
|
|
|
|
email_preview: "2023-05-07T22:00:00Z UTC",
|
2019-01-16 19:53:41 +08:00
|
|
|
timezone: "Europe/Paris",
|
|
|
|
timezones: "America/Los_Angeles|Pacific/Auckland",
|
2018-11-23 00:19:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(PrettyText.format_for_email(cooked)).to match_html(cooked_mail)
|
|
|
|
end
|
2019-01-16 19:53:41 +08:00
|
|
|
|
|
|
|
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
|
2018-05-18 21:35:37 +08:00
|
|
|
end
|
2019-10-17 22:18:06 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "excerpt simplified rendering" do
|
2019-10-17 22:18:06 +08:00
|
|
|
let(:post) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone="America/New_York"]',
|
|
|
|
)
|
2023-01-07 04:42:16 +08:00
|
|
|
end
|
2019-10-17 22:18:06 +08:00
|
|
|
|
|
|
|
it "adds UTC" do
|
|
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
|
|
end
|
|
|
|
end
|
2019-11-21 18:43:35 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "special quotes" do
|
2020-10-21 23:16:36 +08:00
|
|
|
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“]',
|
|
|
|
)
|
|
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
|
|
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»]',
|
|
|
|
)
|
|
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
2019-11-21 18:43:35 +08:00
|
|
|
|
2020-10-21 23:16:36 +08:00
|
|
|
post =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=“America/New_York”]',
|
|
|
|
)
|
2019-11-21 18:43:35 +08:00
|
|
|
excerpt = PrettyText.excerpt(post.cooked, 200)
|
|
|
|
expect(excerpt).to eq("Wednesday, October 16, 2019 6:00 PM (UTC)")
|
|
|
|
end
|
|
|
|
end
|
2020-10-21 22:36:51 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "french quotes" do
|
2020-10-21 22:36:51 +08:00
|
|
|
let(:post) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
raw: '[date=2019-10-16 time=14:00:00 format="LLLL" timezone=«America/New_York»]',
|
|
|
|
)
|
2023-01-07 04:42:16 +08:00
|
|
|
end
|
2020-10-21 22:36:51 +08:00
|
|
|
|
|
|
|
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)")
|
|
|
|
end
|
|
|
|
end
|
2018-05-04 02:15:57 +08:00
|
|
|
end
|