2021-04-19 16:25:08 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-04-19 16:25:08 -06:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2022-06-08 15:46:39 -06:00
|
|
|
"bytes"
|
2021-04-19 16:25:08 -06:00
|
|
|
"io"
|
2024-11-24 01:18:57 -07:00
|
|
|
"path"
|
2021-04-19 16:25:08 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Renderer defines an interface for rendering markup file to HTML
|
|
|
|
type Renderer interface {
|
|
|
|
Name() string // markup format name
|
|
|
|
Extensions() []string
|
2021-06-23 15:09:51 -06:00
|
|
|
SanitizerRules() []setting.MarkupSanitizerRule
|
2021-04-19 16:25:08 -06:00
|
|
|
Render(ctx *RenderContext, input io.Reader, output io.Writer) error
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:33:23 -06:00
|
|
|
// PostProcessRenderer defines an interface for renderers who need post process
|
|
|
|
type PostProcessRenderer interface {
|
|
|
|
NeedPostProcess() bool
|
|
|
|
}
|
|
|
|
|
2024-11-04 03:59:50 -07:00
|
|
|
// ExternalRenderer defines an interface for external renderers
|
2022-06-15 21:33:23 -06:00
|
|
|
type ExternalRenderer interface {
|
|
|
|
// SanitizerDisabled disabled sanitize if return true
|
|
|
|
SanitizerDisabled() bool
|
|
|
|
|
|
|
|
// DisplayInIFrame represents whether render the content with an iframe
|
|
|
|
DisplayInIFrame() bool
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:46:39 -06:00
|
|
|
// RendererContentDetector detects if the content can be rendered
|
|
|
|
// by specified renderer
|
|
|
|
type RendererContentDetector interface {
|
|
|
|
CanRender(filename string, input io.Reader) bool
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:25:08 -06:00
|
|
|
var (
|
|
|
|
extRenderers = make(map[string]Renderer)
|
|
|
|
renderers = make(map[string]Renderer)
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterRenderer registers a new markup file renderer
|
|
|
|
func RegisterRenderer(renderer Renderer) {
|
|
|
|
renderers[renderer.Name()] = renderer
|
|
|
|
for _, ext := range renderer.Extensions() {
|
|
|
|
extRenderers[strings.ToLower(ext)] = renderer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRendererByFileName get renderer by filename
|
|
|
|
func GetRendererByFileName(filename string) Renderer {
|
2024-11-24 01:18:57 -07:00
|
|
|
extension := strings.ToLower(path.Ext(filename))
|
2021-04-19 16:25:08 -06:00
|
|
|
return extRenderers[extension]
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:46:39 -06:00
|
|
|
// 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 ""
|
|
|
|
}
|
|
|
|
|
2024-06-17 21:09:20 -06:00
|
|
|
// DetectMarkupTypeByFileName returns the possible markup format type via the filename
|
|
|
|
func DetectMarkupTypeByFileName(filename string) string {
|
2021-04-19 16:25:08 -06:00
|
|
|
if parser := GetRendererByFileName(filename); parser != nil {
|
|
|
|
return parser.Name()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-03-24 00:12:23 -06:00
|
|
|
func PreviewableExtensions() []string {
|
|
|
|
extensions := make([]string, 0, len(extRenderers))
|
|
|
|
for extension := range extRenderers {
|
|
|
|
extensions = append(extensions, extension)
|
|
|
|
}
|
|
|
|
return extensions
|
|
|
|
}
|