From 4785128e574214ee545c3a58e7633e1b89f85b76 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 13 Apr 2023 15:56:04 +0000 Subject: [PATCH 01/12] Add ability to tune the relative-time elements in `TimeSince` functions --- modules/timeutil/since.go | 8 ++++---- templates/repo/view_list.tmpl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go index bdde54c617540..39f6ce20b70cf 100644 --- a/modules/timeutil/since.go +++ b/modules/timeutil/since.go @@ -115,13 +115,13 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string { } // TimeSince renders relative time HTML given a time.Time -func TimeSince(then time.Time, lang translation.Locale) template.HTML { +func TimeSince(then time.Time, lang translation.Locale, relativeTimeOptions ...string) template.HTML { timestamp := then.UTC().Format(time.RFC3339) // declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip - return template.HTML(fmt.Sprintf(`%s`, lang.Tr("on_date"), timestamp, timestamp)) + return template.HTML(fmt.Sprintf(`%s`, strings.Join(relativeTimeOptions, " "), lang.Tr("on_date"), timestamp, timestamp)) } // TimeSinceUnix renders relative time HTML given a TimeStamp -func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML { - return TimeSince(then.AsLocalTime(), lang) +func TimeSinceUnix(then TimeStamp, lang translation.Locale, relativeTimeOptions ...string) template.HTML { + return TimeSince(then.AsLocalTime(), lang, relativeTimeOptions...) } diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 9540c872c2c5c..a0ae7cf8ab741 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -34,7 +34,7 @@ {{end}} - {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale}}{{end}}{{end}} + {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale "tense=\"past\""}}{{end}}{{end}} @@ -87,7 +87,7 @@ {{end}} - {{if $commit}}{{TimeSince $commit.Committer.When $.locale}}{{end}} + {{if $commit}}{{TimeSince $commit.Committer.When $.locale "tense=\"past\""}}{{end}} {{end}} From a54bcb078b0f53ef2be8ffb82f06f97e5c003405 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 13 Apr 2023 20:24:42 +0000 Subject: [PATCH 02/12] Avoid escaping quotes --- templates/repo/view_list.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index a0ae7cf8ab741..dbb067714b100 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -34,7 +34,7 @@ {{end}} - {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale "tense=\"past\""}}{{end}}{{end}} + {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale 'tense="past"'}}{{end}}{{end}} @@ -87,7 +87,7 @@ {{end}} - {{if $commit}}{{TimeSince $commit.Committer.When $.locale "tense=\"past\""}}{{end}} + {{if $commit}}{{TimeSince $commit.Committer.When $.locale 'tense="past"'}}{{end}} {{end}} From fd4e20ba197baeb842d6dd1f7aeef3b5940f7453 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 13 Apr 2023 21:05:16 +0000 Subject: [PATCH 03/12] Fix quotes --- templates/repo/view_list.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index dbb067714b100..26a66782c12d9 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -34,7 +34,7 @@ {{end}} - {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale 'tense="past"'}}{{end}}{{end}} + {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale "tense=past"}}{{end}}{{end}} @@ -87,7 +87,7 @@ {{end}} - {{if $commit}}{{TimeSince $commit.Committer.When $.locale 'tense="past"'}}{{end}} + {{if $commit}}{{TimeSince $commit.Committer.When $.locale "tense=past"}}{{end}} {{end}} From 9955b2cc1ddc72557b792abb4bdbc433ece10ef0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 14 Apr 2023 22:44:21 +0800 Subject: [PATCH 04/12] improve --- modules/timeutil/since.go | 29 +++++++++++++++---- routers/web/devtest/devtest.go | 11 ++++++++ templates/devtest/gitea-ui.tmpl | 49 +++++++++++++++++++++++++++++---- templates/repo/view_list.tmpl | 4 +-- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go index 39f6ce20b70cf..4c292da322388 100644 --- a/modules/timeutil/since.go +++ b/modules/timeutil/since.go @@ -114,14 +114,31 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string { return strings.TrimPrefix(timeStr, ", ") } -// TimeSince renders relative time HTML given a time.Time -func TimeSince(then time.Time, lang translation.Locale, relativeTimeOptions ...string) template.HTML { - timestamp := then.UTC().Format(time.RFC3339) +func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { + friendlyText := then.Format("2006-01-02 15:04:05 +07:00") + + // document: https://github.com/github/relative-time-element + attrs := `tense="past"` + isFuture := now.Before(then) + if isFuture { + attrs = `format="duration" tense="future"` + } + // declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip - return template.HTML(fmt.Sprintf(`%s`, strings.Join(relativeTimeOptions, " "), lang.Tr("on_date"), timestamp, timestamp)) + htm := fmt.Sprintf(`%s`, + attrs, then.Format(time.RFC3339), friendlyText) + if isFuture { + return template.HTML(lang.Tr("tool.from_now", htm)) + } + return template.HTML(htm) +} + +// TimeSince renders relative time HTML given a time.Time +func TimeSince(then time.Time, lang translation.Locale) template.HTML { + return timeSinceUnix(then, time.Now(), lang) } // TimeSinceUnix renders relative time HTML given a TimeStamp -func TimeSinceUnix(then TimeStamp, lang translation.Locale, relativeTimeOptions ...string) template.HTML { - return TimeSince(then.AsLocalTime(), lang, relativeTimeOptions...) +func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML { + return TimeSince(then.AsLocalTime(), lang) } diff --git a/routers/web/devtest/devtest.go b/routers/web/devtest/devtest.go index 784940909a612..6ee4d897bba1b 100644 --- a/routers/web/devtest/devtest.go +++ b/routers/web/devtest/devtest.go @@ -7,6 +7,7 @@ import ( "net/http" "path" "strings" + "time" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -32,5 +33,15 @@ func List(ctx *context.Context) { } func Tmpl(ctx *context.Context) { + + now := time.Now() + ctx.Data["TimeNow"] = now + ctx.Data["TimePast5s"] = now.Add(-5 * time.Second) + ctx.Data["TimeFuture5s"] = now.Add(5 * time.Second) + ctx.Data["TimePast2m"] = now.Add(-2 * time.Minute) + ctx.Data["TimeFuture2m"] = now.Add(2 * time.Minute) + ctx.Data["TimePast1y"] = now.Add(-1 * 366 * 86400 * time.Second) + ctx.Data["TimeFuture1y"] = now.Add(1 * 366 * 86400 * time.Second) + ctx.HTML(http.StatusOK, base.TplName("devtest"+path.Clean("/"+ctx.Params("sub")))) } diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index c5ab863d00d04..1ab9ae7b7c7aa 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -1,12 +1,51 @@ {{template "base/head" .}} -
+
+
- - +

