Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The readme.txt rendering displays the garbled code #4879

Closed
2 of 7 tasks
Xiphin opened this issue Sep 6, 2018 · 0 comments · Fixed by #4950
Closed
2 of 7 tasks

The readme.txt rendering displays the garbled code #4879

Xiphin opened this issue Sep 6, 2018 · 0 comments · Fixed by #4950
Labels
Milestone

Comments

@Xiphin
Copy link

Xiphin commented 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants