2021-04-19 22:25:08 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-04-19 22:25:08 +00:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2022-06-08 21:46:39 +00:00
|
|
|
"bytes"
|
2021-04-19 22:25:08 +00:00
|
|
|
"io"
|
2024-11-24 08:18:57 +00:00
|
|
|
"path"
|
2021-04-19 22:25:08 +00: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 21:09:51 +00:00
|
|
|
SanitizerRules() []setting.MarkupSanitizerRule
|
2021-04-19 22:25:08 +00:00
|
|
|
Render(ctx *RenderContext, input io.Reader, output io.Writer) error
|
|
|
|
}
|
|
|
|
|
2022-06-16 03:33:23 +00:00
|
|
|
// PostProcessRenderer defines an interface for renderers who need post process
|
|
|
|
type PostProcessRenderer interface {
|
|
|
|
NeedPostProcess() bool
|
|
|
|
}
|
|
|
|
|
2024-11-04 10:59:50 +00:00
|
|
|
// ExternalRenderer defines an interface for external renderers
|
2022-06-16 03:33:23 +00: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 21:46:39 +00: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 22:25:08 +00: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 08:18:57 +00:00
|
|
|
extension := strings.ToLower(path.Ext(filename))
|
2021-04-19 22:25:08 +00:00
|
|
|
return extRenderers[extension]
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:46:39 +00: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-18 03:09:20 +00:00
|
|
|
// DetectMarkupTypeByFileName returns the possible markup format type via the filename
|
|
|
|
func DetectMarkupTypeByFileName(filename string) string {
|
2021-04-19 22:25:08 +00:00
|
|
|
if parser := GetRendererByFileName(filename); parser != nil {
|
|
|
|
return parser.Name()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:12:23 +00:00
|
|
|
func PreviewableExtensions() []string {
|
|
|
|
extensions := make([]string, 0, len(extRenderers))
|
|
|
|
for extension := range extRenderers {
|
|
|
|
extensions = append(extensions, extension)
|
|
|
|
}
|
|
|
|
return extensions
|
|
|
|
}
|