diff options
author | Lauris BH <lauris@nix.lv> | 2022-06-09 00:46:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 00:46:39 +0300 |
commit | f92b7a633179332053f9fe78190a045637e5ee99 (patch) | |
tree | 708f7f073cca813e111a7b3baabde6110fb4f6f8 /modules/markup/console/console.go | |
parent | 8fee7c46c16cbc1b46477cf600160f9668b9f5d0 (diff) | |
download | gitea-f92b7a633179332053f9fe78190a045637e5ee99.tar.gz gitea-f92b7a633179332053f9fe78190a045637e5ee99.zip |
Add support for rendering terminal output with colors (#19497)
Diffstat (limited to 'modules/markup/console/console.go')
-rw-r--r-- | modules/markup/console/console.go | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/modules/markup/console/console.go b/modules/markup/console/console.go new file mode 100644 index 0000000000..b59594acb7 --- /dev/null +++ b/modules/markup/console/console.go @@ -0,0 +1,95 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +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 +} + +// NeedPostProcess implements markup.Renderer +func (Renderer) NeedPostProcess() bool { return false } + +// 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)$`)}, + } +} + +// SanitizerDisabled disabled sanitize if return true +func (Renderer) SanitizerDisabled() bool { + return false +} + +// 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 = trend.Render(buf) + buf = bytes.ReplaceAll(buf, []byte("\n"), []byte(`<br>`)) + _, err = output.Write(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 +} |