The readme.txt rendering displays the garbled code #2301

Closed
opened 2025-11-02 04:31:46 -06:00 by GiteaMirror · 0 comments
Owner

Originally created by @Xiphin on GitHub (Sep 6, 2018).

Description

If the file is readme.txt and the encoding is not UTF-8, the file rendering displays the garbled code.
The reason for the garbled is that it has not been converted to UTF-8.
The code is as follows:
https://github.com/go-gitea/gitea/blob/master/routers/repo/view.go#L214

The solutions are as follows:
Change lines 211-248 into:

		} else {
			// Building code view blocks with line number on server side.
			var fileContent string
			if content, err := templates.ToUTF8WithErr(buf); err != nil {
				if err != nil {
					log.Error(4, "ToUTF8WithErr: %v", err)
				}
				fileContent = string(buf)
			} else {
				fileContent = content
			}

			if readmeExist {
				ctx.Data["IsRenderedHTML"] = true
				ctx.Data["FileContent"] = strings.Replace(
					gotemplate.HTMLEscapeString(fileContent), "\n", `<br>`, -1,
				)
			} else {
				var output bytes.Buffer
				lines := strings.Split(fileContent, "\n")
				//Remove blank line at the end of file
				if len(lines) > 0 && lines[len(lines)-1] == "" {
					lines = lines[:len(lines)-1]
				}
				for index, line := range lines {
					line = gotemplate.HTMLEscapeString(line)
					if index != len(lines)-1 {
						line += "\n"
					}
					output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, line))
				}
				ctx.Data["FileContent"] = gotemplate.HTML(output.String())

				output.Reset()
				for i := 0; i < len(lines); i++ {
					output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
				}
				ctx.Data["LineNums"] = gotemplate.HTML(output.String())
			}
		}

Screenshots

_20180906183141

_20180906183202

Originally created by @Xiphin on GitHub (Sep 6, 2018). <!-- 1. Please speak English, this is the language all of us can speak and write. 2. Please ask questions or configuration/deploy problems on our Discord server (https://discord.gg/NsatcWJ) or forum (https://discourse.gitea.io). 3. Please take a moment to check that your issue doesn't already exist. 4. Please give all relevant information below for bug reports, because incomplete details will be handled as an invalid report. --> - Gitea version (or commit ref): v1.5.1 - Git version: git version 2.18.0 - Operating system: CentOS Linux release 7.5.1804 - Database (use `[x]`): - [ ] PostgreSQL - [x] MySQL - [ ] MSSQL - [ ] SQLite - Can you reproduce the bug at https://try.gitea.io: - [x] Yes (provide example URL): https://try.gitea.io/lunny/testGiteaChardet/src/branch/master/readme.txt - [ ] No - [ ] Not relevant - Log gist: N/A ## Description If the file is readme.txt and the encoding is not UTF-8, the file rendering displays the garbled code. The reason for the garbled is that it has not been converted to UTF-8. The code is as follows: https://github.com/go-gitea/gitea/blob/master/routers/repo/view.go#L214 The solutions are as follows: Change lines 211-248 into: ``` } else { // Building code view blocks with line number on server side. var fileContent string if content, err := templates.ToUTF8WithErr(buf); err != nil { if err != nil { log.Error(4, "ToUTF8WithErr: %v", err) } fileContent = string(buf) } else { fileContent = content } if readmeExist { ctx.Data["IsRenderedHTML"] = true ctx.Data["FileContent"] = strings.Replace( gotemplate.HTMLEscapeString(fileContent), "\n", `<br>`, -1, ) } else { var output bytes.Buffer lines := strings.Split(fileContent, "\n") //Remove blank line at the end of file if len(lines) > 0 && lines[len(lines)-1] == "" { lines = lines[:len(lines)-1] } for index, line := range lines { line = gotemplate.HTMLEscapeString(line) if index != len(lines)-1 { line += "\n" } output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, line)) } ctx.Data["FileContent"] = gotemplate.HTML(output.String()) output.Reset() for i := 0; i < len(lines); i++ { output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1)) } ctx.Data["LineNums"] = gotemplate.HTML(output.String()) } } ``` ## Screenshots ![_20180906183141](https://user-images.githubusercontent.com/3348428/45152304-33e39900-b203-11e8-8eee-6e2bd59f78ff.png) ![_20180906183202](https://user-images.githubusercontent.com/3348428/45152318-3cd46a80-b203-11e8-85c9-08c989f4de92.png)
GiteaMirror added the type/bug label 2025-11-02 04:31:46 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#2301