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/renderer.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/renderer.go')
-rw-r--r-- | modules/markup/renderer.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index 53ecbfce2b..6e4ae4e08c 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -5,6 +5,7 @@ package markup import ( + "bytes" "context" "errors" "fmt" @@ -93,6 +94,12 @@ type Renderer interface { Render(ctx *RenderContext, input io.Reader, output io.Writer) error } +// RendererContentDetector detects if the content can be rendered +// by specified renderer +type RendererContentDetector interface { + CanRender(filename string, input io.Reader) bool +} + var ( extRenderers = make(map[string]Renderer) renderers = make(map[string]Renderer) @@ -117,6 +124,20 @@ func GetRendererByType(tp string) Renderer { return renderers[tp] } +// DetectRendererType detects the markup type of the content +func DetectRendererType(filename string, input io.Reader) string { + buf, err := io.ReadAll(input) + if err != nil { + return "" + } + for _, renderer := range renderers { + if detector, ok := renderer.(RendererContentDetector); ok && detector.CanRender(filename, bytes.NewReader(buf)) { + return renderer.Name() + } + } + return "" +} + // Render renders markup file to HTML with all specific handling stuff. func Render(ctx *RenderContext, input io.Reader, output io.Writer) error { if ctx.Type != "" { |