Tooltip

+
text with tooltip
+
text with interactive tooltip
+
- text with tooltip +

GiteaOriginUrl

+
+
- {{template "shared/combomarkdowneditor" .}} + +
+

LocaleNumber

+
{{LocaleNumber 1}}
+
{{LocaleNumber 12}}
+
{{LocaleNumber 123}}
+
{{LocaleNumber 1234}}
+
{{LocaleNumber 12345}}
+
{{LocaleNumber 123456}}
+
{{LocaleNumber 1234567}}
+
+ +
+

TimeSince

+
Now: {{TimeSince .TimeNow $.locale}}
+
5s past: {{TimeSince .TimePast5s $.locale}}
+
5s future: {{TimeSince .TimeFuture5s $.locale}}
+
2m past: {{TimeSince .TimePast2m $.locale}}
+
2m future: {{TimeSince .TimeFuture2m $.locale}}
+
1y past: {{TimeSince .TimePast1y $.locale}}
+
1y future: {{TimeSince .TimeFuture1y $.locale}}
+
+ +
+

ComboMarkdownEditor

+
ps: no JS code attached, so just a layout
+ {{template "shared/combomarkdowneditor" .}} +
+ +
{{template "base/footer" .}} diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 26a66782c12d9..9540c872c2c5c 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -34,7 +34,7 @@ {{end}} - {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale "tense=past"}}{{end}}{{end}} + {{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale}}{{end}}{{end}} @@ -87,7 +87,7 @@ {{end}} - {{if $commit}}{{TimeSince $commit.Committer.When $.locale "tense=past"}}{{end}} + {{if $commit}}{{TimeSince $commit.Committer.When $.locale}}{{end}} {{end}} From f746425f0607a8eca9a94a84af2a0c803ef2afa7 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 14 Apr 2023 15:04:23 +0000 Subject: [PATCH 05/12] Drop `duration` --- modules/timeutil/since.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go index 4c292da322388..e6a2519d2120b 100644 --- a/modules/timeutil/since.go +++ b/modules/timeutil/since.go @@ -121,15 +121,12 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { attrs := `tense="past"` isFuture := now.Before(then) if isFuture { - attrs = `format="duration" tense="future"` + attrs = `tense="future"` } // declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip htm := fmt.Sprintf(`%s`, attrs, then.Format(time.RFC3339), friendlyText) - if isFuture { - return template.HTML(lang.Tr("tool.from_now", htm)) - } return template.HTML(htm) } From f949758bc68ca22e37e120878c6dc74c8d2b92bf Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 14 Apr 2023 15:06:44 +0000 Subject: [PATCH 06/12] Remove `on_date` --- options/locale/locale_en-US.ini | 2 -- 1 file changed, 2 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index f71ea824e979b..3c2d51d44eaa3 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -112,8 +112,6 @@ never = Never rss_feed = RSS Feed -on_date = on - [aria] navbar = Navigation Bar footer = Footer From 24a4039b4da1bdb1d1bdd0573e085f51f229740d Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 14 Apr 2023 22:59:50 +0800 Subject: [PATCH 07/12] remove untranslatable word --- options/locale/locale_en-US.ini | 2 -- templates/package/view.tmpl | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 3c2d51d44eaa3..c2c8f1e120c1f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3127,8 +3127,6 @@ starred_repo = starred %[2]s watched_repo = started watching %[2]s [tool] -ago = %s ago -from_now = %s from now now = now future = future 1s = 1 second diff --git a/templates/package/view.tmpl b/templates/package/view.tmpl index 9677b8eb09d9f..e46e7245a0980 100644 --- a/templates/package/view.tmpl +++ b/templates/package/view.tmpl @@ -84,9 +84,9 @@ {{.locale.Tr "packages.versions.view_all"}}
{{range .LatestVersions}} -
- {{.Version}} - {{$.locale.Tr "on_date"}} {{.CreatedUnix.FormatDate}} +
+ {{.Version}} + {{.CreatedUnix.FormatDate}}
{{end}}
From b130c9ed24cd552ac2c398d348dbc5e35861df3a Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 14 Apr 2023 15:13:31 +0000 Subject: [PATCH 08/12] Format package view date --- templates/package/view.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/package/view.tmpl b/templates/package/view.tmpl index e46e7245a0980..6911299d06c68 100644 --- a/templates/package/view.tmpl +++ b/templates/package/view.tmpl @@ -86,7 +86,7 @@ {{range .LatestVersions}}
{{.Version}} - {{.CreatedUnix.FormatDate}} + {{template "shared/datetime/short" .CreatedUnix.FormatDate .CreatedUnix.FormatDate}}
{{end}}
From daecc081334e4bce726cc1d3bc64a44cde884f7f Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 14 Apr 2023 15:20:48 +0000 Subject: [PATCH 09/12] Fix render --- templates/package/view.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/package/view.tmpl b/templates/package/view.tmpl index 6911299d06c68..7c622a91b3eb7 100644 --- a/templates/package/view.tmpl +++ b/templates/package/view.tmpl @@ -86,7 +86,7 @@ {{range .LatestVersions}}
{{.Version}} - {{template "shared/datetime/short" .CreatedUnix.FormatDate .CreatedUnix.FormatDate}} + {{template "shared/datetime/short" (dict "Datetime" (.CreatedUnix.FormatDate) "Fallback" (.CreatedUnix.FormatDate))}}
{{end}}
From 5bf5d41b86eeb57bc5a3686a0aef3186e7664824 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Fri, 14 Apr 2023 15:26:44 +0000 Subject: [PATCH 10/12] fmt --- routers/web/devtest/devtest.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/web/devtest/devtest.go b/routers/web/devtest/devtest.go index 6ee4d897bba1b..48875e306d29e 100644 --- a/routers/web/devtest/devtest.go +++ b/routers/web/devtest/devtest.go @@ -33,7 +33,6 @@ func List(ctx *context.Context) { } func Tmpl(ctx *context.Context) { - now := time.Now() ctx.Data["TimeNow"] = now ctx.Data["TimePast5s"] = now.Add(-5 * time.Second) From d8e73a5ee1f61dba195a54974d0a378010fb00d5 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 14 Apr 2023 23:38:37 +0800 Subject: [PATCH 11/12] fix lint --- modules/timeutil/since.go | 22 +++++++++++++++++----- options/locale/locale_en-US.ini | 1 + templates/devtest/gitea-ui.tmpl | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go index e6a2519d2120b..3b113a1c937b6 100644 --- a/modules/timeutil/since.go +++ b/modules/timeutil/since.go @@ -114,7 +114,7 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string { return strings.TrimPrefix(timeStr, ", ") } -func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { +func timeSinceUnix(then, now time.Time, lang translation.Locale, attrsMap ...map[string]any) template.HTML { friendlyText := then.Format("2006-01-02 15:04:05 +07:00") // document: https://github.com/github/relative-time-element @@ -124,18 +124,30 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { attrs = `tense="future"` } + transOnDate := "" + if len(attrsMap) == 1 { + m := attrsMap[0] + if m["tense"] == "auto" { + attrs = `tense="auto"` // "relative-time" doesn't support i18n of "prefix", so we use our translation + transOnDate = lang.Tr("tool.on_date") + } + } + // declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip htm := fmt.Sprintf(`%s`, attrs, then.Format(time.RFC3339), friendlyText) + if transOnDate != "" { + htm = fmt.Sprintf(transOnDate, htm) + } return template.HTML(htm) } // TimeSince renders relative time HTML given a time.Time -func TimeSince(then time.Time, lang translation.Locale) template.HTML { - return timeSinceUnix(then, time.Now(), lang) +func TimeSince(then time.Time, lang translation.Locale, attrs ...map[string]any) template.HTML { + return timeSinceUnix(then, time.Now(), lang, attrs...) } // TimeSinceUnix renders relative time HTML given a TimeStamp -func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML { - return TimeSince(then.AsLocalTime(), lang) +func TimeSinceUnix(then TimeStamp, lang translation.Locale, extra ...map[string]any) template.HTML { + return TimeSince(then.AsLocalTime(), lang, extra...) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index c2c8f1e120c1f..4930f4eb407b6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3127,6 +3127,7 @@ starred_repo = starred %[2]s watched_repo = started watching %[2]s [tool] +on_date = on %s now = now future = future 1s = 1 second diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 1ab9ae7b7c7aa..102e75c4c1f37 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -33,6 +33,8 @@
2m future: {{TimeSince .TimeFuture2m $.locale}}
1y past: {{TimeSince .TimePast1y $.locale}}
1y future: {{TimeSince .TimeFuture1y $.locale}}
+
1y past (tense=auto): {{TimeSince .TimePast1y $.locale (dict "tense" "auto")}}
+
1y future (tense=auto): {{TimeSince .TimeFuture1y $.locale (dict "tense" "auto")}}
From 2a4fe6e91565d2fefc9bd650535769908c8df8a0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 14 Apr 2023 23:53:06 +0800 Subject: [PATCH 12/12] Revert "tense=auto support" This reverts commit d8e73a5ee1f61dba195a54974d0a378010fb00d5. --- modules/timeutil/since.go | 22 +++++----------------- options/locale/locale_en-US.ini | 1 - templates/devtest/gitea-ui.tmpl | 2 -- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go index 3b113a1c937b6..e6a2519d2120b 100644 --- a/modules/timeutil/since.go +++ b/modules/timeutil/since.go @@ -114,7 +114,7 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string { return strings.TrimPrefix(timeStr, ", ") } -func timeSinceUnix(then, now time.Time, lang translation.Locale, attrsMap ...map[string]any) template.HTML { +func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { friendlyText := then.Format("2006-01-02 15:04:05 +07:00") // document: https://github.com/github/relative-time-element @@ -124,30 +124,18 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale, attrsMap ...map attrs = `tense="future"` } - transOnDate := "" - if len(attrsMap) == 1 { - m := attrsMap[0] - if m["tense"] == "auto" { - attrs = `tense="auto"` // "relative-time" doesn't support i18n of "prefix", so we use our translation - transOnDate = lang.Tr("tool.on_date") - } - } - // declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip htm := fmt.Sprintf(`%s`, attrs, then.Format(time.RFC3339), friendlyText) - if transOnDate != "" { - htm = fmt.Sprintf(transOnDate, htm) - } return template.HTML(htm) } // TimeSince renders relative time HTML given a time.Time -func TimeSince(then time.Time, lang translation.Locale, attrs ...map[string]any) template.HTML { - return timeSinceUnix(then, time.Now(), lang, attrs...) +func TimeSince(then time.Time, lang translation.Locale) template.HTML { + return timeSinceUnix(then, time.Now(), lang) } // TimeSinceUnix renders relative time HTML given a TimeStamp -func TimeSinceUnix(then TimeStamp, lang translation.Locale, extra ...map[string]any) template.HTML { - return TimeSince(then.AsLocalTime(), lang, extra...) +func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML { + return TimeSince(then.AsLocalTime(), lang) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 4930f4eb407b6..c2c8f1e120c1f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3127,7 +3127,6 @@ starred_repo = starred %[2]s watched_repo = started watching %[2]s [tool] -on_date = on %s now = now future = future 1s = 1 second diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 102e75c4c1f37..1ab9ae7b7c7aa 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -33,8 +33,6 @@
2m future: {{TimeSince .TimeFuture2m $.locale}}
1y past: {{TimeSince .TimePast1y $.locale}}
1y future: {{TimeSince .TimeFuture1y $.locale}}
-
1y past (tense=auto): {{TimeSince .TimePast1y $.locale (dict "tense" "auto")}}
-
1y future (tense=auto): {{TimeSince .TimeFuture1y $.locale (dict "tense" "auto")}}