mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-01 07:41:24 +01:00
3f9c3e7bc3
There were too many patches to the Render system, it's really difficult to make further improvements. This PR clears the legacy problems and fix TODOs. 1. Rename `RenderContext.Type` to `RenderContext.MarkupType` to clarify its usage. 2. Use `ContentMode` to replace `meta["mode"]` and `IsWiki`, to clarify the rendering behaviors. 3. Use "wiki" mode instead of "mode=gfm + wiki=true" 4. Merge `renderByType` and `renderByFile` 5. Add more comments ---- The problem of "mode=document": in many cases it is not set, so many non-comment places use comment's hard line break incorrectly
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package console
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"path/filepath"
|
|
"regexp"
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
trend "github.com/buildkite/terminal-to-html/v3"
|
|
"github.com/go-enry/go-enry/v2"
|
|
)
|
|
|
|
func init() {
|
|
markup.RegisterRenderer(Renderer{})
|
|
}
|
|
|
|
// Renderer implements markup.Renderer
|
|
type Renderer struct{}
|
|
|
|
// Name implements markup.Renderer
|
|
func (Renderer) Name() string {
|
|
return "console"
|
|
}
|
|
|
|
// Extensions implements markup.Renderer
|
|
func (Renderer) Extensions() []string {
|
|
return []string{".sh-session"}
|
|
}
|
|
|
|
// SanitizerRules implements markup.Renderer
|
|
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
return []setting.MarkupSanitizerRule{
|
|
{Element: "span", AllowAttr: "class", Regexp: regexp.MustCompile(`^term-((fg[ix]?|bg)\d+|container)$`)},
|
|
}
|
|
}
|
|
|
|
// CanRender implements markup.RendererContentDetector
|
|
func (Renderer) CanRender(filename string, input io.Reader) bool {
|
|
buf, err := io.ReadAll(input)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if enry.GetLanguage(filepath.Base(filename), buf) != enry.OtherLanguage {
|
|
return false
|
|
}
|
|
return bytes.ContainsRune(buf, '\x1b')
|
|
}
|
|
|
|
// Render renders terminal colors to HTML with all specific handling stuff.
|
|
func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
|
buf, err := io.ReadAll(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buf = []byte(trend.Render(buf))
|
|
buf = bytes.ReplaceAll(buf, []byte("\n"), []byte(`<br>`))
|
|
_, err = output.Write(buf)
|
|
return err
|
|
}
|