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