2018-09-29 02:33:54 -06:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-04-10 12:20:58 -06:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-04-10 12:20:58 -06:00
|
|
|
|
2016-12-06 10:58:31 -07:00
|
|
|
package templates
|
2014-04-10 12:20:58 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-02-27 00:09:18 -07:00
|
|
|
"html"
|
2014-04-10 12:20:58 -06:00
|
|
|
"html/template"
|
2017-11-28 02:43:51 -07:00
|
|
|
"net/url"
|
2024-06-11 07:07:10 -06:00
|
|
|
"reflect"
|
2024-02-25 03:45:56 -07:00
|
|
|
"slices"
|
2014-04-10 12:20:58 -06:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-05-25 18:11:25 -06:00
|
|
|
|
2023-10-06 01:46:36 -06:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2017-09-16 11:17:57 -06:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-07-12 03:10:56 -06:00
|
|
|
"code.gitea.io/gitea/modules/svg"
|
Use a general Eval function for expressions in templates. (#23927)
One of the proposals in #23328
This PR introduces a simple expression calculator
(templates/eval/eval.go), it can do basic expression calculations.
Many untested template helper functions like `Mul` `Add` can be replaced
by this new approach.
Then these `Add` / `Mul` / `percentage` / `Subtract` / `DiffStatsWidth`
could all use this `Eval`.
And it provides enhancements for Golang templates, and improves
readability.
Some examples:
----
* Before: `{{Add (Mul $glyph.Row 12) 12}}`
* After: `{{Eval $glyph.Row "*" 12 "+" 12}}`
----
* Before: `{{if lt (Add $i 1) (len $.Topics)}}`
* After: `{{if Eval $i "+" 1 "<" (len $.Topics)}}`
## FAQ
### Why not use an existing expression package?
We need a highly customized expression engine:
* do the calculation on the fly, without pre-compiling
* deal with int/int64/float64 types, to make the result could be used in
Golang template.
* make the syntax could be used in the Golang template directly
* do not introduce too much complex or strange syntax, we just need a
simple calculator.
* it needs to strictly follow Golang template's behavior, for example,
Golang template treats all non-zero values as truth, but many 3rd
packages don't do so.
### What's the benefit?
* Developers don't need to add more `Add`/`Mul`/`Sub`-like functions,
they were getting more and more.
Now, only one `Eval` is enough for all cases.
* The new code reads better than old `{{Add (Mul $glyph.Row 12) 12}}`,
the old one isn't familiar to most procedural programming developers
(eg, the Golang expression syntax).
* The `Eval` is fully covered by tests, many old `Add`/`Mul`-like
functions were never tested.
### The performance?
It doesn't use `reflect`, it doesn't need to parse or compile when used
in Golang template, the performance is as fast as native Go template.
### Is it too complex? Could it be unstable?
The expression calculator program is a common homework for computer
science students, and it's widely used as a teaching and practicing
purpose for developers. The algorithm is pretty well-known.
The behavior can be clearly defined, it is stable.
2023-04-07 07:25:49 -06:00
|
|
|
"code.gitea.io/gitea/modules/templates/eval"
|
2019-08-15 08:46:21 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-09-05 20:20:09 -06:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2024-04-23 10:18:41 -06:00
|
|
|
"code.gitea.io/gitea/services/webtheme"
|
2014-04-10 12:20:58 -06:00
|
|
|
)
|
|
|
|
|
2016-11-24 23:23:48 -07:00
|
|
|
// NewFuncMap returns functions for injecting to templates
|
2023-04-30 06:22:23 -06:00
|
|
|
func NewFuncMap() template.FuncMap {
|
2023-07-04 12:36:08 -06:00
|
|
|
return map[string]any{
|
2023-08-07 19:22:47 -06:00
|
|
|
"ctx": func() any { return nil }, // template context function
|
|
|
|
|
2023-04-29 06:02:29 -06:00
|
|
|
"DumpVar": dumpVar,
|
2024-11-10 13:07:54 -07:00
|
|
|
"NIL": func() any { return nil },
|
2023-04-29 06:02:29 -06:00
|
|
|
|
2023-04-08 07:15:22 -06:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// html/template related functions
|
2024-03-01 03:16:19 -07:00
|
|
|
"dict": dict, // it's lowercase because this name has been widely used. Our other functions should have uppercase names.
|
2024-06-18 16:32:45 -06:00
|
|
|
"Iif": iif,
|
|
|
|
"Eval": evalTokens,
|
|
|
|
"SafeHTML": safeHTML,
|
2024-03-01 03:16:19 -07:00
|
|
|
"HTMLFormat": HTMLFormat,
|
2024-06-18 16:32:45 -06:00
|
|
|
"HTMLEscape": htmlEscape,
|
|
|
|
"QueryEscape": queryEscape,
|
|
|
|
"JSEscape": jsEscapeSafe,
|
2024-03-01 03:16:19 -07:00
|
|
|
"SanitizeHTML": SanitizeHTML,
|
|
|
|
"URLJoin": util.URLJoin,
|
2024-06-18 16:32:45 -06:00
|
|
|
"DotEscape": dotEscape,
|
2023-04-08 07:15:22 -06:00
|
|
|
|
|
|
|
"PathEscape": url.PathEscape,
|
|
|
|
"PathEscapeSegments": util.PathEscapeSegments,
|
|
|
|
|
2023-04-22 12:16:22 -06:00
|
|
|
// utils
|
|
|
|
"StringUtils": NewStringUtils,
|
|
|
|
"SliceUtils": NewSliceUtils,
|
2023-04-29 06:02:29 -06:00
|
|
|
"JsonUtils": NewJsonUtils,
|
2024-11-02 15:04:53 -06:00
|
|
|
"DateUtils": NewDateUtils,
|
2023-04-08 07:15:22 -06:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
2024-04-07 10:19:25 -06:00
|
|
|
// svg / avatar / icon / color
|
2023-08-09 21:19:39 -06:00
|
|
|
"svg": svg.RenderHTML,
|
|
|
|
"EntryIcon": base.EntryIcon,
|
2024-06-18 16:32:45 -06:00
|
|
|
"MigrationIcon": migrationIcon,
|
|
|
|
"ActionIcon": actionIcon,
|
|
|
|
"SortArrow": sortArrow,
|
2024-04-07 10:19:25 -06:00
|
|
|
"ContrastColor": util.ContrastColor,
|
2023-04-08 07:15:22 -06:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// time / number / format
|
2024-11-04 04:30:00 -07:00
|
|
|
"FileSize": base.FileSize,
|
|
|
|
"CountFmt": base.FormatNumberSI,
|
|
|
|
"Sec2Time": util.SecToTime,
|
2023-04-08 07:15:22 -06:00
|
|
|
"LoadTimes": func(startTime time.Time) string {
|
|
|
|
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
|
|
|
|
},
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// setting
|
2016-03-06 14:40:04 -07:00
|
|
|
"AppName": func() string {
|
|
|
|
return setting.AppName
|
|
|
|
},
|
|
|
|
"AppSubUrl": func() string {
|
2016-11-27 03:14:25 -07:00
|
|
|
return setting.AppSubURL
|
2016-03-06 14:40:04 -07:00
|
|
|
},
|
2021-05-08 08:27:25 -06:00
|
|
|
"AssetUrlPrefix": func() string {
|
2021-04-28 06:35:06 -06:00
|
|
|
return setting.StaticURLPrefix + "/assets"
|
2019-10-22 06:11:01 -06:00
|
|
|
},
|
2016-03-06 14:40:04 -07:00
|
|
|
"AppUrl": func() string {
|
2023-02-09 09:31:30 -07:00
|
|
|
// The usage of AppUrl should be avoided as much as possible,
|
|
|
|
// because the AppURL(ROOT_URL) may not match user's visiting site and the ROOT_URL in app.ini may be incorrect.
|
|
|
|
// And it's difficult for Gitea to guess absolute URL correctly with zero configuration,
|
|
|
|
// because Gitea doesn't know whether the scheme is HTTP or HTTPS unless the reverse proxy could tell Gitea.
|
2016-11-27 03:14:25 -07:00
|
|
|
return setting.AppURL
|
2016-03-06 14:40:04 -07:00
|
|
|
},
|
|
|
|
"AppVer": func() string {
|
|
|
|
return setting.AppVer
|
|
|
|
},
|
2023-04-07 01:31:41 -06:00
|
|
|
"AppDomain": func() string { // documented in mail-templates.md
|
2016-03-06 14:40:04 -07:00
|
|
|
return setting.Domain
|
|
|
|
},
|
2022-08-23 06:58:04 -06:00
|
|
|
"AssetVersion": func() string {
|
|
|
|
return setting.AssetVersion
|
|
|
|
},
|
2019-05-08 02:41:35 -06:00
|
|
|
"DefaultShowFullName": func() bool {
|
|
|
|
return setting.UI.DefaultShowFullName
|
|
|
|
},
|
2016-08-31 23:01:32 -06:00
|
|
|
"ShowFooterTemplateLoadTime": func() bool {
|
2023-04-22 17:38:25 -06:00
|
|
|
return setting.Other.ShowFooterTemplateLoadTime
|
2016-08-31 23:01:32 -06:00
|
|
|
},
|
2024-04-03 10:01:50 -06:00
|
|
|
"ShowFooterPoweredBy": func() bool {
|
|
|
|
return setting.Other.ShowFooterPoweredBy
|
|
|
|
},
|
2019-12-27 16:43:56 -07:00
|
|
|
"AllowedReactions": func() []string {
|
|
|
|
return setting.UI.Reactions
|
|
|
|
},
|
2021-06-29 08:28:38 -06:00
|
|
|
"CustomEmojis": func() map[string]string {
|
|
|
|
return setting.UI.CustomEmojisMap
|
|
|
|
},
|
2017-03-31 19:03:01 -06:00
|
|
|
"MetaAuthor": func() string {
|
|
|
|
return setting.UI.Meta.Author
|
|
|
|
},
|
|
|
|
"MetaDescription": func() string {
|
|
|
|
return setting.UI.Meta.Description
|
|
|
|
},
|
|
|
|
"MetaKeywords": func() string {
|
|
|
|
return setting.UI.Meta.Keywords
|
|
|
|
},
|
2021-02-19 16:06:56 -07:00
|
|
|
"EnableTimetracking": func() bool {
|
|
|
|
return setting.Service.EnableTimetracking
|
|
|
|
},
|
2017-09-12 03:25:42 -06:00
|
|
|
"DisableGitHooks": func() bool {
|
|
|
|
return setting.DisableGitHooks
|
|
|
|
},
|
2021-02-11 10:34:34 -07:00
|
|
|
"DisableWebhooks": func() bool {
|
|
|
|
return setting.DisableWebhooks
|
|
|
|
},
|
2018-08-23 23:00:22 -06:00
|
|
|
"DisableImportLocal": func() bool {
|
|
|
|
return !setting.ImportLocalPaths
|
|
|
|
},
|
2024-06-18 16:32:45 -06:00
|
|
|
"UserThemeName": userThemeName,
|
2023-07-04 12:36:08 -06:00
|
|
|
"NotificationSettings": func() map[string]any {
|
|
|
|
return map[string]any{
|
2020-05-07 15:49:00 -06:00
|
|
|
"MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
|
|
|
|
"TimeoutStep": int(setting.UI.Notification.TimeoutStep / time.Millisecond),
|
|
|
|
"MaxTimeout": int(setting.UI.Notification.MaxTimeout / time.Millisecond),
|
|
|
|
"EventSourceUpdateTime": int(setting.UI.Notification.EventSourceUpdateTime / time.Millisecond),
|
2020-04-23 21:57:38 -06:00
|
|
|
}
|
|
|
|
},
|
2023-04-08 07:15:22 -06:00
|
|
|
"MermaidMaxSourceCharacters": func() int {
|
|
|
|
return setting.MermaidMaxSourceCharacters
|
|
|
|
},
|
2020-11-08 10:21:54 -07:00
|
|
|
|
2023-04-08 07:15:22 -06:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// render
|
2024-11-04 23:04:26 -07:00
|
|
|
"RenderCodeBlock": renderCodeBlock,
|
|
|
|
"ReactionToEmoji": reactionToEmoji,
|
2023-04-08 07:15:22 -06:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// misc
|
|
|
|
"ShortSha": base.ShortSha,
|
|
|
|
"ActionContent2Commits": ActionContent2Commits,
|
2024-06-18 16:32:45 -06:00
|
|
|
"IsMultilineCommitMessage": isMultilineCommitMessage,
|
2023-04-08 07:15:22 -06:00
|
|
|
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
|
|
|
|
"MirrorRemoteAddress": mirrorRemoteAddress,
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
"FilenameIsImage": filenameIsImage,
|
|
|
|
"TabSizeClass": tabSizeClass,
|
2024-11-04 23:04:26 -07:00
|
|
|
|
|
|
|
// for backward compatibility only, do not use them anymore
|
|
|
|
"TimeSince": timeSinceLegacy,
|
|
|
|
"TimeSinceUnix": timeSinceLegacy,
|
|
|
|
"DateTime": dateTimeLegacy,
|
|
|
|
|
|
|
|
"RenderEmoji": renderEmojiLegacy,
|
|
|
|
"RenderLabel": renderLabelLegacy,
|
|
|
|
"RenderLabels": renderLabelsLegacy,
|
|
|
|
"RenderIssueTitle": renderIssueTitleLegacy,
|
|
|
|
|
|
|
|
"RenderMarkdownToHtml": renderMarkdownToHtmlLegacy,
|
|
|
|
|
|
|
|
"RenderCommitMessage": renderCommitMessageLegacy,
|
|
|
|
"RenderCommitMessageLinkSubject": renderCommitMessageLinkSubjectLegacy,
|
|
|
|
"RenderCommitBody": renderCommitBodyLegacy,
|
2023-04-30 06:22:23 -06:00
|
|
|
}
|
2019-11-07 06:34:28 -07:00
|
|
|
}
|
|
|
|
|
2024-02-25 03:45:56 -07:00
|
|
|
func HTMLFormat(s string, rawArgs ...any) template.HTML {
|
|
|
|
args := slices.Clone(rawArgs)
|
|
|
|
for i, v := range args {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, template.HTML:
|
|
|
|
// for most basic types (including template.HTML which is safe), just do nothing and use it
|
|
|
|
case string:
|
|
|
|
args[i] = template.HTMLEscapeString(v)
|
|
|
|
case fmt.Stringer:
|
|
|
|
args[i] = template.HTMLEscapeString(v.String())
|
|
|
|
default:
|
|
|
|
args[i] = template.HTMLEscapeString(fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return template.HTML(fmt.Sprintf(s, args...))
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
// safeHTML render raw as HTML
|
|
|
|
func safeHTML(s any) template.HTML {
|
2024-02-14 14:48:45 -07:00
|
|
|
switch v := s.(type) {
|
|
|
|
case string:
|
|
|
|
return template.HTML(v)
|
|
|
|
case template.HTML:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("unexpected type %T", s))
|
|
|
|
}
|
|
|
|
|
2024-03-01 03:16:19 -07:00
|
|
|
// SanitizeHTML sanitizes the input by pre-defined markdown rules
|
2024-03-04 05:02:45 -07:00
|
|
|
func SanitizeHTML(s string) template.HTML {
|
|
|
|
return template.HTML(markup.Sanitize(s))
|
2015-08-08 03:10:34 -06:00
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
func htmlEscape(s any) template.HTML {
|
2024-02-14 14:48:45 -07:00
|
|
|
switch v := s.(type) {
|
|
|
|
case string:
|
|
|
|
return template.HTML(html.EscapeString(v))
|
|
|
|
case template.HTML:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("unexpected type %T", s))
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
func jsEscapeSafe(s string) template.HTML {
|
2024-02-18 02:52:02 -07:00
|
|
|
return template.HTML(template.JSEscapeString(s))
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
func queryEscape(s string) template.URL {
|
2024-03-13 07:32:30 -06:00
|
|
|
return template.URL(url.QueryEscape(s))
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
// dotEscape wraps a dots in names with ZWJ [U+200D] in order to prevent auto-linkers from detecting these as urls
|
|
|
|
func dotEscape(raw string) string {
|
2022-03-23 06:34:20 -06:00
|
|
|
return strings.ReplaceAll(raw, ".", "\u200d.\u200d")
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
// iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
|
|
|
|
// and it could be simply used as "{{iif expr trueVal}}" (omit the falseVal).
|
|
|
|
func iif(condition any, vals ...any) any {
|
2024-06-11 08:52:12 -06:00
|
|
|
if isTemplateTruthy(condition) {
|
2024-04-17 09:58:37 -06:00
|
|
|
return vals[0]
|
|
|
|
} else if len(vals) > 1 {
|
|
|
|
return vals[1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-11 08:52:12 -06:00
|
|
|
func isTemplateTruthy(v any) bool {
|
2024-06-11 07:07:10 -06:00
|
|
|
if v == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
return rv.Bool()
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return rv.Int() != 0
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
return rv.Uint() != 0
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return rv.Float() != 0
|
2024-06-11 08:52:12 -06:00
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
return rv.Complex() != 0
|
|
|
|
case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
|
2024-06-11 07:07:10 -06:00
|
|
|
return rv.Len() > 0
|
2024-06-11 08:52:12 -06:00
|
|
|
case reflect.Struct:
|
|
|
|
return true
|
2024-06-11 07:07:10 -06:00
|
|
|
default:
|
2024-06-11 08:52:12 -06:00
|
|
|
return !rv.IsNil()
|
2024-06-11 07:07:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
// evalTokens evaluates the expression by tokens and returns the result, see the comment of eval.Expr for details.
|
Use a general Eval function for expressions in templates. (#23927)
One of the proposals in #23328
This PR introduces a simple expression calculator
(templates/eval/eval.go), it can do basic expression calculations.
Many untested template helper functions like `Mul` `Add` can be replaced
by this new approach.
Then these `Add` / `Mul` / `percentage` / `Subtract` / `DiffStatsWidth`
could all use this `Eval`.
And it provides enhancements for Golang templates, and improves
readability.
Some examples:
----
* Before: `{{Add (Mul $glyph.Row 12) 12}}`
* After: `{{Eval $glyph.Row "*" 12 "+" 12}}`
----
* Before: `{{if lt (Add $i 1) (len $.Topics)}}`
* After: `{{if Eval $i "+" 1 "<" (len $.Topics)}}`
## FAQ
### Why not use an existing expression package?
We need a highly customized expression engine:
* do the calculation on the fly, without pre-compiling
* deal with int/int64/float64 types, to make the result could be used in
Golang template.
* make the syntax could be used in the Golang template directly
* do not introduce too much complex or strange syntax, we just need a
simple calculator.
* it needs to strictly follow Golang template's behavior, for example,
Golang template treats all non-zero values as truth, but many 3rd
packages don't do so.
### What's the benefit?
* Developers don't need to add more `Add`/`Mul`/`Sub`-like functions,
they were getting more and more.
Now, only one `Eval` is enough for all cases.
* The new code reads better than old `{{Add (Mul $glyph.Row 12) 12}}`,
the old one isn't familiar to most procedural programming developers
(eg, the Golang expression syntax).
* The `Eval` is fully covered by tests, many old `Add`/`Mul`-like
functions were never tested.
### The performance?
It doesn't use `reflect`, it doesn't need to parse or compile when used
in Golang template, the performance is as fast as native Go template.
### Is it too complex? Could it be unstable?
The expression calculator program is a common homework for computer
science students, and it's widely used as a teaching and practicing
purpose for developers. The algorithm is pretty well-known.
The behavior can be clearly defined, it is stable.
2023-04-07 07:25:49 -06:00
|
|
|
// To use this helper function in templates, pass each token as a separate parameter.
|
|
|
|
//
|
|
|
|
// {{ $int64 := Eval $var "+" 1 }}
|
|
|
|
// {{ $float64 := Eval $var "+" 1.0 }}
|
|
|
|
//
|
|
|
|
// Golang's template supports comparable int types, so the int64 result can be used in later statements like {{if lt $int64 10}}
|
2024-06-18 16:32:45 -06:00
|
|
|
func evalTokens(tokens ...any) (any, error) {
|
Use a general Eval function for expressions in templates. (#23927)
One of the proposals in #23328
This PR introduces a simple expression calculator
(templates/eval/eval.go), it can do basic expression calculations.
Many untested template helper functions like `Mul` `Add` can be replaced
by this new approach.
Then these `Add` / `Mul` / `percentage` / `Subtract` / `DiffStatsWidth`
could all use this `Eval`.
And it provides enhancements for Golang templates, and improves
readability.
Some examples:
----
* Before: `{{Add (Mul $glyph.Row 12) 12}}`
* After: `{{Eval $glyph.Row "*" 12 "+" 12}}`
----
* Before: `{{if lt (Add $i 1) (len $.Topics)}}`
* After: `{{if Eval $i "+" 1 "<" (len $.Topics)}}`
## FAQ
### Why not use an existing expression package?
We need a highly customized expression engine:
* do the calculation on the fly, without pre-compiling
* deal with int/int64/float64 types, to make the result could be used in
Golang template.
* make the syntax could be used in the Golang template directly
* do not introduce too much complex or strange syntax, we just need a
simple calculator.
* it needs to strictly follow Golang template's behavior, for example,
Golang template treats all non-zero values as truth, but many 3rd
packages don't do so.
### What's the benefit?
* Developers don't need to add more `Add`/`Mul`/`Sub`-like functions,
they were getting more and more.
Now, only one `Eval` is enough for all cases.
* The new code reads better than old `{{Add (Mul $glyph.Row 12) 12}}`,
the old one isn't familiar to most procedural programming developers
(eg, the Golang expression syntax).
* The `Eval` is fully covered by tests, many old `Add`/`Mul`-like
functions were never tested.
### The performance?
It doesn't use `reflect`, it doesn't need to parse or compile when used
in Golang template, the performance is as fast as native Go template.
### Is it too complex? Could it be unstable?
The expression calculator program is a common homework for computer
science students, and it's widely used as a teaching and practicing
purpose for developers. The algorithm is pretty well-known.
The behavior can be clearly defined, it is stable.
2023-04-07 07:25:49 -06:00
|
|
|
n, err := eval.Expr(tokens...)
|
|
|
|
return n.Value, err
|
|
|
|
}
|
2024-04-23 10:18:41 -06:00
|
|
|
|
2024-06-18 16:32:45 -06:00
|
|
|
func userThemeName(user *user_model.User) string {
|
2024-04-23 10:18:41 -06:00
|
|
|
if user == nil || user.Theme == "" {
|
|
|
|
return setting.UI.DefaultTheme
|
|
|
|
}
|
|
|
|
if webtheme.IsThemeAvailable(user.Theme) {
|
|
|
|
return user.Theme
|
|
|
|
}
|
|
|
|
return setting.UI.DefaultTheme
|
|
|
|
}
|
2024-11-04 23:04:26 -07:00
|
|
|
|
|
|
|
func panicIfDevOrTesting() {
|
|
|
|
if !setting.IsProd || setting.IsInTesting {
|
|
|
|
panic("legacy template functions are for backward compatibility only, do not use them in new code")
|
|
|
|
}
|
|
|
|
}
|