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

Apply external markup rendering to non-text files #4996

Closed
2 of 7 tasks
causal-agent opened this issue Sep 29, 2018 · 2 comments · Fixed by #8300
Closed
2 of 7 tasks

Apply external markup rendering to non-text files #4996

causal-agent opened this issue Sep 29, 2018 · 2 comments · Fixed by #8300
Labels
type/enhancement An improvement of existing functionality type/proposal The new feature has not been accepted yet but needs to be discussed first.

Comments

@causal-agent
Copy link

  • Gitea version (or commit ref): 1.4.3
  • Git version: 2.18.0
  • Operating system: FreeBSD 11.2-RELEASE
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant
  • Log gist:

Description

Currently, files are only checked against external markup extensions when the file is a text file:

gitea/routers/repo/view.go

Lines 201 to 214 in fc0001c

case isTextFile:
if blob.Size() >= setting.UI.MaxDisplayFileSize {
ctx.Data["IsFileTooLarge"] = true
break
}
d, _ := ioutil.ReadAll(dataRc)
buf = templates.ToUTF8WithFallback(append(buf, d...))
readmeExist := markup.IsReadmeFile(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist
if markup.Type(blob.Name()) != "" {
ctx.Data["IsMarkup"] = true
ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))

I would like to be able to use external markup renderers to render previews of some binary formats, similar to how PDFs can be displayed inline. Instead, I always get the "view raw" page for binary files.

@lunny lunny added type/proposal The new feature has not been accepted yet but needs to be discussed first. type/enhancement An improvement of existing functionality labels Sep 30, 2018
@HarvsG
Copy link
Contributor

HarvsG commented Jul 25, 2019

I have the same issue trying to render docx files using pandoc. I created an issue #7614.

@HarvsG
Copy link
Contributor

HarvsG commented Jul 25, 2019

As you identified the type of rendering is determined by this switch

gitea/routers/repo/view.go

Lines 200 to 269 in fc0001c

switch {
case isTextFile:
if blob.Size() >= setting.UI.MaxDisplayFileSize {
ctx.Data["IsFileTooLarge"] = true
break
}
d, _ := ioutil.ReadAll(dataRc)
buf = templates.ToUTF8WithFallback(append(buf, d...))
readmeExist := markup.IsReadmeFile(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist
if markup.Type(blob.Name()) != "" {
ctx.Data["IsMarkup"] = true
ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
} else if readmeExist {
ctx.Data["IsRenderedHTML"] = true
ctx.Data["FileContent"] = strings.Replace(
gotemplate.HTMLEscapeString(string(buf)), "\n", `<br>`, -1,
)
} 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
}
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())
}
if ctx.Repo.CanEnableEditor() {
ctx.Data["CanEditFile"] = true
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
} else if !ctx.Repo.IsViewBranch {
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
} else if !ctx.Repo.IsWriter() {
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
}
case base.IsPDFFile(buf):
ctx.Data["IsPDFFile"] = true
case base.IsVideoFile(buf):
ctx.Data["IsVideoFile"] = true
case base.IsImageFile(buf):
ctx.Data["IsImageFile"] = true
}

But interestingly I cant see a case base.IsBinaryFile(buf): or a default: case that would manage binary files. So they must either be handled before the switch or after it.

Edit:

In the case of the render type not being known then this is run which provides the 'view raw' link.

{{else}}
<a href="{{EscapePound $.RawFileLink}}" rel="nofollow" class="btn btn-gray btn-radius">{{.i18n.Tr "repo.file_view_raw"}}</a>

So a change must be made that makes either of .IsMarkup or .IsRenderedHTML options true:

{{if .IsMarkup}}
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
{{else if .IsRenderedHTML}}
<pre>{{if .FileContent}}{{.FileContent | Str2html}}{{end}}</pre>

Edit2, Solution:

Perhaps a solution would be to add

	default:
		if blob.Size() >= setting.UI.MaxDisplayFileSize {
			ctx.Data["IsFileTooLarge"] = true
			break
		}

		d, _ := ioutil.ReadAll(dataRc)
		buf = templates.ToUTF8WithFallback(append(buf, d...))
		if markup.Type(blob.Name()) != "" {
			ctx.Data["IsMarkup"] = true // or ctx.Data["IsRenderedHTML"] = true
			ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
		}

at line

zeripath pushed a commit that referenced this issue Oct 21, 2019
* allow external rendering of other filetypes

fixes #4996 and #7614 
allows rendering of non-tex files, or otherwise accounted for filetypes

* Moves flie-size check before read()

And performs gofmt -s

* Only reads if markType is detected
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type/enhancement An improvement of existing functionality type/proposal The new feature has not been accepted yet but needs to be discussed first.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants