2022-06-08 21:46:39 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-08 21:46:39 +00:00
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2024-11-18 05:25:42 +00:00
|
|
|
"path"
|
2022-06-08 21:46:39 +00:00
|
|
|
|
|
|
|
"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 {
|
2024-11-14 05:02:11 +00:00
|
|
|
return "console"
|
2022-06-08 21:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extensions implements markup.Renderer
|
|
|
|
func (Renderer) Extensions() []string {
|
|
|
|
return []string{".sh-session"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SanitizerRules implements markup.Renderer
|
|
|
|
func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
|
|
return []setting.MarkupSanitizerRule{
|
2024-11-18 05:25:42 +00:00
|
|
|
{Element: "span", AllowAttr: "class", Regexp: `^term-((fg[ix]?|bg)\d+|container)$`},
|
2022-06-08 21:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanRender implements markup.RendererContentDetector
|
|
|
|
func (Renderer) CanRender(filename string, input io.Reader) bool {
|
|
|
|
buf, err := io.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-11-18 05:25:42 +00:00
|
|
|
if enry.GetLanguage(path.Base(filename), buf) != enry.OtherLanguage {
|
2022-06-08 21:46:39 +00:00
|
|
|
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
|
|
|
|
}
|
2024-06-24 13:26:42 +00:00
|
|
|
buf = []byte(trend.Render(buf))
|
2022-06-08 21:46:39 +00:00
|
|
|
buf = bytes.ReplaceAll(buf, []byte("\n"), []byte(`<br>`))
|
|
|
|
_, err = output.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|