mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-21 05:42:28 +03:00
3eb178db49
- Update the `github.com/buildkite/terminal-to-html/v3` dependency from version v3.10.1 to v3.13.0. - Version v3.12.0 introduced an incompatible change, the return type of `AsHTML` changed from `[]byte` to `string`. That same version also introduced streaming mode https://github.com/buildkite/terminal-to-html/pull/126, which allows us to avoid reading the whole input into memory. - Closes #4313
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package console
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"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"
|
|
)
|
|
|
|
// MarkupName describes markup's name
|
|
var MarkupName = "console"
|
|
|
|
func init() {
|
|
markup.RegisterRenderer(Renderer{})
|
|
}
|
|
|
|
// Renderer implements markup.Renderer
|
|
type Renderer struct{}
|
|
|
|
// Name implements markup.Renderer
|
|
func (Renderer) Name() string {
|
|
return MarkupName
|
|
}
|
|
|
|
// 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 {
|
|
screen := &trend.Screen{}
|
|
if _, err := io.Copy(screen, input); err != nil {
|
|
return err
|
|
}
|
|
buf := screen.AsHTML()
|
|
buf = strings.ReplaceAll(buf, "\n", `<br>`)
|
|
_, err := output.Write([]byte(buf))
|
|
return err
|
|
}
|
|
|
|
// Render renders terminal colors to HTML with all specific handling stuff.
|
|
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
|
if ctx.Type == "" {
|
|
ctx.Type = MarkupName
|
|
}
|
|
return markup.Render(ctx, input, output)
|
|
}
|
|
|
|
// RenderString renders terminal colors in string to HTML with all specific handling stuff and return string
|
|
func RenderString(ctx *markup.RenderContext, content string) (string, error) {
|
|
var buf strings.Builder
|
|
if err := Render(ctx, strings.NewReader(content), &buf); err != nil {
|
|
return "", err
|
|
}
|
|
return buf.String(), nil
|
|
}
|