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