2016-08-11 20:48:08 +08:00
|
|
|
{{template "base/head" .}}
|
2023-02-02 06:56:10 +08:00
|
|
|
<div role="main" aria-label="{{.Title}}" class="page-content repository file editor edit">
|
2016-08-11 20:48:08 +08:00
|
|
|
{{template "repo/header" .}}
|
|
|
|
<div class="ui container">
|
|
|
|
{{template "base/alert" .}}
|
2016-08-28 19:31:42 +08:00
|
|
|
<form class="ui edit form" method="post">
|
2016-08-11 20:48:08 +08:00
|
|
|
{{.CsrfTokenHtml}}
|
2016-08-15 14:02:14 +08:00
|
|
|
<input type="hidden" name="last_commit" value="{{.last_commit}}">
|
2020-08-30 05:32:46 +08:00
|
|
|
<input type="hidden" name="page_has_posted" value="{{.PageHasPosted}}">
|
2016-08-11 20:48:08 +08:00
|
|
|
<div class="ui secondary menu">
|
2016-08-15 16:42:20 +08:00
|
|
|
<div class="fitted item treepath">
|
2016-08-25 12:35:03 +08:00
|
|
|
<div class="ui breadcrumb field {{if .Err_TreePath}}error{{end}}">
|
2021-11-17 02:18:25 +08:00
|
|
|
<a class="section" href="{{$.BranchLink}}">{{.Repository.Name}}</a>
|
2022-08-31 23:58:54 +08:00
|
|
|
{{$n := len .TreeNames}}
|
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 21:25:49 +08:00
|
|
|
{{$l := Eval $n "-" 1}}
|
2016-08-11 20:48:08 +08:00
|
|
|
{{range $i, $v := .TreeNames}}
|
|
|
|
<div class="divider"> / </div>
|
|
|
|
{{if eq $i $l}}
|
2022-06-28 04:58:46 +08:00
|
|
|
<input id="file-name" value="{{$v}}" placeholder="{{$.locale.Tr "repo.editor.name_your_file"}}" data-editorconfig="{{$.Editorconfig}}" required autofocus>
|
2023-03-24 18:35:38 +08:00
|
|
|
<span data-tooltip-content="{{$.locale.Tr "repo.editor.filename_help"}}">{{svg "octicon-info"}}</span>
|
2016-08-11 20:48:08 +08:00
|
|
|
{{else}}
|
2021-11-17 02:18:25 +08:00
|
|
|
<span class="section"><a href="{{$.BranchLink}}/{{index $.TreePaths $i | PathEscapeSegments}}">{{$v}}</a></span>
|
2016-08-11 20:48:08 +08:00
|
|
|
{{end}}
|
|
|
|
{{end}}
|
2022-06-28 04:58:46 +08:00
|
|
|
<span>{{.locale.Tr "repo.editor.or"}} <a href="{{$.BranchLink}}{{if not .IsNewFile}}/{{PathEscapeSegments .TreePath}}{{end}}">{{.locale.Tr "repo.editor.cancel_lower"}}</a></span>
|
2016-08-25 12:35:03 +08:00
|
|
|
<input type="hidden" id="tree_path" name="tree_path" value="{{.TreePath}}" required>
|
2016-08-11 20:48:08 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="field">
|
|
|
|
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
|
2022-06-28 04:58:46 +08:00
|
|
|
<a class="active item" data-tab="write">{{svg "octicon-code"}} {{if .IsNewFile}}{{.locale.Tr "repo.editor.new_file"}}{{else}}{{.locale.Tr "repo.editor.edit_file"}}{{end}}</a>
|
2023-03-24 14:12:23 +08:00
|
|
|
<a class="item" data-tab="preview" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}/src/{{.BranchNameSubURL}}" data-markup-mode="file">{{svg "octicon-eye"}} {{.locale.Tr "preview"}}</a>
|
2023-03-26 13:25:41 +08:00
|
|
|
{{if not .IsNewFile}}
|
2022-06-28 04:58:46 +08:00
|
|
|
<a class="item" data-tab="diff" data-url="{{.RepoLink}}/_preview/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}" data-context="{{.BranchLink}}">{{svg "octicon-diff"}} {{.locale.Tr "repo.editor.preview_changes"}}</a>
|
2016-08-15 16:42:20 +08:00
|
|
|
{{end}}
|
2016-08-11 20:48:08 +08:00
|
|
|
</div>
|
|
|
|
<div class="ui bottom attached active tab segment" data-tab="write">
|
2023-02-19 12:06:14 +08:00
|
|
|
<textarea id="edit_area" name="content" class="gt-hidden" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
|
2023-03-24 14:12:23 +08:00
|
|
|
data-url="{{.Repository.Link}}/markup"
|
2016-08-11 20:48:08 +08:00
|
|
|
data-context="{{.RepoLink}}"
|
2023-03-24 14:12:23 +08:00
|
|
|
data-previewable-extensions="{{.PreviewableExtensions}}"
|
2019-10-17 03:28:41 +08:00
|
|
|
data-line-wrap-extensions="{{.LineWrapExtensions}}">
|
2018-04-20 08:25:15 +08:00
|
|
|
{{.FileContent}}</textarea>
|
2020-08-05 03:56:37 +08:00
|
|
|
<div class="editor-loading is-loading"></div>
|
2016-08-11 20:48:08 +08:00
|
|
|
</div>
|
2021-05-07 16:43:41 +08:00
|
|
|
<div class="ui bottom attached tab segment markup" data-tab="preview">
|
2022-06-28 04:58:46 +08:00
|
|
|
{{.locale.Tr "loading"}}
|
2016-08-11 20:48:08 +08:00
|
|
|
</div>
|
2020-05-15 00:06:01 +08:00
|
|
|
<div class="ui bottom attached tab segment diff edit-diff" data-tab="diff">
|
2022-06-28 04:58:46 +08:00
|
|
|
{{.locale.Tr "loading"}}
|
2016-08-11 20:48:08 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-08-28 16:41:44 +08:00
|
|
|
{{template "repo/editor/commit_form" .}}
|
2016-08-11 20:48:08 +08:00
|
|
|
</form>
|
|
|
|
</div>
|
2019-10-17 03:28:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
<div class="ui small basic modal" id="edit-empty-content-modal">
|
|
|
|
<div class="ui icon header">
|
2023-03-21 09:42:02 +08:00
|
|
|
{{svg "octicon-file"}}
|
2022-06-28 04:58:46 +08:00
|
|
|
{{.locale.Tr "repo.editor.commit_empty_file_header"}}
|
2019-10-17 03:28:41 +08:00
|
|
|
</div>
|
|
|
|
<div class="center content">
|
2022-06-28 04:58:46 +08:00
|
|
|
<p>{{.locale.Tr "repo.editor.commit_empty_file_text"}}</p>
|
2019-10-17 03:28:41 +08:00
|
|
|
</div>
|
|
|
|
<div class="actions">
|
2023-03-14 11:34:09 +08:00
|
|
|
<button class="ui red basic cancel inverted button">
|
2023-03-21 09:42:02 +08:00
|
|
|
{{svg "octicon-x"}}
|
2022-06-28 04:58:46 +08:00
|
|
|
{{.locale.Tr "repo.editor.cancel"}}
|
2023-03-14 11:34:09 +08:00
|
|
|
</button>
|
|
|
|
<button class="ui green basic ok inverted button">
|
2023-03-21 09:42:02 +08:00
|
|
|
{{svg "fontawesome-save"}}
|
2022-06-28 04:58:46 +08:00
|
|
|
{{.locale.Tr "repo.editor.commit_changes"}}
|
2023-03-14 11:34:09 +08:00
|
|
|
</button>
|
2019-10-17 03:28:41 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2016-08-11 20:48:08 +08:00
|
|
|
</div>
|
|
|
|
{{template "base/footer" .}}
|