Skip to content

The readme.txt rendering displays the garbled code #4879

Closed
@Xiphin

Description

@Xiphin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions